forked from discourse/discourse-boosts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
149 lines (128 loc) · 3.92 KB
/
Copy pathplugin.rb
File metadata and controls
149 lines (128 loc) · 3.92 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true
# name: discourse-boosts
# about: Allows users to add freeform micro-reactions (boosts) to posts.
# version: 0.1
# authors: discourse
# url: https://github.com/discourse/discourse-boosts
enabled_site_setting :discourse_boosts_enabled
register_asset "stylesheets/common/discourse-boosts.scss"
register_svg_icon "rocket"
register_svg_icon "trash-can"
register_svg_icon "flag"
module ::DiscourseBoosts
PLUGIN_NAME = "discourse-boosts"
end
require_relative "lib/discourse_boosts/engine"
after_initialize do
reloadable_patch do |plugin|
Post.prepend DiscourseBoosts::PostExtension
UserOption.prepend DiscourseBoosts::UserOptionExtension
Reviewable.prepend DiscourseBoosts::ReviewableExtension
TopicView.prepend DiscourseBoosts::TopicViewExtension
end
Discourse::Application.routes.append do
mount DiscourseBoosts::Engine, at: "/"
end
add_to_class(:guardian, :can_boost_post?) do |post|
authenticated? && post && can_see_post?(post) && !user.silenced? &&
!post.topic&.archived? && !post.trashed? && post.user_id.present? &&
!is_my_own?(post)
end
TopicView.on_preload do |topic_view|
if SiteSetting.discourse_boosts_enabled
topic_view.instance_variable_set(
:@posts,
topic_view.posts.includes(boosts: :user)
)
topic_view.boosts_available_flags =
Flag
.enabled
.where("'DiscourseBoosts::Boost' = ANY(applies_to)")
.pluck(:name_key)
end
end
add_to_serializer(
:post,
:boosts,
include_condition: -> do
SiteSetting.discourse_boosts_enabled &&
object.association(:boosts).loaded?
end
) do
boosts = object.boosts
if scope.user
ignored_user_ids = scope.user.ignored_user_ids
if ignored_user_ids.present?
boosts =
boosts.reject do |b|
ignored_user_ids.include?(b.user_id) && !b.user&.staff?
end
end
end
reviewables_by_target =
if scope.user && @topic_view
@topic_view.boosts_reviewables_by_target ||=
begin
all_boost_ids =
@topic_view.posts.flat_map do |p|
p.association(:boosts).loaded? ? p.boosts.map(&:id) : []
end
if all_boost_ids.present?
Reviewable
.includes(:reviewable_scores)
.where(
target_type: "DiscourseBoosts::Boost",
target_id: all_boost_ids
)
.index_by(&:target_id)
else
{}
end
end
else
{}
end
available_flags =
@topic_view&.boosts_available_flags ||
Flag
.enabled
.where("'DiscourseBoosts::Boost' = ANY(applies_to)")
.pluck(:name_key)
boosts.map do |boost|
DiscourseBoosts::BoostSerializer.new(
boost,
scope: scope,
root: false,
reviewables_by_target: reviewables_by_target,
available_flags: available_flags
).as_json
end
end
add_to_serializer(
:post,
:can_boost,
include_condition: -> do
SiteSetting.discourse_boosts_enabled &&
object.association(:boosts).loaded?
end
) do
scope.can_boost_post?(object) &&
object.boosts.none? { |b| b.user_id == scope.user.id } &&
object.boosts.size < SiteSetting.discourse_boosts_max_per_post
end
UserUpdater::OPTION_ATTR.push(:boost_notifications_level)
add_to_serializer(:user_option, :boost_notifications_level) do
object.boost_notifications_level
end
register_reviewable_type DiscourseBoosts::ReviewableBoost
DiscoursePluginRegistry.register_flag_applies_to_type(
"DiscourseBoosts::Boost",
self
)
register_seedfu_fixtures(
Rails.root.join("plugins/discourse-boosts/db/fixtures")
)
register_notification_consolidation_plan(
DiscourseBoosts::NotificationConsolidation.boosted_by_multiple_users_plan
)
end