From fe9c70c04f77ee9f62a11507740e2eb65252bb43 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 21 Sep 2014 14:00:52 -0700 Subject: [PATCH] nmhive.py: Use notmuch behind GET/POST /mid/ With more per-request connections. I don't currently check for read/write lock contention, but ideally we'd return '202 Accepted' in those cases and queue the changes for the next successful read/write connection. --- nmhive.py | 57 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/nmhive.py b/nmhive.py index 841bccf..6f9f346 100755 --- a/nmhive.py +++ b/nmhive.py @@ -34,33 +34,48 @@ def tags(): mimetype='application/json') +def _message_tags(message): + return sorted( + tag[len(TAG_PREFIX):] for tag in message.get_tags() + if tag.startswith(TAG_PREFIX)) + + @app.route('/mid/', methods=['GET', 'POST']) def message_id_tags(message_id): if flask.request.method == 'POST': - tags = _TAGS.get(message_id, set()) - new_tags = tags.copy() - for change in flask.request.get_json(): - if change.startswith('+'): - new_tags.add(change[1:]) - elif change.startswith('-'): - try: - new_tags.remove(change[1:]) - except KeyError: + database = notmuch.Database( + path=NOTMUCH_PATH, + mode=notmuch.Database.MODE.READ_WRITE) + try: + message = database.find_message(message_id) + if not(message): + return flask.Response(status=404) + database.begin_atomic() + message.freeze() + for change in flask.request.get_json(): + if change.startswith('+'): + message.add_tag(TAG_PREFIX + change[1:]) + elif change.startswith('-'): + message.remove_tag(TAG_PREFIX + change[1:]) + else: return flask.Response(status=400) - else: - return flask.Response(status=400) - _TAGS[message_id] = new_tags - return flask.Response( - response=json.dumps(sorted(new_tags)), - mimetype='application/json') + message.thaw() + database.end_atomic() + tags = _message_tags(message=message) + finally: + database.close() elif flask.request.method == 'GET': + database = notmuch.Database(path=NOTMUCH_PATH) try: - tags = _TAGS[message_id] - except KeyError: - return flask.Response(status=404) - return flask.Response( - response=json.dumps(sorted(tags)), - mimetype='application/json') + message = database.find_message(message_id) + if not(message): + return flask.Response(status=404) + tags = _message_tags(message=message) + finally: + database.close() + return flask.Response( + response=json.dumps(tags), + mimetype='application/json') @app.route('/gmane//', methods=['GET']) -- 2.26.2