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
|
|
|
|
|
|
|
# Abort if the post is blank, or is non-regular (e.g. a "topic closed" notification)
|
|
|
|
return if post.blank? || post.post_type != Post.types[:regular]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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
|
|
|
|
f_prec = { "mute" => 0, "thread" => 1, "watch" => 2, "follow" => 3 } #(mute always wins; thread beats watch beats follow)
|
|
|
|
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
|
|
|
|
|
|
|
# If this is not the first post, discard all "follow" rules
|
2017-10-03 05:42:07 -04:00
|
|
|
if !post.is_first_post?
|
2017-06-29 12:50:54 -04:00
|
|
|
matching_rules = matching_rules.select { |rule| rule.filter != "follow" }
|
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
|