-
Notifications
You must be signed in to change notification settings - Fork 34
/
plugin.rb
55 lines (48 loc) · 1.64 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
# name: discourse-whos-online
# about: Display a list of online users at the top of the screen
# meta_topic_id: 52345
# version: 2.0
# authors: David Taylor
# url: https://github.com/discourse/discourse-whos-online
enabled_site_setting :whos_online_enabled
register_asset "stylesheets/whos_online.scss"
after_initialize do
module ::DiscourseWhosOnline
CHANNEL_NAME = "/whos-online/online"
end
register_presence_channel_prefix("whos-online") do |channel_name|
if channel_name == "/whos-online/online"
config = PresenceChannel::Config.new(timeout: SiteSetting.whos_online_active_timeago * 60)
if SiteSetting.whos_online_display_public
config.public = true
else
config.allowed_group_ids = [::Group::AUTO_GROUPS[:trust_level_0]]
end
config.count_only = SiteSetting.whos_online_count_only
config
end
end
on(:user_seen) do |user|
hidden = false
hidden ||= user.user_option.hide_presence if defined?(user.user_option.hide_presence)
hidden ||= user.id < 0
next if hidden
PresenceChannel.new(DiscourseWhosOnline::CHANNEL_NAME).present(
user_id: user.id,
client_id: "seen",
)
rescue PresenceChannel::InvalidAccess
end
add_to_serializer(
:site,
:whos_online_state,
include_condition: -> do
@whos_online_channel ||= PresenceChannel.new(DiscourseWhosOnline::CHANNEL_NAME)
@whos_online_channel.can_view?(user_id: scope.user&.id)
end,
) do
@whos_online_channel ||= PresenceChannel.new(DiscourseWhosOnline::CHANNEL_NAME)
PresenceChannelStateSerializer.new(@whos_online_channel.state, root: nil)
end
end