mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
This cleans up the handoff of page_params data between ui_init and modules that take over ownership of page_params-derived data. Read the long comment in ui_init for a bit more context. Most of this diff is actually test cleanup. And a lot of the diff to "real" code is just glorified `s/page_params/params/` in the `initialize` functions. One little oddity is that we don't actually surrender ownership of `page_params.user_id` to `people.js`. We could plausibly sweep the rest of the codebase to just use `people.my_user_id()` consistently, but it's not a super high priority thing to fix, since the value never changes. The stream_data situation is a bit messy, since we consume `page_params` data in the initialize() function in addition to the `params` data we "own". I added a comment there and intend to follow up. I tried to mostly avoid the "word soup" by extracting three locals at the top. Finally, I don't touch `alert_words` yet, despite it also doing the delete-page-params-data dance. The problem is that `alert_words` doesn't have a proper `initialize()`. We should clean that up and have it use a `Map` internally, too.
76 lines
1.7 KiB
JavaScript
76 lines
1.7 KiB
JavaScript
const away_user_ids = new Set();
|
|
const user_info = new Map();
|
|
|
|
exports.server_update = function (opts) {
|
|
channel.post({
|
|
url: '/json/users/me/status',
|
|
data: {
|
|
away: opts.away,
|
|
status_text: opts.status_text,
|
|
},
|
|
idempotent: true,
|
|
success: function () {
|
|
if (opts.success) {
|
|
opts.success();
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.server_set_away = function () {
|
|
exports.server_update({away: true});
|
|
};
|
|
|
|
exports.server_revoke_away = function () {
|
|
exports.server_update({away: false});
|
|
};
|
|
|
|
exports.set_away = function (user_id) {
|
|
if (typeof user_id !== 'number') {
|
|
blueslip.error('need ints for user_id');
|
|
}
|
|
away_user_ids.add(user_id);
|
|
};
|
|
|
|
exports.revoke_away = function (user_id) {
|
|
if (typeof user_id !== 'number') {
|
|
blueslip.error('need ints for user_id');
|
|
}
|
|
away_user_ids.delete(user_id);
|
|
};
|
|
|
|
exports.is_away = function (user_id) {
|
|
return away_user_ids.has(user_id);
|
|
};
|
|
|
|
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.delete(opts.user_id);
|
|
return;
|
|
}
|
|
|
|
user_info.set(opts.user_id, opts.status_text);
|
|
};
|
|
|
|
exports.initialize = function (params) {
|
|
for (const [str_user_id, dct] of Object.entries(params.user_status)) {
|
|
// JSON does not allow integer keys, so we
|
|
// convert them here.
|
|
const user_id = parseInt(str_user_id, 10);
|
|
|
|
if (dct.away) {
|
|
away_user_ids.add(user_id);
|
|
}
|
|
|
|
if (dct.status_text) {
|
|
user_info.set(user_id, dct.status_text);
|
|
}
|
|
}
|
|
};
|
|
|
|
window.user_status = exports;
|