chore: Enable email channel (#1851)

This commit is contained in:
Sojan Jose
2021-03-04 13:59:59 +05:30
committed by GitHub
parent 6aed01de0c
commit ca4a766b82
15 changed files with 54 additions and 48 deletions

View File

@@ -25,7 +25,7 @@
<woot-code
v-if="isAEmailInbox"
lang="html"
:script="currentInbox.forward_to_address"
:script="currentInbox.forward_to_email"
>
</woot-code>
</div>

View File

@@ -22,7 +22,7 @@ class ApplicationMailbox < ActionMailbox::Base
proc do |inbound_mail_obj|
is_a_support_email = false
inbound_mail_obj.mail.to.each do |email|
channel = Channel::Email.find_by(email: email)
channel = Channel::Email.find_by('email = ? OR forward_to_email = ?', email, email)
if channel.present?
is_a_support_email = true
break
@@ -37,7 +37,10 @@ class ApplicationMailbox < ActionMailbox::Base
end
# routing should be defined below the referenced procs
# routes as a reply to existing conversations
routing(reply_mail? => :reply)
# routes as a new conversation in email channel
routing(support_mail? => :support)
routing(catch_all_mail? => :default)
end

View File

@@ -19,7 +19,7 @@ class SupportMailbox < ApplicationMailbox
def find_channel
mail.to.each do |email|
@channel = Channel::Email.find_by(email: email)
@channel = Channel::Email.find_by('email = ? OR forward_to_email = ?', email, email)
break if @channel.present?
end
raise 'Email channel/inbox not found' if @channel.nil?

View File

@@ -142,11 +142,7 @@ class ConversationReplyMailer < ApplicationMailer
end
def current_domain
@current_domain ||= begin
@account.domain ||
ENV.fetch('MAILER_INBOUND_EMAIL_DOMAIN', false) ||
GlobalConfig.get('MAILER_INBOUND_EMAIL_DOMAIN')['MAILER_INBOUND_EMAIL_DOMAIN']
end
@current_domain ||= @account.inbound_email_domain
end
def account_support_email

View File

@@ -87,6 +87,14 @@ class Account < ApplicationRecord
}
end
def inbound_email_domain
domain || GlobalConfig.get('MAILER_INBOUND_EMAIL_DOMAIN')['MAILER_INBOUND_EMAIL_DOMAIN'] || ENV.fetch('MAILER_INBOUND_EMAIL_DOMAIN', false)
end
def support_email
super || GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] || ENV.fetch('MAILER_SENDER_EMAIL', nil)
end
private
def notify_creation

View File

@@ -2,17 +2,17 @@
#
# Table name: channel_email
#
# id :bigint not null, primary key
# email :string not null
# forward_to_address :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# id :bigint not null, primary key
# email :string not null
# forward_to_email :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
# Indexes
#
# index_channel_email_on_email (email) UNIQUE
# index_channel_email_on_forward_to_address (forward_to_address) UNIQUE
# index_channel_email_on_email (email) UNIQUE
# index_channel_email_on_forward_to_email (forward_to_email) UNIQUE
#
class Channel::Email < ApplicationRecord
@@ -21,10 +21,10 @@ class Channel::Email < ApplicationRecord
validates :account_id, presence: true
belongs_to :account
validates :email, uniqueness: true
validates :forward_to_address, uniqueness: true
validates :forward_to_email, uniqueness: true
has_one :inbox, as: :channel, dependent: :destroy
before_validation :ensure_forward_to_address, on: :create
before_validation :ensure_forward_to_email, on: :create
def name
'Email'
@@ -36,8 +36,7 @@ class Channel::Email < ApplicationRecord
private
def ensure_forward_to_address
email_domain = InstallationConfig.find_by(name: 'MAILER_INBOUND_EMAIL_DOMAIN')&.value
self.forward_to_address ||= "#{SecureRandom.hex}@#{email_domain}"
def ensure_forward_to_email
self.forward_to_email ||= "#{SecureRandom.hex}@#{account.inbound_email_domain}"
end
end

View File

@@ -91,13 +91,13 @@ class MailPresenter < SimpleDelegator
end
def quoted_text_regexes
return sender_agnostic_regexes if @account.nil? || account_support_email.blank?
return sender_agnostic_regexes if @account.nil? || @account.support_email.blank?
[
Regexp.new("From:\s* #{Regexp.escape(account_support_email)}", Regexp::IGNORECASE),
Regexp.new("<#{Regexp.escape(account_support_email)}>", Regexp::IGNORECASE),
Regexp.new("#{Regexp.escape(account_support_email)}\s+wrote:", Regexp::IGNORECASE),
Regexp.new("On(.*)#{Regexp.escape(account_support_email)}(.*)wrote:", Regexp::IGNORECASE)
Regexp.new("From:\s* #{Regexp.escape(@account.support_email)}", Regexp::IGNORECASE),
Regexp.new("<#{Regexp.escape(@account.support_email)}>", Regexp::IGNORECASE),
Regexp.new("#{Regexp.escape(@account.support_email)}\s+wrote:", Regexp::IGNORECASE),
Regexp.new("On(.*)#{Regexp.escape(@account.support_email)}(.*)wrote:", Regexp::IGNORECASE)
] + sender_agnostic_regexes
end
@@ -109,12 +109,4 @@ class MailPresenter < SimpleDelegator
Regexp.new("from:\s*$", Regexp::IGNORECASE)
]
end
def account_support_email
@account_support_email ||= begin
@account.support_email ||
GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] ||
ENV.fetch('MAILER_SENDER_EMAIL', nil)
end
end
end

View File

@@ -17,7 +17,7 @@ json.welcome_tagline resource.channel.try(:welcome_tagline)
json.enable_auto_assignment resource.enable_auto_assignment
json.web_widget_script resource.channel.try(:web_widget_script)
json.website_token resource.channel.try(:website_token)
json.forward_to_address resource.channel.try(:forward_to_address)
json.forward_to_email resource.channel.try(:forward_to_email)
json.phone_number resource.channel.try(:phone_number)
json.selected_feature_flags resource.channel.try(:selected_feature_flags)
json.reply_time resource.channel.try(:reply_time)