2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
module Import
|
|
|
|
class Exchange
|
|
|
|
class Folder
|
|
|
|
include ::Mixin::RailsLogger
|
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
DEFAULT_ROOTS = %i[root msgfolderroot publicfoldersroot].freeze
|
|
|
|
|
2017-08-14 11:56:23 +00:00
|
|
|
def initialize(connection)
|
|
|
|
@connection = connection
|
|
|
|
@lookup_map = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_folder_map
|
2020-04-14 08:14:14 +00:00
|
|
|
@id_folder_map ||= all.index_by(&:id)
|
2017-08-14 11:56:23 +00:00
|
|
|
|
|
|
|
# duplicate object to avoid errors where keys get
|
|
|
|
# added via #get_folder while iterating over
|
|
|
|
# the result of this method
|
|
|
|
@lookup_map = @id_folder_map.dup
|
|
|
|
@id_folder_map
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(id)
|
2018-09-04 09:49:44 +00:00
|
|
|
@lookup_map[id] ||= @connection.get_folder(id)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
2018-07-10 05:32:24 +00:00
|
|
|
@all ||= children(*DEFAULT_ROOTS)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
def children(*parents)
|
|
|
|
return [] if parents.empty?
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2020-11-24 16:20:57 +00:00
|
|
|
direct_descendants = parents.map { |parent| request_children(parent) }
|
2018-07-10 05:32:24 +00:00
|
|
|
.flatten.uniq.compact
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
direct_descendants | children(*direct_descendants)
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_path(folder)
|
2018-09-04 09:49:44 +00:00
|
|
|
display_name = folder.display_name.utf8_encode(fallback: :read_as_sanitized_binary)
|
|
|
|
parent_folder = id_folder_map[folder.parent_folder_id]
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2018-08-13 07:10:24 +00:00
|
|
|
return display_name if parent_folder.blank?
|
2017-08-14 11:56:23 +00:00
|
|
|
|
2018-08-13 07:10:24 +00:00
|
|
|
"#{display_path(parent_folder)} -> #{display_name}"
|
2017-08-14 11:56:23 +00:00
|
|
|
rescue Viewpoint::EWS::EwsError
|
2018-08-13 07:10:24 +00:00
|
|
|
display_name
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-07-10 05:32:24 +00:00
|
|
|
def request_children(parent)
|
|
|
|
parent = parent.id if parent.respond_to?(:id) # type coercion
|
|
|
|
@connection.folders(root: parent)
|
2017-08-14 11:56:23 +00:00
|
|
|
rescue Viewpoint::EWS::EwsFolderNotFound => e
|
2021-03-01 14:23:03 +00:00
|
|
|
logger.warn("Try to get children folders of: #{parent.inspect}")
|
|
|
|
logger.warn(e)
|
|
|
|
nil
|
2017-08-14 11:56:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|