2017-02-02 18:45:43 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2017-02-02 18:45:43 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2017-02-02 18:45:43 +00:00
|
|
|
*/
|
|
|
|
|
2023-08-04 07:36:16 +00:00
|
|
|
import { Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
2020-10-13 16:37:06 +00:00
|
|
|
|
2017-05-25 10:39:08 +00:00
|
|
|
import { _t } from "./languageHandler";
|
2015-09-17 17:23:38 +00:00
|
|
|
|
2020-10-13 16:37:06 +00:00
|
|
|
export function usersTypingApartFromMeAndIgnored(room: Room): RoomMember[] {
|
2023-05-30 09:36:34 +00:00
|
|
|
return usersTyping(room, [room.client.getSafeUserId()].concat(room.client.getIgnoredUsers()));
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
2017-09-15 02:25:02 +00:00
|
|
|
|
2020-10-13 16:37:06 +00:00
|
|
|
export function usersTypingApartFromMe(room: Room): RoomMember[] {
|
2023-05-30 09:36:34 +00:00
|
|
|
return usersTyping(room, [room.client.getSafeUserId()]);
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
2015-09-17 17:23:38 +00:00
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
/**
|
|
|
|
* Given a Room object and, optionally, a list of userID strings
|
|
|
|
* to exclude, return a list of user objects who are typing.
|
|
|
|
* @param {Room} room: room object to get users from.
|
|
|
|
* @param {string[]} exclude: list of user mxids to exclude.
|
2020-10-13 16:37:06 +00:00
|
|
|
* @returns {RoomMember[]} list of user objects who are typing.
|
2019-12-20 00:45:24 +00:00
|
|
|
*/
|
2020-10-13 16:37:06 +00:00
|
|
|
export function usersTyping(room: Room, exclude: string[] = []): RoomMember[] {
|
2023-02-10 18:11:57 +00:00
|
|
|
const whoIsTyping: RoomMember[] = [];
|
2015-09-17 17:23:38 +00:00
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
const memberKeys = Object.keys(room.currentState.members);
|
2022-11-07 13:45:34 +00:00
|
|
|
for (const userId of memberKeys) {
|
2019-12-20 00:45:24 +00:00
|
|
|
if (room.currentState.members[userId].typing) {
|
|
|
|
if (exclude.indexOf(userId) === -1) {
|
|
|
|
whoIsTyping.push(room.currentState.members[userId]);
|
2015-09-17 17:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
2015-09-17 17:23:38 +00:00
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
return whoIsTyping;
|
|
|
|
}
|
2015-09-17 17:23:38 +00:00
|
|
|
|
2020-10-13 16:37:06 +00:00
|
|
|
export function whoIsTypingString(whoIsTyping: RoomMember[], limit: number): string {
|
2019-12-20 00:45:24 +00:00
|
|
|
let othersCount = 0;
|
|
|
|
if (whoIsTyping.length > limit) {
|
|
|
|
othersCount = whoIsTyping.length - limit + 1;
|
|
|
|
}
|
2020-10-13 16:37:06 +00:00
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
if (whoIsTyping.length === 0) {
|
|
|
|
return "";
|
|
|
|
} else if (whoIsTyping.length === 1) {
|
2023-09-05 09:44:41 +00:00
|
|
|
return _t("timeline|typing_indicator|one_user", { displayName: whoIsTyping[0].name });
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
2020-10-13 16:37:06 +00:00
|
|
|
|
|
|
|
const names = whoIsTyping.map((m) => m.name);
|
|
|
|
|
|
|
|
if (othersCount >= 1) {
|
2023-09-05 09:44:41 +00:00
|
|
|
return _t("timeline|typing_indicator|more_users", {
|
2019-12-20 00:45:24 +00:00
|
|
|
names: names.slice(0, limit - 1).join(", "),
|
|
|
|
count: othersCount,
|
2017-01-24 16:01:39 +00:00
|
|
|
});
|
2019-12-20 00:45:24 +00:00
|
|
|
} else {
|
|
|
|
const lastPerson = names.pop();
|
2023-09-05 09:44:41 +00:00
|
|
|
return _t("timeline|typing_indicator|two_users", { names: names.join(", "), lastPerson: lastPerson });
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
|
|
|
}
|