Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 73af10f

Browse files
authored
Simplify the alias deletion logic as an application service. (#13093)
1 parent 5ef05c7 commit 73af10f

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

changelog.d/13093.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simplify the alias deletion logic as an application service.

synapse/rest/client/directory.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@
1717

1818
from twisted.web.server import Request
1919

20-
from synapse.api.errors import (
21-
AuthError,
22-
Codes,
23-
InvalidClientCredentialsError,
24-
NotFoundError,
25-
SynapseError,
26-
)
20+
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
2721
from synapse.http.server import HttpServer
2822
from synapse.http.servlet import RestServlet, parse_json_object_from_request
2923
from synapse.http.site import SynapseRequest
@@ -96,30 +90,27 @@ async def on_DELETE(
9690
self, request: SynapseRequest, room_alias: str
9791
) -> Tuple[int, JsonDict]:
9892
room_alias_obj = RoomAlias.from_string(room_alias)
93+
requester = await self.auth.get_user_by_req(request)
9994

100-
try:
101-
service = self.auth.get_appservice_by_req(request)
95+
if requester.app_service:
10296
await self.directory_handler.delete_appservice_association(
103-
service, room_alias_obj
97+
requester.app_service, room_alias_obj
10498
)
99+
105100
logger.info(
106101
"Application service at %s deleted alias %s",
107-
service.url,
102+
requester.app_service.url,
108103
room_alias_obj.to_string(),
109104
)
110-
return 200, {}
111-
except InvalidClientCredentialsError:
112-
# fallback to default user behaviour if they aren't an AS
113-
pass
114-
115-
requester = await self.auth.get_user_by_req(request)
116-
user = requester.user
117105

118-
await self.directory_handler.delete_association(requester, room_alias_obj)
106+
else:
107+
await self.directory_handler.delete_association(requester, room_alias_obj)
119108

120-
logger.info(
121-
"User %s deleted alias %s", user.to_string(), room_alias_obj.to_string()
122-
)
109+
logger.info(
110+
"User %s deleted alias %s",
111+
requester.user.to_string(),
112+
room_alias_obj.to_string(),
113+
)
123114

124115
return 200, {}
125116

tests/rest/client/test_directory.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from twisted.test.proto_helpers import MemoryReactor
1818

19+
from synapse.appservice import ApplicationService
1920
from synapse.rest import admin
2021
from synapse.rest.client import directory, login, room
2122
from synapse.server import HomeServer
@@ -129,6 +130,39 @@ def test_deleting_alias_via_directory(self) -> None:
129130
)
130131
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
131132

133+
def test_deleting_alias_via_directory_appservice(self) -> None:
134+
user_id = "@as:test"
135+
as_token = "i_am_an_app_service"
136+
137+
appservice = ApplicationService(
138+
as_token,
139+
id="1234",
140+
namespaces={"aliases": [{"regex": "#asns-*", "exclusive": True}]},
141+
sender=user_id,
142+
)
143+
self.hs.get_datastores().main.services_cache.append(appservice)
144+
145+
# Add an alias for the room, as the appservice
146+
alias = RoomAlias(f"asns-{random_string(5)}", self.hs.hostname).to_string()
147+
data = {"room_id": self.room_id}
148+
request_data = json.dumps(data)
149+
150+
channel = self.make_request(
151+
"PUT",
152+
f"/_matrix/client/r0/directory/room/{alias}",
153+
request_data,
154+
access_token=as_token,
155+
)
156+
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
157+
158+
# Then try to remove the alias, as the appservice
159+
channel = self.make_request(
160+
"DELETE",
161+
f"/_matrix/client/r0/directory/room/{alias}",
162+
access_token=as_token,
163+
)
164+
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
165+
132166
def test_deleting_nonexistant_alias(self) -> None:
133167
# Check that no alias exists
134168
alias = "#potato:test"

0 commit comments

Comments
 (0)