-
Notifications
You must be signed in to change notification settings - Fork 71
/
mentioner.rb
75 lines (61 loc) · 2.19 KB
/
mentioner.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
# frozen_string_literal: true
module Mentioner
def after_save_mention(new_mentions)
return if instance_of?(Report)
notify_users_found_by_mentions(new_mentions)
end
def new_mention_users
find_users_from_login_names(extract_login_names_from_mentions(new_mentions))
end
def where_mention
case self
when Product
"#{user.login_name}さんの「#{practice[:title]}」の提出物"
when Report
"#{user.login_name}さんの日報「#{self[:title]}」"
when Comment
"#{target_of_comment(commentable.class, commentable)}へのコメント"
when Answer
"#{receiver.login_name}さんのQ&A「#{question[:title]}」へのコメント"
when Question
"#{user.login_name}さんのQ&A「#{practice[:title]}」"
end
end
def body
self[:body] || self[:description]
end
def notify_all_mention_user
notify_users_found_by_mentions(mentions)
end
private
def notify_users_found_by_mentions(mentions)
notify_mentions(find_users_from_mentions(mentions))
end
def notify_mentions(receivers)
return nil if instance_of?(Comment) && commentable.instance_of?(Talk) # protect mention in talk
receivers.each do |receiver|
ActivityDelivery.with(mentionable: self, receiver: receiver).notify(:mentioned) if sender != receiver
end
end
def find_users_from_login_names(names)
User.where(login_name: names)
end
def find_users_from_mentions(mentions)
names = extract_login_names_from_mentions(mentions)
names.concat(User.mentor.map(&:login_name)) if mentions.include?('@mentor')
# find_users_from_login_names 内のwhereで重複は削除する
find_users_from_login_names(names)
end
def extract_login_names_from_mentions(mentions)
mentions.map { |s| s.gsub(/@/, '') }
end
def target_of_comment(commentable_class, commentable)
{
Report: "#{commentable.user.login_name}さんの日報「#{commentable.title}」",
Product: "#{commentable.user.login_name}さんの#{commentable.title}",
Event: "イベント「#{commentable.title}」",
Page: "Docs「#{commentable.title}」",
Announcement: "お知らせ「#{commentable.title}」"
}[:"#{commentable_class}"]
end
end