2020-02-13 22:34:29 +01:00
|
|
|
const util = require("./util");
|
2019-11-02 00:06:25 +01:00
|
|
|
const Dict = require('./dict').Dict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const typist_dct = new Dict();
|
|
|
|
const 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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const ids = util.sorted_ids(group);
|
2017-03-21 17:45:10 +01:00
|
|
|
return ids.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.add_typist = function (group, typist) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = get_key(group);
|
|
|
|
const current = typist_dct.get(key) || [];
|
2017-03-21 17:45:10 +01:00
|
|
|
typist = to_int(typist);
|
2020-02-08 04:04:36 +01:00
|
|
|
if (!current.includes(typist)) {
|
2017-03-21 17:45:10 +01:00
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = get_key(group);
|
|
|
|
let current = typist_dct.get(key) || [];
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
typist = to_int(typist);
|
2020-02-08 04:04:36 +01:00
|
|
|
if (!current.includes(typist)) {
|
2017-03-21 17:45:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-08 03:51:18 +01:00
|
|
|
current = current.filter(user_id => to_int(user_id) !== to_int(typist));
|
2017-03-21 17:45:10 +01:00
|
|
|
|
|
|
|
typist_dct.set(key, current);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_group_typists = function (group) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = get_key(group);
|
2017-03-21 17:45:10 +01:00
|
|
|
return typist_dct.get(key) || [];
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_all_typists = function () {
|
2020-02-09 06:07:48 +01:00
|
|
|
let typists = [].concat(...Array.from(typist_dct.values()));
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = get_key(group);
|
|
|
|
const timer = inbound_timer_dict.get(key);
|
2017-03-22 16:20:16 +01:00
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
inbound_timer_dict.set(key, undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.kickstart_inbound_timer = function (group, delay, callback) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = get_key(group);
|
2017-03-22 16:20:16 +01:00
|
|
|
exports.clear_inbound_timer(group);
|
2019-11-02 00:06:25 +01:00
|
|
|
const timer = setTimeout(callback, delay);
|
2017-03-22 16:20:16 +01:00
|
|
|
inbound_timer_dict.set(key, timer);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.typing_data = exports;
|