2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-19 07:57:55 +08:00
|
|
|
class IncomingEmail < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :post
|
|
|
|
|
2017-08-04 22:20:44 +08:00
|
|
|
scope :errored, -> { where("NOT is_bounce AND error IS NOT NULL") }
|
2017-11-13 22:20:36 +08:00
|
|
|
|
2018-03-30 20:37:19 +08:00
|
|
|
scope :addressed_to, -> (email) do
|
|
|
|
where(<<~SQL, email: "%#{email}%")
|
|
|
|
incoming_emails.to_addresses ILIKE :email OR
|
|
|
|
incoming_emails.cc_addresses ILIKE :email
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :addressed_to_user, ->(user) do
|
|
|
|
where(<<~SQL, user_id: user.id)
|
|
|
|
EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM user_emails
|
|
|
|
WHERE user_emails.user_id = :user_id AND
|
|
|
|
(incoming_emails.to_addresses ILIKE '%' || user_emails.email || '%' OR
|
|
|
|
incoming_emails.cc_addresses ILIKE '%' || user_emails.email || '%')
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
2016-01-19 07:57:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: incoming_emails
|
|
|
|
#
|
2016-03-28 12:21:45 +08: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-30 08:45:32 +08:00
|
|
|
# is_auto_generated :boolean default(FALSE)
|
|
|
|
# is_bounce :boolean default(FALSE), not null
|
2016-01-19 07:57:55 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_incoming_emails_on_created_at (created_at)
|
|
|
|
# index_incoming_emails_on_error (error)
|
|
|
|
# index_incoming_emails_on_message_id (message_id)
|
2016-05-30 08:45:32 +08:00
|
|
|
# index_incoming_emails_on_post_id (post_id)
|
2019-04-26 20:33:01 +08:00
|
|
|
# index_incoming_emails_on_user_id (user_id) WHERE (user_id IS NOT NULL)
|
2016-01-19 07:57:55 +08:00
|
|
|
#
|