-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsubscription_mailer.rb
79 lines (74 loc) · 2.56 KB
/
subscription_mailer.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
# frozen_string_literal: true
class SubscriptionMailer < ActionMailer::Base
helper :application
include ApplicationHelper
default from: "notifications@#{ActionMailer::Base.default_url_options[:host]}"
def notify_node_creation(node)
subject = '[PublicLab] ' +
(node.has_power_tag('question') ? 'Question: ' : '') +
node.title +
" (##{node.id}) "
@node = node
@tags = node.tags.collect(&:name).join(',')
@footer = feature('email-footer')
recipients = Tag.subscribers(node.tags).values
.map { |obj| obj[:user] }
.collect(&:email)
recipients += node.author.followers.collect(&:email)
recipients.uniq!
mail(
to: "notifications@#{ActionMailer::Base.default_url_options[:host]}",
bcc: recipients,
subject: subject
)
end
def notify_note_liked(node, user)
subject = "[PublicLab] #{user.username} liked your " +
(node.has_power_tag('question') ? 'question' : 'research note') +
" (##{node.id})"
@user = user
@node = node
@tags = node.tags.collect(&:name).join(',')
@footer = feature('email-footer')
mail(to: node.author.email, subject: subject)
end
def notify_tag_added(node, tag, tagging_user)
@tag = tag
@node = node
@tags = node.tags.collect(&:name).join(',')
@tagging_user = tagging_user
given_tags = node.tags.reject { |t| t == tag }
users_to_email = tag.followers_who_dont_follow_tags(given_tags)
users_with_everything_tag = Tag.followers('everything')
final_users_ids = nil
if !users_to_email.nil? && !users_with_everything_tag.nil?
final_users_ids = users_to_email.collect(&:id) -
users_with_everything_tag.collect(&:uid)
elsif !users_to_email.nil?
final_users_ids = users_to_email.collect(&:id)
end
final_users_to_email = User.find(final_users_ids)
recipients = []
final_users_to_email.each do |user|
unless user.id == tagging_user.id
recipients << user.email
end
end
@footer = feature('email-footer')
mail(
to: "notifications@#{ActionMailer::Base.default_url_options[:host]}",
bcc: recipients,
subject: "#{node.title} (##{node.id})"
)
end
def send_digest(user_id, nodes, frequency)
if frequency == User::Frequency::DAILY
@subject = 'Your daily digest'
elsif frequency == User::Frequency::WEEKLY
@subject = 'Your weekly digest'
end
@user = User.find(user_id)
@nodes = nodes
mail(to: @user.email, subject: @subject)
end
end