synapse-old/synapse/rest/client/pusher.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
5.7 KiB
Python
Raw Normal View History

2016-01-06 21:26:29 -07:00
# Copyright 2014-2016 OpenMarket Ltd
# Copyright 2022 The Matrix.org Foundation C.I.C.
#
# 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
from typing import TYPE_CHECKING, Tuple
2018-07-09 00:09:20 -06:00
from synapse.api.errors import Codes, SynapseError
from synapse.http.server import HttpServer
from synapse.http.servlet import (
2018-07-09 00:09:20 -06:00
RestServlet,
assert_params_in_dict,
2018-07-09 00:09:20 -06:00
parse_json_object_from_request,
)
from synapse.http.site import SynapseRequest
2018-07-09 00:09:20 -06:00
from synapse.push import PusherConfigException
from synapse.rest.client._base import client_patterns
from synapse.rest.synapse.client.unsubscribe import UnsubscribeResource
from synapse.types import JsonDict
if TYPE_CHECKING:
from synapse.server import HomeServer
logger = logging.getLogger(__name__)
2015-12-07 04:57:48 -07:00
class PushersRestServlet(RestServlet):
PATTERNS = client_patterns("/pushers$", v1=True)
def __init__(self, hs: "HomeServer"):
super().__init__()
self.hs = hs
self.auth = hs.get_auth()
self._msc3881_enabled = self.hs.config.experimental.msc3881_enabled
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)
user = requester.user
pushers = await self.hs.get_datastores().main.get_pushers_by_user_id(
user.to_string()
)
pusher_dicts = [p.as_dict() for p in pushers]
for pusher in pusher_dicts:
if self._msc3881_enabled:
pusher["org.matrix.msc3881.enabled"] = pusher["enabled"]
pusher["org.matrix.msc3881.device_id"] = pusher["device_id"]
del pusher["enabled"]
del pusher["device_id"]
return 200, {"pushers": pusher_dicts}
2016-04-12 06:33:30 -06:00
class PushersSetRestServlet(RestServlet):
PATTERNS = client_patterns("/pushers/set$", v1=True)
2016-04-12 06:33:30 -06:00
def __init__(self, hs: "HomeServer"):
super().__init__()
self.hs = hs
self.auth = hs.get_auth()
2016-04-12 06:33:30 -06:00
self.notifier = hs.get_notifier()
self.pusher_pool = self.hs.get_pusherpool()
self._msc3881_enabled = self.hs.config.experimental.msc3881_enabled
2016-04-12 06:33:30 -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)
user = requester.user
content = parse_json_object_from_request(request)
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(
content["app_id"], content["pushkey"], user_id=user.to_string()
)
return 200, {}
assert_params_in_dict(
content,
[
"kind",
"app_id",
"app_display_name",
"device_display_name",
"pushkey",
"lang",
"data",
2019-06-20 03:32:02 -06:00
],
)
2015-12-07 05:01:00 -07:00
logger.debug("set pushkey %s to kind %s", content["pushkey"], content["kind"])
logger.debug("Got pushers request with body: %r", content)
append = False
if "append" in content:
append = content["append"]
enabled = True
if self._msc3881_enabled and "org.matrix.msc3881.enabled" in content:
enabled = content["org.matrix.msc3881.enabled"]
if not append:
2019-12-05 08:53:06 -07:00
await self.pusher_pool.remove_pushers_by_app_id_and_pushkey_not_user(
app_id=content["app_id"],
pushkey=content["pushkey"],
not_user_id=user.to_string(),
)
try:
await self.pusher_pool.add_or_update_pusher(
user_id=user.to_string(),
access_token=requester.access_token_id,
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", ""),
enabled=enabled,
device_id=requester.device_id,
)
except PusherConfigException as pce:
2018-09-12 04:41:31 -06:00
raise SynapseError(
400, "Config Error: " + str(pce), errcode=Codes.MISSING_PARAM
)
self.notifier.on_new_replication_data()
return 200, {}
class LegacyPushersRemoveRestServlet(UnsubscribeResource, RestServlet):
"""
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.
"""
2019-06-20 03:32:02 -06:00
PATTERNS = client_patterns("/pushers/remove$", releases=[], v1=False, unstable=True)
async def on_GET(self, request: SynapseRequest) -> None:
# Forward the request to the UnsubscribeResource
await self._async_render(request)
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)
LegacyPushersRemoveRestServlet(hs).register(http_server)