FIX: N+1 query, loadscript error
This commit is contained in:
parent
4d95ffde63
commit
2dbe071f96
|
@ -1,5 +1,6 @@
|
||||||
import { withPluginApi } from 'discourse/lib/plugin-api';
|
import { withPluginApi } from 'discourse/lib/plugin-api';
|
||||||
import showModal from 'discourse/lib/show-modal';
|
import showModal from 'discourse/lib/show-modal';
|
||||||
|
import loadScript from 'discourse/lib/load-script';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'enable-staff-notes',
|
name: 'enable-staff-notes',
|
||||||
|
@ -11,6 +12,7 @@ export default {
|
||||||
|
|
||||||
withPluginApi('0.2', api => {
|
withPluginApi('0.2', api => {
|
||||||
function showStaffNotes(userId, callback) {
|
function showStaffNotes(userId, callback) {
|
||||||
|
return loadScript('defer/html-sanitizer-bundle').then(() => {
|
||||||
return store.find('staff-note', { user_id: userId }).then(model => {
|
return store.find('staff-note', { user_id: userId }).then(model => {
|
||||||
const controller = showModal('staff-notes', { model, title: 'staff_notes.title' });
|
const controller = showModal('staff-notes', { model, title: 'staff_notes.title' });
|
||||||
controller.reset();
|
controller.reset();
|
||||||
|
@ -18,6 +20,7 @@ export default {
|
||||||
controller.set('callback', callback);
|
controller.set('callback', callback);
|
||||||
return controller;
|
return controller;
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function widgetShowStaffNotes() {
|
function widgetShowStaffNotes() {
|
||||||
|
|
25
plugin.rb
25
plugin.rb
|
@ -71,10 +71,7 @@ after_initialize do
|
||||||
end
|
end
|
||||||
|
|
||||||
def created_by
|
def created_by
|
||||||
user = User.where(id: object[:created_by]).first
|
BasicUserSerializer.new(object[:created_by], scope: scope, root: false)
|
||||||
return nil if user.blank?
|
|
||||||
|
|
||||||
BasicUserSerializer.new(user, scope: scope, root: false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def created_at
|
def created_at
|
||||||
|
@ -94,7 +91,7 @@ after_initialize do
|
||||||
notes = ::DiscourseStaffNotes.notes_for(params[:user_id])
|
notes = ::DiscourseStaffNotes.notes_for(params[:user_id])
|
||||||
render json: {
|
render json: {
|
||||||
extras: { username: user.username },
|
extras: { username: user.username },
|
||||||
staff_notes: serialize_data(notes, ::StaffNoteSerializer)
|
staff_notes: create_json(notes)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -103,7 +100,7 @@ after_initialize do
|
||||||
raise Discourse::NotFound if user.blank?
|
raise Discourse::NotFound if user.blank?
|
||||||
staff_note = ::DiscourseStaffNotes.add_note(user, params[:staff_note][:raw], current_user.id)
|
staff_note = ::DiscourseStaffNotes.add_note(user, params[:staff_note][:raw], current_user.id)
|
||||||
|
|
||||||
render json: serialize_data(staff_note, ::StaffNoteSerializer)
|
render json: create_json(staff_note)
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
@ -114,6 +111,22 @@ after_initialize do
|
||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def create_json(obj)
|
||||||
|
# Avoid n+1
|
||||||
|
if obj.is_a?(Array)
|
||||||
|
by_ids = {}
|
||||||
|
User.where(id: obj.map {|o| o[:created_by] }).each do |u|
|
||||||
|
by_ids[u.id] = u
|
||||||
|
end
|
||||||
|
obj.each {|o| o[:created_by] = by_ids[o[:created_by].to_i] }
|
||||||
|
else
|
||||||
|
obj[:created_by] = User.where(id: obj[:created_by]).first
|
||||||
|
end
|
||||||
|
|
||||||
|
serialize_data(obj, ::StaffNoteSerializer)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD)
|
whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD)
|
||||||
|
|
Loading…
Reference in New Issue