/
qa_topic_edits.rb
217 lines (179 loc) · 4.71 KB
/
qa_topic_edits.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
module TopicQAExtension
def reload(options = nil)
@answers = nil
@comments = nil
@last_answerer = nil
super(options)
end
def answers
@answers ||= posts.where(reply_to_post_number: [nil, '']).order("created_at ASC")
end
def comments
@comments ||= posts.where.not(reply_to_post_number: [nil, '']).order("created_at ASC")
end
def answer_count
answers.count - 1 ## minus first post
end
def comment_count
comments.count
end
def last_answered_at
if answers.any?
answers.last[:created_at]
else
nil
end
end
def last_commented_on
if comments.any?
comments.last[:created_at]
else
nil
end
end
def last_answer_post_number
if answers.any?
answers.last[:post_number]
else
nil
end
end
def last_answerer
if answers.any?
@last_answerer ||= ::User.find(answers.last[:user_id])
else
nil
end
end
end
require_dependency 'topic'
class ::Topic
prepend TopicQAExtension
def self.qa_can_vote(topic, user)
return false if !user || !SiteSetting.qa_enabled
topic_vote_count = self.qa_votes(topic, user).length
return false if topic_vote_count > 0 && !SiteSetting.qa_trust_level_vote_limits
topic_vote_limit = SiteSetting.send("qa_tl#{user.trust_level}_vote_limit")
topic_vote_limit.to_i >= topic_vote_count
end
def self.qa_votes(topic, user)
return nil if !user || !SiteSetting.qa_enabled
PostCustomField.where(post_id: topic.posts.map(&:id),
name: 'voted',
value: user.id).pluck(:post_id)
end
def self.qa_enabled(topic)
return false if !SiteSetting.qa_enabled
return false if !topic || !topic.respond_to?(:is_category_topic?) || topic.is_category_topic?
tags = topic.tags.map(&:name)
has_qa_tag = !(tags & SiteSetting.qa_tags.split('|')).empty?
is_qa_category = topic.category && topic.category.custom_fields["qa_enabled"]
is_qa_subtype = topic.subtype == 'question'
has_qa_tag || is_qa_category || is_qa_subtype
end
def self.qa_update_vote_order(topic_id)
return if !SiteSetting.qa_enabled
posts = Post.where(topic_id: topic_id)
posts.where(post_number: 1).update(sort_order: 1)
answers = posts.where(reply_to_post_number: [nil, ''])
.where.not(post_number: 1)
.order("(
SELECT COALESCE ((
SELECT value::integer FROM post_custom_fields
WHERE post_id = posts.id AND name = 'vote_count'
), 0)
) DESC, post_number ASC")
count = 2
answers.each do |a|
a.update(sort_order: count)
comments = posts.where(reply_to_post_number: a.post_number)
.order("post_number ASC")
if comments.any?
comments.each do |c|
count += 1
c.update(sort_order: count)
end
else
count += 1
end
end
end
end
module TopicViewQAExtension
def qa_enabled
::Topic.qa_enabled(@topic)
end
def filter_posts_by_ids(post_ids)
if qa_enabled
posts = ::Post.where(id: post_ids, topic_id: @topic.id)
.includes(:user, :reply_to_user, :incoming_email)
@posts = posts.order("case when post_number = 1 then 0 else 1 end, sort_order ASC")
@posts = filter_post_types(@posts)
@posts = @posts.with_deleted if @guardian.can_see_deleted_posts?
@posts
else
super
end
end
end
class TopicView
prepend TopicViewQAExtension
end
require_dependency 'topic_view_serializer'
require_dependency 'basic_user_serializer'
class ::TopicViewSerializer
attributes :qa_enabled,
:qa_votes,
:qa_can_vote,
:last_answered_at,
:last_commented_on,
:answer_count,
:comment_count,
:last_answer_post_number,
:last_answerer
def qa_enabled
object.qa_enabled
end
def qa_votes
Topic.qa_votes(object.topic, scope.current_user)
end
def qa_can_vote
Topic.qa_can_vote(object.topic, scope.current_user)
end
def last_answered_at
object.topic.last_answered_at
end
def include_last_answered_at?
qa_enabled
end
def last_commented_on
object.topic.last_commented_on
end
def include_last_commented_on?
qa_enabled
end
def answer_count
object.topic.answer_count
end
def include_answer_count?
qa_enabled
end
def comment_count
object.topic.comment_count
end
def include_comment_count?
qa_enabled
end
def last_answer_post_number
object.topic.last_answer_post_number
end
def include_last_answer_post_number?
qa_enabled
end
def last_answerer
::BasicUserSerializer.new(object.topic.last_answerer, scope: scope, root: false)
end
def include_last_answerer
qa_enabled
end
end