2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 02:40:18 -05:00
|
|
|
# name: discourse-presence
|
2021-10-21 06:42:46 -05:00
|
|
|
# about: Show which users are replying to a topic, or editing a post
|
2020-04-28 23:48:55 -05:00
|
|
|
# version: 2.0
|
|
|
|
# authors: André Pereira, David Taylor, tgxworld
|
2021-07-19 10:35:47 -05:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-presence
|
2017-09-07 02:40:18 -05:00
|
|
|
|
|
|
|
enabled_site_setting :presence_enabled
|
2023-08-30 19:01:01 -05:00
|
|
|
hide_plugin
|
2017-09-07 02:40:18 -05:00
|
|
|
|
|
|
|
register_asset "stylesheets/presence.scss"
|
|
|
|
|
|
|
|
after_initialize do
|
2021-10-21 06:42:46 -05: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 15:06:27 -05:00
|
|
|
|
2021-10-21 06:42:46 -05: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 07:54:02 -06:00
|
|
|
config.allowed_group_ids = secure_group_ids + [::Group::AUTO_GROUPS[:admins]]
|
2020-05-13 01:07:54 -05:00
|
|
|
else
|
2021-10-21 06:42:46 -05: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-28 23:48:55 -05:00
|
|
|
end
|
|
|
|
|
2021-10-21 06:42:46 -05: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
|
2023-12-11 22:20:37 -06:00
|
|
|
config.allowed_group_ids += SiteSetting.edit_wiki_post_allowed_groups_map
|
2017-09-07 02:40:18 -05:00
|
|
|
end
|
|
|
|
|
2023-07-07 10:48:14 -05: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 06:42:46 -05:00
|
|
|
end
|
2020-04-28 23:48:55 -05:00
|
|
|
|
2024-09-03 20:38:46 -05:00
|
|
|
if SiteSetting.enable_category_group_moderation? && topic.category
|
|
|
|
config.allowed_group_ids.push(*topic.category.moderating_groups.pluck(:id))
|
2020-04-28 23:48:55 -05:00
|
|
|
end
|
2017-12-06 14:58:59 -06:00
|
|
|
|
2021-10-21 06:42:46 -05:00
|
|
|
config
|
2020-04-28 23:48:55 -05:00
|
|
|
end
|
2021-10-21 06:42:46 -05:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
2017-09-07 02:40:18 -05:00
|
|
|
end
|
|
|
|
end
|