summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-02-07 10:01:43 +0100
committerAnton Khirnov <anton@khirnov.net>2021-02-07 10:01:43 +0100
commit0da20384169847aebe2ca4aa70557bf9cb726c48 (patch)
treefb459f493bcb1d2462bf361e14f83d570fa79373
parent2f112621568fa7f8e90e59176dea905603986609 (diff)
commands/search: refactor applying the tag command
Apply toggle as a single db write rather than two.
-rw-r--r--alot/commands/search.py39
-rw-r--r--alot/db/thread.py23
2 files changed, 22 insertions, 40 deletions
diff --git a/alot/commands/search.py b/alot/commands/search.py
index 9e4a3688..79aaee56 100644
--- a/alot/commands/search.py
+++ b/alot/commands/search.py
@@ -175,24 +175,29 @@ class TagCommand(Command):
if not self._thread:
testquery += ' and (%s)' % searchbuffer.querystring
+ if self._action == 'add':
+ task = ui.dbman.tags_add(testquery, self._tags)
+ if self._action == 'set':
+ task = ui.dbman.tags_set(testquery, self._tags)
+ elif self._action == 'remove':
+ task = ui.dbman.tags_remove(testquery, self._tags)
+ elif self._action == 'toggle':
+ if not self._all_threads:
+ to_remove = set()
+ to_add = set()
+ for t in self._tags:
+ if t in thread.get_tags():
+ to_remove.add(t)
+ else:
+ to_add.add(t)
+
+ write = ui.dbman.db_write_create()
+ write.queue_tag_remove(to_remove, 'thread:' + thread.id)
+ write.queue_tag_add(to_add, 'thread:' + thread.id)
+ task = write.apply()
+
try:
- if self._action == 'add':
- await ui.dbman.tags_add(testquery, self._tags)
- if self._action == 'set':
- await ui.dbman.tags_set(testquery, self._tags)
- elif self._action == 'remove':
- await ui.dbman.tags_remove(testquery, self._tags)
- elif self._action == 'toggle':
- if not self._all_threads:
- to_remove = set()
- to_add = set()
- for t in self._tags:
- if t in thread.get_tags():
- to_remove.add(t)
- else:
- to_add.add(t)
- await thread.tags_remove(to_remove)
- await thread.tags_add(to_add)
+ await task
except DatabaseROError:
ui.notify('index in read-only mode', priority='error')
return
diff --git a/alot/db/thread.py b/alot/db/thread.py
index 65e74a35..13ddabda 100644
--- a/alot/db/thread.py
+++ b/alot/db/thread.py
@@ -142,29 +142,6 @@ class Thread:
tags = tags.intersection(set(m.get_tags()))
return tags
- def tags_add(self, tags):
- """
- Asynchronously add `tags` to all messages in this thread
-
- :param tags: a set of tags to be added
- :type tags: set of str
- """
-
- fut = self._dbman.tags_add('thread:' + self.id, tags)
- fut.add_done_callback(lambda fut: self.refresh())
- return fut
-
- def tags_remove(self, tags):
- """
- Asynchronously remove `tags` from all messages in this thread
- :param tags: a set of tags to be added
- :type tags: set of str
- """
- fut = self._dbman.tags_remove('thread:' + self.id, tags)
- fut.add_done_callback(lambda fut: self.refresh())
- return fut
-
-
def get_authors(self):
"""
returns a list of authors (name, addr) of the messages.