Cache folder IDs recursively

This adds support for nested folders in a mailbox
This commit is contained in:
Stacy Brock
2022-05-24 13:04:05 -07:00
parent ef901f28ed
commit 56883238d0

View File

@@ -93,15 +93,26 @@ class O365MailFilter(object):
# within this class as 'self._filter_message()'
self._filter_message = module.filter_message
def _load_folders(self):
""" retrieve folders for this mailbox and cache their ids """
self._folders = {}
mailbox = self._account.mailbox()
folders = mailbox.get_folders()
def _load_folders(self, mailbox, folders=None, folder_path=None):
""" recursively cache folder IDs for this mailbox """
if folders is None:
folders = mailbox.get_folders()
self._folders = {}
folder_path = ''
for folder in folders:
self._folders[folder.name] = folder.folder_id
if folder_path == '':
current_folder_path = f"{folder.name}"
else:
current_folder_path = f"{folder_path}/{folder.name}"
if not folder.get_folders():
self._folders[current_folder_path] = folder.folder_id
else:
# add child folders to the cache, because get_folders() doesn't
# descend into sub-folders by default
self._load_folders(mailbox, folder.get_folders(),
current_folder_path)
def _clear_cache(self):
""" clear the filtered message cache """
@@ -120,12 +131,12 @@ class O365MailFilter(object):
log(f"{self._repr_message(message)} RESULT: {result}")
def filter(self):
self._load_folders()
self._load_filters()
mailbox = self._account.mailbox()
inbox = mailbox.inbox_folder()
self._load_folders(mailbox)
self._load_filters()
# set limit to max allowed by O365, which is 999 messages
# we have to explicitly set a limit value when calling get_messages() or
# the O365 library will not paginate results correctly