From 8a76094965635eb2afd501038ea3c09f88416ce2 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 11 Apr 2016 18:00:03 +0100 Subject: [PATCH 1/7] Add get endpoint for pushers As per https://github.com/matrix-org/matrix-doc/pull/308 --- synapse/rest/client/v1/pusher.py | 35 +++++++++++++++++++++++++++++++- synapse/storage/pusher.py | 19 +++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/synapse/rest/client/v1/pusher.py b/synapse/rest/client/v1/pusher.py index 9881f068c..5c88a9ba0 100644 --- a/synapse/rest/client/v1/pusher.py +++ b/synapse/rest/client/v1/pusher.py @@ -27,14 +27,47 @@ logger = logging.getLogger(__name__) class PusherRestServlet(ClientV1RestServlet): - PATTERNS = client_path_patterns("/pushers/set$") + PATTERNS = client_path_patterns("/pushers(/set)?$") def __init__(self, hs): super(PusherRestServlet, self).__init__(hs) self.notifier = hs.get_notifier() + @defer.inlineCallbacks + def on_GET(self, request): + if request.postpath != ["pushers"]: + defer.returnValue((405, {})) + + requester = yield self.auth.get_user_by_req(request) + user = requester.user + + pushers = yield self.hs.get_datastore().get_pushers_by_app_user_id( + user.to_string() + ) + + allowed_keys = [ + "app_display_name", + "app_id", + "data", + "device_display_name", + "kind", + "lang", + "profile_tag", + "pushkey", + ] + + for p in pushers: + for k, v in p.items(): + if k not in allowed_keys: + del p[k] + + defer.returnValue((200, {"pushers": pushers})) + @defer.inlineCallbacks def on_POST(self, request): + if request.postpath != ["pushers", "set"]: + defer.returnValue((405, {})) + requester = yield self.auth.get_user_by_req(request) user = requester.user diff --git a/synapse/storage/pusher.py b/synapse/storage/pusher.py index 19888a8e7..11feab72d 100644 --- a/synapse/storage/pusher.py +++ b/synapse/storage/pusher.py @@ -75,6 +75,25 @@ class PusherStore(SQLBaseStore): defer.returnValue(rows) + @defer.inlineCallbacks + def get_pushers_by_app_user_id(self, user_id): + def r(txn): + sql = ( + "SELECT * FROM pushers" + " WHERE user_name = ?" + ) + + txn.execute(sql, (user_id,)) + rows = self.cursor_to_dict(txn) + + return self._decode_pushers_rows(rows) + + result = yield self.runInteraction( + "get_pushers_by_user_id", r + ) + + defer.returnValue(result) + @defer.inlineCallbacks def get_all_pushers(self): def get_pushers(txn): From d937f342bb223111a9cd6b2c4a8f0544e1a810a5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 12 Apr 2016 13:33:30 +0100 Subject: [PATCH 2/7] Split into separate servlet classes --- synapse/rest/client/v1/pusher.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/synapse/rest/client/v1/pusher.py b/synapse/rest/client/v1/pusher.py index 5c88a9ba0..321eadbc2 100644 --- a/synapse/rest/client/v1/pusher.py +++ b/synapse/rest/client/v1/pusher.py @@ -26,18 +26,14 @@ import logging logger = logging.getLogger(__name__) -class PusherRestServlet(ClientV1RestServlet): - PATTERNS = client_path_patterns("/pushers(/set)?$") +class PushersRestServlet(ClientV1RestServlet): + PATTERNS = client_path_patterns("/pushers$") def __init__(self, hs): - super(PusherRestServlet, self).__init__(hs) - self.notifier = hs.get_notifier() + super(PushersRestServlet, self).__init__(hs) @defer.inlineCallbacks def on_GET(self, request): - if request.postpath != ["pushers"]: - defer.returnValue((405, {})) - requester = yield self.auth.get_user_by_req(request) user = requester.user @@ -63,11 +59,19 @@ class PusherRestServlet(ClientV1RestServlet): defer.returnValue((200, {"pushers": pushers})) + def on_OPTIONS(self, _): + return 200, {} + + +class PushersSetRestServlet(ClientV1RestServlet): + PATTERNS = client_path_patterns("/pushers(/set)?$") + + def __init__(self, hs): + super(PushersSetRestServlet, self).__init__(hs) + self.notifier = hs.get_notifier() + @defer.inlineCallbacks def on_POST(self, request): - if request.postpath != ["pushers", "set"]: - defer.returnValue((405, {})) - requester = yield self.auth.get_user_by_req(request) user = requester.user @@ -133,4 +137,5 @@ class PusherRestServlet(ClientV1RestServlet): def register_servlets(hs, http_server): - PusherRestServlet(hs).register(http_server) + PushersRestServlet(hs).register(http_server) + PushersSetRestServlet(hs).register(http_server) From 7b39bcdaae70e3a167d38bb1cdf8e6d4d95f0cca Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 12 Apr 2016 13:35:08 +0100 Subject: [PATCH 3/7] Mis-named function --- synapse/rest/client/v1/pusher.py | 2 +- synapse/storage/pusher.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/rest/client/v1/pusher.py b/synapse/rest/client/v1/pusher.py index 321eadbc2..d792dac64 100644 --- a/synapse/rest/client/v1/pusher.py +++ b/synapse/rest/client/v1/pusher.py @@ -37,7 +37,7 @@ class PushersRestServlet(ClientV1RestServlet): requester = yield self.auth.get_user_by_req(request) user = requester.user - pushers = yield self.hs.get_datastore().get_pushers_by_app_user_id( + pushers = yield self.hs.get_datastore().get_pushers_by_user_id( user.to_string() ) diff --git a/synapse/storage/pusher.py b/synapse/storage/pusher.py index 11feab72d..90ec06809 100644 --- a/synapse/storage/pusher.py +++ b/synapse/storage/pusher.py @@ -76,7 +76,7 @@ class PusherStore(SQLBaseStore): defer.returnValue(rows) @defer.inlineCallbacks - def get_pushers_by_app_user_id(self, user_id): + def get_pushers_by_user_id(self, user_id): def r(txn): sql = ( "SELECT * FROM pushers" From 44891b4a0aaa28a3b202d22ef14c6c3b2bca40a5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 12 Apr 2016 13:47:17 +0100 Subject: [PATCH 4/7] Tidy up get_pusher functions Decodes pushers rows on the main thread rather than the db thread and uses _simple_select_list. Also do the same to the function I copied and factor out the duplication into a helper function. --- synapse/storage/pusher.py | 60 +++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/synapse/storage/pusher.py b/synapse/storage/pusher.py index 90ec06809..774eda0f3 100644 --- a/synapse/storage/pusher.py +++ b/synapse/storage/pusher.py @@ -56,43 +56,41 @@ class PusherStore(SQLBaseStore): ) defer.returnValue(ret is not None) - @defer.inlineCallbacks def get_pushers_by_app_id_and_pushkey(self, app_id, pushkey): - def r(txn): - sql = ( - "SELECT * FROM pushers" - " WHERE app_id = ? AND pushkey = ?" - ) + return self.get_pushers_by({ + "app_id": app_id, + "pushkey": pushkey, + }) - txn.execute(sql, (app_id, pushkey,)) - rows = self.cursor_to_dict(txn) - - return self._decode_pushers_rows(rows) - - rows = yield self.runInteraction( - "get_pushers_by_app_id_and_pushkey", r - ) - - defer.returnValue(rows) + def get_pushers_by_user_id(self, user_id): + return self.get_pushers_by({ + "user_name": user_id, + }) @defer.inlineCallbacks - def get_pushers_by_user_id(self, user_id): - def r(txn): - sql = ( - "SELECT * FROM pushers" - " WHERE user_name = ?" - ) + def get_pushers_by(self, keyvalues): + ret = yield self._simple_select_list( + "pushers", keyvalues, + [ + "id", + "user_name", + "access_token", + "profile_tag", + "kind", + "app_id", + "app_display_name", + "device_display_name", + "pushkey", + "ts", + "lang", + "data", + "last_stream_ordering", + "last_success", + "failing_since", - txn.execute(sql, (user_id,)) - rows = self.cursor_to_dict(txn) - - return self._decode_pushers_rows(rows) - - result = yield self.runInteraction( - "get_pushers_by_user_id", r + ] ) - - defer.returnValue(result) + defer.returnValue(self._decode_pushers_rows(ret)) @defer.inlineCallbacks def get_all_pushers(self): From a04c076b7f72bc4a3ba5605abf6b10ac682826be Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 12 Apr 2016 13:54:41 +0100 Subject: [PATCH 5/7] Make the /set part mandatory --- synapse/rest/client/v1/pusher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/client/v1/pusher.py b/synapse/rest/client/v1/pusher.py index d792dac64..ab928a16d 100644 --- a/synapse/rest/client/v1/pusher.py +++ b/synapse/rest/client/v1/pusher.py @@ -64,7 +64,7 @@ class PushersRestServlet(ClientV1RestServlet): class PushersSetRestServlet(ClientV1RestServlet): - PATTERNS = client_path_patterns("/pushers(/set)?$") + PATTERNS = client_path_patterns("/pushers/set$") def __init__(self, hs): super(PushersSetRestServlet, self).__init__(hs) From c1267d04c5fdd900ea917682056b1239495f90ee Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 12 Apr 2016 13:55:32 +0100 Subject: [PATCH 6/7] Oops, forgot the desc. --- synapse/storage/pusher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/pusher.py b/synapse/storage/pusher.py index 774eda0f3..e0b80d64f 100644 --- a/synapse/storage/pusher.py +++ b/synapse/storage/pusher.py @@ -88,7 +88,7 @@ class PusherStore(SQLBaseStore): "last_success", "failing_since", - ] + ], desc="get_pushers_by" ) defer.returnValue(self._decode_pushers_rows(ret)) From 7984ffdc6a1850f6e0eb46562c53a10ce31e09b0 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 12 Apr 2016 13:55:57 +0100 Subject: [PATCH 7/7] Unneccessarywhitespaceisunnecessary --- synapse/storage/pusher.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/storage/pusher.py b/synapse/storage/pusher.py index e0b80d64f..e64c0dce0 100644 --- a/synapse/storage/pusher.py +++ b/synapse/storage/pusher.py @@ -87,7 +87,6 @@ class PusherStore(SQLBaseStore): "last_stream_ordering", "last_success", "failing_since", - ], desc="get_pushers_by" ) defer.returnValue(self._decode_pushers_rows(ret))