nmhive.py: Add GET/POST /mid/<message_id>
authorW. Trevor King <wking@tremily.us>
Sat, 20 Sep 2014 20:17:50 +0000 (13:17 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Sep 2014 20:43:55 +0000 (13:43 -0700)
So the bookmarklet can get and set tags on a given message.  This
currently just uses an in-memory store, but eventually we'll drop
notmuch in on the backend.

nmhive.py

index 6f1208484fee6530c45f3ad8ae206a7ae133e6f8..08c61fcf79df536ed8c5adde21c8411ffed45117 100755 (executable)
--- a/nmhive.py
+++ b/nmhive.py
@@ -13,6 +13,38 @@ app = flask.Flask(__name__)
 flask_cors.CORS(app)
 
 
+_TAGS = {}
+
+
+@app.route('/mid/<message_id>', 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:
+                    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')
+    elif flask.request.method == 'GET':
+        try:
+            tags = _TAGS[message_id]
+        except KeyError:
+            return flask.Response(status=404)
+        return flask.Response(
+            response=json.dumps(sorted(tags)),
+            mimetype='application/json')
+
+
 @app.route('/gmane/<group>/<int:article>', methods=['GET'])
 def gmane_message_id(group, article):
     url = 'http://download.gmane.org/{}/{}/{}'.format(