2022-03-24 21:33:22 +00:00
|
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
|
|
|
|
|
import SpaceStore from "../stores/spaces/SpaceStore";
|
|
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
|
import DMRoomMap from "./DMRoomMap";
|
|
|
|
|
|
2022-07-11 11:34:23 +00:00
|
|
|
|
export interface RoomContextDetails {
|
2023-02-13 17:01:43 +00:00
|
|
|
|
details: string | null;
|
2022-07-11 11:34:23 +00:00
|
|
|
|
ariaLabel?: string;
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:34:23 +00:00
|
|
|
|
export function roomContextDetails(room: Room): RoomContextDetails | null {
|
2022-03-24 21:33:22 +00:00
|
|
|
|
const dmPartner = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
|
2022-06-30 09:44:56 +00:00
|
|
|
|
// if we’ve got more than 2 users, don’t treat it like a regular DM
|
|
|
|
|
const isGroupDm = room.getMembers().length > 2;
|
2022-07-11 11:34:23 +00:00
|
|
|
|
if (!room.isSpaceRoom() && dmPartner && !isGroupDm) {
|
|
|
|
|
return { details: dmPartner };
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [parent, secondParent, ...otherParents] = SpaceStore.instance.getKnownParents(room.roomId);
|
|
|
|
|
if (secondParent && !otherParents?.length) {
|
|
|
|
|
// exactly 2 edge case for improved i18n
|
2022-07-11 11:34:23 +00:00
|
|
|
|
const space1Name = room.client.getRoom(parent)?.name;
|
|
|
|
|
const space2Name = room.client.getRoom(secondParent)?.name;
|
|
|
|
|
return {
|
|
|
|
|
details: _t("%(space1Name)s and %(space2Name)s", { space1Name, space2Name }),
|
|
|
|
|
ariaLabel: _t("In spaces %(space1Name)s and %(space2Name)s.", { space1Name, space2Name }),
|
|
|
|
|
};
|
2022-03-24 21:33:22 +00:00
|
|
|
|
} else if (parent) {
|
2022-07-11 11:34:23 +00:00
|
|
|
|
const spaceName = room.client.getRoom(parent)?.name;
|
|
|
|
|
const count = otherParents.length;
|
|
|
|
|
return {
|
|
|
|
|
details: _t("%(spaceName)s and %(count)s others", { spaceName, count }),
|
|
|
|
|
ariaLabel: _t("In %(spaceName)s and %(count)s other spaces.", { spaceName, count }),
|
|
|
|
|
};
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 11:34:23 +00:00
|
|
|
|
return { details: room.getCanonicalAlias() };
|
2022-03-24 21:33:22 +00:00
|
|
|
|
}
|