2017-02-02 18:45:43 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations 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.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2020-10-13 16:37:06 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "./MatrixClientPeg";
|
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[] {
|
|
|
|
return usersTyping(room, [MatrixClientPeg.get().getUserId()].concat(MatrixClientPeg.get().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[] {
|
|
|
|
return usersTyping(room, [MatrixClientPeg.get().getUserId()]);
|
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[] {
|
2019-12-20 00:45:24 +00:00
|
|
|
const whoIsTyping = [];
|
2015-09-17 17:23:38 +00:00
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
const memberKeys = Object.keys(room.currentState.members);
|
|
|
|
for (let i = 0; i < memberKeys.length; ++i) {
|
|
|
|
const userId = memberKeys[i];
|
2015-09-17 17:23:38 +00:00
|
|
|
|
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) {
|
2021-06-29 12:11:58 +00:00
|
|
|
return _t('%(displayName)s is typing …', { 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) {
|
2019-12-20 00:45:24 +00:00
|
|
|
return _t('%(names)s and %(count)s others are typing …', {
|
|
|
|
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();
|
2021-06-29 12:11:58 +00:00
|
|
|
return _t('%(names)s and %(lastPerson)s are typing …', { names: names.join(', '), lastPerson: lastPerson });
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
|
|
|
}
|