diff --git a/app/jobs/fs/download_sent_message_job.rb b/app/jobs/fs/download_sent_message_job.rb index 0d12647c..2f5d3356 100644 --- a/app/jobs/fs/download_sent_message_job.rb +++ b/app/jobs/fs/download_sent_message_job.rb @@ -4,7 +4,7 @@ def perform(fs_message_id, box:, fs_client: FsEnvironment.fs_client) raise unless box.is_a?(Fs::Box) return unless box.syncable? - return if box.messages.where(type: [nil, 'Message']).where("metadata ->> 'fs_message_id' = ?", fs_message_id).any? + return if box.messages.except_drafts.where("metadata ->> 'fs_message_id' = ?", fs_message_id).any? ActiveRecord::Base.transaction do fs_api = fs_client.api(api_connection: box.api_connection, box: box) diff --git a/app/jobs/fs/sync_box_job.rb b/app/jobs/fs/sync_box_job.rb index 4ff4065e..9cc359c8 100644 --- a/app/jobs/fs/sync_box_job.rb +++ b/app/jobs/fs/sync_box_job.rb @@ -4,7 +4,7 @@ def perform(box, from: Date.today - 1.week, to: Date.tomorrow) raise unless box.is_a?(Fs::Box) return unless box.syncable? - box.messages.outbox.where(type: [nil, 'Message']).find_each do |outbox_message| + box.messages.outbox.except_drafts.find_each do |outbox_message| DownloadSentMessageRelatedMessagesJob.perform_later(outbox_message, from: from, to: to) end end diff --git a/app/jobs/govbox/process_message_job.rb b/app/jobs/govbox/process_message_job.rb index 65ac7370..d323077c 100644 --- a/app/jobs/govbox/process_message_job.rb +++ b/app/jobs/govbox/process_message_job.rb @@ -7,7 +7,7 @@ class ProcessMessageJob < ApplicationJob retry_on ::ApplicationRecord::FailedToAcquireLockError, wait: :polynomially_longer, attempts: Float::INFINITY def perform(govbox_message) - processed_message = ::Message.where(type: [nil, 'Message']).where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take + processed_message = ::Message.except_drafts.where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take ActiveRecord::Base.transaction do message = Govbox::Message.create_message_with_thread!(govbox_message) diff --git a/app/models/message.rb b/app/models/message.rb index e2e1eaa1..598650a9 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -40,8 +40,9 @@ class Message < ApplicationRecord delegate :tenant, to: :thread delegate :box, to: :thread + scope :except_drafts, -> { where(type: [nil, 'Message']) } scope :outbox, -> { where(outbox: true) } - scope :inbox, -> { where.not(outbox: true).where(type: [nil, 'Message']) } + scope :inbox, -> { except_drafts.where.not(outbox: true) } after_update_commit ->(message) { EventBus.publish(:message_changed, message) } after_destroy_commit ->(message) { EventBus.publish(:message_destroyed, message) }