2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-18 18:57:55 -05:00
|
|
|
class IncomingEmail < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :post
|
2023-01-09 07:20:10 -05:00
|
|
|
belongs_to :group, foreign_key: :imap_group_id, class_name: "Group"
|
2016-01-18 18:57:55 -05:00
|
|
|
|
2021-01-20 21:59:50 -05:00
|
|
|
validates :created_via, presence: true
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
scope :errored, -> { where("NOT is_bounce AND error IS NOT NULL") }
|
2017-11-13 09:20:36 -05:00
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
scope :addressed_to, ->(email) { where(<<~SQL, email: "%#{email}%") }
|
2020-07-10 05:05:55 -04:00
|
|
|
incoming_emails.from_address = :email OR
|
2018-03-30 08:37:19 -04:00
|
|
|
incoming_emails.to_addresses ILIKE :email OR
|
|
|
|
incoming_emails.cc_addresses ILIKE :email
|
|
|
|
SQL
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
scope :addressed_to_user, ->(user) { where(<<~SQL, user_id: user.id) }
|
2018-03-30 08:37:19 -04:00
|
|
|
EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM user_emails
|
|
|
|
WHERE user_emails.user_id = :user_id AND
|
2020-07-10 05:05:55 -04:00
|
|
|
(incoming_emails.from_address = user_emails.email OR
|
|
|
|
incoming_emails.to_addresses ILIKE '%' || user_emails.email || '%' OR
|
2018-03-30 08:37:19 -04:00
|
|
|
incoming_emails.cc_addresses ILIKE '%' || user_emails.email || '%')
|
|
|
|
)
|
|
|
|
SQL
|
2021-01-05 00:32:04 -05:00
|
|
|
|
2021-01-20 20:37:47 -05:00
|
|
|
scope :without_raw, -> { select(self.column_names - ["raw"]) }
|
|
|
|
|
2021-01-19 22:22:41 -05:00
|
|
|
def self.created_via_types
|
2023-01-09 07:20:10 -05:00
|
|
|
@types ||= Enum.new(unknown: 0, handle_mail: 1, pop3_poll: 2, imap: 3, group_smtp: 4)
|
2021-01-19 22:22:41 -05:00
|
|
|
end
|
|
|
|
|
2021-01-20 20:37:47 -05:00
|
|
|
def as_mail_message
|
|
|
|
@mail_message ||= Mail.new(self.raw)
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_headers
|
|
|
|
as_mail_message.header.raw_source
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_body
|
|
|
|
as_mail_message.body
|
|
|
|
end
|
|
|
|
|
2021-01-14 19:54:46 -05:00
|
|
|
def to_addresses_split
|
|
|
|
self.to_addresses&.split(";") || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def cc_addresses_split
|
|
|
|
self.cc_addresses&.split(";") || []
|
|
|
|
end
|
|
|
|
|
2021-01-05 00:32:04 -05:00
|
|
|
def to_addresses=(to)
|
2023-01-09 07:20:10 -05:00
|
|
|
to = to.map(&:downcase).join(";") if to&.is_a?(Array)
|
2021-01-05 00:32:04 -05:00
|
|
|
super(to)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cc_addresses=(cc)
|
2023-01-09 07:20:10 -05:00
|
|
|
cc = cc.map(&:downcase).join(";") if cc&.is_a?(Array)
|
2021-01-05 00:32:04 -05:00
|
|
|
super(cc)
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_address=(from)
|
2023-01-09 07:20:10 -05:00
|
|
|
from = from.first if from&.is_a?(Array)
|
2021-01-05 00:32:04 -05:00
|
|
|
super(from)
|
|
|
|
end
|
2016-01-18 18:57:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: incoming_emails
|
|
|
|
#
|
2016-03-28 00:21:45 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer
|
|
|
|
# topic_id :integer
|
|
|
|
# post_id :integer
|
|
|
|
# raw :text
|
|
|
|
# error :text
|
|
|
|
# message_id :text
|
|
|
|
# from_address :text
|
|
|
|
# to_addresses :text
|
|
|
|
# cc_addresses :text
|
|
|
|
# subject :text
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# rejection_message :text
|
2016-05-29 20:45:32 -04:00
|
|
|
# is_auto_generated :boolean default(FALSE)
|
|
|
|
# is_bounce :boolean default(FALSE), not null
|
2020-07-10 05:05:55 -04:00
|
|
|
# imap_uid_validity :integer
|
|
|
|
# imap_uid :integer
|
|
|
|
# imap_sync :boolean
|
2020-08-02 23:10:17 -04:00
|
|
|
# imap_group_id :bigint
|
2021-07-05 18:14:15 -04:00
|
|
|
# imap_missing :boolean default(FALSE), not null
|
2021-01-20 21:59:50 -05:00
|
|
|
# created_via :integer default(0), not null
|
2016-01-18 18:57:55 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2020-08-02 23:10:17 -04:00
|
|
|
# index_incoming_emails_on_created_at (created_at)
|
|
|
|
# index_incoming_emails_on_error (error)
|
|
|
|
# index_incoming_emails_on_imap_group_id (imap_group_id)
|
|
|
|
# index_incoming_emails_on_imap_sync (imap_sync)
|
|
|
|
# index_incoming_emails_on_message_id (message_id)
|
|
|
|
# index_incoming_emails_on_post_id (post_id)
|
|
|
|
# index_incoming_emails_on_user_id (user_id) WHERE (user_id IS NOT NULL)
|
2016-01-18 18:57:55 -05:00
|
|
|
#
|