-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathplugin.rb
111 lines (94 loc) · 3.97 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true
# name: discourse-follow
# about: Allows users to follow other users, list the latest topics involving them, and receive notifications when they post.
# meta_topic_id: 110579
# version: 1.0
# authors: Angus McLeod, Robert Barrow, CDCK Inc
# url: https://github.com/discourse/discourse-follow
enabled_site_setting :discourse_follow_enabled
register_asset "stylesheets/common/follow.scss"
register_svg_icon "discourse-follow-new-reply"
register_svg_icon "discourse-follow-new-follower"
register_svg_icon "discourse-follow-new-topic"
module ::Follow
PLUGIN_NAME = "discourse-follow"
end
require_relative "lib/follow/engine"
after_initialize do
Notification.types[:following] = 800
Notification.types[:following_created_topic] = 801
Notification.types[:following_replied] = 802
reloadable_patch { |plugin| User.prepend(Follow::UserExtension) }
add_to_serializer(:user, :can_see_following) do
FollowPagesVisibility.can_see_following_page?(user: scope.current_user, target_user: user)
end
add_to_serializer(:user, :can_see_followers) do
FollowPagesVisibility.can_see_followers_page?(user: scope.current_user, target_user: user)
end
add_to_serializer(:user, :can_see_network_tab) do
user_is_current_user || can_see_following || can_see_followers
end
# UserSerializer in core inherits from UserCardSerializer.
# we don't need to duplicate these attrs for UserSerializer.
#
# the `!options.key?(:each_serializer)` check is a temporary hack to exclude
# the attributes we add here from the user card serializer when multiple user
# objects are being serialized (e.g. the /user-cards.json route in core). If
# we don't do this, we end up introducing a horrible 3N+1 on the
# /user-cards.json route and it's not easily fixable.
# when serializing a single user object, the options of the serializer
# doesn't have a `each_serializer` key.
add_to_serializer(:user_card, :can_follow) do
!options.key?(:each_serializer) && scope.current_user.present? && user.allow_people_to_follow_me
end
add_to_serializer(:user_card, :is_followed) do
!options.key?(:each_serializer) && scope.current_user.present? &&
scope.current_user.following.where(id: user.id).exists?
end
add_to_serializer(
:user_card,
:total_followers,
include_condition: -> do
!options.key?(:each_serializer) && SiteSetting.discourse_follow_enabled &&
SiteSetting.follow_show_statistics_on_profile &&
FollowPagesVisibility.can_see_followers_page?(user: scope.current_user, target_user: object)
end,
) { object.followers.count }
add_to_serializer(
:user_card,
:total_following,
include_condition: -> do
!options.key?(:each_serializer) && SiteSetting.discourse_follow_enabled &&
SiteSetting.follow_show_statistics_on_profile &&
FollowPagesVisibility.can_see_following_page?(user: scope.current_user, target_user: object)
end,
) { object.following.count }
%i[
notify_me_when_followed
notify_followed_user_when_followed
notify_me_when_followed_replies
notify_me_when_followed_creates_topic
allow_people_to_follow_me
].each do |field|
add_to_class(:user, field) do
v = custom_fields[field]
if !v.nil?
HasCustomFields::Helpers::CUSTOM_FIELD_TRUE.include?(v.to_s.downcase)
else
SiteSetting.public_send(:"default_#{field}")
end
end
User.register_custom_field_type(field, :boolean)
DiscoursePluginRegistry.serialized_current_user_fields << field
add_to_serializer(:user, field) { object.public_send(field) }
register_editable_user_custom_field(field)
end
on(:post_alerter_before_post) do |post, new_record, notified|
Follow::NotificationHandler.new(post, notified).handle if new_record
end
# TODO(2022-08-30): Remove when post_alerter_before_post is available
on(:post_alerter_after_save_post) do |post, new_record, notified|
next if !new_record
Follow::NotificationHandler.new(post, notified).handle
end
end