2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:39:51 +01:00
|
|
|
import * as channel from "./channel";
|
|
|
|
|
2020-01-12 22:17:02 +01:00
|
|
|
const away_user_ids = new Set();
|
2020-02-03 09:42:50 +01:00
|
|
|
const user_info = new Map();
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function server_update(opts) {
|
2018-12-19 17:11:17 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/status",
|
2019-01-23 19:25:22 +01:00
|
|
|
data: {
|
|
|
|
away: opts.away,
|
|
|
|
status_text: opts.status_text,
|
|
|
|
},
|
2018-12-19 17:11:17 +01:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2019-01-26 21:14:33 +01:00
|
|
|
if (opts.success) {
|
|
|
|
opts.success();
|
|
|
|
}
|
|
|
|
},
|
2018-12-19 17:11:17 +01:00
|
|
|
});
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function server_set_away() {
|
|
|
|
server_update({away: true});
|
|
|
|
}
|
2019-01-23 19:25:22 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function server_revoke_away() {
|
|
|
|
server_update({away: false});
|
|
|
|
}
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function set_away(user_id) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof user_id !== "number") {
|
|
|
|
blueslip.error("need ints for user_id");
|
2020-01-12 22:17:02 +01:00
|
|
|
}
|
|
|
|
away_user_ids.add(user_id);
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function revoke_away(user_id) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof user_id !== "number") {
|
|
|
|
blueslip.error("need ints for user_id");
|
2020-01-12 22:17:02 +01:00
|
|
|
}
|
|
|
|
away_user_ids.delete(user_id);
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function is_away(user_id) {
|
2018-12-19 17:11:17 +01:00
|
|
|
return away_user_ids.has(user_id);
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function get_status_text(user_id) {
|
2019-01-23 19:25:22 +01:00
|
|
|
return user_info.get(user_id);
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|
2019-01-23 19:25:22 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function set_status_text(opts) {
|
2019-01-23 19:25:22 +01:00
|
|
|
if (!opts.status_text) {
|
2020-02-03 07:41:38 +01:00
|
|
|
user_info.delete(opts.user_id);
|
2019-01-23 19:25:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
user_info.set(opts.user_id, opts.status_text);
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|
2019-01-23 19:25:22 +01:00
|
|
|
|
2021-02-10 17:03:21 +01:00
|
|
|
export function initialize(params) {
|
2021-03-08 16:08:27 +01:00
|
|
|
away_user_ids.clear();
|
|
|
|
user_info.clear();
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
for (const [str_user_id, dct] of Object.entries(params.user_status)) {
|
2020-01-12 22:09:39 +01:00
|
|
|
// JSON does not allow integer keys, so we
|
|
|
|
// convert them here.
|
2020-10-07 09:17:30 +02:00
|
|
|
const user_id = Number.parseInt(str_user_id, 10);
|
2020-01-12 22:09:39 +01:00
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
if (dct.away) {
|
2020-01-12 22:17:02 +01:00
|
|
|
away_user_ids.add(user_id);
|
2019-01-21 19:06:03 +01:00
|
|
|
}
|
2019-01-23 19:25:22 +01:00
|
|
|
|
|
|
|
if (dct.status_text) {
|
|
|
|
user_info.set(user_id, dct.status_text);
|
|
|
|
}
|
2020-02-06 04:31:32 +01:00
|
|
|
}
|
2021-02-10 17:03:21 +01:00
|
|
|
}
|