2022-11-02 09:41:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# name: chat
|
2024-09-10 01:16:16 -04:00
|
|
|
# about: Adds chat functionality to your site so it can natively support both long-form and short-form communication needs of your online community.
|
2023-11-07 18:14:10 -05:00
|
|
|
# meta_topic_id: 230881
|
2022-11-02 09:41:30 -04:00
|
|
|
# version: 0.4
|
|
|
|
# authors: Kane York, Mark VanLandingham, Martin Brennan, Joffrey Jaffeux
|
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/chat
|
2023-10-09 20:16:13 -04:00
|
|
|
# meta_topic_id: 230881
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
enabled_site_setting :chat_enabled
|
|
|
|
|
|
|
|
register_asset "stylesheets/colors.scss", :color_definitions
|
2023-03-17 09:24:38 -04:00
|
|
|
register_asset "stylesheets/mixins/index.scss"
|
|
|
|
register_asset "stylesheets/common/index.scss"
|
|
|
|
register_asset "stylesheets/desktop/index.scss", :desktop
|
|
|
|
register_asset "stylesheets/mobile/index.scss", :mobile
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
register_svg_icon "comments"
|
|
|
|
register_svg_icon "comment-slash"
|
2024-08-20 09:16:05 -04:00
|
|
|
register_svg_icon "comment-dots"
|
2022-11-02 09:41:30 -04:00
|
|
|
register_svg_icon "lock"
|
2023-10-04 10:14:37 -04:00
|
|
|
register_svg_icon "clipboard"
|
2022-11-02 09:41:30 -04:00
|
|
|
register_svg_icon "file-audio"
|
|
|
|
register_svg_icon "file-video"
|
|
|
|
register_svg_icon "file-image"
|
2024-12-05 02:20:02 -05:00
|
|
|
register_svg_icon "circle-stop"
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
# route: /admin/plugins/chat
|
2024-09-10 01:16:16 -04:00
|
|
|
add_admin_route "chat.admin.title", "chat", use_new_show_route: true
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
GlobalSetting.add_default(:allow_unsecure_chat_uploads, false)
|
|
|
|
|
2023-03-17 09:24:38 -04:00
|
|
|
module ::Chat
|
|
|
|
PLUGIN_NAME = "chat"
|
2024-02-01 11:28:10 -05:00
|
|
|
RETENTION_SETTINGS_TO_USER_OPTION_FIELDS = {
|
|
|
|
chat_channel_retention_days: :dismissed_channel_retention_reminder,
|
|
|
|
chat_dm_retention_days: :dismissed_dm_retention_reminder,
|
|
|
|
}
|
2023-03-17 09:24:38 -04:00
|
|
|
end
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2023-03-17 09:24:38 -04:00
|
|
|
require_relative "lib/chat/engine"
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2023-03-17 09:24:38 -04:00
|
|
|
after_initialize do
|
2022-11-02 09:41:30 -04:00
|
|
|
register_seedfu_fixtures(Rails.root.join("plugins", "chat", "db", "fixtures"))
|
|
|
|
|
|
|
|
UserNotifications.append_view_path(File.expand_path("../app/views", __FILE__))
|
|
|
|
|
|
|
|
register_category_custom_field_type(Chat::HAS_CHAT_ENABLED, :boolean)
|
|
|
|
|
2024-03-18 03:35:07 -04:00
|
|
|
register_user_custom_field_type(Chat::LAST_CHAT_CHANNEL_ID, :integer)
|
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields << Chat::LAST_CHAT_CHANNEL_ID
|
2024-07-02 18:45:37 -04:00
|
|
|
DiscoursePluginRegistry.register_flag_applies_to_type("Chat::Message", self)
|
2024-03-18 03:35:07 -04:00
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
UserUpdater::OPTION_ATTR.push(:chat_enabled)
|
|
|
|
UserUpdater::OPTION_ATTR.push(:only_chat_push_notifications)
|
|
|
|
UserUpdater::OPTION_ATTR.push(:chat_sound)
|
|
|
|
UserUpdater::OPTION_ATTR.push(:ignore_channel_wide_mention)
|
2024-04-29 05:20:01 -04:00
|
|
|
UserUpdater::OPTION_ATTR.push(:show_thread_title_prompts)
|
2022-11-02 09:41:30 -04:00
|
|
|
UserUpdater::OPTION_ATTR.push(:chat_email_frequency)
|
2023-02-28 20:01:44 -05:00
|
|
|
UserUpdater::OPTION_ATTR.push(:chat_header_indicator_preference)
|
2023-08-18 14:33:07 -04:00
|
|
|
UserUpdater::OPTION_ATTR.push(:chat_separate_sidebar_mode)
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2023-03-17 09:24:38 -04:00
|
|
|
register_reviewable_type Chat::ReviewableMessage
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
reloadable_patch do |plugin|
|
|
|
|
Site.preloaded_category_custom_fields << Chat::HAS_CHAT_ENABLED
|
|
|
|
|
|
|
|
Guardian.prepend Chat::GuardianExtensions
|
|
|
|
UserNotifications.prepend Chat::UserNotificationsExtension
|
2024-09-02 08:45:55 -04:00
|
|
|
Notifications::ConsolidationPlan.prepend Chat::NotificationConsolidationExtension
|
2022-11-02 09:41:30 -04:00
|
|
|
UserOption.prepend Chat::UserOptionExtension
|
|
|
|
Category.prepend Chat::CategoryExtension
|
2023-03-17 09:24:38 -04:00
|
|
|
Reviewable.prepend Chat::ReviewableExtension
|
|
|
|
Bookmark.prepend Chat::BookmarkExtension
|
2022-11-02 09:41:30 -04:00
|
|
|
User.prepend Chat::UserExtension
|
DEV: Redesign chat mentions (#24752)
At the moment, when someone is mentioning a group, or using here or
all mention, we create a chat_mention record per user. What we want
instead is to have special kinds of mentions, so we can create only one
chat_mention record in such cases. This PR implements that.
Note, that such mentions will still have N related notifications, one
notification per a user. We don't expect we'll have performance
problems on the notifications side, but if at some point we do, we
should be able to solve them on the side of notifications
(notifications are handled in jobs, also some little delays with
the notifications are acceptable, so we can make sure notifications
are properly queued, and that processing of every notification is
fast enough to make delays small enough).
The preparation work for this PR was done in fbd24fa, where we make
it possible for one mention to have several related notifications.
A pretty tricky part of this PR is schema and data migration, I've explained
related details inline on the migration files.
2024-01-17 06:24:01 -05:00
|
|
|
Group.prepend Chat::GroupExtension
|
2023-03-17 09:24:38 -04:00
|
|
|
Plugin::Instance.prepend Chat::PluginInstanceExtension
|
2024-02-01 11:28:10 -05:00
|
|
|
Jobs::ExportCsvFile.prepend Chat::MessagesExporter
|
2023-09-13 16:31:42 -04:00
|
|
|
WebHook.prepend Chat::OutgoingWebHookExtension
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if Oneboxer.respond_to?(:register_local_handler)
|
|
|
|
Oneboxer.register_local_handler("chat/chat") do |url, route|
|
2023-09-04 10:55:02 -04:00
|
|
|
Chat::OneboxHandler.handle(url, route)
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if InlineOneboxer.respond_to?(:register_local_handler)
|
|
|
|
InlineOneboxer.register_local_handler("chat/chat") do |url, route|
|
2023-02-01 10:39:23 -05:00
|
|
|
if route[:message_id].present?
|
2023-03-17 09:24:38 -04:00
|
|
|
message = Chat::Message.find_by(id: route[:message_id])
|
2022-11-02 09:41:30 -04:00
|
|
|
next if !message
|
|
|
|
|
|
|
|
chat_channel = message.chat_channel
|
|
|
|
user = message.user
|
|
|
|
next if !chat_channel || !user
|
|
|
|
|
|
|
|
title =
|
|
|
|
I18n.t(
|
|
|
|
"chat.onebox.inline_to_message",
|
|
|
|
message_id: message.id,
|
|
|
|
chat_channel: chat_channel.name,
|
|
|
|
username: user.username,
|
|
|
|
)
|
|
|
|
else
|
2023-03-17 09:24:38 -04:00
|
|
|
chat_channel = Chat::Channel.find_by(id: route[:channel_id])
|
2022-11-02 09:41:30 -04:00
|
|
|
next if !chat_channel
|
|
|
|
|
|
|
|
title =
|
|
|
|
if chat_channel.name.present?
|
|
|
|
I18n.t("chat.onebox.inline_to_channel", chat_channel: chat_channel.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-06 01:37:32 -05:00
|
|
|
next if !Guardian.new.can_preview_chat_channel?(chat_channel)
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
{ url: url, title: title }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if respond_to?(:register_upload_in_use)
|
|
|
|
register_upload_in_use do |upload|
|
2023-03-17 09:24:38 -04:00
|
|
|
Chat::Message.where(
|
2022-11-02 09:41:30 -04:00
|
|
|
"message LIKE ? OR message LIKE ?",
|
|
|
|
"%#{upload.sha1}%",
|
|
|
|
"%#{upload.base62_sha1}%",
|
|
|
|
).exists? ||
|
2023-03-17 09:24:38 -04:00
|
|
|
Chat::Draft.where(
|
2022-11-02 09:41:30 -04:00
|
|
|
"data LIKE ? OR data LIKE ?",
|
|
|
|
"%#{upload.sha1}%",
|
|
|
|
"%#{upload.base62_sha1}%",
|
|
|
|
).exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:user_card, :can_chat_user) do
|
2024-06-10 09:38:22 -04:00
|
|
|
return false if !SiteSetting.chat_enabled
|
2024-06-25 06:52:17 -04:00
|
|
|
return false if scope.user.blank?
|
2024-06-10 09:38:22 -04:00
|
|
|
return false if !scope.user.user_option.chat_enabled || !object.user_option.chat_enabled
|
|
|
|
|
|
|
|
scope.can_direct_message? && Guardian.new(object).can_chat?
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:hidden_profile, :can_chat_user) do
|
2022-11-02 09:41:30 -04:00
|
|
|
return false if !SiteSetting.chat_enabled
|
2024-06-25 06:52:17 -04:00
|
|
|
return false if scope.user.blank?
|
2024-03-20 04:24:34 -04:00
|
|
|
return false if !scope.user.user_option.chat_enabled || !object.user_option.chat_enabled
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-03-20 04:24:34 -04:00
|
|
|
scope.can_direct_message? && Guardian.new(object).can_chat?
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
2023-04-24 07:17:51 -04:00
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:can_chat,
|
|
|
|
include_condition: -> do
|
|
|
|
return @can_chat if defined?(@can_chat)
|
|
|
|
@can_chat = SiteSetting.chat_enabled && scope.can_chat?
|
|
|
|
end,
|
|
|
|
) { true }
|
|
|
|
|
2024-02-18 21:25:59 -05:00
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:can_direct_message,
|
|
|
|
include_condition: -> do
|
|
|
|
return @can_direct_message if defined?(@can_direct_message)
|
2024-03-20 04:24:34 -04:00
|
|
|
@can_direct_message = include_has_chat_enabled? && scope.can_direct_message?
|
2024-02-18 21:25:59 -05:00
|
|
|
end,
|
|
|
|
) { true }
|
|
|
|
|
2023-04-24 07:17:51 -04:00
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:has_chat_enabled,
|
|
|
|
include_condition: -> do
|
|
|
|
return @has_chat_enabled if defined?(@has_chat_enabled)
|
|
|
|
@has_chat_enabled = include_can_chat? && object.user_option.chat_enabled
|
|
|
|
end,
|
|
|
|
) { true }
|
|
|
|
|
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:chat_sound,
|
|
|
|
include_condition: -> { include_has_chat_enabled? && object.user_option.chat_sound },
|
|
|
|
) { object.user_option.chat_sound }
|
|
|
|
|
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:needs_channel_retention_reminder,
|
|
|
|
include_condition: -> do
|
|
|
|
include_has_chat_enabled? && object.staff? &&
|
|
|
|
!object.user_option.dismissed_channel_retention_reminder &&
|
|
|
|
!SiteSetting.chat_channel_retention_days.zero?
|
|
|
|
end,
|
|
|
|
) { true }
|
|
|
|
|
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:needs_dm_retention_reminder,
|
|
|
|
include_condition: -> do
|
|
|
|
include_has_chat_enabled? && !object.user_option.dismissed_dm_retention_reminder &&
|
|
|
|
!SiteSetting.chat_dm_retention_days.zero?
|
|
|
|
end,
|
|
|
|
) { true }
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
add_to_serializer(:current_user, :has_joinable_public_channels) do
|
2023-03-17 09:24:38 -04:00
|
|
|
Chat::ChannelFetcher.secured_public_channel_search(
|
2022-11-02 09:41:30 -04:00
|
|
|
self.scope,
|
|
|
|
following: false,
|
|
|
|
limit: 1,
|
|
|
|
status: :open,
|
2022-12-19 02:34:07 -05:00
|
|
|
).exists?
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
2023-04-24 07:17:51 -04:00
|
|
|
add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:chat_drafts,
|
|
|
|
include_condition: -> { include_has_chat_enabled? },
|
|
|
|
) do
|
2023-03-17 09:24:38 -04:00
|
|
|
Chat::Draft
|
2022-11-02 09:41:30 -04:00
|
|
|
.where(user_id: object.id)
|
2023-01-25 06:50:10 -05:00
|
|
|
.order(updated_at: :desc)
|
|
|
|
.limit(20)
|
2023-11-22 05:54:23 -05:00
|
|
|
.pluck(:chat_channel_id, :data, :thread_id)
|
|
|
|
.map { |row| { channel_id: row[0], data: row[1], thread_id: row[2] } }
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
2024-03-15 12:08:37 -04:00
|
|
|
add_to_serializer(
|
|
|
|
:user_notification_total,
|
|
|
|
:chat_notifications,
|
|
|
|
include_condition: -> do
|
|
|
|
return @has_chat_enabled if defined?(@has_chat_enabled)
|
|
|
|
@has_chat_enabled =
|
|
|
|
SiteSetting.chat_enabled && scope.can_chat? && object.user_option.chat_enabled
|
|
|
|
end,
|
|
|
|
) { Chat::ChannelFetcher.unreads_total(self.scope) }
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
add_to_serializer(:user_option, :chat_enabled) { object.chat_enabled }
|
|
|
|
|
2023-04-24 07:17:51 -04:00
|
|
|
add_to_serializer(
|
|
|
|
:user_option,
|
|
|
|
:chat_sound,
|
|
|
|
include_condition: -> { !object.chat_sound.blank? },
|
|
|
|
) { object.chat_sound }
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
add_to_serializer(:user_option, :only_chat_push_notifications) do
|
|
|
|
object.only_chat_push_notifications
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:user_option, :ignore_channel_wide_mention) do
|
|
|
|
object.ignore_channel_wide_mention
|
|
|
|
end
|
|
|
|
|
2024-04-29 05:20:01 -04:00
|
|
|
add_to_serializer(:user_option, :show_thread_title_prompts) { object.show_thread_title_prompts }
|
|
|
|
|
|
|
|
add_to_serializer(:current_user_option, :show_thread_title_prompts) do
|
|
|
|
object.show_thread_title_prompts
|
|
|
|
end
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
add_to_serializer(:user_option, :chat_email_frequency) { object.chat_email_frequency }
|
|
|
|
|
2023-02-28 20:01:44 -05:00
|
|
|
add_to_serializer(:user_option, :chat_header_indicator_preference) do
|
|
|
|
object.chat_header_indicator_preference
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:current_user_option, :chat_header_indicator_preference) do
|
|
|
|
object.chat_header_indicator_preference
|
|
|
|
end
|
|
|
|
|
2023-08-18 14:33:07 -04:00
|
|
|
add_to_serializer(:user_option, :chat_separate_sidebar_mode) { object.chat_separate_sidebar_mode }
|
|
|
|
|
|
|
|
add_to_serializer(:current_user_option, :chat_separate_sidebar_mode) do
|
|
|
|
object.chat_separate_sidebar_mode
|
|
|
|
end
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
on(:site_setting_changed) do |name, old_value, new_value|
|
2024-02-01 11:28:10 -05:00
|
|
|
user_option_field = Chat::RETENTION_SETTINGS_TO_USER_OPTION_FIELDS[name.to_sym]
|
2022-11-02 09:41:30 -04:00
|
|
|
begin
|
|
|
|
if user_option_field && old_value != new_value && !new_value.zero?
|
|
|
|
UserOption.where(user_option_field => true).update_all(user_option_field => false)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Error updating user_options fields after chat retention settings changed: #{e}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if name == :secure_uploads && old_value == false && new_value == true
|
|
|
|
Chat::SecureUploadsCompatibility.update_settings
|
|
|
|
end
|
2023-03-21 20:19:59 -04:00
|
|
|
|
|
|
|
if name == :chat_allowed_groups
|
2024-11-11 23:00:59 -05:00
|
|
|
Jobs.enqueue(Jobs::Chat::AutoJoinUsers, event: "chat_allowed_groups_changed")
|
2023-03-21 20:19:59 -04:00
|
|
|
end
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
on(:post_alerter_after_save_post) do |post, new_record, notified|
|
|
|
|
next if !new_record
|
|
|
|
Chat::PostNotificationHandler.new(post, notified).handle
|
|
|
|
end
|
|
|
|
|
2024-11-11 23:00:59 -05:00
|
|
|
on(:group_destroyed) do |group, _user_ids|
|
|
|
|
Chat::AutoLeaveChannels.call(params: { group_id: group.id, event: :group_destroyed })
|
2023-03-21 20:19:59 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
register_presence_channel_prefix("chat") do |channel_name|
|
|
|
|
next nil unless channel_name == "/chat/online"
|
|
|
|
config = PresenceChannel::Config.new
|
|
|
|
config.allowed_group_ids = Chat.allowed_group_ids
|
|
|
|
config
|
|
|
|
end
|
|
|
|
|
|
|
|
register_presence_channel_prefix("chat-reply") do |channel_name|
|
2024-11-03 16:14:35 -05:00
|
|
|
if (
|
|
|
|
channel_id, thread_id =
|
|
|
|
channel_name.match(%r{^/chat-reply/(\d+)(?:/thread/(\d+))?$})&.captures
|
|
|
|
)
|
|
|
|
chat_channel = nil
|
|
|
|
if thread_id
|
|
|
|
chat_channel = Chat::Thread.find_by!(id: thread_id, channel_id: channel_id).channel
|
|
|
|
else
|
|
|
|
chat_channel = Chat::Channel.find(channel_id)
|
|
|
|
end
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
PresenceChannel::Config.new.tap do |config|
|
|
|
|
config.allowed_group_ids = chat_channel.allowed_group_ids
|
|
|
|
config.allowed_user_ids = chat_channel.allowed_user_ids
|
|
|
|
config.public = !chat_channel.read_restricted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
register_push_notification_filter do |user, payload|
|
|
|
|
if user.user_option.only_chat_push_notifications && user.user_option.chat_enabled
|
2024-02-01 11:28:10 -05:00
|
|
|
payload[:notification_type].in?(::Notification.types.values_at(:chat_mention, :chat_message))
|
2022-11-02 09:41:30 -04:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
on(:user_seen) do |user|
|
|
|
|
if user.last_seen_at == user.first_seen_at
|
2024-11-11 23:00:59 -05:00
|
|
|
Chat::AutoJoinChannels.call(params: { user_id: user.id })
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
on(:user_confirmed_email) do |user|
|
2024-11-11 23:00:59 -05:00
|
|
|
Chat::AutoJoinChannels.call(params: { user_id: user.id }) if user.active?
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
2024-11-11 23:00:59 -05:00
|
|
|
on(:user_added_to_group) do |user, _group|
|
|
|
|
Chat::AutoJoinChannels.call(params: { user_id: user.id })
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
2024-11-11 23:00:59 -05:00
|
|
|
on(:user_removed_from_group) do |user, _group|
|
|
|
|
Chat::AutoLeaveChannels.call(params: { user_id: user.id, event: :user_removed_from_group })
|
2023-03-21 20:19:59 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
on(:category_updated) do |category|
|
|
|
|
# There's a bug on core where this event is triggered with an `#update` result (true/false)
|
2024-11-11 23:00:59 -05:00
|
|
|
next unless category.is_a?(Category)
|
|
|
|
next unless category_channel = Chat::Channel.find_by(chatable: category)
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-11-11 23:00:59 -05:00
|
|
|
if category_channel.auto_join_users
|
|
|
|
Chat::AutoJoinChannels.call(params: { category_id: category.id })
|
2023-01-27 07:58:12 -05:00
|
|
|
end
|
2024-11-11 23:00:59 -05:00
|
|
|
Chat::AutoLeaveChannels.call(params: { category_id: category.id, event: :category_updated })
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
2023-09-13 16:31:42 -04:00
|
|
|
# outgoing webhook events
|
|
|
|
%i[
|
|
|
|
chat_message_created
|
|
|
|
chat_message_edited
|
|
|
|
chat_message_trashed
|
|
|
|
chat_message_restored
|
|
|
|
].each do |chat_message_event|
|
|
|
|
on(chat_message_event) do |message, channel, user|
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
message: Chat::MessageSerializer.new(message, { scope: guardian, root: false }).as_json,
|
|
|
|
channel:
|
|
|
|
Chat::ChannelSerializer.new(
|
|
|
|
channel,
|
|
|
|
{ scope: guardian, membership: channel.membership_for(user), root: false },
|
|
|
|
).as_json,
|
|
|
|
}
|
|
|
|
|
|
|
|
category_id = channel.chatable_type == "Category" ? channel.chatable_id : nil
|
|
|
|
|
|
|
|
WebHook.enqueue_chat_message_hooks(
|
|
|
|
chat_message_event,
|
|
|
|
payload.to_json,
|
|
|
|
category_id: category_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-02 09:41:30 -04:00
|
|
|
Discourse::Application.routes.append do
|
|
|
|
mount ::Chat::Engine, at: "/chat"
|
2023-03-17 09:24:38 -04:00
|
|
|
|
2024-06-10 06:41:35 -04:00
|
|
|
get "/admin/plugins/chat/hooks" => "chat/admin/incoming_webhooks#index",
|
2022-11-02 09:41:30 -04:00
|
|
|
:constraints => StaffConstraint.new
|
2023-03-17 09:24:38 -04:00
|
|
|
post "/admin/plugins/chat/hooks" => "chat/admin/incoming_webhooks#create",
|
2022-11-02 09:41:30 -04:00
|
|
|
:constraints => StaffConstraint.new
|
|
|
|
put "/admin/plugins/chat/hooks/:incoming_chat_webhook_id" =>
|
2023-03-17 09:24:38 -04:00
|
|
|
"chat/admin/incoming_webhooks#update",
|
2022-11-02 09:41:30 -04:00
|
|
|
:constraints => StaffConstraint.new
|
2024-09-10 01:16:16 -04:00
|
|
|
get "/admin/plugins/chat/hooks/new" => "chat/admin/incoming_webhooks#new",
|
|
|
|
:constraints => StaffConstraint.new
|
2024-12-11 18:49:17 -05:00
|
|
|
get "/admin/plugins/chat/hooks/:incoming_chat_webhook_id/edit" =>
|
|
|
|
"chat/admin/incoming_webhooks#edit",
|
2024-09-10 01:16:16 -04:00
|
|
|
:constraints => StaffConstraint.new
|
2022-11-02 09:41:30 -04:00
|
|
|
delete "/admin/plugins/chat/hooks/:incoming_chat_webhook_id" =>
|
2023-03-17 09:24:38 -04:00
|
|
|
"chat/admin/incoming_webhooks#destroy",
|
2022-11-02 09:41:30 -04:00
|
|
|
:constraints => StaffConstraint.new
|
|
|
|
get "u/:username/preferences/chat" => "users#preferences",
|
|
|
|
:constraints => {
|
|
|
|
username: RouteFormat.username,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-09-05 07:05:19 -04:00
|
|
|
add_automation_scriptable("send_chat_message") do
|
|
|
|
field :chat_channel_id, component: :text, required: true
|
|
|
|
field :message, component: :message, required: true, accepts_placeholders: true
|
|
|
|
field :sender, component: :user
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-09-05 07:05:19 -04:00
|
|
|
placeholder :channel_name
|
2024-10-08 08:55:11 -04:00
|
|
|
placeholder :post_quote, triggerable: :post_created_edited
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-09-05 11:17:18 -04:00
|
|
|
triggerables %i[recurring topic_tags_changed post_created_edited]
|
2022-11-11 09:58:05 -05:00
|
|
|
|
2024-09-05 07:05:19 -04:00
|
|
|
script do |context, fields, automation|
|
|
|
|
sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
|
|
|
|
channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
|
|
|
|
placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-10-08 08:55:11 -04:00
|
|
|
if context["kind"] == "post_created_edited"
|
|
|
|
placeholders[:post_quote] = utils.build_quote(context["post"])
|
|
|
|
end
|
|
|
|
|
2024-09-05 07:05:19 -04:00
|
|
|
creator =
|
|
|
|
::Chat::CreateMessage.call(
|
|
|
|
guardian: sender.guardian,
|
DEV: Provide user input to services using `params` key
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
2024-10-18 11:45:47 -04:00
|
|
|
params: {
|
|
|
|
chat_channel_id: channel.id,
|
|
|
|
message: utils.apply_placeholders(fields.dig("message", "value"), placeholders),
|
|
|
|
},
|
2024-09-05 07:05:19 -04:00
|
|
|
)
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-09-05 07:05:19 -04:00
|
|
|
if creator.failure?
|
|
|
|
Rails.logger.warn "[discourse-automation] Chat message failed to send:\n#{creator.inspect_steps.inspect}\n#{creator.inspect_steps.error}"
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_api_key_scope(
|
|
|
|
:chat,
|
2023-09-07 02:57:29 -04:00
|
|
|
{
|
|
|
|
create_message: {
|
|
|
|
actions: %w[chat/api/channel_messages#create],
|
|
|
|
params: %i[chat_channel_id],
|
|
|
|
},
|
|
|
|
},
|
2022-11-02 09:41:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Dark mode email styles
|
|
|
|
Email::Styles.register_plugin_style do |fragment|
|
|
|
|
fragment.css(".chat-summary-header").each { |element| element[:dm] = "header" }
|
|
|
|
fragment.css(".chat-summary-content").each { |element| element[:dm] = "body" }
|
|
|
|
end
|
|
|
|
|
2023-03-17 09:24:38 -04:00
|
|
|
register_email_unsubscriber("chat_summary", EmailControllerHelper::ChatSummaryUnsubscriber)
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-09-04 12:58:14 -04:00
|
|
|
register_stat("chat_messages", expose_via_api: true) { Chat::Statistics.about_messages }
|
2024-01-04 07:10:03 -05:00
|
|
|
register_stat("chat_users", expose_via_api: true) { Chat::Statistics.about_users }
|
2023-11-09 15:44:05 -05:00
|
|
|
register_stat("chat_channels", expose_via_api: true) { Chat::Statistics.about_channels }
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2024-01-04 07:10:03 -05:00
|
|
|
register_stat("chat_channel_messages") { Chat::Statistics.channel_messages }
|
|
|
|
register_stat("chat_direct_messages") { Chat::Statistics.direct_messages }
|
|
|
|
register_stat("chat_open_channels_with_threads_enabled") do
|
|
|
|
Chat::Statistics.open_channels_with_threads_enabled
|
|
|
|
end
|
|
|
|
register_stat("chat_threaded_messages") { Chat::Statistics.threaded_messages }
|
FEATURE: Generic hashtag autocomplete lookup and markdown cooking (#18937)
This commit fleshes out and adds functionality for the new `#hashtag` search and
lookup system, still hidden behind the `enable_experimental_hashtag_autocomplete`
feature flag.
**Serverside**
We have two plugin API registration methods that are used to define data sources
(`register_hashtag_data_source`) and hashtag result type priorities depending on
the context (`register_hashtag_type_in_context`). Reading the comments in plugin.rb
should make it clear what these are doing. Reading the `HashtagAutocompleteService`
in full will likely help a lot as well.
Each data source is responsible for providing its own **lookup** and **search**
method that returns hashtag results based on the arguments provided. For example,
the category hashtag data source has to take into account parent categories and
how they relate, and each data source has to define their own icon to use for the
hashtag, and so on.
The `Site` serializer has two new attributes that source data from `HashtagAutocompleteService`.
There is `hashtag_icons` that is just a simple array of all the different icons that
can be used for allowlisting in our markdown pipeline, and there is `hashtag_context_configurations`
that is used to store the type priority orders for each registered context.
When sending emails, we cannot render the SVG icons for hashtags, so
we need to change the HTML hashtags to the normal `#hashtag` text.
**Markdown**
The `hashtag-autocomplete.js` file is where I have added the new `hashtag-autocomplete`
markdown rule, and like all of our rules this is used to cook the raw text on both the clientside
and on the serverside using MiniRacer. Only on the server side do we actually reach out to
the database with the `hashtagLookup` function, on the clientside we just render a plainer
version of the hashtag HTML. Only in the composer preview do we do further lookups based
on this.
This rule is the first one (that I can find) that uses the `currentUser` based on a passed
in `user_id` for guardian checks in markdown rendering code. This is the `last_editor_id`
for both the post and chat message. In some cases we need to cook without a user present,
so the `Discourse.system_user` is used in this case.
**Chat Channels**
This also contains the changes required for chat so that chat channels can be used
as a data source for hashtag searches and lookups. This data source will only be
used when `enable_experimental_hashtag_autocomplete` is `true`, so we don't have
to worry about channel results suddenly turning up.
------
**Known Rough Edges**
- Onebox excerpts will not render the icon svg/use tags, I plan to address that in a follow up PR
- Selecting a hashtag + pressing the Quote button will result in weird behaviour, I plan to address that in a follow up PR
- Mixed hashtag contexts for hashtags without a type suffix will not work correctly, e.g. #ux which is both a category and a channel slug will resolve to a category when used inside a post or within a [chat] transcript in that post. Users can get around this manually by adding the correct suffix, for example ::channel. We may get to this at some point in future
- Icons will not show for the hashtags in emails since SVG support is so terrible in email (this is not likely to be resolved, but still noting for posterity)
- Additional refinements and review fixes wil
2022-11-20 17:37:06 -05:00
|
|
|
|
|
|
|
# Make sure to update spec/system/hashtag_autocomplete_spec.rb when changing this.
|
2023-03-17 09:24:38 -04:00
|
|
|
register_hashtag_data_source(Chat::ChannelHashtagDataSource)
|
2022-12-18 22:46:17 -05:00
|
|
|
register_hashtag_type_priority_for_context("channel", "chat-composer", 200)
|
|
|
|
register_hashtag_type_priority_for_context("category", "chat-composer", 100)
|
|
|
|
register_hashtag_type_priority_for_context("tag", "chat-composer", 50)
|
|
|
|
register_hashtag_type_priority_for_context("channel", "topic-composer", 10)
|
FEATURE: Generic hashtag autocomplete lookup and markdown cooking (#18937)
This commit fleshes out and adds functionality for the new `#hashtag` search and
lookup system, still hidden behind the `enable_experimental_hashtag_autocomplete`
feature flag.
**Serverside**
We have two plugin API registration methods that are used to define data sources
(`register_hashtag_data_source`) and hashtag result type priorities depending on
the context (`register_hashtag_type_in_context`). Reading the comments in plugin.rb
should make it clear what these are doing. Reading the `HashtagAutocompleteService`
in full will likely help a lot as well.
Each data source is responsible for providing its own **lookup** and **search**
method that returns hashtag results based on the arguments provided. For example,
the category hashtag data source has to take into account parent categories and
how they relate, and each data source has to define their own icon to use for the
hashtag, and so on.
The `Site` serializer has two new attributes that source data from `HashtagAutocompleteService`.
There is `hashtag_icons` that is just a simple array of all the different icons that
can be used for allowlisting in our markdown pipeline, and there is `hashtag_context_configurations`
that is used to store the type priority orders for each registered context.
When sending emails, we cannot render the SVG icons for hashtags, so
we need to change the HTML hashtags to the normal `#hashtag` text.
**Markdown**
The `hashtag-autocomplete.js` file is where I have added the new `hashtag-autocomplete`
markdown rule, and like all of our rules this is used to cook the raw text on both the clientside
and on the serverside using MiniRacer. Only on the server side do we actually reach out to
the database with the `hashtagLookup` function, on the clientside we just render a plainer
version of the hashtag HTML. Only in the composer preview do we do further lookups based
on this.
This rule is the first one (that I can find) that uses the `currentUser` based on a passed
in `user_id` for guardian checks in markdown rendering code. This is the `last_editor_id`
for both the post and chat message. In some cases we need to cook without a user present,
so the `Discourse.system_user` is used in this case.
**Chat Channels**
This also contains the changes required for chat so that chat channels can be used
as a data source for hashtag searches and lookups. This data source will only be
used when `enable_experimental_hashtag_autocomplete` is `true`, so we don't have
to worry about channel results suddenly turning up.
------
**Known Rough Edges**
- Onebox excerpts will not render the icon svg/use tags, I plan to address that in a follow up PR
- Selecting a hashtag + pressing the Quote button will result in weird behaviour, I plan to address that in a follow up PR
- Mixed hashtag contexts for hashtags without a type suffix will not work correctly, e.g. #ux which is both a category and a channel slug will resolve to a category when used inside a post or within a [chat] transcript in that post. Users can get around this manually by adding the correct suffix, for example ::channel. We may get to this at some point in future
- Icons will not show for the hashtags in emails since SVG support is so terrible in email (this is not likely to be resolved, but still noting for posterity)
- Additional refinements and review fixes wil
2022-11-20 17:37:06 -05:00
|
|
|
|
2023-11-08 14:13:25 -05:00
|
|
|
register_post_stripper do |nokogiri_fragment|
|
|
|
|
nokogiri_fragment.css(".chat-transcript .mention").remove
|
|
|
|
end
|
|
|
|
|
FEATURE: Generic hashtag autocomplete lookup and markdown cooking (#18937)
This commit fleshes out and adds functionality for the new `#hashtag` search and
lookup system, still hidden behind the `enable_experimental_hashtag_autocomplete`
feature flag.
**Serverside**
We have two plugin API registration methods that are used to define data sources
(`register_hashtag_data_source`) and hashtag result type priorities depending on
the context (`register_hashtag_type_in_context`). Reading the comments in plugin.rb
should make it clear what these are doing. Reading the `HashtagAutocompleteService`
in full will likely help a lot as well.
Each data source is responsible for providing its own **lookup** and **search**
method that returns hashtag results based on the arguments provided. For example,
the category hashtag data source has to take into account parent categories and
how they relate, and each data source has to define their own icon to use for the
hashtag, and so on.
The `Site` serializer has two new attributes that source data from `HashtagAutocompleteService`.
There is `hashtag_icons` that is just a simple array of all the different icons that
can be used for allowlisting in our markdown pipeline, and there is `hashtag_context_configurations`
that is used to store the type priority orders for each registered context.
When sending emails, we cannot render the SVG icons for hashtags, so
we need to change the HTML hashtags to the normal `#hashtag` text.
**Markdown**
The `hashtag-autocomplete.js` file is where I have added the new `hashtag-autocomplete`
markdown rule, and like all of our rules this is used to cook the raw text on both the clientside
and on the serverside using MiniRacer. Only on the server side do we actually reach out to
the database with the `hashtagLookup` function, on the clientside we just render a plainer
version of the hashtag HTML. Only in the composer preview do we do further lookups based
on this.
This rule is the first one (that I can find) that uses the `currentUser` based on a passed
in `user_id` for guardian checks in markdown rendering code. This is the `last_editor_id`
for both the post and chat message. In some cases we need to cook without a user present,
so the `Discourse.system_user` is used in this case.
**Chat Channels**
This also contains the changes required for chat so that chat channels can be used
as a data source for hashtag searches and lookups. This data source will only be
used when `enable_experimental_hashtag_autocomplete` is `true`, so we don't have
to worry about channel results suddenly turning up.
------
**Known Rough Edges**
- Onebox excerpts will not render the icon svg/use tags, I plan to address that in a follow up PR
- Selecting a hashtag + pressing the Quote button will result in weird behaviour, I plan to address that in a follow up PR
- Mixed hashtag contexts for hashtags without a type suffix will not work correctly, e.g. #ux which is both a category and a channel slug will resolve to a category when used inside a post or within a [chat] transcript in that post. Users can get around this manually by adding the correct suffix, for example ::channel. We may get to this at some point in future
- Icons will not show for the hashtags in emails since SVG support is so terrible in email (this is not likely to be resolved, but still noting for posterity)
- Additional refinements and review fixes wil
2022-11-20 17:37:06 -05:00
|
|
|
Site.markdown_additional_options["chat"] = {
|
2023-03-17 09:24:38 -04:00
|
|
|
limited_pretty_text_features: Chat::Message::MARKDOWN_FEATURES,
|
|
|
|
limited_pretty_text_markdown_rules: Chat::Message::MARKDOWN_IT_RULES,
|
FEATURE: Generic hashtag autocomplete lookup and markdown cooking (#18937)
This commit fleshes out and adds functionality for the new `#hashtag` search and
lookup system, still hidden behind the `enable_experimental_hashtag_autocomplete`
feature flag.
**Serverside**
We have two plugin API registration methods that are used to define data sources
(`register_hashtag_data_source`) and hashtag result type priorities depending on
the context (`register_hashtag_type_in_context`). Reading the comments in plugin.rb
should make it clear what these are doing. Reading the `HashtagAutocompleteService`
in full will likely help a lot as well.
Each data source is responsible for providing its own **lookup** and **search**
method that returns hashtag results based on the arguments provided. For example,
the category hashtag data source has to take into account parent categories and
how they relate, and each data source has to define their own icon to use for the
hashtag, and so on.
The `Site` serializer has two new attributes that source data from `HashtagAutocompleteService`.
There is `hashtag_icons` that is just a simple array of all the different icons that
can be used for allowlisting in our markdown pipeline, and there is `hashtag_context_configurations`
that is used to store the type priority orders for each registered context.
When sending emails, we cannot render the SVG icons for hashtags, so
we need to change the HTML hashtags to the normal `#hashtag` text.
**Markdown**
The `hashtag-autocomplete.js` file is where I have added the new `hashtag-autocomplete`
markdown rule, and like all of our rules this is used to cook the raw text on both the clientside
and on the serverside using MiniRacer. Only on the server side do we actually reach out to
the database with the `hashtagLookup` function, on the clientside we just render a plainer
version of the hashtag HTML. Only in the composer preview do we do further lookups based
on this.
This rule is the first one (that I can find) that uses the `currentUser` based on a passed
in `user_id` for guardian checks in markdown rendering code. This is the `last_editor_id`
for both the post and chat message. In some cases we need to cook without a user present,
so the `Discourse.system_user` is used in this case.
**Chat Channels**
This also contains the changes required for chat so that chat channels can be used
as a data source for hashtag searches and lookups. This data source will only be
used when `enable_experimental_hashtag_autocomplete` is `true`, so we don't have
to worry about channel results suddenly turning up.
------
**Known Rough Edges**
- Onebox excerpts will not render the icon svg/use tags, I plan to address that in a follow up PR
- Selecting a hashtag + pressing the Quote button will result in weird behaviour, I plan to address that in a follow up PR
- Mixed hashtag contexts for hashtags without a type suffix will not work correctly, e.g. #ux which is both a category and a channel slug will resolve to a category when used inside a post or within a [chat] transcript in that post. Users can get around this manually by adding the correct suffix, for example ::channel. We may get to this at some point in future
- Icons will not show for the hashtags in emails since SVG support is so terrible in email (this is not likely to be resolved, but still noting for posterity)
- Additional refinements and review fixes wil
2022-11-20 17:37:06 -05:00
|
|
|
hashtag_configurations: HashtagAutocompleteService.contexts_with_ordered_types,
|
|
|
|
}
|
2022-11-28 11:32:57 -05:00
|
|
|
|
|
|
|
register_user_destroyer_on_content_deletion_callback(
|
2023-03-17 09:24:38 -04:00
|
|
|
Proc.new { |user| Jobs.enqueue(Jobs::Chat::DeleteUserMessages, user_id: user.id) },
|
2022-11-28 11:32:57 -05:00
|
|
|
)
|
2023-03-07 19:39:12 -05:00
|
|
|
|
2024-09-02 08:45:55 -04:00
|
|
|
register_notification_consolidation_plan(
|
|
|
|
Chat::NotificationConsolidationExtension.watched_thread_message_plan,
|
|
|
|
)
|
|
|
|
|
2023-03-17 09:24:38 -04:00
|
|
|
register_bookmarkable(Chat::MessageBookmarkable)
|
2024-11-05 00:56:30 -05:00
|
|
|
|
|
|
|
# When we eventually allow secure_uploads in chat, this will need to be
|
|
|
|
# removed. Depending on the channel, uploads may end up being secure.
|
|
|
|
UploadSecurity.register_custom_public_type("chat-composer")
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if Rails.env == "test"
|
|
|
|
Dir[Rails.root.join("plugins/chat/spec/support/**/*.rb")].each { |f| require f }
|
|
|
|
end
|