2019-03-24 08:37:53 +01:00
|
|
|
var Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2017-03-21 17:45:10 +01:00
|
|
|
var typing_data = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
2017-11-08 17:55:36 +01:00
|
|
|
// See docs/subsystems/typing-indicators.md for details on typing indicators.
|
2017-09-25 20:33:29 +02:00
|
|
|
|
2017-03-21 17:45:10 +01:00
|
|
|
var typist_dct = new Dict();
|
2017-03-22 16:20:16 +01:00
|
|
|
var inbound_timer_dict = new Dict();
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
function to_int(s) {
|
|
|
|
return parseInt(s, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_key(group) {
|
2018-04-25 22:55:32 +02:00
|
|
|
var ids = util.sorted_ids(group);
|
2017-03-21 17:45:10 +01:00
|
|
|
return ids.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.add_typist = function (group, typist) {
|
|
|
|
var key = get_key(group);
|
|
|
|
var current = typist_dct.get(key) || [];
|
|
|
|
typist = to_int(typist);
|
|
|
|
if (!_.contains(current, typist)) {
|
|
|
|
current.push(typist);
|
|
|
|
}
|
2018-04-25 22:55:32 +02:00
|
|
|
typist_dct.set(key, util.sorted_ids(current));
|
2017-03-21 17:45:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.remove_typist = function (group, typist) {
|
|
|
|
var key = get_key(group);
|
|
|
|
var current = typist_dct.get(key) || [];
|
|
|
|
|
|
|
|
typist = to_int(typist);
|
|
|
|
if (!_.contains(current, typist)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = _.reject(current, function (user_id) {
|
|
|
|
return to_int(user_id) === to_int(typist);
|
|
|
|
});
|
|
|
|
|
|
|
|
typist_dct.set(key, current);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_group_typists = function (group) {
|
|
|
|
var key = get_key(group);
|
|
|
|
return typist_dct.get(key) || [];
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_all_typists = function () {
|
|
|
|
var typists = _.flatten(typist_dct.values(), true);
|
2018-04-25 22:55:32 +02:00
|
|
|
typists = util.sorted_ids(typists);
|
2017-03-21 17:45:10 +01:00
|
|
|
typists = _.uniq(typists, true);
|
|
|
|
return typists;
|
|
|
|
};
|
|
|
|
|
2017-03-22 16:20:16 +01:00
|
|
|
// The next functions aren't pure data, but it is easy
|
|
|
|
// enough to mock the setTimeout/clearTimeout functions.
|
|
|
|
exports.clear_inbound_timer = function (group) {
|
|
|
|
var key = get_key(group);
|
|
|
|
var timer = inbound_timer_dict.get(key);
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
inbound_timer_dict.set(key, undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.kickstart_inbound_timer = function (group, delay, callback) {
|
|
|
|
var key = get_key(group);
|
|
|
|
exports.clear_inbound_timer(group);
|
|
|
|
var timer = setTimeout(callback, delay);
|
|
|
|
inbound_timer_dict.set(key, timer);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-21 17:45:10 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = typing_data;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.typing_data = typing_data;
|