2014-11-19 19:20:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-11-19 19:20:59 +01:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
import logging
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.api.errors import Codes, StoreError, SynapseError
|
|
|
|
from synapse.http.server import finish_request
|
2016-06-01 18:40:52 +02:00
|
|
|
from synapse.http.servlet import (
|
2018-07-09 08:09:20 +02:00
|
|
|
RestServlet,
|
2018-07-13 21:53:01 +02:00
|
|
|
assert_params_in_dict,
|
2018-07-09 08:09:20 +02:00
|
|
|
parse_json_object_from_request,
|
|
|
|
parse_string,
|
2016-06-01 18:40:52 +02:00
|
|
|
)
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.push import PusherConfigException
|
2016-03-09 12:26:26 +01:00
|
|
|
|
2015-12-01 18:34:32 +01:00
|
|
|
from .base import ClientV1RestServlet, client_path_patterns
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-12-07 12:52:20 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-12-07 12:57:48 +01:00
|
|
|
|
2016-04-12 14:33:30 +02:00
|
|
|
class PushersRestServlet(ClientV1RestServlet):
|
|
|
|
PATTERNS = client_path_patterns("/pushers$")
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2016-03-15 18:41:06 +01:00
|
|
|
def __init__(self, hs):
|
2016-04-12 14:33:30 +02:00
|
|
|
super(PushersRestServlet, self).__init__(hs)
|
2016-03-15 18:41:06 +01:00
|
|
|
|
2016-04-11 19:00:03 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_GET(self, request):
|
|
|
|
requester = yield self.auth.get_user_by_req(request)
|
|
|
|
user = requester.user
|
|
|
|
|
2016-04-12 14:35:08 +02:00
|
|
|
pushers = yield self.hs.get_datastore().get_pushers_by_user_id(
|
2016-04-11 19:00:03 +02:00
|
|
|
user.to_string()
|
|
|
|
)
|
|
|
|
|
|
|
|
allowed_keys = [
|
|
|
|
"app_display_name",
|
|
|
|
"app_id",
|
|
|
|
"data",
|
|
|
|
"device_display_name",
|
|
|
|
"kind",
|
|
|
|
"lang",
|
|
|
|
"profile_tag",
|
|
|
|
"pushkey",
|
|
|
|
]
|
|
|
|
|
|
|
|
for p in pushers:
|
2018-09-12 12:41:31 +02:00
|
|
|
for k, v in list(p.items()):
|
2016-04-11 19:00:03 +02:00
|
|
|
if k not in allowed_keys:
|
|
|
|
del p[k]
|
|
|
|
|
|
|
|
defer.returnValue((200, {"pushers": pushers}))
|
|
|
|
|
2016-04-12 14:33:30 +02:00
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
|
|
|
class PushersSetRestServlet(ClientV1RestServlet):
|
2016-04-12 14:54:41 +02:00
|
|
|
PATTERNS = client_path_patterns("/pushers/set$")
|
2016-04-12 14:33:30 +02:00
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(PushersSetRestServlet, self).__init__(hs)
|
|
|
|
self.notifier = hs.get_notifier()
|
2017-07-06 18:55:51 +02:00
|
|
|
self.pusher_pool = self.hs.get_pusherpool()
|
2016-04-12 14:33:30 +02:00
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
@defer.inlineCallbacks
|
2014-12-18 15:49:22 +01:00
|
|
|
def on_POST(self, request):
|
2016-01-11 16:29:57 +01:00
|
|
|
requester = yield self.auth.get_user_by_req(request)
|
|
|
|
user = requester.user
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2016-03-09 12:26:26 +01:00
|
|
|
content = parse_json_object_from_request(request)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-01-29 18:04:31 +01:00
|
|
|
if ('pushkey' in content and 'app_id' in content
|
2015-02-10 17:30:48 +01:00
|
|
|
and 'kind' in content and
|
|
|
|
content['kind'] is None):
|
2017-07-06 18:55:51 +02:00
|
|
|
yield self.pusher_pool.remove_pusher(
|
2016-01-13 14:08:59 +01:00
|
|
|
content['app_id'], content['pushkey'], user_id=user.to_string()
|
2015-01-29 18:04:31 +01:00
|
|
|
)
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2018-07-13 21:53:01 +02:00
|
|
|
assert_params_in_dict(
|
2018-07-13 21:40:14 +02:00
|
|
|
content,
|
|
|
|
['kind', 'app_id', 'app_display_name',
|
|
|
|
'device_display_name', 'pushkey', 'lang', 'data']
|
|
|
|
)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-12-07 13:01:00 +01:00
|
|
|
logger.debug("set pushkey %s to kind %s", content['pushkey'], content['kind'])
|
2015-12-07 12:52:20 +01:00
|
|
|
logger.debug("Got pushers request with body: %r", content)
|
|
|
|
|
2015-03-25 20:06:22 +01:00
|
|
|
append = False
|
|
|
|
if 'append' in content:
|
|
|
|
append = content['append']
|
|
|
|
|
|
|
|
if not append:
|
2017-07-06 18:55:51 +02:00
|
|
|
yield self.pusher_pool.remove_pushers_by_app_id_and_pushkey_not_user(
|
2015-03-25 20:06:22 +01:00
|
|
|
app_id=content['app_id'],
|
|
|
|
pushkey=content['pushkey'],
|
|
|
|
not_user_id=user.to_string()
|
|
|
|
)
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
try:
|
2017-07-06 18:55:51 +02:00
|
|
|
yield self.pusher_pool.add_pusher(
|
2016-01-13 14:08:59 +01:00
|
|
|
user_id=user.to_string(),
|
2016-01-11 16:29:57 +01:00
|
|
|
access_token=requester.access_token_id,
|
2014-12-03 14:37:02 +01:00
|
|
|
kind=content['kind'],
|
|
|
|
app_id=content['app_id'],
|
|
|
|
app_display_name=content['app_display_name'],
|
|
|
|
device_display_name=content['device_display_name'],
|
2014-12-18 15:49:22 +01:00
|
|
|
pushkey=content['pushkey'],
|
2015-01-16 12:24:10 +01:00
|
|
|
lang=content['lang'],
|
2016-02-18 17:05:13 +01:00
|
|
|
data=content['data'],
|
|
|
|
profile_tag=content.get('profile_tag', ""),
|
2014-12-03 14:37:02 +01:00
|
|
|
)
|
2014-11-19 19:20:59 +01:00
|
|
|
except PusherConfigException as pce:
|
2018-09-12 12:41:31 +02:00
|
|
|
raise SynapseError(400, "Config Error: " + str(pce),
|
2014-12-03 14:37:02 +01:00
|
|
|
errcode=Codes.MISSING_PARAM)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2016-03-15 18:41:06 +01:00
|
|
|
self.notifier.on_new_replication_data()
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2014-12-03 14:37:02 +01:00
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2016-06-01 18:40:52 +02:00
|
|
|
class PushersRemoveRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
To allow pusher to be delete by clicking a link (ie. GET request)
|
|
|
|
"""
|
|
|
|
PATTERNS = client_path_patterns("/pushers/remove$")
|
2018-12-03 12:27:41 +01:00
|
|
|
SUCCESS_HTML = b"<html><body>You have been unsubscribed</body><html>"
|
2016-06-01 18:40:52 +02:00
|
|
|
|
|
|
|
def __init__(self, hs):
|
2018-07-13 21:40:14 +02:00
|
|
|
super(PushersRemoveRestServlet, self).__init__()
|
2016-06-02 18:21:31 +02:00
|
|
|
self.hs = hs
|
2016-06-01 18:40:52 +02:00
|
|
|
self.notifier = hs.get_notifier()
|
2018-04-30 21:58:30 +02:00
|
|
|
self.auth = hs.get_auth()
|
2017-07-06 18:55:51 +02:00
|
|
|
self.pusher_pool = self.hs.get_pusherpool()
|
2016-06-01 18:40:52 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_GET(self, request):
|
2016-06-02 18:21:31 +02:00
|
|
|
requester = yield self.auth.get_user_by_req(request, rights="delete_pusher")
|
2016-06-01 18:40:52 +02:00
|
|
|
user = requester.user
|
|
|
|
|
|
|
|
app_id = parse_string(request, "app_id", required=True)
|
|
|
|
pushkey = parse_string(request, "pushkey", required=True)
|
|
|
|
|
|
|
|
try:
|
2017-07-06 18:55:51 +02:00
|
|
|
yield self.pusher_pool.remove_pusher(
|
2016-06-01 18:40:52 +02:00
|
|
|
app_id=app_id,
|
|
|
|
pushkey=pushkey,
|
|
|
|
user_id=user.to_string(),
|
|
|
|
)
|
|
|
|
except StoreError as se:
|
|
|
|
if se.code != 404:
|
|
|
|
# This is fine: they're already unsubscribed
|
|
|
|
raise
|
|
|
|
|
|
|
|
self.notifier.on_new_replication_data()
|
|
|
|
|
|
|
|
request.setResponseCode(200)
|
|
|
|
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
|
|
|
|
request.setHeader(b"Content-Length", b"%d" % (
|
|
|
|
len(PushersRemoveRestServlet.SUCCESS_HTML),
|
|
|
|
))
|
|
|
|
request.write(PushersRemoveRestServlet.SUCCESS_HTML)
|
|
|
|
finish_request(request)
|
|
|
|
defer.returnValue(None)
|
|
|
|
|
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
def register_servlets(hs, http_server):
|
2016-04-12 14:33:30 +02:00
|
|
|
PushersRestServlet(hs).register(http_server)
|
|
|
|
PushersSetRestServlet(hs).register(http_server)
|
2016-06-01 18:40:52 +02:00
|
|
|
PushersRemoveRestServlet(hs).register(http_server)
|