Class: Imap::Sync
- Inherits:
-
Object
- Object
- Imap::Sync
- Defined in:
- lib/imap/sync.rb
Instance Method Summary collapse
- #can_idle? ⇒ Boolean
- #connect! ⇒ Object
- #disconnect! ⇒ Object
- #disconnected? ⇒ Boolean
-
#initialize(group, opts = {}) ⇒ Sync
constructor
A new instance of Sync.
- #process(idle: false, import_limit: nil, old_emails_limit: nil, new_emails_limit: nil) ⇒ Object
- #update_topic(email, incoming_email, opts = {}) ⇒ Object
Constructor Details
#initialize(group, opts = {}) ⇒ Sync
Returns a new instance of Sync.
7 8 9 10 11 |
# File 'lib/imap/sync.rb', line 7 def initialize(group, opts = {}) @group = group @provider = Imap::Providers::Detector.init_with_detected_provider(@group.imap_config) connect! end |
Instance Method Details
#can_idle? ⇒ Boolean
25 26 27 |
# File 'lib/imap/sync.rb', line 25 def can_idle? SiteSetting.enable_imap_idle && @provider.can?("IDLE") end |
#connect! ⇒ Object
13 14 15 |
# File 'lib/imap/sync.rb', line 13 def connect! @provider.connect! end |
#disconnect! ⇒ Object
17 18 19 |
# File 'lib/imap/sync.rb', line 17 def disconnect! @provider.disconnect! end |
#disconnected? ⇒ Boolean
21 22 23 |
# File 'lib/imap/sync.rb', line 21 def disconnected? @provider.disconnected? end |
#process(idle: false, import_limit: nil, old_emails_limit: nil, new_emails_limit: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/imap/sync.rb', line 29 def process(idle: false, import_limit: nil, old_emails_limit: nil, new_emails_limit: nil) raise "disconnected" if disconnected? import_limit ||= SiteSetting.imap_batch_import_email old_emails_limit ||= SiteSetting.imap_polling_old_emails new_emails_limit ||= SiteSetting.imap_polling_new_emails # IMAP server -> Discourse (download): discovers updates to old emails # (synced emails) and fetches new emails. # TODO: Use `Net::IMAP.encode_utf7(@group.imap_mailbox_name)`? @status = @provider.open_mailbox(@group.imap_mailbox_name) if @status[:uid_validity] != @group.imap_uid_validity # If UID validity changes, the whole mailbox must be synchronized (all # emails are considered new and will be associated to existent topics # in Email::Receiver by matching Message-Ids). ImapSyncLog.warn( "UIDVALIDITY = #{@status[:uid_validity]} does not match expected #{@group.imap_uid_validity}, invalidating IMAP cache and resyncing emails for mailbox #{@group.imap_mailbox_name}", @group, ) @group.imap_last_uid = 0 end if idle && !can_idle? ImapSyncLog.warn( "IMAP server for group cannot IDLE or imap idle site setting is disabled", @group, ) idle = false end if idle raise "IMAP IDLE is disabled" if !SiteSetting.enable_imap_idle # Thread goes into sleep and it is better to return any connection # back to the pool. 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, db: false, ) @provider .imap .idle(idle_polling_mins) do |resp| if resp.kind_of?(Net::IMAP::UntaggedResponse) && resp.name == "EXISTS" @provider.imap.idle_done end end end # Fetching UIDs of old (already imported into Discourse, but might need # update) and new (not downloaded yet) emails. if @group.imap_last_uid == 0 old_uids = [] new_uids = @provider.uids else old_uids = @provider.uids(to: @group.imap_last_uid) # 1 .. seen new_uids = @provider.uids(from: @group.imap_last_uid + 1) # seen+1 .. inf end # Sometimes, new_uids contains elements from old_uids. new_uids = new_uids - old_uids ImapSyncLog.debug( "Remote email server has #{old_uids.size} old emails and #{new_uids.size} new emails", @group, ) all_old_uids_size = old_uids.size all_new_uids_size = new_uids.size @group.update_columns( imap_last_error: nil, imap_old_emails: all_old_uids_size, imap_new_emails: all_new_uids_size, ) import_mode = import_limit > -1 && new_uids.size > import_limit old_uids = old_uids.sample(old_emails_limit).sort! if old_emails_limit > -1 new_uids = new_uids[0..new_emails_limit - 1] if new_emails_limit > 0 # if there are no old_uids that is OK, this could indicate that some # UIDs have been sent to the trash process_old_uids(old_uids) if new_uids.present? process_new_uids(new_uids, import_mode, all_old_uids_size, all_new_uids_size) end # Discourse -> IMAP server (upload): syncs updated flags and labels. sync_to_server { remaining: all_new_uids_size - new_uids.size } end |
#update_topic(email, incoming_email, opts = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/imap/sync.rb', line 129 def update_topic(email, incoming_email, opts = {}) if !incoming_email || incoming_email.imap_sync || !incoming_email.topic || incoming_email.post&.post_number != 1 return end update_topic_archived_state(email, incoming_email, opts) (email, incoming_email, opts) end |