-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcomment.rb
341 lines (292 loc) · 9.58 KB
/
comment.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
class Comment < ApplicationRecord
include CommentsShared # common methods for comment-like models
belongs_to :node, foreign_key: 'nid', touch: true, counter_cache: true
# dependent: :destroy, counter_cache: true
belongs_to :drupal_user, foreign_key: 'uid'
belongs_to :answer, foreign_key: 'aid'
has_many :likes, :as => :likeable
validates :comment, presence: true
self.table_name = 'comments'
self.primary_key = 'cid'
COMMENT_FILTER = "<!-- @@$$%% Trimmed Content @@$$%% -->".freeze
def self.inheritance_column
'rails_type'
end
def self.search(query)
Comment.where('MATCH(comment) AGAINST(?)', query)
.where(status: 1)
end
def self.comment_weekly_tallies(span = 52, time = Time.now)
weeks = {}
(0..span).each do |week|
weeks[span - week] = Comment.select(:timestamp)
.where(timestamp: time.to_i - week.weeks.to_i..time.to_i - (week - 1).weeks.to_i)
.count
end
weeks
end
def self.contribution_graph_making(span = 52, time = Time.now)
weeks = {}
week = span
count = 0
while week >= 1
# initialising month variable with the month of the starting day
# of the week
month = (time - (week * 7 - 1).days).strftime('%m')
month = month.to_i
# Now fetching comments per week
current_week = Comment.select(:timestamp)
.where(timestamp: time.to_i - week.weeks.to_i..time.to_i - (week - 1).weeks.to_i)
.count
weeks[count] = [month, current_week]
count += 1
week -= 1
end
weeks
end
def id
cid
end
def created_at
Time.at(timestamp)
end
def body
finder = comment.gsub(Callouts.const_get(:FINDER), Callouts.const_get(:PRETTYLINKMD))
finder = finder.gsub(Callouts.const_get(:HASHTAGNUMBER), Callouts.const_get(:NODELINKMD))
finder = finder.gsub(Callouts.const_get(:HASHTAG), Callouts.const_get(:HASHLINKMD))
ApplicationController.helpers.emojify(finder)
end
def body_markdown
RDiscount.new(body, :autolink).to_html
end
def icon
"<i class='icon-comment'></i>"
end
def type
'comment'
end
def tags
[]
end
def next_thread
(thread.split('/').first.to_i(16) + 1).to_s(16).rjust(2, '0') + '/'
end
def parent
if aid == 0
node
else
return answer.node unless answer.nil?
end
end
def mentioned_users
usernames = comment.scan(Callouts.const_get(:FINDER))
User.where(username: usernames.map { |m| m[1] }).distinct
end
def followers_of_mentioned_tags
tagnames = comment.scan(Callouts.const_get(:HASHTAG))
tagnames.map { |tagname| Tag.followers(tagname[1]) }.flatten.uniq
end
def notify_callout_users
# notify mentioned users
mentioned_users.each do |user|
CommentMailer.notify_callout(self, user).deliver_now if user.username != author.username
end
end
def notify_tag_followers(already_mailed_uids = [])
# notify users who follow the tags mentioned in the comment
followers_of_mentioned_tags.each do |user|
CommentMailer.notify_tag_followers(self, user).deliver_now unless already_mailed_uids.include?(user.uid)
end
end
def notify_users(uids, current_user)
DrupalUser.where('uid IN (?)', uids).each do |user|
if user.uid != current_user.uid
CommentMailer.notify(user.user, self).deliver_now
end
end
end
# email all users in this thread
# plus all who've starred it
def notify(current_user)
if parent.uid != current_user.uid && !UserTag.exists?(parent.uid, 'notify-comment-direct:false')
CommentMailer.notify_note_author(parent.author, self).deliver_now
end
notify_callout_users
# notify other commenters, revisers, and likers, but not those already @called out
already = mentioned_users.collect(&:uid) + [parent.uid]
uids = uids_to_notify - already
notify_users(uids, current_user)
notify_tag_followers(already + uids)
end
def answer_comment_notify(current_user)
# notify answer author
if answer.uid != current_user.uid
CommentMailer.notify_answer_author(answer.author, self).deliver_now
end
notify_callout_users
already = mentioned_users.collect(&:uid) + [answer.uid]
uids = []
# notify other answer commenter and users who liked the answer
# except mentioned users and answer author
(answer.comments.collect(&:uid) + answer.likers.collect(&:uid)).uniq.each do |u|
uids << u unless already.include?(u)
end
notify_users(uids, current_user)
notify_tag_followers(already + uids)
end
def spam
self.status = 0
save
self
end
def publish
self.status = 1
save
self
end
def liked_by(user_id)
likes.where(user_id: user_id).count > 0
end
def likers
User.where(id: likes.pluck(:user_id))
end
def emoji_likes
likes.group(:emoji_type).count
end
def user_reactions_map
likes_map = likes.where.not(emoji_type: nil).includes(:user).group_by(&:emoji_type)
user_like_map = {}
likes_map.each do |reaction, likes|
users = []
likes.each do |like|
users << like.user.name
end
emoji_type = reaction.underscore.humanize.downcase
users_string = (users.length > 1 ? users[0..-2].join(", ") + " and " + users[-1] : users[0]) + " reacted with " + emoji_type + " emoji"
user_like_map[reaction] = users_string
end
user_like_map
end
def self.receive_mail(mail)
user = User.where(email: mail.from.first).first
if user
node_id = mail.subject[/#([\d]+)/, 1] # This tooks out the node ID from the subject line
if node_id.nil?
answer_id = mail.subject[/#a([\d]+)/, 1] # This tooks out the answer ID from the subject line
unless answer_id.nil?
add_answer_comment(mail, answer_id, user)
end
else
add_comment(mail, node_id, user)
end
end
end
def self.add_answer_comment(mail, answer_id, user)
answer = Answer.where(id: answer_id).first
if answer
mail_doc = Nokogiri::HTML(mail.html_part.body.decoded) # To parse the mail to extract comment content and reply content
domain = get_domain mail.from.first
content = if domain == "gmail"
gmail_parsed_mail mail_doc
elsif domain == "yahoo"
yahoo_parsed_mail mail_doc
elsif gmail_quote_present?(mail_doc)
gmail_parsed_mail mail_doc
else
{
"comment_content" => mail_doc,
"extra_content" => nil
}
end
if content["extra_content"].nil?
comment_content_markdown = ReverseMarkdown.convert content["comment_content"]
else
extra_content_markdown = ReverseMarkdown.convert content["extra_content"]
comment_content_markdown = ReverseMarkdown.convert content["comment_content"]
comment_content_markdown = comment_content_markdown + COMMENT_FILTER + extra_content_markdown
end
message_id = mail.message_id
comment = Comment.new(uid: user.uid,
aid: answer_id,
comment: comment_content_markdown,
comment_via: 1,
message_id: message_id,
timestamp: Time.now.to_i)
if comment.save
comment.answer_comment_notify(user)
end
end
end
def self.add_comment(mail, node_id, user)
node = Node.where(nid: node_id).first
if node
mail_doc = Nokogiri::HTML(mail.html_part.body.decoded) # To parse the mail to extract comment content and reply content
domain = get_domain mail.from.first
content = if domain == "gmail"
gmail_parsed_mail mail_doc
elsif domain == "yahoo"
yahoo_parsed_mail mail_doc
elsif gmail_quote_present?(mail_doc)
gmail_parsed_mail mail_doc
else
{
"comment_content" => mail_doc,
"extra_content" => nil
}
end
if content["extra_content"].nil?
comment_content_markdown = ReverseMarkdown.convert content["comment_content"]
else
extra_content_markdown = ReverseMarkdown.convert content["extra_content"]
comment_content_markdown = ReverseMarkdown.convert content["comment_content"]
comment_content_markdown = comment_content_markdown + COMMENT_FILTER + extra_content_markdown
end
message_id = mail.message_id
comment = node.add_comment(uid: user.uid, body: comment_content_markdown, comment_via: 1, message_id: message_id)
comment.notify user
end
end
def self.gmail_quote_present?(mail_doc)
mail_doc.css(".gmail_quote").any?
end
def self.get_domain(email)
domain = email[/(?<=@)[^.]+(?=\.)/, 0]
end
def self.yahoo_parsed_mail(mail_doc)
if mail_doc.css(".yahoo_quoted")
extra_content = mail_doc.css(".yahoo_quoted")[0]
mail_doc.css(".yahoo_quoted")[0].remove
comment_content = mail_doc
else
comment_content = mail_doc
extra_content = nil
end
{
"comment_content" => comment_content,
"extra_content" => extra_content
}
end
def self.gmail_parsed_mail(mail_doc)
if mail_doc.css(".gmail_quote").any?
extra_content = mail_doc.css(".gmail_quote")[0]
mail_doc.css(".gmail_quote")[0].remove
comment_content = mail_doc
else
comment_content = mail_doc
extra_content = nil
end
{
"comment_content" => comment_content,
"extra_content" => extra_content
}
end
def trimmed_content?
comment.include?(COMMENT_FILTER)
end
def render_body
RDiscount.new(
title_suggestion(self),
:autolink
).to_html
end
end