diff --git a/app/models/imap_sync_log.rb b/app/models/imap_sync_log.rb index 4d3809bebe9..e1073dcd16a 100644 --- a/app/models/imap_sync_log.rb +++ b/app/models/imap_sync_log.rb @@ -9,20 +9,25 @@ class ImapSyncLog < ActiveRecord::Base @levels ||= Enum.new(:debug, :info, :warn, :error) end - def self.log(message, level, group_id = nil) + def self.log(message, level, group_id = nil, db = true) now = Time.now.strftime("%Y-%m-%d %H:%M:%S.%L") - new_log = create(message: message, level: ImapSyncLog.levels[level], group_id: group_id) + + new_log = if db + create(message: message, level: ImapSyncLog.levels[level], group_id: group_id) + end + if ENV["DEBUG_IMAP"] Rails.logger.send(:warn, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}") else Rails.logger.send(level, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}") end + new_log end - def self.debug(message, group_or_id) + def self.debug(message, group_or_id, db: true) group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id - log(message, :debug, group_id) + log(message, :debug, group_id, db) end def self.info(message, group_or_id) diff --git a/app/models/incoming_email.rb b/app/models/incoming_email.rb index cd902a6d542..66d7053fddb 100644 --- a/app/models/incoming_email.rb +++ b/app/models/incoming_email.rb @@ -29,6 +29,8 @@ class IncomingEmail < ActiveRecord::Base SQL end + scope :without_raw, -> { select(self.column_names - ["raw"]) } + def self.created_via_types @types ||= Enum.new( handle_mail: 1, @@ -38,6 +40,18 @@ class IncomingEmail < ActiveRecord::Base ) end + 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 + def to_addresses_split self.to_addresses&.split(";") || [] end diff --git a/app/models/post.rb b/app/models/post.rb index afaf6e11dae..1ce6628a419 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -166,6 +166,10 @@ class Post < ActiveRecord::Base includes(:post_details).find_by(post_details: { key: key, value: value }) end + def self.find_by_number(topic_id, post_number) + find_by(topic_id: topic_id, post_number: post_number) + end + def whisper? post_type == Post.types[:whisper] end diff --git a/lib/demon/email_sync.rb b/lib/demon/email_sync.rb index 8777b3e5e15..12fedc5724d 100644 --- a/lib/demon/email_sync.rb +++ b/lib/demon/email_sync.rb @@ -23,7 +23,7 @@ class Demon::EmailSync < ::Demon::Base def start_thread(db, group) Thread.new do RailsMultisite::ConnectionManagement.with_connection(db) do - ImapSyncLog.debug("Thread started for group #{group.name} in db #{db}", group) + ImapSyncLog.debug("Thread started for group #{group.name} in db #{db}", group, db: false) begin syncer = Imap::Sync.new(group) rescue Net::IMAP::NoResponseError => e @@ -44,7 +44,7 @@ class Demon::EmailSync < ::Demon::Base ) if !syncer.can_idle? && status[:remaining] == 0 - ImapSyncLog.debug("Going to sleep for group #{group.name} in db #{db} to wait for new emails", group) + ImapSyncLog.debug("Going to sleep for group #{group.name} in db #{db} to wait for new emails", group, db: false) # Thread goes into sleep for a bit so it is better to return any # connection back to the pool. @@ -133,7 +133,7 @@ class Demon::EmailSync < ::Demon::Base # Spawn new threads for groups that are now synchronized. groups.each do |group_id, group| if !@sync_data[db][group_id] - ImapSyncLog.debug("Starting thread for group #{group.name} mailbox #{group.imap_mailbox_name}", group) + ImapSyncLog.debug("Starting thread for group #{group.name} mailbox #{group.imap_mailbox_name}", group, db: false) @sync_data[db][group_id] = { thread: start_thread(db, group), diff --git a/lib/imap/sync.rb b/lib/imap/sync.rb index 7877edbd937..96e85ba746d 100644 --- a/lib/imap/sync.rb +++ b/lib/imap/sync.rb @@ -60,7 +60,7 @@ module Imap ActiveRecord::Base.connection_handler.clear_active_connections! idle_polling_mins = SiteSetting.imap_polling_period_mins.minutes.to_i - ImapSyncLog.debug("Going IDLE for #{idle_polling_mins} seconds to wait for more work", @group) + ImapSyncLog.debug("Going IDLE for #{idle_polling_mins} seconds to wait for more work", @group, db: false) @provider.imap.idle(idle_polling_mins) do |resp| if resp.kind_of?(Net::IMAP::UntaggedResponse) && resp.name == 'EXISTS' diff --git a/spec/jobs/cleanup_imap_sync_log_spec.rb b/spec/jobs/cleanup_imap_sync_log_spec.rb index 8b52141ac27..3975c76966e 100644 --- a/spec/jobs/cleanup_imap_sync_log_spec.rb +++ b/spec/jobs/cleanup_imap_sync_log_spec.rb @@ -17,4 +17,9 @@ describe Jobs::CleanupImapSyncLog do expect(ImapSyncLog.count).to eq(1) end + + it "does not write the log to the db if specified" do + ImapSyncLog.debug("test", Fabricate(:group), db: false) + expect(ImapSyncLog.count).to eq(0) + end end