FEATURE: Create logs for Group Moderator changes (#10271)

FEATURE: Create logs for Group Moderator changes (#10271)

diff --git a/app/assets/javascripts/admin/components/staff-actions.js b/app/assets/javascripts/admin/components/staff-actions.js
index 1c29579..fe65087 100644
--- a/app/assets/javascripts/admin/components/staff-actions.js
+++ b/app/assets/javascripts/admin/components/staff-actions.js
@@ -23,5 +23,17 @@ export default Component.extend({
         return false;
       }
     );
+
+    $(this.element).on(
+      "click.discourse-staff-logs",
+      "[data-link-topic-id]",
+      e => {
+        let topicId = $(e.target).attr("data-link-topic-id");
+
+        DiscourseURL.routeTo(`/t/${topicId}`);
+
+        return false;
+      }
+    );
   }
 });
diff --git a/app/assets/javascripts/admin/models/staff-action-log.js b/app/assets/javascripts/admin/models/staff-action-log.js
index a077d05..5dc942d 100644
--- a/app/assets/javascripts/admin/models/staff-action-log.js
+++ b/app/assets/javascripts/admin/models/staff-action-log.js
@@ -47,10 +47,14 @@ const StaffActionLog = RestModel.extend({
       ? `<a href data-link-post-id="${postId}">${postId}</a>`
       : null;
 
+    const topicLink = topicId
+      ? `<a href data-link-topic-id="${topicId}">${topicId}</a>`
+      : null;
+
     let lines = [
       format("email", email),
       format("admin.logs.ip_address", ipAddress),
-      format("admin.logs.topic_id", topicId),
+      format("admin.logs.topic_id", topicLink, false),
       format("admin.logs.post_id", postLink, false),
       format("admin.logs.category_id", categoryId)
     ];
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 09cde3a..ade423c 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -477,6 +477,8 @@ class PostsController < ApplicationController
     post = find_post_from_params
     raise Discourse::NotFound unless guardian.can_edit_staff_notes?(post.topic)
 
+    previous_notice = post.custom_fields[Post::NOTICE_ARGS]
+
     if params[:notice].present?
       post.custom_fields[Post::NOTICE_TYPE] = Post.notices[:custom]
       post.custom_fields[Post::NOTICE_ARGS] = PrettyText.cook(params[:notice], features: { onebox: false })
@@ -485,6 +487,9 @@ class PostsController < ApplicationController
       post.delete_post_notices
     end
 
+    details = { new_raw_value: params[:notice], old_value: previous_notice }
+    StaffActionLogger.new(current_user).log_post_staff_note(post, details)
+
     render body: nil
   end
 
diff --git a/app/models/topic.rb b/app/models/topic.rb
index cbf29d1..de3034e 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -621,6 +621,12 @@ class Topic < ActiveRecord::Base
     TopicStatusUpdater.new(self, user).update!(status, enabled, opts)
     DiscourseEvent.trigger(:topic_status_updated, self, status, enabled)
 
+    if status == 'closed'
+      StaffActionLogger.new(user).log_topic_closed(self, closed: enabled)
+    elsif status == 'archived'
+      StaffActionLogger.new(user).log_topic_archived(self, archived: enabled)
+    end
+
     if enabled && private_message? && status.to_s["closed"]
       group_ids = user.groups.pluck(:id)
       if group_ids.present?
diff --git a/app/models/user_history.rb b/app/models/user_history.rb
index 26e7b8a..d9b7848 100644
--- a/app/models/user_history.rb
+++ b/app/models/user_history.rb
@@ -109,6 +109,12 @@ class UserHistory < ActiveRecord::Base
       add_email: 88,
       update_email: 89,
       destroy_email: 90,
+      topic_closed: 91,
+      topic_opened: 92,
+      topic_archived: 93,
+      topic_unarchived: 94,
+      post_staff_note_create: 95,
+      post_staff_note_destroy: 96
     )
   end
 
@@ -193,7 +199,13 @@ class UserHistory < ActiveRecord::Base
       :page_unpublished,
       :add_email,
       :update_email,
-      :destroy_email
+      :destroy_email,
+      :topic_closed,
+      :topic_opened,
+      :topic_archived,
+      :topic_unarchived,
+      :post_staff_note_create,
+      :post_staff_note_destroy
     ]
   end
 
diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb
index 0181a55..aa5e52d 100644
--- a/app/services/staff_action_logger.rb
+++ b/app/services/staff_action_logger.rb
@@ -148,6 +148,35 @@ class StaffActionLogger
     ))
   end
 
+  def log_topic_closed(topic, opts = {})
+    raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
+    UserHistory.create!(params(opts).merge(
+      action: UserHistory.actions[opts[:closed] ? :topic_closed : :topic_opened],
+      topic_id: topic.id
+    ))
+  end
+
+  def log_topic_archived(topic, opts = {})
+    raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
+    UserHistory.create!(params(opts).merge(
+      action: UserHistory.actions[opts[:archived] ? :topic_archived : :topic_unarchived],
+      topic_id: topic.id
+    ))
+  end
+
+  def log_post_staff_note(post, opts = {})
+    raise Discourse::InvalidParameters.new(:post) unless post && post.is_a?(Post)
+
+    args = params(opts).merge(
+      action: UserHistory.actions[opts[:new_raw_value].present? ? :post_staff_note_create : :post_staff_note_destroy],
+      post_id: post.id
+    )
+    args[:new_value] = opts[:new_raw_value] if opts[:new_raw_value].present?
+    args[:previous_value] = opts[:old_value] if opts[:old_value].present?
+
+    UserHistory.create!(params(opts).merge(args))
+  end
+
   def log_site_setting_change(setting_name, previous_value, new_value, opts = {})
     raise Discourse::InvalidParameters.new(:setting_name) unless setting_name.present? && SiteSetting.respond_to?(setting_name)
     UserHistory.create!(params(opts).merge(
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 9ddc636..1e3ddae 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4236,6 +4236,12 @@ en:
             add_email: "add email"
             update_email: "update email"
             destroy_email: "destroy email"
+            topic_closed: "topic closed"
+            topic_opened: "topic opened"
+            topic_archived: "topic archived"
+            topic_unarchived: "topic unarchived"
+            post_staff_note_create: "add staff note"
+            post_staff_note_destroy: "destroy staff note"
         screened_emails:
           title: "Screened Emails"
           description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index dbe0c74..b8be9f3 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -1082,6 +1082,10 @@ describe Topic do
     end
 
     context 'archived' do
+      it 'should create a staff action log entry' do
+        expect { topic.update_status('archived', true, @user) }.to change { UserHistory.where(action: UserHistory.actions[:topic_archived]).count }.by(1)
+      end
+
       context 'disable' do
         before do
           @archived_topic = Fabricate(:topic, archived: true, bumped_at: 1.hour.ago)
@@ -1155,6 +1159,10 @@ describe Topic do
 
         expect { topic.update_status(status, true, @user) }.to change(topic.group_archived_messages, :count).by(1)
       end
+
+      it 'should create a staff action log entry' do
+        expect { topic.update_status(status, true, @user) }.to change { UserHistory.where(action: UserHistory.actions[:topic_closed]).count }.by(1)
+      end
     end
 
     context 'autoclosed' do
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 65d11dd..ef5bf4e 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -1805,6 +1805,7 @@ describe PostsController do
       expect(public_post.custom_fields[Post::NOTICE_TYPE]).to eq(Post.notices[:custom])
       expect(public_post.custom_fields[Post::NOTICE_ARGS]).to include('<p>Hello <em>world</em>!</p>')

[... diff too long, it was truncated ...]

GitHub sha: 0ed784b4

1 Like

This commit appears in #10271 which was merged by jbrw.