2014-11-19 13:20:59 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2014-11-19 13:20:59 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2021-08-23 08:14:17 -04:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2022-06-14 09:12:08 -04:00
|
|
|
from synapse.api.errors import Codes, SynapseError
|
|
|
|
from synapse.http.server import HttpServer
|
2016-06-01 12:40:52 -04:00
|
|
|
from synapse.http.servlet import (
|
2018-07-09 02:09:20 -04:00
|
|
|
RestServlet,
|
2018-07-13 15:53:01 -04:00
|
|
|
assert_params_in_dict,
|
2018-07-09 02:09:20 -04:00
|
|
|
parse_json_object_from_request,
|
2016-06-01 12:40:52 -04:00
|
|
|
)
|
2021-08-23 08:14:17 -04:00
|
|
|
from synapse.http.site import SynapseRequest
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.push import PusherConfigException
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client._base import client_patterns
|
2022-06-14 09:12:08 -04:00
|
|
|
from synapse.rest.synapse.client.unsubscribe import UnsubscribeResource
|
2021-08-23 08:14:17 -04:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
2014-11-19 13:20:59 -05:00
|
|
|
|
2015-12-07 06:52:20 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-19 13:20:59 -05:00
|
|
|
|
2015-12-07 06:57:48 -05:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
class PushersRestServlet(RestServlet):
|
|
|
|
PATTERNS = client_patterns("/pushers$", v1=True)
|
2014-11-19 13:20:59 -05:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2019-06-03 07:28:59 -04:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2022-09-21 10:39:01 -04:00
|
|
|
self._msc3881_enabled = self.hs.config.experimental.msc3881_enabled
|
2016-03-15 13:41:06 -04:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 10:53:06 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2016-04-11 13:00:03 -04:00
|
|
|
user = requester.user
|
|
|
|
|
2022-02-23 06:04:02 -05:00
|
|
|
pushers = await self.hs.get_datastores().main.get_pushers_by_user_id(
|
|
|
|
user.to_string()
|
|
|
|
)
|
2016-04-11 13:00:03 -04:00
|
|
|
|
2022-09-21 10:39:01 -04:00
|
|
|
pusher_dicts = [p.as_dict() for p in pushers]
|
2016-04-11 13:00:03 -04:00
|
|
|
|
2022-09-21 10:39:01 -04:00
|
|
|
for pusher in pusher_dicts:
|
|
|
|
if self._msc3881_enabled:
|
|
|
|
pusher["org.matrix.msc3881.enabled"] = pusher["enabled"]
|
2022-09-21 11:31:53 -04:00
|
|
|
pusher["org.matrix.msc3881.device_id"] = pusher["device_id"]
|
2022-09-21 10:39:01 -04:00
|
|
|
del pusher["enabled"]
|
2022-09-21 11:31:53 -04:00
|
|
|
del pusher["device_id"]
|
2022-09-21 10:39:01 -04:00
|
|
|
|
|
|
|
return 200, {"pushers": pusher_dicts}
|
2016-04-11 13:00:03 -04:00
|
|
|
|
2016-04-12 08:33:30 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
class PushersSetRestServlet(RestServlet):
|
|
|
|
PATTERNS = client_patterns("/pushers/set$", v1=True)
|
2016-04-12 08:33:30 -04:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2019-06-03 07:28:59 -04:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2016-04-12 08:33:30 -04:00
|
|
|
self.notifier = hs.get_notifier()
|
2017-07-06 12:55:51 -04:00
|
|
|
self.pusher_pool = self.hs.get_pusherpool()
|
2022-09-21 10:39:01 -04:00
|
|
|
self._msc3881_enabled = self.hs.config.experimental.msc3881_enabled
|
2016-04-12 08:33:30 -04:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 10:53:06 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2016-01-11 10:29:57 -05:00
|
|
|
user = requester.user
|
2014-11-19 13:20:59 -05:00
|
|
|
|
2016-03-09 06:26:26 -05:00
|
|
|
content = parse_json_object_from_request(request)
|
2014-11-19 13:20:59 -05:00
|
|
|
|
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()
|
2015-01-29 12:04:31 -05:00
|
|
|
)
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {}
|
2015-01-29 12:04:31 -05:00
|
|
|
|
2018-07-13 15:53:01 -04:00
|
|
|
assert_params_in_dict(
|
2018-07-13 15:40:14 -04:00
|
|
|
content,
|
2019-06-20 05:32:02 -04:00
|
|
|
[
|
|
|
|
"kind",
|
|
|
|
"app_id",
|
|
|
|
"app_display_name",
|
|
|
|
"device_display_name",
|
|
|
|
"pushkey",
|
|
|
|
"lang",
|
|
|
|
"data",
|
|
|
|
],
|
2018-07-13 15:40:14 -04:00
|
|
|
)
|
2014-11-19 13:20:59 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
logger.debug("set pushkey %s to kind %s", content["pushkey"], content["kind"])
|
2015-12-07 06:52:20 -05:00
|
|
|
logger.debug("Got pushers request with body: %r", content)
|
|
|
|
|
2015-03-25 15:06:22 -04:00
|
|
|
append = False
|
2019-06-20 05:32:02 -04:00
|
|
|
if "append" in content:
|
|
|
|
append = content["append"]
|
2015-03-25 15:06:22 -04:00
|
|
|
|
2022-09-21 10:39:01 -04:00
|
|
|
enabled = True
|
|
|
|
if self._msc3881_enabled and "org.matrix.msc3881.enabled" in content:
|
|
|
|
enabled = content["org.matrix.msc3881.enabled"]
|
|
|
|
|
2015-03-25 15:06:22 -04:00
|
|
|
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(),
|
2015-03-25 15:06:22 -04:00
|
|
|
)
|
|
|
|
|
2014-11-19 13:20:59 -05:00
|
|
|
try:
|
2022-09-21 10:39:01 -04:00
|
|
|
await self.pusher_pool.add_or_update_pusher(
|
2016-01-13 08:08:59 -05:00
|
|
|
user_id=user.to_string(),
|
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", ""),
|
2022-09-21 10:39:01 -04:00
|
|
|
enabled=enabled,
|
2022-09-21 11:31:53 -04:00
|
|
|
device_id=requester.device_id,
|
2014-12-03 08:37:02 -05:00
|
|
|
)
|
2014-11-19 13:20:59 -05:00
|
|
|
except PusherConfigException as pce:
|
2019-06-20 05:32:02 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400, "Config Error: " + str(pce), errcode=Codes.MISSING_PARAM
|
|
|
|
)
|
2014-11-19 13:20:59 -05:00
|
|
|
|
2016-03-15 13:41:06 -04:00
|
|
|
self.notifier.on_new_replication_data()
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {}
|
2014-11-19 13:20:59 -05:00
|
|
|
|
|
|
|
|
2022-06-14 09:12:08 -04:00
|
|
|
class LegacyPushersRemoveRestServlet(UnsubscribeResource, RestServlet):
|
2016-06-01 12:40:52 -04:00
|
|
|
"""
|
2022-06-14 09:12:08 -04:00
|
|
|
A servlet to handle legacy "email unsubscribe" links, forwarding requests to the ``UnsubscribeResource``
|
|
|
|
|
|
|
|
This should be kept for some time, so unsubscribe links in past emails stay valid.
|
2016-06-01 12:40:52 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2022-06-14 09:12:08 -04:00
|
|
|
PATTERNS = client_patterns("/pushers/remove$", releases=[], v1=False, unstable=True)
|
2016-06-01 12:40:52 -04:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> None:
|
2022-06-14 09:12:08 -04:00
|
|
|
# Forward the request to the UnsubscribeResource
|
|
|
|
await self._async_render(request)
|
2016-06-01 12:40:52 -04:00
|
|
|
|
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2016-04-12 08:33:30 -04:00
|
|
|
PushersRestServlet(hs).register(http_server)
|
|
|
|
PushersSetRestServlet(hs).register(http_server)
|
2022-06-14 09:12:08 -04:00
|
|
|
LegacyPushersRemoveRestServlet(hs).register(http_server)
|