Class: Imap::Backup::Serializer::Appender

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/serializer/appender.rb

Overview

Appends messages to the local store

Instance Method Summary collapse

Constructor Details

#initialize(folder:, imap:, mbox:) ⇒ Appender

Returns a new instance of Appender.



10
11
12
13
14
# File 'lib/imap/backup/serializer/appender.rb', line 10

def initialize(folder:, imap:, mbox:)
  @folder = folder
  @imap = imap
  @mbox = mbox
end

Instance Method Details

#append(uid:, message:, flags:) ⇒ Object

Adds a message to the metadata file and the mailbox. Wraps any errors with information about the message that caused them.



18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/imap/backup/serializer/appender.rb', line 18

def append(uid:, message:, flags:)
  raise "Can't add messages without uid_validity" if !imap.uid_validity

  uid = uid.to_i
  existing = imap.get(uid)
  if existing
    Logger.logger.debug(
      "[#{folder}] message #{uid} already downloaded - skipping"
    )
    return
  end

  begin
    serialized = to_serialized(message)
  rescue StandardError => e
    raise wrap_error(
      error: e,
      note: "failed to serialize message",
      folder: folder,
      uid: uid,
      message: message
    )
  end

  rollback_on_error do
    mbox.append serialized
    imap.append uid, serialized.bytesize, flags: flags
  rescue StandardError => e
    raise wrap_error(
      error: e,
      note: "failed to append message",
      folder: folder,
      uid: uid,
      message: message
    )
  end
end