2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 03:40:18 -04:00
|
|
|
# name: discourse-presence
|
2021-10-21 07:42:46 -04:00
|
|
|
# about: Show which users are replying to a topic, or editing a post
|
2020-04-29 00:48:55 -04:00
|
|
|
# version: 2.0
|
|
|
|
# authors: André Pereira, David Taylor, tgxworld
|
2021-07-19 11:35:47 -04:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-presence
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
enabled_site_setting :presence_enabled
|
2018-05-15 18:43:09 -04:00
|
|
|
hide_plugin if self.respond_to?(:hide_plugin)
|
2017-09-07 03:40:18 -04:00
|
|
|
|
|
|
|
register_asset "stylesheets/presence.scss"
|
|
|
|
|
|
|
|
after_initialize do
|
2021-10-21 07:42:46 -04:00
|
|
|
register_presence_channel_prefix("discourse-presence") do |channel_name|
|
|
|
|
if topic_id = channel_name[%r{/discourse-presence/reply/(\d+)}, 1]
|
|
|
|
topic = Topic.find(topic_id)
|
|
|
|
config = PresenceChannel::Config.new
|
2017-09-08 16:06:27 -04:00
|
|
|
|
2021-10-21 07:42:46 -04:00
|
|
|
if topic.private_message?
|
|
|
|
config.allowed_user_ids = topic.allowed_users.pluck(:id)
|
|
|
|
config.allowed_group_ids =
|
|
|
|
topic.allowed_groups.pluck(:group_id) + [::Group::AUTO_GROUPS[:staff]]
|
|
|
|
elsif secure_group_ids = topic.secure_group_ids
|
2021-11-08 08:54:02 -05:00
|
|
|
config.allowed_group_ids = secure_group_ids + [::Group::AUTO_GROUPS[:admins]]
|
2020-05-13 02:07:54 -04:00
|
|
|
else
|
2021-10-21 07:42:46 -04:00
|
|
|
# config.public=true would make data available to anon, so use the tl0 group instead
|
|
|
|
config.allowed_group_ids = [::Group::AUTO_GROUPS[:trust_level_0]]
|
2020-04-29 00:48:55 -04:00
|
|
|
end
|
|
|
|
|
2021-10-21 07:42:46 -04:00
|
|
|
config
|
|
|
|
elsif topic_id = channel_name[%r{/discourse-presence/whisper/(\d+)}, 1]
|
|
|
|
Topic.find(topic_id) # Just ensure it exists
|
|
|
|
PresenceChannel::Config.new(allowed_group_ids: [::Group::AUTO_GROUPS[:staff]])
|
|
|
|
elsif post_id = channel_name[%r{/discourse-presence/edit/(\d+)}, 1]
|
|
|
|
post = Post.find(post_id)
|
|
|
|
topic = Topic.find(post.topic_id)
|
|
|
|
|
|
|
|
config = PresenceChannel::Config.new
|
|
|
|
config.allowed_group_ids = [::Group::AUTO_GROUPS[:staff]]
|
|
|
|
|
|
|
|
# Locked and whisper posts are staff only
|
|
|
|
next config if post.locked? || post.whisper?
|
|
|
|
|
|
|
|
config.allowed_user_ids = [post.user_id]
|
|
|
|
|
|
|
|
if topic.private_message? && post.wiki
|
|
|
|
# Ignore trust level and just publish to all allowed groups since
|
|
|
|
# trying to figure out which users in the allowed groups have
|
|
|
|
# the necessary trust levels can lead to a large array of user ids
|
|
|
|
# if the groups are big.
|
|
|
|
config.allowed_user_ids += topic.allowed_users.pluck(:id)
|
|
|
|
config.allowed_group_ids += topic.allowed_groups.pluck(:id)
|
|
|
|
elsif post.wiki
|
|
|
|
config.allowed_group_ids << Group::AUTO_GROUPS[
|
|
|
|
:"trust_level_#{SiteSetting.min_trust_to_edit_wiki_post}"
|
|
|
|
]
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
|
2023-07-07 11:48:14 -04:00
|
|
|
if !topic.private_message? && SiteSetting.edit_all_post_groups.present?
|
|
|
|
config.allowed_group_ids += SiteSetting.edit_all_post_groups.split("|").map(&:to_i)
|
2021-10-21 07:42:46 -04:00
|
|
|
end
|
2020-04-29 00:48:55 -04:00
|
|
|
|
2021-10-21 07:42:46 -04:00
|
|
|
if SiteSetting.enable_category_group_moderation? &&
|
|
|
|
group_id = topic.category&.reviewable_by_group_id
|
|
|
|
config.allowed_group_ids << group_id
|
2020-04-29 00:48:55 -04:00
|
|
|
end
|
2017-12-06 15:58:59 -05:00
|
|
|
|
2021-10-21 07:42:46 -04:00
|
|
|
config
|
2020-04-29 00:48:55 -04:00
|
|
|
end
|
2021-10-21 07:42:46 -04:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
2017-09-07 03:40:18 -04:00
|
|
|
end
|
|
|
|
end
|