-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin.rb
254 lines (213 loc) · 9.04 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
# frozen_string_literal: true
# name: discourse-post-voting
# about: Allows the creation of topics with votable posts.
# meta_topic_id: 227808
# version: 0.0.1
# authors: Alan Tan
# url: https://github.com/discourse/discourse-post-voting
%i[common mobile desktop].each do |type|
register_asset "stylesheets/#{type}/post-voting.scss", type
end
register_asset "stylesheets/common/post-voting-crawler.scss"
enabled_site_setting :post_voting_enabled
module ::PostVoting
PLUGIN_NAME = "discourse-post-voting"
end
after_initialize do
require_relative "lib/post_voting/engine"
require_relative "lib/post_voting/vote_manager"
require_relative "lib/post_voting/guardian_extension"
require_relative "lib/post_voting/comment_creator"
require_relative "lib/post_voting/comment_review_queue"
require_relative "extensions/post_extension"
require_relative "extensions/post_serializer_extension"
require_relative "extensions/topic_extension"
require_relative "extensions/topic_list_item_serializer_extension"
require_relative "extensions/topic_view_serializer_extension"
require_relative "extensions/topic_view_extension"
require_relative "extensions/user_extension"
require_relative "extensions/composer_messages_finder_extension"
require_relative "app/validators/post_voting_comment_validator"
require_relative "app/controllers/post_voting/votes_controller"
require_relative "app/controllers/post_voting/comments_controller"
require_relative "app/models/post_voting_vote"
require_relative "app/models/post_voting_comment"
require_relative "app/models/post_voting_comment_custom_field"
require_relative "app/models/reviewable_post_voting_comment"
require_relative "app/serializers/basic_voter_serializer"
require_relative "app/serializers/post_voting_comment_serializer"
require_relative "app/serializers/reviewable_post_voting_comments_serializer"
require_relative "config/routes"
if respond_to?(:register_svg_icon)
register_svg_icon "angle-up"
register_svg_icon "info"
end
register_post_custom_field_type("vote_history", :json)
register_post_custom_field_type("vote_count", :integer)
reloadable_patch do
Post.include(PostVoting::PostExtension)
Topic.include(PostVoting::TopicExtension)
PostSerializer.include(PostVoting::PostSerializerExtension)
TopicView.prepend(PostVoting::TopicViewExtension)
TopicViewSerializer.include(PostVoting::TopicViewSerializerExtension)
TopicListItemSerializer.include(PostVoting::TopicListItemSerializerExtension)
User.include(PostVoting::UserExtension)
Guardian.prepend(PostVoting::GuardianExtension)
ComposerMessagesFinder.prepend(PostVoting::ComposerMessagesFinderExtension)
end
# TODO: Performance of the query degrades as the number of posts a user has voted
# on increases. We should probably keep a counter cache in the user's
# custom fields.
add_to_class(:user, :vote_count) { Post.where(user_id: self.id).sum(:qa_vote_count) }
add_to_serializer(:user_card, :vote_count) { object.vote_count }
add_to_class(:topic_view, :user_voted_posts) do |user|
@user_voted_posts ||= {}
@user_voted_posts[user.id] ||= begin
PostVotingVote.where(user: user, post: @posts).distinct.pluck(:post_id)
end
end
add_to_class(:topic_view, :user_voted_posts_last_timestamp) do |user|
@user_voted_posts_last_timestamp ||= {}
@user_voted_posts_last_timestamp[user.id] ||= begin
PostVotingVote
.where(user: user, post: @posts)
.group(:votable_id, :created_at)
.pluck(:votable_id, :created_at)
end
end
TopicView.apply_custom_default_scope do |scope, topic_view|
if topic_view.topic.is_post_voting? &&
!topic_view.instance_variable_get(:@replies_to_post_number) &&
!topic_view.instance_variable_get(:@post_ids)
scope = scope.where(reply_to_post_number: nil)
if topic_view.instance_variable_get(:@filter) != TopicView::ACTIVITY_FILTER
scope =
scope
.where.not(post_type: [Post.types[:whisper], Post.types[:small_action]])
.unscope(:order)
.order("CASE post_number WHEN 1 THEN 0 ELSE 1 END, qa_vote_count DESC, post_number ASC")
end
scope
else
scope
end
end
TopicView.on_preload do |topic_view|
next if !topic_view.topic.is_post_voting?
post_ids = topic_view.posts.pluck(:id)
next if post_ids.blank?
post_ids_sql = post_ids.join(",")
comment_ids_sql = <<~SQL
SELECT
post_voting_comments.id
FROM post_voting_comments
INNER JOIN LATERAL (
SELECT 1
FROM (
SELECT
comments.id
FROM post_voting_comments comments
WHERE comments.post_id = post_voting_comments.post_id
AND comments.deleted_at IS NULL
ORDER BY comments.id ASC
LIMIT #{TopicView::PRELOAD_COMMENTS_COUNT}
) X
WHERE X.id = post_voting_comments.id
) Y ON true
WHERE post_voting_comments.post_id IN (#{post_ids_sql})
AND post_voting_comments.deleted_at IS NULL
SQL
topic_view.comments = {}
PostVotingComment
.includes(:user)
.where("id IN (#{comment_ids_sql})")
.order(id: :asc)
.each do |comment|
topic_view.comments[comment.post_id] ||= []
topic_view.comments[comment.post_id] << comment
end
topic_view.comments_counts = PostVotingComment.where(post_id: post_ids).group(:post_id).count
topic_view.posts_user_voted = {}
topic_view.comments_user_voted = {}
if topic_view.guardian.user
PostVotingVote
.where(user: topic_view.guardian.user, votable_type: "Post", votable_id: post_ids)
.pluck(:votable_id, :direction)
.each { |post_id, direction| topic_view.posts_user_voted[post_id] = direction }
PostVotingVote
.joins(
"INNER JOIN post_voting_comments comments ON comments.id = post_voting_votes.votable_id",
)
.where(user: topic_view.guardian.user, votable_type: "PostVotingComment")
.where("comments.post_id IN (?)", post_ids)
.pluck(:votable_id)
.each { |votable_id| topic_view.comments_user_voted[votable_id] = true }
end
topic_view.posts_voted_on =
PostVotingVote.where(votable_type: "Post", votable_id: post_ids).distinct.pluck(:votable_id)
end
add_permitted_post_create_param(:create_as_post_voting)
# TODO: Core should be exposing the following as proper plugin interfaces.
NewPostManager.add_plugin_payload_attribute(:subtype)
TopicSubtype.register(Topic::POST_VOTING_SUBTYPE)
NewPostManager.add_handler do |manager|
if !manager.args[:topic_id] && manager.args[:create_as_post_voting] == "true" &&
(manager.args[:archetype].blank? || manager.args[:archetype] == Archetype.default)
manager.args[:subtype] = Topic::POST_VOTING_SUBTYPE
end
false
end
register_modifier(:topic_embed_import_create_args) do |args|
category_id = args[:category]
next args unless category_id
next args if args[:archetype] != Archetype.default && !args[:archetype].blank?
category = Category.find_by(id: category_id)
if category&.create_as_post_voting_default || category&.only_post_voting_in_this_category
args[:subtype] = Topic::POST_VOTING_SUBTYPE
end
args
end
register_category_custom_field_type(PostVoting::CREATE_AS_POST_VOTING_DEFAULT, :boolean)
if respond_to?(:register_preloaded_category_custom_fields)
register_preloaded_category_custom_fields(PostVoting::CREATE_AS_POST_VOTING_DEFAULT)
else
# TODO: Drop the if-statement and this if-branch in Discourse v3.2
Site.preloaded_category_custom_fields << PostVoting::CREATE_AS_POST_VOTING_DEFAULT
end
add_to_class(:category, :create_as_post_voting_default) do
ActiveModel::Type::Boolean.new.cast(
self.custom_fields[PostVoting::CREATE_AS_POST_VOTING_DEFAULT],
)
end
add_to_serializer(:basic_category, :create_as_post_voting_default) do
object.create_as_post_voting_default
end
add_to_serializer(:current_user, :can_flag_post_voting_comments) do
scope.can_flag_post_voting_comments?
end
register_category_custom_field_type(PostVoting::ONLY_POST_VOTING_IN_THIS_CATEGORY, :boolean)
if Site.respond_to? :preloaded_category_custom_fields
Site.preloaded_category_custom_fields << PostVoting::ONLY_POST_VOTING_IN_THIS_CATEGORY
Site.preloaded_category_custom_fields << PostVoting::CREATE_AS_POST_VOTING_DEFAULT
end
add_to_class(:category, :only_post_voting_in_this_category) do
ActiveModel::Type::Boolean.new.cast(
self.custom_fields[PostVoting::ONLY_POST_VOTING_IN_THIS_CATEGORY],
)
end
add_to_serializer(:basic_category, :only_post_voting_in_this_category) do
object.only_post_voting_in_this_category
end
add_model_callback(:post, :before_create) do
if SiteSetting.post_voting_enabled && self.is_post_voting_topic? && self.via_email &&
self.reply_to_post_number == 1
self.reply_to_post_number = nil
end
end
register_user_destroyer_on_content_deletion_callback(
Proc.new do |user|
PostVotingComment.where(user_id: user.id).delete_all
PostVoting::VoteManager.bulk_remove_votes_by(user)
end,
)
end