Skip to content

Commit cdcc3d0

Browse files
committed
chore: use string user_id in Restrictions object
1 parent c543999 commit cdcc3d0

5 files changed

Lines changed: 13 additions & 21 deletions

File tree

synapse/media/media_repository.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,7 @@ async def is_media_visible(
319319
Verify that media requested for download should be visible to the user making
320320
the request
321321
"""
322-
# Handle both string and UserID types for requester.user
323-
if isinstance(requester.user, UserID):
324-
requester_user_id_str = requester.user.to_string()
325-
else:
326-
requester_user_id_str = str(requester.user) # type: ignore
322+
requester_user_id_str = requester.user.to_string()
327323
if not self.enable_media_restriction:
328324
return
329325

@@ -349,12 +345,8 @@ async def is_media_visible(
349345
f"Media requested ('{media_info_object.media_id}') is restricted"
350346
)
351347

352-
attachments = media_info_object.attachments.to_dict().get(
353-
"org.matrix.msc3911.restrictions", {}
354-
)
355-
# Safely extract values from the nested structure, handling missing keys
356-
attached_event_id = attachments.get("event_id")
357-
attached_profile_user_id = attachments.get("profile_user_id")
348+
attached_event_id = media_info_object.attachments.event_id
349+
attached_profile_user_id = media_info_object.attachments.profile_user_id
358350

359351
if attached_event_id:
360352
# Check if event is redacted or not. If it is redacted, normal user no

synapse/storage/databases/main/media_repository.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ class MediaRestrictions:
7474
"""
7575

7676
event_id: Optional[str] = None
77-
profile_user_id: Optional[UserID] = None
77+
profile_user_id: Optional[str] = None
7878

7979
def to_dict(self) -> dict:
8080
if self.event_id:
81-
return {"org.matrix.msc3911.restrictions": {"event_id": str(self.event_id)}}
81+
return {"org.matrix.msc3911.restrictions": {"event_id": self.event_id}}
8282
if self.profile_user_id:
8383
return {
8484
"org.matrix.msc3911.restrictions": {
85-
"profile_user_id": self.profile_user_id.to_string()
85+
"profile_user_id": self.profile_user_id
8686
}
8787
}
8888
return {}
@@ -1172,7 +1172,7 @@ async def get_media_restrictions(
11721172
if row:
11731173
event_id = row[0][0]
11741174
# Because the UserID object can be None, the 'to_string()' method may not exist
1175-
profile_user_id = UserID.from_string(row[0][1]) if row[0][1] else None
1175+
profile_user_id = row[0][1] if row[0][1] else None
11761176
return MediaRestrictions(event_id=event_id, profile_user_id=profile_user_id)
11771177

11781178
return None

tests/rest/client/test_media.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4594,7 +4594,7 @@ def test_downloading_remote_media_with_restrictions_is_in_database(self) -> None
45944594
)
45954595
assert isinstance(restrictions, MediaRestrictions)
45964596
assert restrictions.profile_user_id is not None
4597-
assert restrictions.profile_user_id.to_string() == "@bob:example.com"
4597+
assert restrictions.profile_user_id == "@bob:example.com"
45984598

45994599
def test_downloading_remote_media_with_no_restrictions_does_not_save_to_db(
46004600
self,

tests/rest/client/test_profile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ def test_can_attach_media_to_profile_update(self) -> None:
10131013
)
10141014
assert restrictions is not None, str(restrictions)
10151015
assert restrictions.event_id is None
1016-
assert restrictions.profile_user_id == UserID.from_string(self.user)
1016+
assert restrictions.profile_user_id == self.user
10171017

10181018
def test_attaching_nonexistent_local_media_to_profile_fails(self) -> None:
10191019
"""
@@ -1274,7 +1274,7 @@ def test_profile_update_with_media_is_copied_and_attached_to_member_events(
12741274
media_info = self.get_success(self.store.get_local_media(mxc_uri.media_id))
12751275
assert media_info is not None
12761276
assert media_info.attachments is not None
1277-
assert media_info.attachments.profile_user_id == UserID.from_string(self.user)
1277+
assert media_info.attachments.profile_user_id == self.user
12781278

12791279
# Check the media was copied and attached to a member event
12801280
events = self.get_success(
@@ -1457,7 +1457,7 @@ def test_profile_update_with_media_is_copied_and_attached_to_member_events(
14571457
media_info = self.get_success(self.store.get_local_media(mxc_uri.media_id))
14581458
assert media_info is not None
14591459
assert media_info.attachments is not None
1460-
assert media_info.attachments.profile_user_id == UserID.from_string(self.user)
1460+
assert media_info.attachments.profile_user_id == self.user
14611461

14621462
# Check the media was copied and attached to a member event
14631463
events = self.get_success(

tests/storage/test_media.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def test_store_and_retrieve_media_restrictions_by_event_id(self) -> None:
4545
assert retrieved_restrictions.profile_user_id is None
4646

4747
def test_store_and_retrieve_media_restrictions_by_profile_user_id(self) -> None:
48-
user_id = UserID.from_string("@frank:test")
48+
user_id = "@frank:test"
4949
media_id = random_string(24)
5050
self.get_success_or_raise(
5151
self.store.set_media_restricted_to_user_profile(
52-
self.server_name, media_id, user_id.to_string()
52+
self.server_name, media_id, user_id
5353
)
5454
)
5555

0 commit comments

Comments
 (0)