-
Notifications
You must be signed in to change notification settings - Fork 25
/
plugin.rb
281 lines (224 loc) · 7.2 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# frozen_string_literal: true
# name: discourse-ratings
# about: A Discourse plugin that lets you use topics to rate things
# version: 0.2.3
# authors: Angus McLeod, Faizaan Gagan
# url: https://github.com/paviliondev/discourse-ratings
# contact_emails: development@pavilion.tech
enabled_site_setting :rating_enabled
register_asset 'stylesheets/common/ratings.scss'
register_asset 'stylesheets/desktop/ratings.scss', :desktop
register_asset 'stylesheets/mobile/ratings.scss', :mobile
if respond_to?(:register_svg_icon)
register_svg_icon "info"
register_svg_icon "save"
end
add_admin_route "admin.ratings.settings_page", "ratings"
on(:post_created) do |post, opts, user|
if opts[:ratings].present?
begin
ratings = JSON.parse(opts[:ratings])
rescue JSON::ParserError
ratings = []
end
topic = post.topic
user_can_rate = topic.user_can_rate(user)
ratings = DiscourseRatings::Rating.build_list(ratings)
.select { |r| user_can_rate.include?(r.type) }
if ratings.any?
post.update_ratings(ratings)
end
end
end
on(:post_edited) do |post, topic_changed, revisor|
if revisor.ratings.present?
topic = post.topic
user = post.user
user_has_rated = post.rated_types
user_can_rate = topic.user_can_rate(user)
ratings = revisor.ratings.select do |r|
user_has_rated.include?(r.type) ||
user_can_rate.include?(r.type)
end
post.update_ratings(ratings)
end
revisor.clear_ratings_cache!
end
on(:post_destroyed) do |post, opts, user|
if (ratings = post.ratings).present?
post.update_ratings(ratings)
end
end
on(:post_recovered) do |post, opts, user|
if (ratings = post.ratings).present?
post.update_ratings(ratings)
end
end
after_initialize do
%w[
../lib/ratings/engine.rb
../lib/ratings/cache.rb
../lib/ratings/rating.rb
../lib/ratings/rating_type.rb
../lib/ratings/object.rb
../config/routes.rb
../jobs/regular/destroy_rating_type.rb
../jobs/regular/destroy_ratings.rb
../jobs/regular/migrate_ratings.rb
../app/serializers/ratings/object.rb
../app/serializers/ratings/rating.rb
../app/serializers/ratings/rating_type.rb
../app/controllers/ratings/object.rb
../app/controllers/ratings/rating.rb
../app/controllers/ratings/rating_type.rb
../extensions/post_revisor.rb
../extensions/posts_controller.rb
../extensions/topic.rb
].each do |path|
load File.expand_path(path, __FILE__)
end
###### Site ######
add_to_class(:site, :rating_type_names) do
map = {}
DiscourseRatings::RatingType.all.each { |t| map[t.type] = t.name }
map
end
add_to_serializer(:site, :rating_type_names) do
object.rating_type_names
end
add_to_serializer(:site, :category_rating_types) do
build_object_list(DiscourseRatings::Object.list('category'))
end
add_to_serializer(:site, :tag_rating_types) do
build_object_list(DiscourseRatings::Object.list('tag'))
end
add_to_class(:site_serializer, :build_object_list) do |list|
result = {}
list.each { |obj| result[obj.name] = obj.types }
result
end
###### Category && Tag ######
add_to_class(:category, :rating_types) do
DiscourseRatings::Object.get('category', rating_key)
end
add_to_class(:category, :rating_key) do
slug_path.join("/")
end
add_to_class(:tag, :rating_types) do
DiscourseRatings::Object.get('tag', name)
end
###### Post ######
add_permitted_post_create_param("ratings")
### These monkey patches are necessary as there is currently
### no way to add post attributes on update
::PostsController.prepend PostsControllerRatingsExtension
::PostRevisor.prepend PostRevisorRatingsExtension
add_to_class(:post, :ratings) do
DiscourseRatings::Rating.build_model_list(custom_fields, topic.rating_types)
end
add_to_class(:post, :rated_types) do
ratings.select { |r| r.weight > 0 }.map(&:type)
end
add_to_class(:post, :update_ratings) do |ratings|
Post.transaction do
DiscourseRatings::Rating.set_custom_fields(self, ratings)
save_custom_fields(true)
update_topic_ratings
end
push_ratings_to_clients
end
add_to_class(:post, :update_topic_ratings) do
types = topic.rating_types
post_ratings = topic.reload.posts.map { |p| p.ratings }.flatten
return if types.blank? || post_ratings.blank?
types.each do |type|
type_ratings = post_ratings.select { |r| r.type === type.to_s }
included_type_ratings = type_ratings.select { |r| r.weight > 0 }
average = 0
count = 0
if included_type_ratings.any?
sum = included_type_ratings.map { |r| r.value }.inject(:+)
count = included_type_ratings.length
average = (sum / count).to_f
end
topic_rating = {
type: type,
value: average,
count: count
}
DiscourseRatings::Rating.build_and_set(topic, topic_rating)
end
topic.save_custom_fields(true)
end
add_to_class(:post, :push_ratings_to_clients) do
publish_change_to_clients!("ratings",
ratings: topic.ratings.as_json,
user_can_rate: topic.user_can_rate(user)
)
end
add_to_serializer(:post, :ratings, respect_plugin_enabled: false) do
DiscourseRatings::Rating.serialize(object.ratings)
end
add_to_serializer(:post, :include_ratings?) do
# we need to explictly check for plugin enabled when defining custom include method
SiteSetting.rating_enabled &&
(
!SiteSetting.rating_hide_except_own_entry ||
(
scope.current_user.present? &&
(
scope.current_user.staff? ||
scope.current_user.id === object.user.id
)
)
)
end
###### Topic ######
add_to_class(:topic, :ratings) do
DiscourseRatings::Rating.build_model_list(custom_fields, rating_types)
end
add_to_class(:topic, :rating_types) do
types = []
types.push(category.rating_types) if category.present?
types.push(tags.map { |tag| tag.rating_types }) if tags.present?
types.flatten.uniq
end
add_to_class(:topic, :rating_enabled?) do
SiteSetting.rating_enabled && rating_types.any?
end
add_to_class(:topic, :user_can_rate) do |user|
rating_types.select do |type|
user_has_rated(user).exclude?(type)
end
end
add_to_class(:topic, :user_has_rated) do |user|
posts.select do |post|
post.user_id === user.id
end.map do |post|
post.rated_types
end.flatten
end
add_to_serializer(:topic_view, :ratings) do
DiscourseRatings::Rating.serialize(object.topic.ratings)
end
add_to_serializer(:topic_view, :show_ratings) do
SiteSetting.rating_topic_average_enabled &&
object.topic.rating_enabled? &&
object.topic.ratings.present?
end
add_to_serializer(:topic_view, :user_can_rate) do
object.topic.user_can_rate(scope.current_user)
end
add_to_serializer(:topic_view, :include_user_can_rate?) do
scope.current_user && object.topic.rating_enabled?
end
::Topic.singleton_class.prepend TopicRatingsExtension
add_to_serializer(:topic_list_item, :ratings) do
DiscourseRatings::Rating.serialize(object.ratings)
end
add_to_serializer(:topic_list_item, :show_ratings) do
SiteSetting.rating_topic_list_average_enabled &&
object.rating_enabled? &&
object.ratings.present?
end
end