From 5546c658163b7ef64045171c1884ec4cb0c61f06 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 7 Feb 2021 11:24:21 +0100 Subject: commands/thread: refactor applying the tag command Do all the changes in one batch rather than separately. --- alot/commands/thread.py | 55 ++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 98355f4c..0ff38b55 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -1068,6 +1068,8 @@ class TagCommand(Command): """manipulate message tags""" repeatable = True + _tags = None + def __init__(self, tags='', action='add', all=False, **kwargs): """ :param tags: comma separated list of tagstrings to set @@ -1079,37 +1081,48 @@ class TagCommand(Command): :param all: tag all messages in thread :type all: bool """ - self.tagsstring = tags + self._tags = frozenset(filter(None, tags.split(','))) self.all = all self.action = action super().__init__(**kwargs) async def apply(self, ui): if self.all: - messages = ui.current_buffer.thread.messages.values() + query = 'thread:' + ui.current_buffer.thread.id else: - messages = [ui.current_buffer.get_selected_message()] + message = ui.current_buffer.get_selected_message() + query = 'id:' + message.id + + if self.action == 'add': + task = ui.dbman.tags_add(query, self._tags) + elif self.action == 'set': + task = ui.dbman.tags_set(query, self._tags) + elif self.action == 'remove': + task = ui.dbman.tags_remove(query, self._tags) + elif self.action == 'toggle': + write = ui.dbman.db_write_create() + + if self.all: + messages = ui.current_buffer.thread.messages.values() + else: + messages = [ui.current_buffer.get_selected_message()] - tags = frozenset(filter(None, self.tagsstring.split(','))) - try: for m in messages: - if self.action == 'add': - await m.tags_add(tags) - if self.action == 'set': - await m.tags_set(tags) - elif self.action == 'remove': - await m.tags_remove(tags) - elif self.action == 'toggle': - to_remove = set() - to_add = set() - for t in tags: - if t in m.get_tags(): - to_remove.add(t) - else: - to_add.add(t) - await m.tags_remove(to_remove) - await m.tags_add(to_add) + to_remove = set() + to_add = set() + for t in self._tags: + if t in m.get_tags(): + to_remove.add(t) + else: + to_add.add(t) + write.queue_tag_remove(to_remove, 'id:' + m.id) + write.queue_tag_add(to_add, 'id:' + m.id) + + task = write.apply() + + try: + await task except DatabaseROError: ui.notify('index in read-only mode', priority='error') return -- cgit v1.2.3