2016-04-21 12:19:07 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-08-25 11:32:15 -06:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2016-04-21 12:19:07 -06:00
|
|
|
import re
|
2016-05-23 15:55:11 -06:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2016-04-21 12:19:07 -06: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"
|
|
|
|
|
|
|
|
|
2016-08-25 11:32:15 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def calculate_room_name(store, room_state_ids, user_id, fallback_to_members=True,
|
2016-06-24 04:41:11 -06:00
|
|
|
fallback_to_single_member=True):
|
2016-04-29 12:47:35 -06:00
|
|
|
"""
|
|
|
|
Works out a user-facing name for the given room as per Matrix
|
|
|
|
spec recommendations.
|
|
|
|
Does not yet support internationalisation.
|
|
|
|
Args:
|
|
|
|
room_state: Dictionary of the room's state
|
|
|
|
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.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
(string or None) A human readable name for the room.
|
|
|
|
"""
|
2016-04-21 12:19:07 -06:00
|
|
|
# does it have a name?
|
2016-08-25 11:32:15 -06:00
|
|
|
if ("m.room.name", "") in room_state_ids:
|
|
|
|
m_room_name = yield store.get_event(
|
|
|
|
room_state_ids[("m.room.name", "")], allow_none=True
|
|
|
|
)
|
|
|
|
if m_room_name and m_room_name.content and m_room_name.content["name"]:
|
|
|
|
defer.returnValue(m_room_name.content["name"])
|
2016-04-21 12:19:07 -06:00
|
|
|
|
2016-04-25 07:42:59 -06:00
|
|
|
# does it have a canonical alias?
|
2016-08-25 11:32:15 -06:00
|
|
|
if ("m.room.canonical_alias", "") in room_state_ids:
|
|
|
|
canon_alias = yield store.get_event(
|
|
|
|
room_state_ids[("m.room.canonical_alias", "")], allow_none=True
|
|
|
|
)
|
2016-04-21 12:19:07 -06:00
|
|
|
if (
|
2016-08-25 11:32:15 -06:00
|
|
|
canon_alias and canon_alias.content and canon_alias.content["alias"] and
|
2016-04-25 11:27:04 -06:00
|
|
|
_looks_like_an_alias(canon_alias.content["alias"])
|
2016-04-21 12:19:07 -06:00
|
|
|
):
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(canon_alias.content["alias"])
|
2016-04-21 12:19:07 -06: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
|
2016-08-25 11:32:15 -06:00
|
|
|
room_state_bytype_ids = _state_as_two_level_dict(room_state_ids)
|
2016-04-21 12:19:07 -06:00
|
|
|
|
|
|
|
# right then, any aliases at all?
|
2016-08-25 11:32:15 -06:00
|
|
|
if "m.room.aliases" in room_state_bytype_ids:
|
|
|
|
m_room_aliases = room_state_bytype_ids["m.room.aliases"]
|
|
|
|
for alias_id in m_room_aliases.values():
|
|
|
|
alias_event = yield store.get_event(
|
|
|
|
alias_id, allow_none=True
|
|
|
|
)
|
2016-08-30 03:54:30 -06:00
|
|
|
if alias_event and alias_event.content.get("aliases"):
|
2016-08-25 11:32:15 -06:00
|
|
|
the_aliases = alias_event.content["aliases"]
|
2016-04-25 11:27:04 -06:00
|
|
|
if len(the_aliases) > 0 and _looks_like_an_alias(the_aliases[0]):
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(the_aliases[0])
|
2016-04-21 12:19:07 -06:00
|
|
|
|
2016-04-28 04:49:36 -06:00
|
|
|
if not fallback_to_members:
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(None)
|
2016-04-28 04:49:36 -06:00
|
|
|
|
2016-04-21 12:19:07 -06:00
|
|
|
my_member_event = None
|
2016-08-25 11:32:15 -06:00
|
|
|
if ("m.room.member", user_id) in room_state_ids:
|
|
|
|
my_member_event = yield store.get_event(
|
|
|
|
room_state_ids[("m.room.member", user_id)], allow_none=True
|
|
|
|
)
|
2016-04-21 12:19:07 -06:00
|
|
|
|
|
|
|
if (
|
|
|
|
my_member_event is not None and
|
|
|
|
my_member_event.content['membership'] == "invite"
|
|
|
|
):
|
2016-08-25 11:32:15 -06:00
|
|
|
if ("m.room.member", my_member_event.sender) in room_state_ids:
|
|
|
|
inviter_member_event = yield store.get_event(
|
|
|
|
room_state_ids[("m.room.member", my_member_event.sender)],
|
|
|
|
allow_none=True,
|
|
|
|
)
|
|
|
|
if inviter_member_event:
|
|
|
|
if fallback_to_single_member:
|
|
|
|
defer.returnValue(
|
|
|
|
"Invite from %s" % (
|
|
|
|
name_from_member_event(inviter_member_event),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return
|
2016-04-21 12:19:07 -06:00
|
|
|
else:
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue("Room Invite")
|
2016-04-21 12:19:07 -06: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.
|
2016-08-25 11:32:15 -06:00
|
|
|
if "m.room.member" in room_state_bytype_ids:
|
|
|
|
member_events = yield store.get_events(
|
2018-05-31 03:03:47 -06:00
|
|
|
list(room_state_bytype_ids["m.room.member"].values())
|
2016-08-25 11:32:15 -06:00
|
|
|
)
|
2016-04-21 12:19:07 -06:00
|
|
|
all_members = [
|
2016-08-25 11:32:15 -06:00
|
|
|
ev for ev in member_events.values()
|
2016-04-22 10:28:42 -06:00
|
|
|
if ev.content['membership'] == "join" or ev.content['membership'] == "invite"
|
2016-04-21 12:19:07 -06:00
|
|
|
]
|
2016-04-22 11:33:36 -06: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 10:28:42 -06:00
|
|
|
other_members = [m for m in all_members if m.state_key != user_id]
|
2016-04-21 12:19:07 -06: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:
|
2016-08-25 11:32:15 -06:00
|
|
|
if "m.room.third_party_invite" in room_state_bytype_ids:
|
2016-05-23 15:55:11 -06:00
|
|
|
third_party_invites = (
|
2016-08-25 11:32:15 -06:00
|
|
|
room_state_bytype_ids["m.room.third_party_invite"].values()
|
2016-05-23 15:55:11 -06:00
|
|
|
)
|
|
|
|
|
2016-04-21 12:19:07 -06:00
|
|
|
if len(third_party_invites) > 0:
|
|
|
|
# technically third party invite events are not member
|
|
|
|
# events, but they are close enough
|
2016-05-23 15:55:11 -06: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)
|
|
|
|
# )
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue("Inviting email address")
|
2016-04-21 12:19:07 -06:00
|
|
|
else:
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(ALL_ALONE)
|
2016-04-21 12:19:07 -06:00
|
|
|
else:
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(name_from_member_event(all_members[0]))
|
2016-04-21 12:19:07 -06:00
|
|
|
else:
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(ALL_ALONE)
|
2016-06-24 04:41:11 -06:00
|
|
|
elif len(other_members) == 1 and not fallback_to_single_member:
|
2016-08-25 11:32:15 -06:00
|
|
|
return
|
2016-04-21 12:19:07 -06:00
|
|
|
else:
|
2016-08-25 11:32:15 -06:00
|
|
|
defer.returnValue(descriptor_from_member_events(other_members))
|
2016-04-21 12:19:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
def descriptor_from_member_events(member_events):
|
|
|
|
if len(member_events) == 0:
|
|
|
|
return "nobody"
|
|
|
|
elif len(member_events) == 1:
|
|
|
|
return name_from_member_event(member_events[0])
|
2016-04-22 10:28:42 -06: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 12:19:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
def name_from_member_event(member_event):
|
|
|
|
if (
|
|
|
|
member_event.content and "displayname" in member_event.content and
|
|
|
|
member_event.content["displayname"]
|
|
|
|
):
|
|
|
|
return member_event.content["displayname"]
|
2016-04-25 11:27:04 -06:00
|
|
|
return member_event.state_key
|
|
|
|
|
|
|
|
|
|
|
|
def _state_as_two_level_dict(state):
|
|
|
|
ret = {}
|
|
|
|
for k, v in state.items():
|
|
|
|
ret.setdefault(k[0], {})[k[1]] = v
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def _looks_like_an_alias(string):
|
2016-04-29 03:07:30 -06:00
|
|
|
return ALIAS_RE.match(string) is not None
|