2017-02-02 21:45:43 +03: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.
|
|
|
|
*/
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const MatrixClientPeg = require("./MatrixClientPeg");
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from './languageHandler';
|
2015-09-17 20:23:38 +03:00
|
|
|
|
|
|
|
module.exports = {
|
2017-09-15 05:25:02 +03:00
|
|
|
usersTypingApartFromMeAndIgnored: function(room) {
|
|
|
|
return this.usersTyping(
|
2017-10-11 19:56:17 +03:00
|
|
|
room, [MatrixClientPeg.get().credentials.userId].concat(MatrixClientPeg.get().getIgnoredUsers()),
|
2017-09-15 05:25:02 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-09-17 20:23:38 +03:00
|
|
|
usersTypingApartFromMe: function(room) {
|
|
|
|
return this.usersTyping(
|
2017-10-11 19:56:17 +03:00
|
|
|
room, [MatrixClientPeg.get().credentials.userId],
|
2015-09-17 20:23:38 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a Room object and, optionally, a list of userID strings
|
|
|
|
* to exclude, return a list of user objects who are typing.
|
|
|
|
*/
|
|
|
|
usersTyping: function(room, exclude) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const whoIsTyping = [];
|
2015-09-17 20:23:38 +03:00
|
|
|
|
|
|
|
if (exclude === undefined) {
|
|
|
|
exclude = [];
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const memberKeys = Object.keys(room.currentState.members);
|
|
|
|
for (let i = 0; i < memberKeys.length; ++i) {
|
|
|
|
const userId = memberKeys[i];
|
2015-09-17 20:23:38 +03:00
|
|
|
|
|
|
|
if (room.currentState.members[userId].typing) {
|
|
|
|
if (exclude.indexOf(userId) == -1) {
|
|
|
|
whoIsTyping.push(room.currentState.members[userId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return whoIsTyping;
|
|
|
|
},
|
|
|
|
|
2017-02-09 13:01:51 +03:00
|
|
|
whoIsTypingString: function(whoIsTyping, limit) {
|
2017-02-09 13:30:06 +03:00
|
|
|
let othersCount = 0;
|
|
|
|
if (whoIsTyping.length > limit) {
|
|
|
|
othersCount = whoIsTyping.length - limit + 1;
|
|
|
|
}
|
2015-09-17 20:23:38 +03:00
|
|
|
if (whoIsTyping.length == 0) {
|
2017-01-24 19:01:39 +03:00
|
|
|
return '';
|
2015-09-17 20:23:38 +03:00
|
|
|
} else if (whoIsTyping.length == 1) {
|
2018-11-14 12:06:08 +03:00
|
|
|
return _t('%(displayName)s is typing …', {displayName: whoIsTyping[0].name});
|
2017-01-24 19:01:39 +03:00
|
|
|
}
|
|
|
|
const names = whoIsTyping.map(function(m) {
|
|
|
|
return m.name;
|
|
|
|
});
|
2017-10-24 19:32:50 +03:00
|
|
|
if (othersCount>=1) {
|
2018-11-14 12:06:08 +03:00
|
|
|
return _t('%(names)s and %(count)s others are typing …', {names: names.slice(0, limit - 1).join(', '), count: othersCount});
|
2015-09-17 20:23:38 +03:00
|
|
|
} else {
|
2017-01-24 19:01:39 +03:00
|
|
|
const lastPerson = names.pop();
|
2018-11-14 12:06:08 +03:00
|
|
|
return _t('%(names)s and %(lastPerson)s are typing …', {names: names.join(', '), lastPerson: lastPerson});
|
2015-09-17 20:23:38 +03:00
|
|
|
}
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|