2021-02-10 17:08:23 +01:00
|
|
|
import _ from "lodash";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-10 17:08:23 +01:00
|
|
|
import * as util from "./util";
|
2020-07-25 02:02:35 +02: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
|
|
|
|
2020-02-03 09:39:58 +01:00
|
|
|
const typist_dct = new Map();
|
|
|
|
const inbound_timer_dict = new Map();
|
2017-03-21 17:45:10 +01:00
|
|
|
|
2021-03-15 16:17:37 +01:00
|
|
|
export function clear_for_testing() {
|
|
|
|
typist_dct.clear();
|
|
|
|
inbound_timer_dict.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-21 17:45:10 +01:00
|
|
|
function to_int(s) {
|
2020-10-07 09:17:30 +02:00
|
|
|
return Number.parseInt(s, 10);
|
2017-03-21 17:45:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_key(group) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const ids = util.sorted_ids(group);
|
2020-07-15 01:29:15 +02:00
|
|
|
return ids.join(",");
|
2017-03-21 17:45:10 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 17:08:23 +01:00
|
|
|
export function add_typist(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));
|
2021-02-10 17:08:23 +01:00
|
|
|
}
|
2017-03-21 17:45:10 +01:00
|
|
|
|
2021-02-10 17:08:23 +01:00
|
|
|
export function remove_typist(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-07-02 01:39:34 +02: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;
|
2021-02-10 17:08:23 +01:00
|
|
|
}
|
2017-03-21 17:45:10 +01:00
|
|
|
|
2021-02-10 17:08:23 +01:00
|
|
|
export function get_group_typists(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) || [];
|
2021-02-10 17:08:23 +01:00
|
|
|
}
|
2017-03-21 17:45:10 +01:00
|
|
|
|
2021-02-10 17:08:23 +01:00
|
|
|
export function get_all_typists() {
|
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);
|
2020-07-25 02:02:35 +02:00
|
|
|
typists = _.sortedUniq(typists);
|
2017-03-21 17:45:10 +01:00
|
|
|
return typists;
|
2021-02-10 17:08:23 +01:00
|
|
|
}
|
2017-03-21 17:45:10 +01:00
|
|
|
|
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.
|
2021-02-10 17:08:23 +01:00
|
|
|
export function clear_inbound_timer(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);
|
|
|
|
}
|
2021-02-10 17:08:23 +01:00
|
|
|
}
|
2017-03-22 16:20:16 +01:00
|
|
|
|
2021-02-10 17:08:23 +01:00
|
|
|
export function kickstart_inbound_timer(group, delay, callback) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = get_key(group);
|
2021-02-10 17:08:23 +01:00
|
|
|
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);
|
2021-02-10 17:08:23 +01:00
|
|
|
}
|