summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-02-07 11:24:21 +0100
committerAnton Khirnov <anton@khirnov.net>2021-02-07 11:24:21 +0100
commit5546c658163b7ef64045171c1884ec4cb0c61f06 (patch)
tree2250968f560e20b1b15a8bc6aafaff277527cd48
parent0da20384169847aebe2ca4aa70557bf9cb726c48 (diff)
commands/thread: refactor applying the tag command
Do all the changes in one batch rather than separately.
-rw-r--r--alot/commands/thread.py55
1 files 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