2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-11-19 11:20:59 -07: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 00:09:20 -06:00
|
|
|
import logging
|
2021-08-23 06:14:17 -06:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
2018-07-09 00:09:20 -06:00
|
|
|
|
|
|
|
from synapse.api.errors import Codes, StoreError, SynapseError
|
2021-08-23 06:14:17 -06:00
|
|
|
from synapse.http.server import HttpServer, respond_with_html_bytes
|
2016-06-01 10:40:52 -06:00
|
|
|
from synapse.http.servlet import (
|
2018-07-09 00:09:20 -06:00
|
|
|
RestServlet,
|
2018-07-13 13:53:01 -06:00
|
|
|
assert_params_in_dict,
|
2018-07-09 00:09:20 -06:00
|
|
|
parse_json_object_from_request,
|
|
|
|
parse_string,
|
2016-06-01 10:40:52 -06:00
|
|
|
)
|
2021-08-23 06:14:17 -06:00
|
|
|
from synapse.http.site import SynapseRequest
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.push import PusherConfigException
|
2021-08-17 05:57:58 -06:00
|
|
|
from synapse.rest.client._base import client_patterns
|
2021-08-23 06:14:17 -06:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2015-12-07 04:52:20 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2015-12-07 04:57:48 -07:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
class PushersRestServlet(RestServlet):
|
|
|
|
PATTERNS = client_patterns("/pushers$", v1=True)
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__()
|
2019-06-03 05:28:59 -06:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2016-03-15 11:41:06 -06:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 08:53:06 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2016-04-11 11:00:03 -06:00
|
|
|
user = requester.user
|
|
|
|
|
2022-02-23 04:04:02 -07:00
|
|
|
pushers = await self.hs.get_datastores().main.get_pushers_by_user_id(
|
|
|
|
user.to_string()
|
|
|
|
)
|
2016-04-11 11:00:03 -06:00
|
|
|
|
2020-12-16 09:25:30 -07:00
|
|
|
filtered_pushers = [p.as_dict() for p in pushers]
|
2016-04-11 11:00:03 -06:00
|
|
|
|
2019-12-18 07:26:58 -07:00
|
|
|
return 200, {"pushers": filtered_pushers}
|
2016-04-11 11:00:03 -06:00
|
|
|
|
2016-04-12 06:33:30 -06:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
class PushersSetRestServlet(RestServlet):
|
|
|
|
PATTERNS = client_patterns("/pushers/set$", v1=True)
|
2016-04-12 06:33:30 -06:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__()
|
2019-06-03 05:28:59 -06:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2016-04-12 06:33:30 -06:00
|
|
|
self.notifier = hs.get_notifier()
|
2017-07-06 10:55:51 -06:00
|
|
|
self.pusher_pool = self.hs.get_pusherpool()
|
2016-04-12 06:33:30 -06:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 08:53:06 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2016-01-11 08:29:57 -07:00
|
|
|
user = requester.user
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2016-03-09 04:26:26 -07:00
|
|
|
content = parse_json_object_from_request(request)
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2015-01-29 10:04:31 -07:00
|
|
|
if (
|
|
|
|
"pushkey" in content
|
|
|
|
and "app_id" in content
|
2015-02-10 09:30:48 -07:00
|
|
|
and "kind" in content
|
|
|
|
and content["kind"] is None
|
|
|
|
):
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.pusher_pool.remove_pusher(
|
2016-01-13 06:08:59 -07:00
|
|
|
content["app_id"], content["pushkey"], user_id=user.to_string()
|
2015-01-29 10:04:31 -07:00
|
|
|
)
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {}
|
2015-01-29 10:04:31 -07:00
|
|
|
|
2018-07-13 13:53:01 -06:00
|
|
|
assert_params_in_dict(
|
2018-07-13 13:40:14 -06:00
|
|
|
content,
|
|
|
|
[
|
|
|
|
"kind",
|
|
|
|
"app_id",
|
|
|
|
"app_display_name",
|
|
|
|
"device_display_name",
|
|
|
|
"pushkey",
|
|
|
|
"lang",
|
|
|
|
"data",
|
2019-06-20 03:32:02 -06:00
|
|
|
],
|
2018-07-13 13:40:14 -06:00
|
|
|
)
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2015-12-07 05:01:00 -07:00
|
|
|
logger.debug("set pushkey %s to kind %s", content["pushkey"], content["kind"])
|
2015-12-07 04:52:20 -07:00
|
|
|
logger.debug("Got pushers request with body: %r", content)
|
|
|
|
|
2015-03-25 13:06:22 -06:00
|
|
|
append = False
|
|
|
|
if "append" in content:
|
|
|
|
append = content["append"]
|
|
|
|
|
|
|
|
if not append:
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.pusher_pool.remove_pushers_by_app_id_and_pushkey_not_user(
|
2015-03-25 13:06:22 -06:00
|
|
|
app_id=content["app_id"],
|
|
|
|
pushkey=content["pushkey"],
|
|
|
|
not_user_id=user.to_string(),
|
|
|
|
)
|
|
|
|
|
2014-11-19 11:20:59 -07:00
|
|
|
try:
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.pusher_pool.add_pusher(
|
2016-01-13 06:08:59 -07:00
|
|
|
user_id=user.to_string(),
|
2016-01-11 08:29:57 -07:00
|
|
|
access_token=requester.access_token_id,
|
2014-12-03 06:37:02 -07: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 07:49:22 -07:00
|
|
|
pushkey=content["pushkey"],
|
2015-01-16 04:24:10 -07:00
|
|
|
lang=content["lang"],
|
2016-02-18 09:05:13 -07:00
|
|
|
data=content["data"],
|
|
|
|
profile_tag=content.get("profile_tag", ""),
|
2014-12-03 06:37:02 -07:00
|
|
|
)
|
2014-11-19 11:20:59 -07:00
|
|
|
except PusherConfigException as pce:
|
2018-09-12 04:41:31 -06:00
|
|
|
raise SynapseError(
|
|
|
|
400, "Config Error: " + str(pce), errcode=Codes.MISSING_PARAM
|
2014-12-03 06:37:02 -07:00
|
|
|
)
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2016-03-15 11:41:06 -06:00
|
|
|
self.notifier.on_new_replication_data()
|
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, {}
|
2014-11-19 11:20:59 -07:00
|
|
|
|
|
|
|
|
2016-06-01 10:40:52 -06:00
|
|
|
class PushersRemoveRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
To allow pusher to be delete by clicking a link (ie. GET request)
|
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
PATTERNS = client_patterns("/pushers/remove$", v1=True)
|
2018-12-03 04:27:41 -07:00
|
|
|
SUCCESS_HTML = b"<html><body>You have been unsubscribed</body><html>"
|
2016-06-01 10:40:52 -06:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__()
|
2016-06-02 10:21:31 -06:00
|
|
|
self.hs = hs
|
2016-06-01 10:40:52 -06:00
|
|
|
self.notifier = hs.get_notifier()
|
2018-04-30 13:58:30 -06:00
|
|
|
self.auth = hs.get_auth()
|
2017-07-06 10:55:51 -06:00
|
|
|
self.pusher_pool = self.hs.get_pusherpool()
|
2016-06-01 10:40:52 -06:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> None:
|
2019-12-05 08:53:06 -07:00
|
|
|
requester = await self.auth.get_user_by_req(request, rights="delete_pusher")
|
2016-06-01 10:40:52 -06:00
|
|
|
user = requester.user
|
|
|
|
|
|
|
|
app_id = parse_string(request, "app_id", required=True)
|
|
|
|
pushkey = parse_string(request, "pushkey", required=True)
|
|
|
|
|
|
|
|
try:
|
2019-12-05 08:53:06 -07:00
|
|
|
await self.pusher_pool.remove_pusher(
|
2016-06-01 10:40:52 -06: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()
|
|
|
|
|
2020-07-01 07:10:23 -06:00
|
|
|
respond_with_html_bytes(
|
|
|
|
request,
|
|
|
|
200,
|
|
|
|
PushersRemoveRestServlet.SUCCESS_HTML,
|
2016-06-01 10:40:52 -06:00
|
|
|
)
|
2019-07-23 07:00:55 -06:00
|
|
|
return None
|
2016-06-01 10:40:52 -06:00
|
|
|
|
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2016-04-12 06:33:30 -06:00
|
|
|
PushersRestServlet(hs).register(http_server)
|
|
|
|
PushersSetRestServlet(hs).register(http_server)
|
2016-06-01 10:40:52 -06:00
|
|
|
PushersRemoveRestServlet(hs).register(http_server)
|