forked from tareko/discourse-replyto-individual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
81 lines (67 loc) · 2.6 KB
/
plugin.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
# name: replyto-individual plugin
# about: A plugin that allows exposure of the sender's email address for functionality
# similar to GNU/Mailman's Reply_Goes_To_List = Poster
# version: 0.0.2
# authors: Alan Schmitz <[email protected]>, Tarek Loubani <[email protected]>
# license: aGPLv3
# url: https://github.com/alankeny/discourse-replyto-individual
PLUGIN_NAME ||= "replyto-individual".freeze
enabled_site_setting :replyto_individual_enabled
after_initialize do
Email::MessageBuilder.class_eval do
attr_reader :template_args
ALLOW_REPLY_BY_EMAIL_HEADER = 'X-Discourse-Allow-Reply-By-Email'.freeze
def header_args
result = {}
if @opts[:add_unsubscribe_link]
unsubscribe_url = @template_args[:unsubscribe_url].presence || @template_args[:user_preferences_url]
result['List-Unsubscribe'] = "<#{unsubscribe_url}>"
end
result['X-Discourse-Post-Id'] = @opts[:post_id].to_s if @opts[:post_id]
result['X-Discourse-Topic-Id'] = @opts[:topic_id].to_s if @opts[:topic_id]
# please, don't send us automatic responses...
result['X-Auto-Response-Suppress'] = 'All'
if allow_reply_by_email?
result[ALLOW_REPLY_BY_EMAIL_HEADER] = true
result['Reply-To'] = reply_by_email_address
else
result['Reply-To'] = from_value
end
if SiteSetting.replyto_individual_enabled?
if private_reply?
result['Reply-To'] = reply_by_email_address
else
p = Post.find_by_id @opts[:post_id]
if p
result['Reply-To'] = "#{p.user.name} <#{p.user.email}>"
if SiteSetting.replyto_individual_cc?
result['CC'] = reply_by_email_address
end
end
end
end
result.merge(Email::MessageBuilder.custom_headers(SiteSetting.email_custom_headers))
end
end
# Fix the Email::Sender method to also insert the reply_key into CC
Email::Sender.class_eval do
def set_reply_key(post_id, user_id)
return unless user_id &&
post_id &&
header_value(Email::MessageBuilder::ALLOW_REPLY_BY_EMAIL_HEADER).present?
# use safe variant here cause we tend to see concurrency issue
reply_key = PostReplyKey.find_or_create_by_safe!(
post_id: post_id,
user_id: user_id
).reply_key
if header_value('Reply-To') =~ /reply_key/i
@message.header['Reply-To'] =
header_value('Reply-To').gsub!("%{reply_key}", reply_key)
end
if !header_value('CC').blank?
@message.header['CC'] =
header_value('CC').gsub!("%{reply_key}", reply_key)
end
end
end
end