2016-04-21 14:19:07 -04: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 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]
|
2016-04-21 14:19:07 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2016-05-23 17:55:11 -04:00
|
|
|
import logging
|
2018-07-09 02:09:20 -04:00
|
|
|
import re
|
2020-12-11 11:43:53 -05:00
|
|
|
from typing import TYPE_CHECKING, Dict, Iterable, Optional
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2021-01-25 07:27:16 -05:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
2020-12-11 11:43:53 -05:00
|
|
|
from synapse.events import EventBase
|
|
|
|
from synapse.types import StateMap
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.storage.databases.main import DataStore
|
2020-02-21 12:50:48 -05:00
|
|
|
|
2016-05-23 17:55:11 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-21 14:19:07 -04:00
|
|
|
|
|
|
|
# intentionally looser than what aliases we allow to be registered since
|
|
|
|
# other HSes may allow aliases that we would not
|
|
|
|
ALIAS_RE = re.compile(r"^#.*:.+$")
|
|
|
|
|
|
|
|
ALL_ALONE = "Empty Room"
|
|
|
|
|
|
|
|
|
2020-07-27 12:21:34 -04:00
|
|
|
async def calculate_room_name(
|
2020-12-11 11:43:53 -05:00
|
|
|
store: "DataStore",
|
|
|
|
room_state_ids: StateMap[str],
|
|
|
|
user_id: str,
|
|
|
|
fallback_to_members: bool = True,
|
|
|
|
fallback_to_single_member: bool = True,
|
|
|
|
) -> Optional[str]:
|
2016-04-29 14:47:35 -04:00
|
|
|
"""
|
|
|
|
Works out a user-facing name for the given room as per Matrix
|
|
|
|
spec recommendations.
|
|
|
|
Does not yet support internationalisation.
|
|
|
|
Args:
|
2020-12-11 11:43:53 -05:00
|
|
|
store: The data store to query.
|
|
|
|
room_state_ids: Dictionary of the room's state IDs.
|
2016-04-29 14:47:35 -04:00
|
|
|
user_id: The ID of the user to whom the room name is being presented
|
|
|
|
fallback_to_members: If False, return None instead of generating a name
|
|
|
|
based on the room's members if the room has no
|
|
|
|
title or aliases.
|
2020-12-11 11:43:53 -05:00
|
|
|
fallback_to_single_member: If False, return None instead of generating a
|
|
|
|
name based on the user who invited this user to the room if the room
|
|
|
|
has no title or aliases.
|
2016-04-29 14:47:35 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-12-11 11:43:53 -05:00
|
|
|
A human readable name for the room, if possible.
|
2016-04-29 14:47:35 -04:00
|
|
|
"""
|
2016-04-21 14:19:07 -04:00
|
|
|
# does it have a name?
|
2020-02-21 12:50:48 -05:00
|
|
|
if (EventTypes.Name, "") in room_state_ids:
|
2020-07-27 12:21:34 -04:00
|
|
|
m_room_name = await store.get_event(
|
2020-02-21 12:50:48 -05:00
|
|
|
room_state_ids[(EventTypes.Name, "")], allow_none=True
|
2016-08-25 13:32:15 -04:00
|
|
|
)
|
2021-01-25 07:27:16 -05:00
|
|
|
if m_room_name and m_room_name.content and m_room_name.content.get("name"):
|
2019-07-23 09:00:55 -04:00
|
|
|
return m_room_name.content["name"]
|
2016-04-21 14:19:07 -04:00
|
|
|
|
2016-04-25 09:42:59 -04:00
|
|
|
# does it have a canonical alias?
|
2020-02-21 12:50:48 -05:00
|
|
|
if (EventTypes.CanonicalAlias, "") in room_state_ids:
|
2020-07-27 12:21:34 -04:00
|
|
|
canon_alias = await store.get_event(
|
2020-02-21 12:50:48 -05:00
|
|
|
room_state_ids[(EventTypes.CanonicalAlias, "")], allow_none=True
|
2016-08-25 13:32:15 -04:00
|
|
|
)
|
2016-04-21 14:19:07 -04:00
|
|
|
if (
|
2019-06-20 05:32:02 -04:00
|
|
|
canon_alias
|
|
|
|
and canon_alias.content
|
2021-01-25 07:27:16 -05:00
|
|
|
and canon_alias.content.get("alias")
|
2019-06-20 05:32:02 -04:00
|
|
|
and _looks_like_an_alias(canon_alias.content["alias"])
|
2016-04-21 14:19:07 -04:00
|
|
|
):
|
2019-07-23 09:00:55 -04:00
|
|
|
return canon_alias.content["alias"]
|
2016-04-21 14:19:07 -04:00
|
|
|
|
2016-04-28 06:49:36 -04:00
|
|
|
if not fallback_to_members:
|
2019-07-23 09:00:55 -04:00
|
|
|
return None
|
2016-04-28 06:49:36 -04:00
|
|
|
|
2016-04-21 14:19:07 -04:00
|
|
|
my_member_event = None
|
2020-02-21 12:50:48 -05:00
|
|
|
if (EventTypes.Member, user_id) in room_state_ids:
|
2020-07-27 12:21:34 -04:00
|
|
|
my_member_event = await store.get_event(
|
2020-02-21 12:50:48 -05:00
|
|
|
room_state_ids[(EventTypes.Member, user_id)], allow_none=True
|
2016-08-25 13:32:15 -04:00
|
|
|
)
|
2016-04-21 14:19:07 -04:00
|
|
|
|
|
|
|
if (
|
2019-06-20 05:32:02 -04:00
|
|
|
my_member_event is not None
|
2021-01-25 07:27:16 -05:00
|
|
|
and my_member_event.content.get("membership") == Membership.INVITE
|
2016-04-21 14:19:07 -04:00
|
|
|
):
|
2020-02-21 12:50:48 -05:00
|
|
|
if (EventTypes.Member, my_member_event.sender) in room_state_ids:
|
2020-07-27 12:21:34 -04:00
|
|
|
inviter_member_event = await store.get_event(
|
2020-02-21 12:50:48 -05:00
|
|
|
room_state_ids[(EventTypes.Member, my_member_event.sender)],
|
2016-08-25 13:32:15 -04:00
|
|
|
allow_none=True,
|
|
|
|
)
|
|
|
|
if inviter_member_event:
|
|
|
|
if fallback_to_single_member:
|
2019-07-23 09:00:55 -04:00
|
|
|
return "Invite from %s" % (
|
|
|
|
name_from_member_event(inviter_member_event),
|
2016-08-25 13:32:15 -04:00
|
|
|
)
|
|
|
|
else:
|
2020-12-11 11:43:53 -05:00
|
|
|
return None
|
2016-04-21 14:19:07 -04:00
|
|
|
else:
|
2019-07-23 09:00:55 -04:00
|
|
|
return "Room Invite"
|
2016-04-21 14:19:07 -04:00
|
|
|
|
2021-01-25 07:27:16 -05:00
|
|
|
# at this point we're going to need to search the state by all state keys
|
|
|
|
# for an event type, so rearrange the data structure
|
|
|
|
room_state_bytype_ids = _state_as_two_level_dict(room_state_ids)
|
|
|
|
|
2016-04-21 14:19:07 -04:00
|
|
|
# we're going to have to generate a name based on who's in the room,
|
|
|
|
# so find out who is in the room that isn't the user.
|
2020-02-21 12:50:48 -05:00
|
|
|
if EventTypes.Member in room_state_bytype_ids:
|
2020-07-27 12:21:34 -04:00
|
|
|
member_events = await store.get_events(
|
2020-02-21 12:50:48 -05:00
|
|
|
list(room_state_bytype_ids[EventTypes.Member].values())
|
2016-08-25 13:32:15 -04:00
|
|
|
)
|
2016-04-21 14:19:07 -04:00
|
|
|
all_members = [
|
2019-06-20 05:32:02 -04:00
|
|
|
ev
|
|
|
|
for ev in member_events.values()
|
2021-01-25 07:27:16 -05:00
|
|
|
if ev.content.get("membership") == Membership.JOIN
|
|
|
|
or ev.content.get("membership") == Membership.INVITE
|
2016-04-21 14:19:07 -04:00
|
|
|
]
|
2016-04-22 13:33:36 -04:00
|
|
|
# Sort the member events oldest-first so the we name people in the
|
|
|
|
# order the joined (it should at least be deterministic rather than
|
|
|
|
# dictionary iteration order)
|
|
|
|
all_members.sort(key=lambda e: e.origin_server_ts)
|
2016-04-22 12:28:42 -04:00
|
|
|
other_members = [m for m in all_members if m.state_key != user_id]
|
2016-04-21 14:19:07 -04:00
|
|
|
else:
|
|
|
|
other_members = []
|
|
|
|
all_members = []
|
|
|
|
|
|
|
|
if len(other_members) == 0:
|
|
|
|
if len(all_members) == 1:
|
|
|
|
# self-chat, peeked room with 1 participant,
|
|
|
|
# or inbound invite, or outbound 3PID invite.
|
|
|
|
if all_members[0].sender == user_id:
|
2020-02-21 12:50:48 -05:00
|
|
|
if EventTypes.ThirdPartyInvite in room_state_bytype_ids:
|
2019-06-20 05:32:02 -04:00
|
|
|
third_party_invites = room_state_bytype_ids[
|
2020-02-21 12:50:48 -05:00
|
|
|
EventTypes.ThirdPartyInvite
|
2019-06-20 05:32:02 -04:00
|
|
|
].values()
|
2016-05-23 17:55:11 -04:00
|
|
|
|
2016-04-21 14:19:07 -04:00
|
|
|
if len(third_party_invites) > 0:
|
|
|
|
# technically third party invite events are not member
|
|
|
|
# events, but they are close enough
|
2016-05-23 17:55:11 -04:00
|
|
|
|
|
|
|
# FIXME: no they're not - they look nothing like a member;
|
|
|
|
# they have a great big encrypted thing as their name to
|
|
|
|
# prevent leaking the 3PID name...
|
|
|
|
# return "Inviting %s" % (
|
|
|
|
# descriptor_from_member_events(third_party_invites)
|
|
|
|
# )
|
2019-07-23 09:00:55 -04:00
|
|
|
return "Inviting email address"
|
2016-04-21 14:19:07 -04:00
|
|
|
else:
|
2019-07-23 09:00:55 -04:00
|
|
|
return ALL_ALONE
|
2016-04-21 14:19:07 -04:00
|
|
|
else:
|
2019-07-23 09:00:55 -04:00
|
|
|
return name_from_member_event(all_members[0])
|
2016-04-21 14:19:07 -04:00
|
|
|
else:
|
2019-07-23 09:00:55 -04:00
|
|
|
return ALL_ALONE
|
2016-06-24 06:41:11 -04:00
|
|
|
elif len(other_members) == 1 and not fallback_to_single_member:
|
2020-12-11 11:43:53 -05:00
|
|
|
return None
|
|
|
|
|
|
|
|
return descriptor_from_member_events(other_members)
|
2016-04-21 14:19:07 -04:00
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def descriptor_from_member_events(member_events: Iterable[EventBase]) -> str:
|
2019-06-07 05:57:39 -04:00
|
|
|
"""Get a description of the room based on the member events.
|
|
|
|
|
|
|
|
Args:
|
2020-12-11 11:43:53 -05:00
|
|
|
member_events: The events of a room.
|
2019-06-07 05:57:39 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-12-11 11:43:53 -05:00
|
|
|
The room description
|
2019-06-07 05:57:39 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
member_events = list(member_events)
|
|
|
|
|
2016-04-21 14:19:07 -04:00
|
|
|
if len(member_events) == 0:
|
|
|
|
return "nobody"
|
|
|
|
elif len(member_events) == 1:
|
|
|
|
return name_from_member_event(member_events[0])
|
2016-04-22 12:28:42 -04:00
|
|
|
elif len(member_events) == 2:
|
|
|
|
return "%s and %s" % (
|
|
|
|
name_from_member_event(member_events[0]),
|
|
|
|
name_from_member_event(member_events[1]),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return "%s and %d others" % (
|
|
|
|
name_from_member_event(member_events[0]),
|
|
|
|
len(member_events) - 1,
|
|
|
|
)
|
2016-04-21 14:19:07 -04:00
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def name_from_member_event(member_event: EventBase) -> str:
|
2021-01-25 07:27:16 -05:00
|
|
|
if member_event.content and member_event.content.get("displayname"):
|
2016-04-21 14:19:07 -04:00
|
|
|
return member_event.content["displayname"]
|
2016-04-25 13:27:04 -04:00
|
|
|
return member_event.state_key
|
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def _state_as_two_level_dict(state: StateMap[str]) -> Dict[str, Dict[str, str]]:
|
2021-07-15 06:02:43 -04:00
|
|
|
ret: Dict[str, Dict[str, str]] = {}
|
2016-04-25 13:27:04 -04:00
|
|
|
for k, v in state.items():
|
|
|
|
ret.setdefault(k[0], {})[k[1]] = v
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def _looks_like_an_alias(string: str) -> bool:
|
2016-04-29 05:07:30 -04:00
|
|
|
return ALIAS_RE.match(string) is not None
|