From d7c74701f2e3392227f24c23df0d6ff692c74282 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Tue, 21 Feb 2017 11:26:45 -0500 Subject: [PATCH] optionally apply a regex to post body when assigning stuff out --- config/locales/server.en.yml | 2 ++ config/settings.yml | 2 ++ plugin.rb | 26 ++++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index d13de81..de788f4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -4,6 +4,8 @@ en: assigns_user_url_path: "Path to link all user links for assigned (use: {username} to subtitue username)" assigns_by_staff_mention: "If a staff member mentions another staff member, topic is automatically assigned" unassign_creates_tracking_post: "If you unassign a topic a whisper or small action will be created to track change" + assign_self_regex: "Regex that needs to pass for self assign. Example 'my list'" + assign_other_regex: "Regex that needs to pass for assigning topics to others via mention. Example 'your list'." discourse_assign: assigned_to: "Topic assigned to @%{username}" unassigned: "Topic was unassigned" diff --git a/config/settings.yml b/config/settings.yml index de7000f..b397801 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -2,6 +2,8 @@ plugins: assigns_by_staff_mention: false unassign_creates_tracking_post: true assigns_public: false + assign_self_regex: "" + assign_other_regex: "" assigns_user_url_path: client: true default: "/latest?assigned={username}" diff --git a/plugin.rb b/plugin.rb index 78f1ad2..bbfc262 100644 --- a/plugin.rb +++ b/plugin.rb @@ -45,14 +45,36 @@ SQL puts "#{assigned} topics where automatically assigned to staff members" end + def self.assign_self_passes?(post) + return false unless SiteSetting.assign_self_regex.present? + regex = Regexp.new(SiteSetting.assign_self_regex) rescue nil + + !!(regex && regex.match(post.raw)) + end + + def self.assign_other_passes?(post) + return true unless SiteSetting.assign_other_regex.present? + regex = Regexp.new(SiteSetting.assign_other_regex) rescue nil + + !!(regex && regex.match(post.raw)) + end + def self.auto_assign(post, force: false) return unless SiteSetting.assigns_by_staff_mention if post.user && post.topic && post.user.staff? can_assign = force || post.topic.custom_fields["assigned_to_id"].nil? - if can_assign && is_last_staff_post?(post) && user = mentioned_staff(post) + + assign_other = assign_other_passes?(post) && mentioned_staff(post) + assign_self = assign_self_passes?(post) && post.user + + if can_assign && is_last_staff_post?(post) assigner = new(post.topic, post.user) - assigner.assign(user, silent: true) + if assign_other + assigner.assign(assign_other, silent: true) + elsif assign_self + assigner.assign(assign_self, silent: true) + end end end end