2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration
|
2017-06-27 14:21:27 -04:00
|
|
|
module Manager
|
|
|
|
def self.guardian
|
2017-07-03 06:08:14 -04:00
|
|
|
Guardian.new(User.find_by(username: SiteSetting.chat_integration_discourse_username))
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.trigger_notifications(post_id)
|
|
|
|
post = Post.find_by(id: post_id)
|
|
|
|
|
|
|
|
# Abort if the chat_user doesn't have permission to see the post
|
2017-08-01 15:53:39 -04:00
|
|
|
return if !guardian.can_see?(post)
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2023-04-06 07:55:47 -04:00
|
|
|
# Abort if the post is blank
|
|
|
|
return if post.blank?
|
|
|
|
|
2023-04-18 14:27:17 -04:00
|
|
|
# Abort if post is not either regular, or a 'tags_changed'/'category_changed' small action
|
2023-04-06 07:55:47 -04:00
|
|
|
if (post.post_type != Post.types[:regular]) &&
|
2023-04-18 14:27:17 -04:00
|
|
|
!(
|
|
|
|
post.post_type == Post.types[:small_action] &&
|
|
|
|
%w[tags_changed category_changed].include?(post.action_code)
|
|
|
|
)
|
2023-04-06 07:55:47 -04:00
|
|
|
return
|
|
|
|
end
|
2017-06-27 14:21:27 -04:00
|
|
|
|
|
|
|
topic = post.topic
|
2017-07-28 15:27:49 -04:00
|
|
|
return if topic.blank?
|
|
|
|
|
|
|
|
# If it's a private message, filter rules by groups, otherwise filter rules by category
|
|
|
|
if topic.archetype == Archetype.private_message
|
|
|
|
group_ids_with_access = topic.topic_allowed_groups.pluck(:group_id)
|
|
|
|
return if group_ids_with_access.empty?
|
2022-12-29 07:31:05 -05:00
|
|
|
matching_rules =
|
|
|
|
DiscourseChatIntegration::Rule.with_type("group_message").with_group_ids(
|
|
|
|
group_ids_with_access,
|
|
|
|
)
|
2017-07-28 15:27:49 -04:00
|
|
|
else
|
2022-12-29 07:31:05 -05:00
|
|
|
matching_rules =
|
|
|
|
DiscourseChatIntegration::Rule.with_type("normal").with_category_id(topic.category_id)
|
2017-07-28 15:27:49 -04:00
|
|
|
if topic.category # Also load the rules for the wildcard category
|
2022-12-29 07:31:05 -05:00
|
|
|
matching_rules += DiscourseChatIntegration::Rule.with_type("normal").with_category_id(nil)
|
2017-08-01 12:11:34 -04:00
|
|
|
end
|
|
|
|
|
2017-08-01 17:15:27 -04:00
|
|
|
# If groups are mentioned, check for any matching rules and append them
|
|
|
|
mentions = post.raw_mentions
|
|
|
|
if mentions && mentions.length > 0
|
2022-12-29 07:31:05 -05:00
|
|
|
groups = Group.where("LOWER(name) IN (?)", mentions)
|
2017-08-01 17:15:27 -04:00
|
|
|
if groups.exists?
|
2022-12-29 07:31:05 -05:00
|
|
|
matching_rules +=
|
|
|
|
DiscourseChatIntegration::Rule.with_type("group_mention").with_group_ids(
|
|
|
|
groups.map(&:id),
|
|
|
|
)
|
2017-08-01 17:15:27 -04:00
|
|
|
end
|
2017-07-28 15:27:49 -04:00
|
|
|
end
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2023-04-06 07:55:47 -04:00
|
|
|
if post.action_code == "tags_changed"
|
|
|
|
# Post is a small_action post regarding tags changing for the topic. Check if any tags were _added_
|
|
|
|
# and if so, corresponding rules with `filter: tag_added`
|
|
|
|
tags_added = post.custom_fields["tags_added"]
|
|
|
|
tags_added = [tags_added].compact if !tags_added.is_a?(Array)
|
|
|
|
return if tags_added.blank?
|
|
|
|
|
|
|
|
tags_removed = post.custom_fields["tags_removed"]
|
|
|
|
tags_removed = [tags_removed].compact if !tags_removed.is_a?(Array)
|
|
|
|
|
|
|
|
unchanged_tags = topic.tags.map(&:name) - tags_added - tags_removed
|
|
|
|
|
|
|
|
matching_rules =
|
|
|
|
matching_rules.select do |rule|
|
|
|
|
# Only rules that match this post, are ones where the filter is "tag_added"
|
|
|
|
next false if rule.filter != "tag_added"
|
|
|
|
next true if rule.tags.blank?
|
|
|
|
|
|
|
|
# Skip if the topic already has one of the tags in the rule, applied
|
|
|
|
next false if unchanged_tags.any? && (unchanged_tags & rule.tags).any?
|
|
|
|
|
|
|
|
# We don't need to do any additional filtering here because topics are filtered
|
|
|
|
# by tag later
|
|
|
|
true
|
|
|
|
end
|
2023-04-21 08:30:03 -04:00
|
|
|
else
|
|
|
|
matching_rules = matching_rules.select { |rule| rule.filter != "tag_added" }
|
2023-04-06 07:55:47 -04:00
|
|
|
end
|
|
|
|
|
2017-06-27 14:21:27 -04:00
|
|
|
# If tagging is enabled, thow away rules that don't apply to this topic
|
|
|
|
if SiteSetting.tagging_enabled
|
|
|
|
topic_tags = topic.tags.present? ? topic.tags.pluck(:name) : []
|
2022-12-29 07:31:05 -05:00
|
|
|
matching_rules =
|
|
|
|
matching_rules.select do |rule|
|
|
|
|
next true if rule.tags.nil? || rule.tags.empty? # Filter has no tags specified
|
|
|
|
any_tags_match = !((rule.tags & topic_tags).empty?)
|
|
|
|
next any_tags_match # If any tags match, keep this filter, otherwise throw away
|
|
|
|
end
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
2017-08-01 16:09:01 -04:00
|
|
|
# Sort by order of precedence
|
2022-12-29 07:31:05 -05:00
|
|
|
t_prec = { "group_message" => 0, "group_mention" => 1, "normal" => 2 } # Group things win
|
2023-04-06 07:55:47 -04:00
|
|
|
f_prec = { "mute" => 0, "thread" => 1, "watch" => 2, "follow" => 3, "tag_added" => 4 } #(mute always wins; thread beats watch beats follow)
|
2022-12-29 07:31:05 -05:00
|
|
|
sort_func =
|
|
|
|
proc { |a, b| [t_prec[a.type], f_prec[a.filter]] <=> [t_prec[b.type], f_prec[b.filter]] }
|
2017-06-27 14:21:27 -04:00
|
|
|
matching_rules = matching_rules.sort(&sort_func)
|
|
|
|
|
|
|
|
# Take the first rule for each channel
|
2017-07-13 16:47:15 -04:00
|
|
|
uniq_func = proc { |rule| [rule.channel_id] }
|
2017-06-27 14:21:27 -04:00
|
|
|
matching_rules = matching_rules.uniq(&uniq_func)
|
|
|
|
|
|
|
|
# If a matching rule is set to mute, we can discard it now
|
2017-06-29 12:50:54 -04:00
|
|
|
matching_rules = matching_rules.select { |rule| rule.filter != "mute" }
|
2017-06-27 14:21:27 -04:00
|
|
|
|
2023-04-18 14:27:17 -04:00
|
|
|
# If this is not the first post, discard all "follow" rules. Unless it's a
|
|
|
|
# category_changed action post. If category changed, filter out and rules
|
|
|
|
# that aren't specific to a category
|
2017-10-03 05:42:07 -04:00
|
|
|
if !post.is_first_post?
|
2023-04-18 14:27:17 -04:00
|
|
|
matching_rules =
|
|
|
|
if post.action_code == "category_changed"
|
|
|
|
matching_rules.select { |rule| rule.category_id.present? }
|
|
|
|
else
|
|
|
|
matching_rules.select { |rule| rule.filter != "follow" }
|
|
|
|
end
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# All remaining rules now require a notification to be sent
|
|
|
|
# If there are none left, abort
|
|
|
|
return false if matching_rules.empty?
|
|
|
|
|
|
|
|
# Loop through each rule, and trigger appropriate notifications
|
|
|
|
matching_rules.each do |rule|
|
2017-07-18 18:08:06 -04:00
|
|
|
# If there are any issues, skip to the next rule
|
|
|
|
next unless channel = rule.channel
|
2021-07-13 15:36:16 -04:00
|
|
|
next unless provider = ::DiscourseChatIntegration::Provider.get_by_name(channel.provider)
|
|
|
|
next unless is_enabled = ::DiscourseChatIntegration::Provider.is_enabled(provider)
|
2017-07-18 18:08:06 -04:00
|
|
|
|
|
|
|
begin
|
2020-06-15 11:45:25 -04:00
|
|
|
provider.trigger_notification(post, channel, rule)
|
2022-12-29 07:31:05 -05:00
|
|
|
channel.update_attribute("error_key", nil) if channel.error_key
|
2017-07-18 18:08:06 -04:00
|
|
|
rescue => e
|
2022-12-29 07:31:05 -05:00
|
|
|
if e.class == (DiscourseChatIntegration::ProviderError) && e.info.key?(:error_key) &&
|
|
|
|
!e.info[:error_key].nil?
|
|
|
|
channel.update_attribute("error_key", e.info[:error_key])
|
2017-07-18 18:08:06 -04:00
|
|
|
else
|
2022-12-29 07:31:05 -05:00
|
|
|
channel.update_attribute("error_key", "chat_integration.channel_exception")
|
2017-07-04 14:37:56 -04:00
|
|
|
end
|
2022-12-29 07:31:05 -05:00
|
|
|
channel.update_attribute("error_info", JSON.pretty_generate(e.try(:info)))
|
2017-07-18 18:08:06 -04:00
|
|
|
|
|
|
|
# Log the error
|
2018-08-20 07:05:59 -04:00
|
|
|
# Discourse.handle_job_exception(e,
|
|
|
|
# message: "Triggering notifications failed",
|
|
|
|
# extra: { provider_name: provider::PROVIDER_NAME,
|
|
|
|
# channel: rule.channel,
|
|
|
|
# post_id: post.id,
|
2021-07-13 15:36:16 -04:00
|
|
|
# error_info: e.class == DiscourseChatIntegration::ProviderError ? e.info : nil }
|
2018-08-20 07:05:59 -04:00
|
|
|
# )
|
2017-06-27 14:21:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|