From 569a87b136afbff0ec2a3d07e4e0b7b598537640 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 12 Jan 2018 16:09:46 -0500 Subject: [PATCH] FEATURE: New site setting to ensure flags are claimed --- .../show-not-assigned.hbs | 22 ++++++++++++------ .../initializers/extend-for-assigns.js.es6 | 23 +++++++++++++++---- config/locales/client.en.yml | 1 + config/locales/server.en.yml | 4 +++- config/settings.yml | 3 +++ plugin.rb | 8 +++++++ 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/assets/javascripts/discourse-assign/connectors/flagged-post-below-controls/show-not-assigned.hbs b/assets/javascripts/discourse-assign/connectors/flagged-post-below-controls/show-not-assigned.hbs index af28b80..2936879 100644 --- a/assets/javascripts/discourse-assign/connectors/flagged-post-below-controls/show-not-assigned.hbs +++ b/assets/javascripts/discourse-assign/connectors/flagged-post-below-controls/show-not-assigned.hbs @@ -1,12 +1,20 @@ {{#unless canAct}} - {{#if flaggedPost.topic.assigned_to_user}} -
-
- {{i18n "discourse_assign.cant_act"}} + {{#if actableFilter}} + {{#if flaggedPost.topic.assigned_to_user}} +
+
+ {{i18n "discourse_assign.cant_act"}} +
+
+ {{assigned-to user=flaggedPost.topic.assigned_to_user}} +
-
- {{assigned-to user=flaggedPost.topic.assigned_to_user}} + {{else}} +
+
+ {{i18n "discourse_assign.cant_act_unclaimed"}} +
-
+ {{/if}} {{/if}} {{/unless}} diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 index 1d136f2..a06f4ff 100644 --- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 +++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 @@ -40,13 +40,26 @@ function modifySelectKit(api) { function initialize(api) { // You can't act on flags claimed by another user api.modifyClass('component:flagged-post', { - @computed('flaggedPost.topic.assigned_to_user_id', 'filter') - canAct(assignedToUserId, filter) { - if (this.siteSettings.assign_locks_flags && assignedToUserId && this.currentUser.id !== assignedToUserId) { - return false; + @computed('flaggedPost.topic.assigned_to_user_id') + canAct(assignedToUserId) { + let { siteSettings } = this; + + if (siteSettings.assign_locks_flags) { + + let unassigned = (this.currentUser.id !== assignedToUserId); + + // Can never act on another user's flags + if (assignedToUserId && unassigned) { + return false; + } + + // If flags require assignment + if (this.siteSettings.flags_require_assign && unassigned) { + return false; + } } - return this._super(filter); + return this.get('actableFilter'); }, didInsertElement() { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index cce266a..669f30f 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -5,6 +5,7 @@ en: unassigned: "unassigned %{who} %{when}" discourse_assign: cant_act: "You cannot act on flags that have been assigned to other users" + cant_act_unclaimed: "You must claim this topic before acting on flags." assigned: "Assigned" assigned_to: "Assigned to" assign_notification: "

{{username}} {{description}}

" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 300bdb9..f4f71c8 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -11,11 +11,13 @@ en: unassign_on_close: "When a topic is closed unassign topic" assign_locks_flags: "When a topic is assigned to a staff member, its flags can only be handled by that person" assign_mailer_enabled: "When enabled, the assigned user will receive a notification email on each assignment" + flags_require_assign: "When enabled, flags cannot be handled unless assigned to a user." discourse_assign: assigned_to: "Topic assigned to @%{username}" unassigned: "Topic was unassigned" already_claimed: "That topic has already been claimed." - flag_assigned: "Sorry, flag's topic is assigned to another user" + flag_assigned: "Sorry, that flag's topic is assigned to another user" + flag_unclaimed: "You must claim that topic before acting on the flag" assign_mailer: title: "Assign Mailer" subject_template: "[%{email_prefix}] %{assignee_name} assigned you to '%{topic_title}'!" diff --git a/config/settings.yml b/config/settings.yml index 4a63fc9..44c98f6 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -15,4 +15,7 @@ plugins: assign_locks_flags: default: true client: true + flags_require_assign: + default: false + client: true assign_mailer_enabled: false diff --git a/plugin.rb b/plugin.rb index 0bda268..cd7eec3 100644 --- a/plugin.rb +++ b/plugin.rb @@ -21,6 +21,7 @@ after_initialize do # not assigned to them DiscourseEvent.on(:before_staff_flag_action) do |args| if SiteSetting.assign_locks_flags? + if custom_fields = args[:post].topic.custom_fields if assigned_to_id = custom_fields['assigned_to_id'] unless assigned_to_id.to_i == args[:user].id @@ -30,8 +31,15 @@ after_initialize do custom_message: 'discourse_assign.flag_assigned' ) end + elsif SiteSetting.flags_require_assign? + raise Discourse::InvalidAccess.new( + "Flags must be assigned before they can be acted on", + nil, + custom_message: 'discourse_assign.flag_unclaimed' + ) end end + end end