/
event_mailer.rb
113 lines (97 loc) · 3.86 KB
/
event_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
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
class EventMailer < BaseMailer
REPLY_DELIMITER = ""*4 # surprise! this is actually U+FEFF
# TODO this should be NotificationMailer, and take a notification id
def event(recipient_id, event_id)
@current_user = @recipient = User.active.find_by!(id: recipient_id)
@event = Event.find_by!(id: event_id)
@notification = Notification.find_by(user_id: recipient_id, event_id: event_id)
return if @event.eventable.nil?
return if @event.eventable.respond_to?(:discarded?) && @event.eventable.discarded?
if %w[Poll Stance Outcome].include? @event.eventable_type
@poll = @event.eventable.poll
end
if @event.eventable.respond_to? :discussion
@discussion = @event.eventable.discussion
end
if @event.eventable.respond_to?(:group_id) && @event.eventable.group_id
@membership = Membership.active.find_by(
group_id: @event.eventable.group_id,
user_id: recipient_id
)
# this might be necessary to comply with anti-spam rules
# if someone does not respond to the invitation, don't send them more emails
return if @membership &&
!@recipient.email_verified &&
!["membership_created", "membership_resent"].include?(@event.kind)
end
@utm_hash = { utm_medium: 'email', utm_campaign: @event.kind }
thread_kinds = %w[
new_comment
new_discussion
discussion_edited
discussion_announced
]
headers = {
"Precedence": :bulk,
"X-Auto-Response-Suppress": :OOF,
"Auto-Submitted": :"auto-generated"
}
if @event.eventable.respond_to?(:calendar_invite) && @event.eventable.calendar_invite
attachments['meeting.ics'] = {
content_type: 'text/calendar',
content_transfer_encoding: 'base64',
content: Base64.encode64(@event.eventable.calendar_invite)
}
end
template_name = @event.eventable_type.tableize.singularize
template_name = 'poll' if @event.eventable_type == 'Outcome'
template_name = 'group' if @event.eventable_type == 'Membership'
# this should be notification.i18n_key
@event_key = if (@event.kind == 'user_mentioned' &&
@event.eventable.respond_to?(:parent) &&
@event.eventable.parent.present? &&
@event.eventable.parent.author == @recipient)
"comment_replied_to"
elsif @event.kind == 'poll_created'
'poll_announced'
else
@event.kind
end
subject_params = {
title: @event.eventable.title,
group_name: @event.eventable.title, # cope for old translations
poll_type: @poll && I18n.t("poll_types.#{@poll.poll_type}"),
actor: @event.user.name,
site_name: AppConfig.theme[:site_name]
}
send_single_mail(
to: @recipient.email,
from: from_user_via_loomio(@event.user),
locale: @recipient.locale,
reply_to: reply_to_address_with_group_name(model: @event.eventable, user: @recipient),
subject_prefix: group_name_prefix(@event),
subject_key: "notifications.with_title.#{@event_key}",
subject_params: subject_params,
subject_is_title: thread_kinds.include?(@event.kind),
template_name: template_name
)
end
private
def group_name_prefix(event)
model = event.eventable
if %w[membership_requested membership_created].include? event.kind
''
else
model.group.present? ? "[#{model.group.handle || model.group.full_name}] " : ''
end
end
def reply_to_address_with_group_name(model:, user:)
return nil unless user.is_logged_in?
return nil unless model.respond_to?(:discussion) && model.discussion.present?
if model.discussion.group.present?
"\"#{I18n.transliterate(model.discussion.group.full_name).truncate(50).delete('"')}\" <#{reply_to_address(model: model, user: user)}>"
else
"\"#{user.name}\" <#{reply_to_address(model: model, user: user)}>"
end
end
end