diff --git a/assets/javascripts/discourse/adapters/staff-note.js.es6 b/assets/javascripts/discourse/adapters/staff-note.js.es6 index a9189d3..261faf0 100644 --- a/assets/javascripts/discourse/adapters/staff-note.js.es6 +++ b/assets/javascripts/discourse/adapters/staff-note.js.es6 @@ -8,4 +8,3 @@ export default RestAdapter.extend({ return ajax(`${path}?user_id=${userId}`, { method: 'DELETE' }); } }); - diff --git a/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 b/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 index d81bcf4..4811a42 100644 --- a/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 +++ b/assets/javascripts/discourse/initializers/enable-staff-notes.js.es6 @@ -8,7 +8,12 @@ export default { if (!siteSettings.staff_notes_enabled) { return; } withPluginApi('0.2', api => { - api.decorateWidget('poster-name:after', dec => dec.attach('staff-notes-icon')); + api.decorateWidget('poster-name:after', dec => { + const cfs = dec.attrs.userCustomFields || {}; + if (cfs.has_staff_notes) { + return dec.attach('staff-notes-icon'); + } + }); api.createWidget('staff-notes-icon', { tagName: 'span.staff-notes-icon', diff --git a/plugin.rb b/plugin.rb index a319254..3da1858 100644 --- a/plugin.rb +++ b/plugin.rb @@ -7,6 +7,8 @@ enabled_site_setting :staff_notes_enabled register_asset 'stylesheets/staff_notes.scss' +STAFF_NOTES_FIELD = "has_staff_notes" + after_initialize do require_dependency 'user' @@ -25,25 +27,29 @@ after_initialize do PluginStore.get('staff_notes', key_for(user_id)) || [] end - def self.add_note(user_id, raw, created_by) - notes = notes_for(user_id) - record = { id: SecureRandom.hex(16), user_id: user_id, raw: raw, created_by: created_by, created_at: Time.now } + def self.add_note(user, raw, created_by) + notes = notes_for(user.id) + record = { id: SecureRandom.hex(16), user_id: user.id, raw: raw, created_by: created_by, created_at: Time.now } notes << record - ::PluginStore.set("staff_notes", key_for(user_id), notes) + ::PluginStore.set("staff_notes", key_for(user.id), notes) + + user.custom_fields[STAFF_NOTES_FIELD] = true + user.save_custom_fields record end - def self.remove_note(user_id, note_id) - notes = notes_for(user_id) + def self.remove_note(user, note_id) + notes = notes_for(user.id) notes.reject! {|n| n[:id] == note_id} if notes.size > 0 - ::PluginStore.set("staff_notes", key_for(user_id), notes) + ::PluginStore.set("staff_notes", key_for(user.id), notes) else - ::PluginStore.remove("staff_notes", key_for(user_id)) + ::PluginStore.remove("staff_notes", key_for(user.id)) + user.custom_fields.delete(STAFF_NOTES_FIELD) + user.save_custom_fields end - end end @@ -93,11 +99,9 @@ after_initialize do end def create - user_id = params[:staff_note][:user_id] - - user = User.where(id: user_id).first + user = User.where(id: params[:staff_note][:user_id]).first raise Discourse::NotFound if user.blank? - staff_note = ::DiscourseStaffNotes.add_note(user.id, 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) end @@ -106,12 +110,14 @@ after_initialize do user = User.where(id: params[:user_id]).first raise Discourse::NotFound if user.blank? - ::DiscourseStaffNotes.remove_note(user.id, params[:id]) + ::DiscourseStaffNotes.remove_note(user, params[:id]) render json: success_json end end + whitelist_staff_user_custom_field(STAFF_NOTES_FIELD) + DiscourseStaffNotes::Engine.routes.draw do get '/' => 'staff_notes#index' post '/' => 'staff_notes#create'