/
plugin.rb
executable file
·239 lines (202 loc) · 8.28 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
# frozen_string_literal: true
# name: discourse-topic-voting
# about: Adds the ability to vote on topics in a specified category.
# meta_topic_id: 40121
# version: 0.5
# author: Joe Buhlig joebuhlig.com, Sam Saffron
# url: https://github.com/discourse/discourse-topic-voting
register_asset "stylesheets/common/topic-voting.scss"
register_asset "stylesheets/desktop/topic-voting.scss", :desktop
register_asset "stylesheets/mobile/topic-voting.scss", :mobile
enabled_site_setting :voting_enabled
Discourse.top_menu_items.push(:votes)
Discourse.anonymous_top_menu_items.push(:votes)
Discourse.filters.push(:votes)
Discourse.anonymous_filters.push(:votes)
after_initialize do
module ::DiscourseTopicVoting
PLUGIN_NAME = "discourse-topic-voting"
class Engine < ::Rails::Engine
isolate_namespace DiscourseTopicVoting
end
end
%w[
app/controllers/discourse_topic_voting/votes_controller.rb
app/jobs/onceoff/voting_ensure_consistency.rb
app/jobs/regular/vote_reclaim.rb
app/jobs/regular/vote_release.rb
app/models/discourse_topic_voting/category_setting.rb
app/models/discourse_topic_voting/topic_vote_count.rb
app/models/discourse_topic_voting/vote.rb
lib/discourse_topic_voting/categories_controller_extension.rb
lib/discourse_topic_voting/category_extension.rb
lib/discourse_topic_voting/list_controller_extension.rb
lib/discourse_topic_voting/topic_extension.rb
lib/discourse_topic_voting/topic_query_extension.rb
lib/discourse_topic_voting/user_extension.rb
lib/discourse_topic_voting/web_hook_extension.rb
].each { |path| require_relative path }
reloadable_patch do
CategoriesController.prepend(DiscourseTopicVoting::CategoriesControllerExtension)
Category.prepend(DiscourseTopicVoting::CategoryExtension)
ListController.prepend(DiscourseTopicVoting::ListControllerExtension)
Topic.prepend(DiscourseTopicVoting::TopicExtension)
TopicQuery.prepend(DiscourseTopicVoting::TopicQueryExtension)
User.prepend(DiscourseTopicVoting::UserExtension)
WebHook.prepend(DiscourseTopicVoting::WebHookExtension)
end
add_to_serializer(:post, :can_vote, include_condition: -> { object.post_number == 1 }) do
object.topic&.can_vote?
end
add_to_serializer(:topic_view, :can_vote) { object.topic.can_vote? }
add_to_serializer(:topic_view, :vote_count) { object.topic.vote_count }
add_to_serializer(:topic_view, :user_voted) do
scope.user ? object.topic.user_voted?(scope.user) : false
end
if TopicQuery.respond_to?(:results_filter_callbacks)
TopicQuery.results_filter_callbacks << ->(_type, result, user, options) do
result = result.includes(:topic_vote_count)
if user
result =
result.select(
"topics.*, COALESCE((SELECT 1 FROM discourse_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted",
)
if options[:state] == "my_votes"
result =
result.joins(
"INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id AND discourse_voting_votes.user_id = #{user.id}",
)
end
end
if options[:order] == "votes"
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
result =
result.joins(
"LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id",
).reorder(
"COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC",
)
end
result
end
end
register_category_custom_field_type("enable_topic_voting", :boolean)
add_to_serializer(:category, :custom_fields, respect_plugin_enabled: false) do
return object.custom_fields if !SiteSetting.voting_enabled
object.custom_fields.merge(
enable_topic_voting:
DiscourseTopicVoting::CategorySetting.find_by(category_id: object.id).present?,
)
end
add_to_serializer(:topic_list_item, :vote_count, include_condition: -> { object.can_vote? }) do
object.vote_count
end
add_to_serializer(:topic_list_item, :can_vote, include_condition: -> { object.regular? }) do
object.can_vote?
end
add_to_serializer(:topic_list_item, :user_voted, include_condition: -> { object.can_vote? }) do
object.user_voted?(scope.user) if scope.user
end
add_to_serializer(
:basic_category,
:can_vote,
include_condition: -> { Category.can_vote?(object.id) },
) { true }
register_search_advanced_filter(/^min_vote_count:(\d+)$/) do |posts, match|
posts.where(
"(SELECT votes_count FROM discourse_voting_topic_vote_count WHERE discourse_voting_topic_vote_count.topic_id = posts.topic_id) >= ?",
match.to_i,
)
end
register_search_advanced_order(:votes) do |posts|
posts.reorder(
"COALESCE((SELECT dvtvc.votes_count FROM discourse_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = topics.id), 0) DESC",
)
end
add_to_serializer(:current_user, :votes_exceeded) { object.reached_voting_limit? }
add_to_serializer(:current_user, :votes_count) { object.vote_count }
add_to_serializer(:current_user, :votes_left) { [object.vote_limit - object.vote_count, 0].max }
on(:topic_status_updated) do |topic, status, enabled|
next if topic.trashed?
next unless %w[closed autoclosed archived].include?(status)
if enabled
Jobs.enqueue(:vote_release, topic_id: topic.id)
else
is_closing_unarchived = %w[closed autoclosed].include?(status) && !topic.archived
is_archiving_open = status == "archived" && !topic.closed
Jobs.enqueue(:vote_reclaim, topic_id: topic.id) if is_closing_unarchived || is_archiving_open
end
end
on(:topic_trashed) do |topic|
if !topic.closed && !topic.archived
Jobs.enqueue(:vote_release, topic_id: topic.id, trashed: true)
end
end
on(:topic_recovered) do |topic|
Jobs.enqueue(:vote_reclaim, topic_id: topic.id) if !topic.closed && !topic.archived
end
on(:post_edited) do |post, _, revisor|
if SiteSetting.voting_enabled && revisor.topic_diff.has_key?("category_id") &&
DiscourseTopicVoting::Vote.exists?(topic_id: post.topic_id) && !post.topic.closed &&
!post.topic.archived && !post.topic.trashed?
new_category_id = post.reload.topic.category_id
if Category.can_vote?(new_category_id)
Jobs.enqueue(:vote_reclaim, topic_id: post.topic_id)
else
Jobs.enqueue(:vote_release, topic_id: post.topic_id)
end
end
end
on(:topic_merged) do |orig, dest|
moved_votes = 0
duplicated_votes = 0
if orig.who_voted.present? && orig.closed
orig.who_voted.each do |user|
next if user.blank?
user_votes = user.topics_with_vote.pluck(:topic_id)
user_archived_votes = user.topics_with_archived_vote.pluck(:topic_id)
if user_votes.include?(orig.id) || user_archived_votes.include?(orig.id)
if user_votes.include?(dest.id) || user_archived_votes.include?(dest.id)
duplicated_votes += 1
user.votes.destroy_by(topic_id: orig.id)
else
user
.votes
.find_by(topic_id: orig.id, user_id: user.id)
.update!(topic_id: dest.id, archive: dest.closed)
moved_votes += 1
end
else
next
end
end
end
if moved_votes > 0
orig.update_vote_count
dest.update_vote_count
if moderator_post = orig.ordered_posts.where(action_code: "split_topic").last
moderator_post.raw << "\n\n#{I18n.t("topic_voting.votes_moved", count: moved_votes)}"
if duplicated_votes > 0
moderator_post.raw << " #{I18n.t("topic_voting.duplicated_votes", count: duplicated_votes)}"
end
moderator_post.save!
end
end
end
DiscourseTopicVoting::Engine.routes.draw do
post "vote" => "votes#vote"
post "unvote" => "votes#unvote"
get "who" => "votes#who"
end
Discourse::Application.routes.prepend do
get "c/*category_slug_path_with_id/l/votes.rss" => "list#votes_feed", :format => :rss
end
Discourse::Application.routes.append do
mount ::DiscourseTopicVoting::Engine, at: "/voting"
get "topics/voted-by/:username" => "list#voted_by",
:as => "voted_by",
:constraints => {
username: RouteFormat.username,
}
end
end