2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 15:40:18 +08:00
|
|
|
# name: discourse-presence
|
2021-10-21 19:42:46 +08:00
|
|
|
# about: Show which users are replying to a topic, or editing a post
|
2020-04-29 12:48:55 +08:00
|
|
|
# version: 2.0
|
|
|
|
# authors: André Pereira, David Taylor, tgxworld
|
2021-07-19 23:35:47 +08:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-presence
|
2017-09-07 15:40:18 +08:00
|
|
|
|
|
|
|
enabled_site_setting :presence_enabled
|
2023-08-31 08:01:01 +08:00
|
|
|
hide_plugin
|
2017-09-07 15:40:18 +08:00
|
|
|
|
|
|
|
register_asset "stylesheets/presence.scss"
|
|
|
|
|
|
|
|
after_initialize do
|
2021-10-21 19:42:46 +08: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-09 04:06:27 +08:00
|
|
|
|
2021-10-21 19:42:46 +08: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 21:54:02 +08:00
|
|
|
config.allowed_group_ids = secure_group_ids + [::Group::AUTO_GROUPS[:admins]]
|
2020-05-13 14:07:54 +08:00
|
|
|
else
|
2021-10-21 19:42:46 +08: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 12:48:55 +08:00
|
|
|
end
|
|
|
|
|
2021-10-21 19:42:46 +08:00
|
|
|
config
|
|
|
|
elsif topic_id = channel_name[%r{/discourse-presence/whisper/(\d+)}, 1]
|
|
|
|
Topic.find(topic_id) # Just ensure it exists
|
2024-12-04 18:47:05 +08:00
|
|
|
PresenceChannel::Config.new(allowed_group_ids: SiteSetting.whispers_allowed_groups_map)
|
2021-10-21 19:42:46 +08:00
|
|
|
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]]
|
|
|
|
|
2024-12-04 18:47:05 +08:00
|
|
|
# Locked posts are staff only
|
|
|
|
next config if post.locked?
|
|
|
|
|
|
|
|
# Whispers posts are for allowed whisper groups
|
|
|
|
if post.whisper?
|
|
|
|
config.allowed_group_ids += SiteSetting.whispers_allowed_groups_map
|
|
|
|
next config
|
|
|
|
end
|
2021-10-21 19:42:46 +08:00
|
|
|
|
|
|
|
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
|
2023-12-12 12:20:37 +08:00
|
|
|
config.allowed_group_ids += SiteSetting.edit_wiki_post_allowed_groups_map
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
|
2024-12-04 18:47:05 +08:00
|
|
|
if !topic.private_message? && SiteSetting.edit_all_post_groups_map.present?
|
|
|
|
config.allowed_group_ids += SiteSetting.edit_all_post_groups_map
|
2021-10-21 19:42:46 +08:00
|
|
|
end
|
2020-04-29 12:48:55 +08:00
|
|
|
|
2024-09-04 09:38:46 +08:00
|
|
|
if SiteSetting.enable_category_group_moderation? && topic.category
|
|
|
|
config.allowed_group_ids.push(*topic.category.moderating_groups.pluck(:id))
|
2020-04-29 12:48:55 +08:00
|
|
|
end
|
2017-12-07 04:58:59 +08:00
|
|
|
|
2021-10-21 19:42:46 +08:00
|
|
|
config
|
2020-04-29 12:48:55 +08:00
|
|
|
end
|
2021-10-21 19:42:46 +08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
2017-09-07 15:40:18 +08:00
|
|
|
end
|
|
|
|
end
|