DEV: Rename files, functions and DB entries plus two small fixes

1. Shows the full list of notes for a user in the admin report (instead of just the first note).

2. Fixes a UI-only warning when adding or removing a note from the user card.
This commit is contained in:
Penar Musaraj 2019-07-24 22:58:08 -04:00
parent 65f01e6867
commit 03366e344b
27 changed files with 159 additions and 144 deletions

View File

@ -1 +0,0 @@
{{show-staff-notes show="showStaffNotes" count=staffNotesCount}}

View File

@ -1,3 +0,0 @@
{{#if staffNotesCount}}
{{show-staff-notes show="showStaffNotes" count=staffNotesCount}}
{{/if}}

View File

@ -1,9 +0,0 @@
{{#if staffNotesCount}}
<a href {{action "showStaffNotes"}} title="{{staffNotesTitle}}">
{{#if emojiEnabled}}
<img src={{emojiUrl}} title={{staffNotesTitle}} alt="pencil" class="emoji">
{{else}}
{{d-icon "sticky-note"}}
{{/if}}
</a>
{{/if}}

View File

@ -1 +0,0 @@
<li>{{show-staff-notes show="showStaffNotes" count=staffNotesCount}}</li>

View File

@ -1,4 +1,4 @@
import connector from "discourse/plugins/discourse-user-notes/discourse-staff-notes/connectors/user-profile-controls/show-notes-on-profile";
import connector from "discourse/plugins/discourse-user-notes/discourse-user-notes/connectors/user-profile-controls/show-notes-on-profile";
// Same as other connector
export default connector;

View File

@ -1,4 +1,4 @@
import { showStaffNotes } from "discourse/plugins/discourse-user-notes/discourse-staff-notes/lib/staff-notes";
import { showUserNotes } from "discourse/plugins/discourse-user-notes/discourse-user-notes/lib/user-notes";
import { getOwner } from "discourse-common/lib/get-owner";
export default {
@ -9,17 +9,17 @@ export default {
setupComponent(args, component) {
let model = args.flaggedPost.get("user");
component.set(
"staffNotesCount",
model.get("custom_fields.staff_notes_count") || 0
"userNotesCount",
model.get("custom_fields.user_notes_count") || 0
);
},
actions: {
showStaffNotes() {
showUserNotes() {
const store = getOwner(this).lookup("store:main");
const user = this.get("args.flaggedPost.user");
showStaffNotes(store, user.get("id"), count =>
this.set("staffNotesCount", count)
showUserNotes(store, user.get("id"), count =>
this.set("userNotesCount", count)
);
}
}

View File

@ -1,4 +1,4 @@
import { showStaffNotes } from "discourse/plugins/discourse-user-notes/discourse-staff-notes/lib/staff-notes";
import { showUserNotes } from "discourse/plugins/discourse-user-notes/discourse-user-notes/lib/user-notes";
import { getOwner } from "discourse-common/lib/get-owner";
import { emojiUrlFor } from "discourse/lib/text";
@ -11,24 +11,28 @@ export default {
setupComponent(args, component) {
const { user } = args;
const count =
user.get("staff_notes_count") ||
user.get("custom_fields.staff_notes_count") ||
user.get("user_notes_count") ||
user.get("custom_fields.user_notes_count") ||
0;
component.set("staffNotesCount", count);
component.set("userNotesCount", count);
component.set("emojiEnabled", component.siteSettings.enable_emoji);
component.set("emojiUrl", emojiUrlFor("pencil"));
component.set("user", user);
component.set("staffNotesTitle", I18n.t("staff_notes.show", { count }));
component.set("userNotesTitle", I18n.t("user_notes.show", { count }));
},
actions: {
showStaffNotes() {
showUserNotes() {
this.parentView.parentView._close();
const store = getOwner(this).lookup("store:main");
const user = this.get("args.user");
showStaffNotes(store, user.get("id"), count =>
this.set("staffNotesCount", count)
);
showUserNotes(store, user.get("id"), count => {
if (this.isDestroying || this.isDestroyed) {
return;
}
this.set("userNotesCount", count);
});
}
}
};

View File

@ -1,4 +1,4 @@
import { showStaffNotes } from "discourse/plugins/discourse-user-notes/discourse-staff-notes/lib/staff-notes";
import { showUserNotes } from "discourse/plugins/discourse-user-notes/discourse-user-notes/lib/user-notes";
import { getOwner } from "discourse-common/lib/get-owner";
export default {
@ -10,19 +10,19 @@ export default {
setupComponent(args, component) {
const { model } = args;
component.set(
"staffNotesCount",
model.get("staff_notes_count") ||
model.get("custom_fields.staff_notes_count") ||
"userNotesCount",
model.get("user_notes_count") ||
model.get("custom_fields.user_notes_count") ||
0
);
},
actions: {
showStaffNotes() {
showUserNotes() {
const store = getOwner(this).lookup("store:main");
const user = this.get("args.model");
showStaffNotes(store, user.get("id"), count =>
this.set("staffNotesCount", count)
showUserNotes(store, user.get("id"), count =>
this.set("userNotesCount", count)
);
}
}

View File

@ -1,14 +1,14 @@
import showModal from "discourse/lib/show-modal";
import loadScript from "discourse/lib/load-script";
export function showStaffNotes(store, userId, callback, opts) {
export function showUserNotes(store, userId, callback, opts) {
opts = opts || {};
return loadScript("defer/html-sanitizer-bundle").then(() => {
return store.find("staff-note", { user_id: userId }).then(model => {
const controller = showModal("staff-notes", {
return store.find("user-note", { user_id: userId }).then(model => {
const controller = showModal("user-notes", {
model,
title: "staff_notes.title",
title: "user_notes.title",
addModalBodyView: true
});
controller.reset();

View File

@ -0,0 +1 @@
{{show-user-notes show="showUserNotes" count=userNotesCount}}

View File

@ -0,0 +1,3 @@
{{#if userNotesCount}}
{{show-user-notes show="showUserNotes" count=userNotesCount}}
{{/if}}

View File

@ -0,0 +1,9 @@
{{#if userNotesCount}}
<a href {{action "showUserNotes"}} title="{{userNotesTitle}}">
{{#if emojiEnabled}}
<img src={{emojiUrl}} title={{userNotesTitle}} alt="pencil" class="emoji">
{{else}}
{{d-icon "sticky-note"}}
{{/if}}
</a>
{{/if}}

View File

@ -0,0 +1 @@
<li>{{show-user-notes show="showUserNotes" count=userNotesCount}}</li>

View File

@ -28,7 +28,7 @@ export default Ember.Controller.extend({
actions: {
attachNote() {
const note = this.store.createRecord("staff-note");
const note = this.store.createRecord("user-note");
const userId = parseInt(this.get("userId"));
this.set("saving", true);
@ -53,7 +53,7 @@ export default Ember.Controller.extend({
removeNote(note) {
bootbox.confirm(
I18n.t("staff_notes.delete_confirm"),
I18n.t("user_notes.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {

View File

@ -1,9 +1,9 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import { iconNode } from "discourse-common/lib/icon-library";
import { showStaffNotes } from "discourse/plugins/discourse-user-notes/discourse-staff-notes/lib/staff-notes";
import { showUserNotes } from "discourse/plugins/discourse-user-notes/discourse-user-notes/lib/user-notes";
export default {
name: "enable-staff-notes",
name: "enable-user-notes",
initialize(container) {
const siteSettings = container.lookup("site-settings:main");
const currentUser = container.lookup("current-user:main");
@ -17,12 +17,12 @@ export default {
const store = container.lookup("store:main");
withPluginApi("0.8.15", api => {
function widgetShowStaffNotes() {
showStaffNotes(
function widgetshowUserNotes() {
showUserNotes(
store,
this.attrs.user_id,
count => {
this.sendWidgetAction("refreshStaffNotes", count);
this.sendWidgetAction("refreshUserNotes", count);
},
{
postId: this.attrs.id
@ -30,29 +30,29 @@ export default {
);
}
api.attachWidgetAction("post", "refreshStaffNotes", function(count) {
api.attachWidgetAction("post", "refreshUserNotes", function(count) {
const cfs = this.model.get("user_custom_fields") || {};
cfs.staff_notes_count = count;
cfs.user_notes_count = count;
this.model.set("user_custom_fields", cfs);
});
api.modifyClass("controller:user", {
staffNotesCount: null,
userNotesCount: null,
_modelChanged: function() {
this.set(
"staffNotesCount",
this.get("model.custom_fields.staff_notes_count") || 0
"userNotesCount",
this.get("model.custom_fields.user_notes_count") || 0
);
}
.observes("model")
.on("init"),
actions: {
showStaffNotes() {
showUserNotes() {
const user = this.get("model");
showStaffNotes(store, user.get("id"), count =>
this.set("staffNotesCount", count)
showUserNotes(store, user.get("id"), count =>
this.set("userNotesCount", count)
);
}
}
@ -66,8 +66,8 @@ export default {
}
const cfs = dec.attrs.userCustomFields || {};
if (cfs.staff_notes_count > 0) {
return dec.attach("staff-notes-icon");
if (cfs.user_notes_count > 0) {
return dec.attach("user-notes-icon");
}
});
@ -77,24 +77,24 @@ export default {
}
const cfs = dec.attrs.userCustomFields || {};
if (cfs.staff_notes_count > 0) {
return dec.attach("staff-notes-icon");
if (cfs.user_notes_count > 0) {
return dec.attach("user-notes-icon");
}
});
api.decorateWidget("post-admin-menu:after", dec => {
return dec.attach("post-admin-menu-button", {
icon: "pencil",
label: "staff_notes.attach",
action: "showStaffNotes"
label: "user_notes.attach",
action: "showUserNotes"
});
});
api.attachWidgetAction("post", "showStaffNotes", widgetShowStaffNotes);
api.attachWidgetAction("post", "showUserNotes", widgetshowUserNotes);
api.createWidget("staff-notes-icon", {
tagName: "span.staff-notes-icon",
click: widgetShowStaffNotes,
api.createWidget("user-notes-icon", {
tagName: "span.user-notes-icon",
click: widgetshowUserNotes,
html() {
if (siteSettings.enable_emoji) {

View File

@ -1,8 +1,8 @@
<a {{action "show"}} href class="btn">
{{d-icon "pencil-alt"}}
{{#if showCount}}
{{i18n 'staff_notes.show' count=count}}
{{i18n 'user_notes.show' count=count}}
{{else}}
{{i18n 'staff_notes.title'}}
{{i18n 'user_notes.title'}}
{{/if}}
</a>

View File

@ -1,6 +1,6 @@
{{#if siteSettings.user_notes_enabled}}
{{admin-report
dataSourceName="staff_notes"
dataSourceName="user_notes"
startDate=lastWeek
endDate=endDate}}
{{/if}}

View File

@ -1,9 +1,9 @@
{{#d-modal-body class="staff-notes-modal"}}
{{#d-modal-body class="user-notes-modal"}}
{{textarea value=newNote}}
{{d-button action="attachNote" label="staff_notes.attach" class="btn-primary" disabled=attachDisabled}}
{{d-button action="attachNote" label="user_notes.attach" class="btn-primary" disabled=attachDisabled}}
{{#each model as |n|}}
<div class='staff-note'>
<div class='user-note'>
<div class='posted-by'>
{{#user-link user=n.created_by}}
{{avatar n.created_by imageSize="small"}}
@ -19,7 +19,7 @@
{{d-button action=(action "removeNote" n)
icon="far-trash-alt"
class="btn-small btn-danger"
title="staff_notes.remove"}}
title="user_notes.remove"}}
</span>
{{/if}}
</div>
@ -30,7 +30,7 @@
{{#if n.post_id}}
<a class="btn btn-small" href={{n.post_url}}>
{{i18n "staff_notes.show_post"}}
{{i18n "user_notes.show_post"}}
</a>
{{/if}}
</div>

View File

@ -1,10 +1,10 @@
#discourse-modal {
.modal-body.staff-notes-modal {
.modal-body.user-notes-modal {
max-height: 80vh !important;
}
}
.modal-body.staff-notes-modal {
.modal-body.user-notes-modal {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
@ -36,7 +36,7 @@
}
}
.staff-note {
.user-note {
border-top: 1px solid $primary-low;
padding-top: 1em;
@ -53,7 +53,7 @@
}
}
.staff-notes-icon {
.user-notes-icon {
.mobile-view & {
order: 2;
margin-left: 10px;
@ -61,7 +61,7 @@
cursor: pointer;
}
.admin-report.staff-notes {
.admin-report.user-notes {
grid-column: span 12;
.admin-report-table {

View File

@ -1,6 +1,6 @@
en:
js:
staff_notes:
user_notes:
title: "User Notes"
attach: "Add User Note"
remove: "Remove User Note"

View File

@ -3,13 +3,13 @@ en:
user_notes_enabled: "Allow staff users to attach notes to users"
user_notes_moderators_delete: "Allow moderators to delete user notes"
staff_notes:
user_notes:
official_warning: "Received an official warning from @%{username} -- %{warning_link}"
user_suspended: "@%{username} suspended this account until %{suspended_till}. Reason: %{reason}"
user_silenced: "@%{username} silenced this account until %{silenced_till}. Reason: %{reason}"
reports:
staff_notes:
user_notes:
title: "User notes"
description: "List most recent user notes."
labels:

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class RenameStaffNotesRowsAndFields < ActiveRecord::Migration[5.2]
def change
execute "UPDATE user_custom_fields SET name = 'user_notes_count' WHERE name = 'staff_notes_count'"
execute "UPDATE plugin_store_rows SET plugin_name = 'user_notes' WHERE plugin_name = 'staff_notes'"
end
end

View File

@ -4,24 +4,24 @@
# about: Gives the ability for staff members to attach notes to users
# version: 0.0.2
# authors: Robin Ward
# url: https://github.com/discourse/discourse-staff-notes
# url: https://github.com/discourse/discourse-user-notes
enabled_site_setting :user_notes_enabled
register_asset 'stylesheets/staff_notes.scss'
register_asset 'stylesheets/user_notes.scss'
register_svg_icon "sticky-note" if respond_to?(:register_svg_icon)
STAFF_NOTE_COUNT_FIELD = "staff_notes_count"
COUNT_FIELD = "user_notes_count"
after_initialize do
require_dependency 'user'
module ::DiscourseStaffNotes
module ::DiscourseUserNotes
class Engine < ::Rails::Engine
engine_name "discourse_staff_notes"
isolate_namespace DiscourseStaffNotes
engine_name "discourse_user_notes"
isolate_namespace DiscourseUserNotes
end
def self.key_for(user_id)
@ -29,7 +29,7 @@ after_initialize do
end
def self.notes_for(user_id)
PluginStore.get('staff_notes', key_for(user_id)) || []
PluginStore.get('user_notes', key_for(user_id)) || []
end
def self.add_note(user, raw, created_by, opts = nil)
@ -45,9 +45,9 @@ after_initialize do
}.merge(opts)
notes << record
::PluginStore.set("staff_notes", key_for(user.id), notes)
::PluginStore.set("user_notes", key_for(user.id), notes)
user.custom_fields[STAFF_NOTE_COUNT_FIELD] = notes.size
user.custom_fields[COUNT_FIELD] = notes.size
user.save_custom_fields
record
@ -58,18 +58,18 @@ after_initialize do
notes.reject! { |n| n[:id] == note_id }
if notes.size > 0
::PluginStore.set("staff_notes", key_for(user.id), notes)
::PluginStore.set("user_notes", key_for(user.id), notes)
else
::PluginStore.remove("staff_notes", key_for(user.id))
::PluginStore.remove("user_notes", key_for(user.id))
end
user.custom_fields[STAFF_NOTE_COUNT_FIELD] = notes.size
user.custom_fields[COUNT_FIELD] = notes.size
user.save_custom_fields
end
end
require_dependency 'application_serializer'
class ::StaffNoteSerializer < ApplicationSerializer
class ::UserNoteSerializer < ApplicationSerializer
attributes(
:id,
:user_id,
@ -103,7 +103,7 @@ after_initialize do
end
def can_delete
scope.can_delete_staff_notes?
scope.can_delete_user_notes?
end
def post_id
@ -131,7 +131,7 @@ after_initialize do
end
require_dependency 'application_controller'
class DiscourseStaffNotes::StaffNotesController < ::ApplicationController
class DiscourseUserNotes::UserNotesController < ::ApplicationController
before_action :ensure_logged_in
before_action :ensure_staff
@ -139,38 +139,38 @@ after_initialize do
user = User.where(id: params[:user_id]).first
raise Discourse::NotFound if user.blank?
notes = ::DiscourseStaffNotes.notes_for(params[:user_id])
notes = ::DiscourseUserNotes.notes_for(params[:user_id])
render json: {
extras: { username: user.username },
staff_notes: create_json(notes.reverse)
user_notes: create_json(notes.reverse)
}
end
def create
user = User.where(id: params[:staff_note][:user_id]).first
user = User.where(id: params[:user_note][:user_id]).first
raise Discourse::NotFound if user.blank?
extras = {}
if post_id = params[:staff_note][:post_id]
if post_id = params[:user_note][:post_id]
extras[:post_id] = post_id
end
staff_note = ::DiscourseStaffNotes.add_note(
user_note = ::DiscourseUserNotes.add_note(
user,
params[:staff_note][:raw],
params[:user_note][:raw],
current_user.id,
extras
)
render json: create_json(staff_note)
render json: create_json(user_note)
end
def destroy
user = User.where(id: params[:user_id]).first
raise Discourse::NotFound if user.blank?
raise Discourse::InvalidAccess.new unless guardian.can_delete_staff_notes?
raise Discourse::InvalidAccess.new unless guardian.can_delete_user_notes?
::DiscourseStaffNotes.remove_note(user, params[:id])
::DiscourseUserNotes.remove_note(user, params[:id])
render json: success_json
end
@ -196,36 +196,36 @@ after_initialize do
obj[:post] = Post.with_deleted.where(id: obj[:post_id]).first
end
serialize_data(obj, ::StaffNoteSerializer)
serialize_data(obj, ::UserNoteSerializer)
end
end
whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD)
whitelist_staff_user_custom_field(COUNT_FIELD)
add_to_class(Guardian, :can_delete_staff_notes?) do
add_to_class(Guardian, :can_delete_user_notes?) do
(SiteSetting.user_notes_moderators_delete? && user.staff?) || user.admin?
end
add_to_serializer(:admin_detailed_user, :staff_notes_count, false) do
object.custom_fields && object.custom_fields['staff_notes_count'].to_i
add_to_serializer(:admin_detailed_user, :user_notes_count, false) do
object.custom_fields && object.custom_fields['user_notes_count'].to_i
end
DiscourseStaffNotes::Engine.routes.draw do
get '/' => 'staff_notes#index'
post '/' => 'staff_notes#create'
delete '/:id' => 'staff_notes#destroy'
DiscourseUserNotes::Engine.routes.draw do
get '/' => 'user_notes#index'
post '/' => 'user_notes#create'
delete '/:id' => 'user_notes#destroy'
end
Discourse::Application.routes.append do
mount ::DiscourseStaffNotes::Engine, at: "/staff_notes"
mount ::DiscourseUserNotes::Engine, at: "/user_notes"
end
add_model_callback(UserWarning, :after_commit, on: :create) do
user = User.find_by_id(self.user_id)
created_by_user = User.find_by_id(self.created_by_id)
warning_topic = Topic.find_by_id(self.topic_id)
raw_note = I18n.t("staff_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
::DiscourseStaffNotes.add_note(
raw_note = I18n.t("user_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
::DiscourseUserNotes.add_note(
user,
raw_note,
Discourse::SYSTEM_USER_ID,
@ -237,8 +237,8 @@ after_initialize do
return unless self.action == UserHistory.actions[:suspend_user]
target_user = User.find_by_id(self.target_user_id)
created_by_user = User.find_by_id(self.acting_user_id)
raw_note = I18n.t("staff_notes.user_suspended", username: created_by_user.username, suspended_till: I18n.l(target_user.suspended_till, format: :date_only), reason: self.details)
::DiscourseStaffNotes.add_note(
raw_note = I18n.t("user_notes.user_suspended", username: created_by_user.username, suspended_till: I18n.l(target_user.suspended_till, format: :date_only), reason: self.details)
::DiscourseUserNotes.add_note(
target_user,
raw_note,
Discourse::SYSTEM_USER_ID,
@ -249,7 +249,7 @@ after_initialize do
on(:user_silenced) do |details|
raw_note = I18n.t(
"staff_notes.user_silenced",
"user_notes.user_silenced",
username: details[:silenced_by]&.username || '',
silenced_till: I18n.l(details[:silenced_till], format: :date_only),
reason: details[:reason]
@ -259,7 +259,7 @@ after_initialize do
note_args = { post_id: post.id, topic_id: post.topic_id }
end
::DiscourseStaffNotes.add_note(
::DiscourseUserNotes.add_note(
details[:user],
raw_note,
Discourse::SYSTEM_USER_ID,
@ -268,7 +268,7 @@ after_initialize do
end
if respond_to? :add_report
add_report('staff_notes') do |report|
add_report('user_notes') do |report|
report.modes = [:table]
report.data = []
@ -281,7 +281,7 @@ after_initialize do
id: :user_id,
avatar: :user_avatar_template,
},
title: I18n.t("reports.staff_notes.labels.user")
title: I18n.t("reports.user_notes.labels.user")
},
{
type: :user,
@ -290,21 +290,22 @@ after_initialize do
id: :moderator_id,
avatar: :moderator_avatar_template,
},
title: I18n.t("reports.staff_notes.labels.moderator")
title: I18n.t("reports.user_notes.labels.moderator")
},
{ type: :text, property: :note, title: I18n.t("reports.staff_notes.labels.note") }
{ type: :text, property: :note, title: I18n.t("reports.user_notes.labels.note") }
]
values = []
values = PluginStoreRow.where(plugin_name: 'staff_notes')
values = PluginStoreRow.where(plugin_name: 'user_notes')
.where("value::json->0->>'created_at'>?", report.start_date)
.where("value::json->0->>'created_at'<?", report.end_date)
.pluck(:value)
values.each do |value|
notes = JSON.parse(value)
notes.each do |note|
data = {}
note = JSON.parse(value)[0]
created_at = Time.parse(note['created_at'])
user = User.find_by(id: note['user_id'])
moderator = User.find_by(id: note['created_by'])
@ -318,9 +319,11 @@ after_initialize do
data[:moderator_username] = moderator.username_lower
data[:moderator_avatar_template] = User.avatar_template(moderator.username_lower, moderator.uploaded_avatar_id)
data[:note] = note['raw']
report.data << data
end
end
end
end
end
end

View File

@ -15,7 +15,7 @@ describe UserHistory do
it "should create staff note for suspension" do
UserHistory.create!(action: UserHistory.actions[:suspend_user], target_user_id: user.id, acting_user_id: admin.id)
expect(PluginStore.get('staff_notes', "notes:#{user.id}")).to be_present
expect(PluginStore.get('user_notes', "notes:#{user.id}")).to be_present
end
end
end

View File

@ -16,7 +16,7 @@ describe UserWarning do
it "should create staff note for warning" do
UserWarning.create(topic_id: topic.id, user_id: user.id, created_by_id: admin.id)
expect(PluginStore.get('staff_notes', "notes:#{user.id}")).to be_present
expect(PluginStore.get('user_notes', "notes:#{user.id}")).to be_present
end
end
end