anonymousland-synapse/synapse/rest/client/v1/pusher.py

197 lines
5.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-01-06 23:26:29 -05:00
# Copyright 2014-2016 OpenMarket Ltd
#
# 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 02:09:20 -04:00
import logging
from synapse.api.errors import Codes, StoreError, SynapseError
from synapse.http.server import finish_request
from synapse.http.servlet import (
2018-07-09 02:09:20 -04:00
RestServlet,
assert_params_in_dict,
2018-07-09 02:09:20 -04:00
parse_json_object_from_request,
parse_string,
)
2018-07-09 02:09:20 -04:00
from synapse.push import PusherConfigException
from synapse.rest.client.v2_alpha._base import client_patterns
logger = logging.getLogger(__name__)
ALLOWED_KEYS = {
"app_display_name",
"app_id",
"data",
"device_display_name",
"kind",
"lang",
"profile_tag",
"pushkey",
}
2015-12-07 06:57:48 -05:00
class PushersRestServlet(RestServlet):
PATTERNS = client_patterns("/pushers$", v1=True)
def __init__(self, hs):
super(PushersRestServlet, self).__init__()
self.hs = hs
self.auth = hs.get_auth()
2019-12-05 10:53:06 -05:00
async def on_GET(self, request):
requester = await self.auth.get_user_by_req(request)
user = requester.user
2019-12-05 10:53:06 -05:00
pushers = await self.hs.get_datastore().get_pushers_by_user_id(user.to_string())
filtered_pushers = [
{k: v for k, v in p.items() if k in ALLOWED_KEYS} for p in pushers
]
return 200, {"pushers": filtered_pushers}
2016-04-12 08:33:30 -04:00
def on_OPTIONS(self, _):
return 200, {}
class PushersSetRestServlet(RestServlet):
PATTERNS = client_patterns("/pushers/set$", v1=True)
2016-04-12 08:33:30 -04:00
def __init__(self, hs):
super(PushersSetRestServlet, self).__init__()
self.hs = hs
self.auth = hs.get_auth()
2016-04-12 08:33:30 -04:00
self.notifier = hs.get_notifier()
self.pusher_pool = self.hs.get_pusherpool()
2016-04-12 08:33:30 -04:00
2019-12-05 10:53:06 -05:00
async def on_POST(self, request):
requester = await self.auth.get_user_by_req(request)
user = requester.user
content = parse_json_object_from_request(request)
2019-06-20 05:32:02 -04:00
if (
"pushkey" in content
and "app_id" in content
and "kind" in content
and content["kind"] is None
):
2019-12-05 10:53:06 -05:00
await self.pusher_pool.remove_pusher(
2019-06-20 05:32:02 -04:00
content["app_id"], content["pushkey"], user_id=user.to_string()
)
return 200, {}
assert_params_in_dict(
content,
2019-06-20 05:32:02 -04:00
[
"kind",
"app_id",
"app_display_name",
"device_display_name",
"pushkey",
"lang",
"data",
],
)
2019-06-20 05:32:02 -04:00
logger.debug("set pushkey %s to kind %s", content["pushkey"], content["kind"])
logger.debug("Got pushers request with body: %r", content)
append = False
2019-06-20 05:32:02 -04:00
if "append" in content:
append = content["append"]
if not append:
2019-12-05 10:53:06 -05:00
await self.pusher_pool.remove_pushers_by_app_id_and_pushkey_not_user(
2019-06-20 05:32:02 -04:00
app_id=content["app_id"],
pushkey=content["pushkey"],
not_user_id=user.to_string(),
)
try:
2019-12-05 10:53:06 -05:00
await self.pusher_pool.add_pusher(
user_id=user.to_string(),
access_token=requester.access_token_id,
2019-06-20 05:32:02 -04:00
kind=content["kind"],
app_id=content["app_id"],
app_display_name=content["app_display_name"],
device_display_name=content["device_display_name"],
pushkey=content["pushkey"],
lang=content["lang"],
data=content["data"],
profile_tag=content.get("profile_tag", ""),
)
except PusherConfigException as pce:
2019-06-20 05:32:02 -04:00
raise SynapseError(
400, "Config Error: " + str(pce), errcode=Codes.MISSING_PARAM
)
self.notifier.on_new_replication_data()
return 200, {}
def on_OPTIONS(self, _):
return 200, {}
class PushersRemoveRestServlet(RestServlet):
"""
To allow pusher to be delete by clicking a link (ie. GET request)
"""
2019-06-20 05:32:02 -04:00
PATTERNS = client_patterns("/pushers/remove$", v1=True)
2018-12-03 06:27:41 -05:00
SUCCESS_HTML = b"<html><body>You have been unsubscribed</body><html>"
def __init__(self, hs):
super(PushersRemoveRestServlet, self).__init__()
self.hs = hs
self.notifier = hs.get_notifier()
self.auth = hs.get_auth()
self.pusher_pool = self.hs.get_pusherpool()
2019-12-05 10:53:06 -05:00
async def on_GET(self, request):
requester = await self.auth.get_user_by_req(request, rights="delete_pusher")
user = requester.user
app_id = parse_string(request, "app_id", required=True)
pushkey = parse_string(request, "pushkey", required=True)
try:
2019-12-05 10:53:06 -05:00
await self.pusher_pool.remove_pusher(
2019-06-20 05:32:02 -04: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")
2019-06-20 05:32:02 -04:00
request.setHeader(
b"Content-Length", b"%d" % (len(PushersRemoveRestServlet.SUCCESS_HTML),)
)
request.write(PushersRemoveRestServlet.SUCCESS_HTML)
finish_request(request)
return None
def on_OPTIONS(self, _):
return 200, {}
def register_servlets(hs, http_server):
2016-04-12 08:33:30 -04:00
PushersRestServlet(hs).register(http_server)
PushersSetRestServlet(hs).register(http_server)
PushersRemoveRestServlet(hs).register(http_server)