From ba396a5384fcc253db2ae3e6c55afbe00c936696 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Tue, 13 Aug 2019 20:55:05 +0300 Subject: [PATCH] DEV: Use ResultSet with staff action logs. (#7661) --- .../admin/adapters/staff-action-log.js.es6 | 7 + .../admin-logs-staff-action-logs.js.es6 | 74 ++++------- .../admin/models/staff-action-log.js.es6 | 17 ++- .../templates/logs/staff-action-logs.hbs | 124 +++++++++--------- .../admin/staff_action_logs_controller.rb | 23 +++- app/models/user_history.rb | 5 - .../staff_action_logs_serializer.rb | 14 -- .../staff_action_logs_controller_spec.rb | 4 +- .../helpers/create-pretender.js.es6 | 5 +- 9 files changed, 122 insertions(+), 151 deletions(-) create mode 100644 app/assets/javascripts/admin/adapters/staff-action-log.js.es6 delete mode 100644 app/serializers/staff_action_logs_serializer.rb diff --git a/app/assets/javascripts/admin/adapters/staff-action-log.js.es6 b/app/assets/javascripts/admin/adapters/staff-action-log.js.es6 new file mode 100644 index 00000000000..8e0f087c2be --- /dev/null +++ b/app/assets/javascripts/admin/adapters/staff-action-log.js.es6 @@ -0,0 +1,7 @@ +import RestAdapter from "discourse/adapters/rest"; + +export default RestAdapter.extend({ + basePath() { + return "/admin/logs/"; + } +}); diff --git a/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 b/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 index c1cd77b63b0..a3d332c1f51 100644 --- a/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 @@ -1,21 +1,15 @@ import { exportEntity } from "discourse/lib/export-csv"; import { outputExportResult } from "discourse/lib/export-result"; -import StaffActionLog from "admin/models/staff-action-log"; import { default as computed, on } from "ember-addons/ember-computed-decorators"; export default Ember.Controller.extend({ - loading: false, - filters: null, - userHistoryActions: [], model: null, - nextPage: 0, - lastPage: null, - + filters: null, filtersExists: Ember.computed.gt("filterCount", 0), - showTable: Ember.computed.gt("model.length", 0), + userHistoryActions: null, @computed("filters.action_name") actionFilter(name) { @@ -25,34 +19,21 @@ export default Ember.Controller.extend({ @on("init") resetFilters() { this.setProperties({ - filters: Ember.Object.create(), - model: [], - nextPage: 0, - lastPage: null + model: Ember.Object.create({ loadingMore: true }), + filters: Ember.Object.create() }); this.scheduleRefresh(); }, _changeFilters(props) { + this.set("model", Ember.Object.create({ loadingMore: true })); this.filters.setProperties(props); - this.setProperties({ - model: [], - nextPage: 0, - lastPage: null - }); this.scheduleRefresh(); }, _refresh() { - if (this.lastPage && this.nextPage >= this.lastPage) { - return; - } - - this.set("loading", true); - - const page = this.nextPage; let filters = this.filters; - let params = { page }; + let params = {}; let count = 0; // Don't send null values @@ -65,32 +46,23 @@ export default Ember.Controller.extend({ }); this.set("filterCount", count); - StaffActionLog.findAll(params) - .then(result => { - this.setProperties({ - model: this.model.concat(result.staff_action_logs), - nextPage: page + 1 - }); + this.store.findAll("staff-action-log", params).then(result => { + this.set("model", result); - if (result.staff_action_logs.length === 0) { - this.set("lastPage", page); - } - - if (this.userHistoryActions.length === 0) { - this.set( - "userHistoryActions", - result.user_history_actions - .map(action => ({ - id: action.id, - action_id: action.action_id, - name: I18n.t("admin.logs.staff_actions.actions." + action.id), - name_raw: action.id - })) - .sort((a, b) => (a.name > b.name ? 1 : -1)) - ); - } - }) - .finally(() => this.set("loading", false)); + if (!this.userHistoryActions) { + this.set( + "userHistoryActions", + result.extras.user_history_actions + .map(action => ({ + id: action.id, + action_id: action.action_id, + name: I18n.t("admin.logs.staff_actions.actions." + action.id), + name_raw: action.id + })) + .sort((a, b) => a.name.localeCompare(b.name)) + ); + } + }); }, scheduleRefresh() { @@ -153,7 +125,7 @@ export default Ember.Controller.extend({ }, loadMore() { - this._refresh(); + this.model.loadMore(); } } }); diff --git a/app/assets/javascripts/admin/models/staff-action-log.js.es6 b/app/assets/javascripts/admin/models/staff-action-log.js.es6 index 3451cffa227..2d63019ddad 100644 --- a/app/assets/javascripts/admin/models/staff-action-log.js.es6 +++ b/app/assets/javascripts/admin/models/staff-action-log.js.es6 @@ -2,6 +2,7 @@ import computed from "ember-addons/ember-computed-decorators"; import { ajax } from "discourse/lib/ajax"; import AdminUser from "admin/models/admin-user"; import { escapeExpression } from "discourse/lib/utilities"; +import RestModel from "discourse/models/rest"; function format(label, value, escape = true) { return value @@ -9,7 +10,7 @@ function format(label, value, escape = true) { : ""; } -const StaffActionLog = Discourse.Model.extend({ +const StaffActionLog = RestModel.extend({ showFullDetails: false, @computed("action_name") @@ -80,16 +81,14 @@ const StaffActionLog = Discourse.Model.extend({ }); StaffActionLog.reopenClass({ - create(attrs) { - attrs = attrs || {}; - - if (attrs.acting_user) { - attrs.acting_user = AdminUser.create(attrs.acting_user); + munge(json) { + if (json.acting_user) { + json.acting_user = AdminUser.create(json.acting_user); } - if (attrs.target_user) { - attrs.target_user = AdminUser.create(attrs.target_user); + if (json.target_user) { + json.target_user = AdminUser.create(json.target_user); } - return this._super(attrs); + return json; }, findAll(data) { diff --git a/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs b/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs index d072bfec45b..bba9a724ca0 100644 --- a/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs +++ b/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs @@ -39,70 +39,66 @@ {{#staff-actions}} -{{#load-more selector=".staff-logs tr" action=(action "loadMore")}} - {{#if showTable}} - - - - - - - - - - - - - {{#each model as |item|}} - - + + + {{/each}} + +
{{i18n 'admin.logs.staff_actions.staff_user'}}{{i18n 'admin.logs.action'}}{{i18n 'admin.logs.staff_actions.subject'}}{{i18n 'admin.logs.staff_actions.when'}}{{i18n 'admin.logs.staff_actions.details'}}{{i18n 'admin.logs.staff_actions.context'}}
-
- {{#if item.acting_user}} - {{#link-to 'adminUser' item.acting_user}}{{avatar item.acting_user imageSize="tiny"}}{{/link-to}} - {{item.acting_user.username}} - {{else}} - - {{d-icon "far-trash-alt"}} - + {{#load-more selector=".staff-logs tr" action=(action "loadMore")}} + {{#if model.content}} + + + + + + + + + + + {{#each model.content as |item|}} + + + + + + - - - - - - - {{/each}} - - -
{{i18n 'admin.logs.staff_actions.staff_user'}}{{i18n 'admin.logs.action'}}{{i18n 'admin.logs.staff_actions.subject'}}{{i18n 'admin.logs.staff_actions.when'}}{{i18n 'admin.logs.staff_actions.details'}}{{i18n 'admin.logs.staff_actions.context'}}
+
+ {{#if item.acting_user}} + {{#link-to 'adminUser' item.acting_user}}{{avatar item.acting_user imageSize="tiny"}}{{/link-to}} + {{item.acting_user.username}} + {{else}} + + {{d-icon "far-trash-alt"}} + + {{/if}} +
+
+ {{item.actionName}} + +
+ {{#if item.target_user}} + {{#link-to 'adminUser' item.target_user}}{{avatar item.target_user imageSize="tiny"}}{{/link-to}} + {{item.target_user.username}} + {{/if}} + {{#if item.subject}} + {{item.subject}} + {{/if}} +
+
{{age-with-tooltip item.created_at}} + {{{item.formattedDetails}}} + {{#if item.useCustomModalForDetails}} + {{d-icon "info-circle"}} {{i18n 'admin.logs.staff_actions.show'}} {{/if}} - - - {{item.actionName}} - -
- - {{#if item.target_user}} - {{#link-to 'adminUser' item.target_user}}{{avatar item.target_user imageSize="tiny"}}{{/link-to}} - {{item.target_user.username}} - {{/if}} - {{#if item.subject}} - {{item.subject}} - {{/if}} -
-
{{age-with-tooltip item.created_at}} - {{{item.formattedDetails}}} - {{#if item.useCustomModalForDetails}} - {{d-icon "info-circle"}} {{i18n 'admin.logs.staff_actions.show'}} - {{/if}} - {{#if item.useModalForDetails}} - {{d-icon "info-circle"}} {{i18n 'admin.logs.staff_actions.show'}} - {{/if}} - {{item.context}}
- {{else}} - {{i18n 'search.no_results'}} - {{/if}} - - {{conditional-loading-spinner condition=loading}} -{{/load-more}} + {{#if item.useModalForDetails}} + {{d-icon "info-circle"}} {{i18n 'admin.logs.staff_actions.show'}} + {{/if}} +
{{item.context}}
+ {{else if model.loadingMore}} + {{conditional-loading-spinner condition=model.loadingMore}} + {{else}} + {{i18n 'search.no_results'}} + {{/if}} + {{/load-more}} {{/staff-actions}} diff --git a/app/controllers/admin/staff_action_logs_controller.rb b/app/controllers/admin/staff_action_logs_controller.rb index 28e83dfe328..9926c3ce1eb 100644 --- a/app/controllers/admin/staff_action_logs_controller.rb +++ b/app/controllers/admin/staff_action_logs_controller.rb @@ -5,11 +5,24 @@ class Admin::StaffActionLogsController < Admin::AdminController def index filters = params.slice(*UserHistory.staff_filters + [:page, :limit]) - staff_action_logs = UserHistory.staff_action_records(current_user, filters).to_a - render json: StaffActionLogsSerializer.new({ - staff_action_logs: staff_action_logs, - user_history_actions: staff_available_actions - }, root: false) + page = (params[:page] || 0).to_i + page_size = (params[:limit] || 200).to_i.clamp(1, 200) + + staff_action_logs = UserHistory.staff_action_records(current_user, filters) + count = staff_action_logs.count + staff_action_logs = staff_action_logs.offset(page * page_size).limit(page_size).to_a + + load_more_params = params.permit(UserHistory.staff_filters) + load_more_params.merge!(page: page + 1, page_size: page_size) + + render_json_dump( + staff_action_logs: serialize_data(staff_action_logs, UserHistorySerializer), + total_rows_staff_action_logs: count, + load_more_staff_action_logs: admin_staff_action_logs_path(load_more_params), + extras: { + user_history_actions: staff_available_actions + } + ) end def diff diff --git a/app/models/user_history.rb b/app/models/user_history.rb index 0c5d87dc10b..015d5bd15f1 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -222,14 +222,9 @@ class UserHistory < ActiveRecord::Base opts[:action_id] = self.actions[opts[:action_name].to_sym] if opts[:action_name] end - page = (opts[:page] || 0).to_i - page_size = (opts[:limit] || 200).to_i - query = self .with_filters(opts.slice(*staff_filters)) .only_staff_actions - .limit(page_size) - .offset(page * page_size) .order('id DESC') .includes(:acting_user, :target_user) query = query.where(admin_only: false) unless viewer && viewer.admin? diff --git a/app/serializers/staff_action_logs_serializer.rb b/app/serializers/staff_action_logs_serializer.rb deleted file mode 100644 index a9830f5df9d..00000000000 --- a/app/serializers/staff_action_logs_serializer.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class StaffActionLogsSerializer < ApplicationSerializer - attributes :user_history_actions - has_many :staff_action_logs, serializer: UserHistorySerializer, embed: :objects - - def staff_action_logs - object[:staff_action_logs] - end - - def user_history_actions - object[:user_history_actions] - end -end diff --git a/spec/requests/admin/staff_action_logs_controller_spec.rb b/spec/requests/admin/staff_action_logs_controller_spec.rb index 7ced305d033..e79c44bbaed 100644 --- a/spec/requests/admin/staff_action_logs_controller_spec.rb +++ b/spec/requests/admin/staff_action_logs_controller_spec.rb @@ -26,7 +26,7 @@ describe Admin::StaffActionLogsController do expect(json["staff_action_logs"].length).to eq(1) expect(json["staff_action_logs"][0]["action_name"]).to eq("delete_topic") - expect(json["user_history_actions"]).to include( + expect(json["extras"]["user_history_actions"]).to include( "id" => 'delete_topic', "action_id" => UserHistory.actions[:delete_topic] ) end @@ -60,7 +60,7 @@ describe Admin::StaffActionLogsController do get "/admin/logs/staff_action_logs.json", params: {} json = JSON.parse(response.body) - action = json['user_history_actions'].first + action = json['extras']['user_history_actions'].first expect(action['id']).to eq plugin_extended_action.to_s expect(action['action_id']).to eq UserHistory.actions[:custom_staff] diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index 781cdb919ce..ea7a4cd71d7 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -604,7 +604,10 @@ export default function() { this.delete("/admin/badges/:id", success); this.get("/admin/logs/staff_action_logs.json", () => { - return response(200, { staff_action_logs: [], user_history_actions: [] }); + return response(200, { + staff_action_logs: [], + extras: { user_history_actions: [] } + }); }); this.get("/admin/logs/watched_words", () => {