Skip to content

Commit

Permalink
Merge branch 'feature/refactor-core-classes'
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyates committed Jul 12, 2023
2 parents 8843852 + eebc394 commit 2b8edda
Show file tree
Hide file tree
Showing 70 changed files with 1,116 additions and 1,002 deletions.
16 changes: 8 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-07-08 06:54:16 UTC using RuboCop version 1.51.0.
# on 2023-07-12 16:10:36 UTC using RuboCop version 1.51.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 26
# Offense count: 25
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 63

# Offense count: 2
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 32
Max: 40

# Offense count: 9
# Offense count: 8
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 180
Max: 182

# Offense count: 5
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 49
# Offense count: 53
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 38
Max: 42

# Offense count: 4
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
2 changes: 0 additions & 2 deletions lib/imap/backup.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Imap; end

require "imap/backup/utils"
require "imap/backup/account/connection"
require "imap/backup/account/folder"
require "imap/backup/configuration"
require "imap/backup/downloader"
Expand Down
18 changes: 16 additions & 2 deletions lib/imap/backup/account.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require "imap/backup/account/client_factory"
require "imap/backup/account/restore"

module Imap; end

module Imap::Backup
Expand Down Expand Up @@ -26,13 +29,23 @@ def initialize(options)
@connection_options = options[:connection_options]
@multi_fetch_size = options[:multi_fetch_size]
@reset_seen_flags_after_fetch = options[:reset_seen_flags_after_fetch]
@client = nil
@connection = nil
@changes = {}
@marked_for_deletion = false
end

def connection
@connection ||= Account::Connection.new(self)
def client
@client ||= Account::ClientFactory.new(account: self).run
end

def namespaces
client.namespace
end

def restore
restore = Account::Restore.new(account: self)
restore.run
end

def valid?
Expand Down Expand Up @@ -145,6 +158,7 @@ def update(field, value)
changes[field] = {from: current, to: value} if value != current
end

@client = nil
@connection = nil
instance_variable_set(key, value)
end
Expand Down
65 changes: 65 additions & 0 deletions lib/imap/backup/account/backup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "imap/backup/account/folder_ensurer"
require "imap/backup/account/local_only_folder_deleter"
require "imap/backup/account/serialized_folders"
require "imap/backup/downloader"
require "imap/backup/flag_refresher"
require "imap/backup/local_only_message_deleter"

module Imap::Backup
class Account; end

class Account::Backup
attr_reader :account
attr_reader :refresh

def initialize(account:, refresh: false)
@account = account
@refresh = refresh
end

def run
Logger.logger.info "Running backup of account: #{account.username}"
# start the connection so we get logging messages in the right order
account.client

Account::FolderEnsurer.new(account: account).run
Account::LocalOnlyFolderDeleter.new(account: account).run if account.mirror_mode
each_folder do |folder, serializer|
begin
next if !folder.exist?
rescue Encoding::UndefinedConversionError
message = "Skipping backup for '#{folder.name}' " \
"as it is not UTF-7 encoded correctly"
Logger.logger.info message
next
end

Logger.logger.debug "[#{folder.name}] running backup"
serializer.apply_uid_validity(folder.uid_validity)
Downloader.new(
folder,
serializer,
multi_fetch_size: account.multi_fetch_size,
reset_seen_flags_after_fetch: account.reset_seen_flags_after_fetch
).run
if account.mirror_mode
Logger.logger.info "Mirror mode - Deleting messages only present locally"
LocalOnlyMessageDeleter.new(folder, serializer).run
end
FlagRefresher.new(folder, serializer).run if account.mirror_mode || refresh
end
end

private

def each_folder
backup_folders = Account::BackupFolders.new(
client: account.client, account: account
)
backup_folders.each do |folder|
serializer = Serializer.new(account.local_path, folder.name)
yield folder, serializer
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module Imap::Backup
class Account; end
class Account::Connection; end

class Account::Connection::BackupFolders
class Account::BackupFolders
attr_reader :account
attr_reader :client

Expand All @@ -11,8 +10,10 @@ def initialize(client:, account:)
@account = account
end

def run
all_names = Account::Connection::FolderNames.new(client: client, account: account).run
def each(&block)
return enum_for(:each) if !block

all_names = client.list

configured =
case
Expand All @@ -31,7 +32,13 @@ def run
all_names & configured
end

names.map { |name| Account::Folder.new(account.connection, name) }
names.each { |name| block.call(Account::Folder.new(client, name)) }
end

def map(&block)
each.map do |folder|
block.call(folder)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
require "socket"

require "email/provider"
require "imap/backup/client/apple_mail"
require "imap/backup/client/default"
require "retry_on_error"

module Imap::Backup
class Account::Connection::ClientFactory
class Account; end

class Account::ClientFactory
include RetryOnError

LOGIN_RETRY_CLASSES = [EOFError, Errno::ECONNRESET, SocketError].freeze
LOGIN_RETRY_CLASSES = [::EOFError, ::Errno::ECONNRESET, ::SocketError].freeze

attr_reader :account

Expand All @@ -23,23 +29,17 @@ def run
)
client =
if provider.is_a?(Email::Provider::AppleMail)
Client::AppleMail.new(server, options)
Client::AppleMail.new(server, account, options)
else
Client::Default.new(server, options)
Client::Default.new(server, account, options)
end
Logger.logger.debug "Logging in: #{account.username}/#{masked_password}"
client.login(account.username, account.password)
Logger.logger.debug "Login complete"
client.login
client
end
end

private

def masked_password
account.password.gsub(/./, "x")
end

def provider
@provider ||= Email::Provider.for_address(account.username)
end
Expand Down
136 changes: 0 additions & 136 deletions lib/imap/backup/account/connection.rb

This file was deleted.

Loading

0 comments on commit 2b8edda

Please sign in to comment.