2015-09-18 01:23:38 +08:00
|
|
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
usersTypingApartFromMe: function(room) {
|
|
|
|
return this.usersTyping(
|
|
|
|
room, [MatrixClientPeg.get().credentials.userId]
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
var whoIsTyping = [];
|
|
|
|
|
|
|
|
if (exclude === undefined) {
|
|
|
|
exclude = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var memberKeys = Object.keys(room.currentState.members);
|
|
|
|
for (var i = 0; i < memberKeys.length; ++i) {
|
|
|
|
var userId = memberKeys[i];
|
|
|
|
|
|
|
|
if (room.currentState.members[userId].typing) {
|
|
|
|
if (exclude.indexOf(userId) == -1) {
|
|
|
|
whoIsTyping.push(room.currentState.members[userId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return whoIsTyping;
|
|
|
|
},
|
|
|
|
|
2017-01-25 00:01:39 +08:00
|
|
|
whoIsTypingString: function(room, limit) {
|
|
|
|
const whoIsTyping = this.usersTypingApartFromMe(room);
|
2017-01-25 01:18:56 +08:00
|
|
|
const othersCount = limit === undefined ?
|
|
|
|
0 : Math.max(whoIsTyping.length - limit, 0);
|
2015-09-18 01:23:38 +08:00
|
|
|
if (whoIsTyping.length == 0) {
|
2017-01-25 00:01:39 +08:00
|
|
|
return '';
|
2015-09-18 01:23:38 +08:00
|
|
|
} else if (whoIsTyping.length == 1) {
|
|
|
|
return whoIsTyping[0].name + ' is typing';
|
2017-01-25 00:01:39 +08:00
|
|
|
}
|
|
|
|
const names = whoIsTyping.map(function(m) {
|
|
|
|
return m.name;
|
|
|
|
});
|
|
|
|
if (othersCount) {
|
|
|
|
const other = ' other' + (othersCount > 1 ? 's' : '');
|
2017-01-25 01:18:56 +08:00
|
|
|
return names.slice(0, limit).join(', ') + ' and ' +
|
|
|
|
othersCount + other + ' are typing';
|
2015-09-18 01:23:38 +08:00
|
|
|
} else {
|
2017-01-25 00:01:39 +08:00
|
|
|
const lastPerson = names.pop();
|
2015-09-18 01:23:38 +08:00
|
|
|
return names.join(', ') + ' and ' + lastPerson + ' are typing';
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 22:22:27 +08:00
|
|
|
};
|