2020-01-12 22:09:39 +01:00
|
|
|
const IntDict = require('./int_dict').IntDict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-01-12 22:17:02 +01:00
|
|
|
const away_user_ids = new Set();
|
2020-01-12 22:09:39 +01:00
|
|
|
const user_info = new IntDict();
|
2018-12-19 17:11:17 +01:00
|
|
|
|
2019-01-23 19:25:22 +01:00
|
|
|
exports.server_update = function (opts) {
|
2018-12-19 17:11:17 +01:00
|
|
|
channel.post({
|
|
|
|
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,
|
2019-01-26 21:14:33 +01:00
|
|
|
success: function () {
|
|
|
|
if (opts.success) {
|
|
|
|
opts.success();
|
|
|
|
}
|
|
|
|
},
|
2018-12-19 17:11:17 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-23 19:25:22 +01:00
|
|
|
exports.server_set_away = function () {
|
|
|
|
exports.server_update({away: true});
|
|
|
|
};
|
|
|
|
|
2018-12-19 17:11:17 +01:00
|
|
|
exports.server_revoke_away = function () {
|
2019-01-23 19:25:22 +01:00
|
|
|
exports.server_update({away: false});
|
2018-12-19 17:11:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_away = function (user_id) {
|
2020-01-12 22:17:02 +01:00
|
|
|
if (typeof user_id !== 'number') {
|
|
|
|
blueslip.error('need ints for user_id');
|
|
|
|
}
|
|
|
|
away_user_ids.add(user_id);
|
2018-12-19 17:11:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.revoke_away = function (user_id) {
|
2020-01-12 22:17:02 +01:00
|
|
|
if (typeof user_id !== 'number') {
|
|
|
|
blueslip.error('need ints for user_id');
|
|
|
|
}
|
|
|
|
away_user_ids.delete(user_id);
|
2018-12-19 17:11:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_away = function (user_id) {
|
|
|
|
return away_user_ids.has(user_id);
|
|
|
|
};
|
|
|
|
|
2019-01-23 19:25:22 +01:00
|
|
|
exports.get_status_text = function (user_id) {
|
|
|
|
return user_info.get(user_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_status_text = function (opts) {
|
|
|
|
if (!opts.status_text) {
|
|
|
|
user_info.del(opts.user_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
user_info.set(opts.user_id, opts.status_text);
|
|
|
|
};
|
|
|
|
|
2018-12-19 17:11:17 +01:00
|
|
|
exports.initialize = function () {
|
2020-01-12 22:09:39 +01:00
|
|
|
_.each(page_params.user_status, function (dct, str_user_id) {
|
|
|
|
// JSON does not allow integer keys, so we
|
|
|
|
// convert them here.
|
|
|
|
const user_id = parseInt(str_user_id, 10);
|
|
|
|
|
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);
|
|
|
|
}
|
2018-12-19 17:11:17 +01:00
|
|
|
});
|
|
|
|
|
2019-01-21 19:06:03 +01:00
|
|
|
delete page_params.user_status;
|
2018-12-19 17:11:17 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.user_status = exports;
|