2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-03 06:08:14 -04:00
|
|
|
# name: discourse-chat-integration
|
2023-11-08 14:27:54 -05:00
|
|
|
# about: Allows integration with several external chat system providers
|
|
|
|
# meta_topic_id: 66522
|
2017-06-26 10:08:06 -04:00
|
|
|
# version: 0.1
|
2017-07-03 06:08:14 -04:00
|
|
|
# url: https://github.com/discourse/discourse-chat-integration
|
2017-07-13 08:32:11 -04:00
|
|
|
# author: David Taylor
|
2022-03-04 13:27:42 -05:00
|
|
|
# transpile_js: true
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2017-07-03 06:08:14 -04:00
|
|
|
enabled_site_setting :chat_integration_enabled
|
2017-06-26 10:08:06 -04:00
|
|
|
|
2017-07-03 17:11:17 -04:00
|
|
|
register_asset "stylesheets/chat-integration-admin.scss"
|
|
|
|
|
2018-11-01 18:03:39 -04:00
|
|
|
register_svg_icon "rocket" if respond_to?(:register_svg_icon)
|
2020-06-15 11:45:25 -04:00
|
|
|
register_svg_icon "fa-arrow-circle-o-right" if respond_to?(:register_svg_icon)
|
2018-11-01 18:03:39 -04:00
|
|
|
|
2017-07-03 11:28:26 -04:00
|
|
|
# Site setting validators must be loaded before initialize
|
2021-07-13 15:36:16 -04:00
|
|
|
require_relative "lib/discourse_chat_integration/provider/slack/slack_enabled_setting_validator"
|
2017-06-29 12:50:54 -04:00
|
|
|
|
2017-06-26 10:08:06 -04:00
|
|
|
after_initialize do
|
2021-07-13 15:36:16 -04:00
|
|
|
require_relative "app/initializers/discourse_chat_integration"
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2017-10-09 22:08:26 -04:00
|
|
|
on(:post_created) do |post|
|
|
|
|
# This will run for every post, even PMs. Don't worry, they're filtered out later.
|
|
|
|
time = SiteSetting.chat_integration_delay_seconds.seconds
|
|
|
|
Jobs.enqueue_in(time, :notify_chats, post_id: post.id)
|
2017-06-26 14:19:50 -04:00
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
add_admin_route "chat_integration.menu_title", "chat-integration"
|
2017-06-26 14:19:50 -04:00
|
|
|
|
2018-08-20 07:05:59 -04:00
|
|
|
AdminDashboardData.add_problem_check do
|
2022-03-31 07:14:40 -04:00
|
|
|
next if !SiteSetting.chat_integration_enabled
|
2022-03-25 11:53:49 -04:00
|
|
|
|
2018-12-04 06:45:17 -05:00
|
|
|
error = false
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Channel.find_each do |channel|
|
2022-03-25 11:53:49 -04:00
|
|
|
next if channel.error_key.blank?
|
|
|
|
next if !::DiscourseChatIntegration::Provider.is_enabled(channel.provider)
|
|
|
|
error = true
|
2018-08-20 07:05:59 -04:00
|
|
|
end
|
2018-11-07 17:26:36 -05:00
|
|
|
|
|
|
|
if error
|
|
|
|
base_path = Discourse.respond_to?(:base_path) ? Discourse.base_path : Discourse.base_uri
|
|
|
|
I18n.t("chat_integration.admin_error", base_path: base_path)
|
|
|
|
end
|
2018-08-20 07:05:59 -04:00
|
|
|
end
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Provider.mount_engines
|
2022-02-25 14:17:20 -05:00
|
|
|
|
|
|
|
if defined?(DiscourseAutomation)
|
2022-12-29 07:31:05 -05:00
|
|
|
add_automation_scriptable("send_slack_message") do
|
2022-02-25 14:17:20 -05:00
|
|
|
field :message, component: :message, required: true, accepts_placeholders: true
|
|
|
|
field :url, component: :text, required: true
|
|
|
|
field :channel, component: :text, required: true
|
|
|
|
|
|
|
|
version 1
|
|
|
|
|
|
|
|
triggerables %i[point_in_time recurring]
|
|
|
|
|
|
|
|
script do |context, fields, automation|
|
|
|
|
sender = Discourse.system_user
|
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
content = fields.dig("message", "value")
|
|
|
|
url = fields.dig("url", "value")
|
2022-02-25 14:17:20 -05:00
|
|
|
full_content = "#{content} - #{url}"
|
2022-12-29 07:31:05 -05:00
|
|
|
channel_name = fields.dig("channel", "value")
|
|
|
|
channel =
|
|
|
|
DiscourseChatIntegration::Channel.new(
|
|
|
|
provider: "slack",
|
|
|
|
data: {
|
|
|
|
identifier: "##{channel_name}",
|
|
|
|
},
|
|
|
|
)
|
2022-02-25 14:17:20 -05:00
|
|
|
|
|
|
|
icon_url =
|
|
|
|
if SiteSetting.chat_integration_slack_icon_url.present?
|
|
|
|
"#{Discourse.base_url}#{SiteSetting.chat_integration_slack_icon_url}"
|
2022-12-29 07:31:05 -05:00
|
|
|
elsif (
|
|
|
|
url = (SiteSetting.try(:site_logo_small_url) || SiteSetting.logo_small_url)
|
|
|
|
).present?
|
2022-02-25 14:17:20 -05:00
|
|
|
"#{Discourse.base_url}#{url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
slack_username =
|
|
|
|
if SiteSetting.chat_integration_slack_username.present?
|
|
|
|
SiteSetting.chat_integration_slack_username
|
|
|
|
else
|
|
|
|
SiteSetting.title || "Discourse"
|
|
|
|
end
|
|
|
|
|
|
|
|
message = {
|
|
|
|
channel: "##{channel_name}",
|
|
|
|
username: slack_username,
|
|
|
|
icon_url: icon_url,
|
2022-12-29 07:31:05 -05:00
|
|
|
attachments: [],
|
2022-02-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
summary = {
|
|
|
|
fallback: content.truncate(100),
|
|
|
|
author_name: sender,
|
|
|
|
color: nil,
|
|
|
|
text: full_content,
|
|
|
|
mrkdwn_in: ["text"],
|
|
|
|
title: content.truncate(100),
|
|
|
|
title_link: url,
|
2022-12-29 07:31:05 -05:00
|
|
|
thumb_url: nil,
|
2022-02-25 14:17:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
message[:attachments].push(summary)
|
|
|
|
|
|
|
|
DiscourseChatIntegration::Provider::SlackProvider.send_via_api(nil, channel, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-26 10:08:06 -04:00
|
|
|
end
|