Files
zulip/static/js/user_status.js
YashRE42 263a79738f user_status: Fix status emoji handling of deactivated custom emoji.
Previously, if a user had a realm emoji set as their status emoji and
someone deleted the realm emoji, the app would fail to initialize,
because of the error we throw from `./shared/js/emoji.js`.

This commit fixes this by just displaying the deactivated emoji,
similar to how we do when realm_emoji used as reactions are deleted.

As part of the fix, we add a function get_emoji_details_for_rendering,
which duplicates some of the logic used in `reactions.js`, we can
refactor to remove the duplication in `reactions.js` in future
commits.

Note that the following behaviour is a part of our design:
If a user sets their emoji to a particular realm emoji, say for
example "octo-ninja", and "octo-ninja" was then deleted, and a new
emoji was added with the name "octo-ninja", the user's status emoji
would change to show the new emoji instead of the deleted emoji.

Also note that in the `user_status.js` node test, we were able to
change the name for the 991 realm_emoji because it had not been
previously used anywhere in the test (possibly added as just a copy
paste artifact?).

Fixes: #20274.

emoji: Use reaction_type parameter to analyze emoji.
2021-11-20 20:57:54 -08:00

113 lines
2.7 KiB
JavaScript

import * as emoji from "../shared/js/emoji";
import * as blueslip from "./blueslip";
import * as channel from "./channel";
import {user_settings} from "./user_settings";
const away_user_ids = new Set();
const user_info = new Map();
const user_status_emoji_info = new Map();
export function server_update(opts) {
channel.post({
url: "/json/users/me/status",
data: {
away: opts.away,
status_text: opts.status_text,
emoji_name: opts.emoji_name,
emoji_code: opts.emoji_code,
reaction_type: opts.reaction_type,
},
idempotent: true,
success() {
if (opts.success) {
opts.success();
}
},
});
}
export function server_set_away() {
server_update({away: true});
}
export function server_revoke_away() {
server_update({away: false});
}
export function set_away(user_id) {
if (typeof user_id !== "number") {
blueslip.error("need ints for user_id");
}
away_user_ids.add(user_id);
}
export function revoke_away(user_id) {
if (typeof user_id !== "number") {
blueslip.error("need ints for user_id");
}
away_user_ids.delete(user_id);
}
export function is_away(user_id) {
return away_user_ids.has(user_id);
}
export function get_status_text(user_id) {
return user_info.get(user_id);
}
export function set_status_text(opts) {
if (!opts.status_text) {
user_info.delete(opts.user_id);
return;
}
user_info.set(opts.user_id, opts.status_text);
}
export function get_status_emoji(user_id) {
return user_status_emoji_info.get(user_id);
}
export function set_status_emoji(opts) {
if (!opts.emoji_name) {
user_status_emoji_info.delete(opts.user_id);
return;
}
user_status_emoji_info.set(opts.user_id, {
emoji_alt_code: user_settings.emojiset === "text",
...emoji.get_emoji_details_for_rendering({
emoji_name: opts.emoji_name,
emoji_code: opts.emoji_code,
reaction_type: opts.reaction_type,
}),
});
}
export function initialize(params) {
away_user_ids.clear();
user_info.clear();
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 = Number.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);
}
if (dct.emoji_name) {
user_status_emoji_info.set(user_id, {
...emoji.get_emoji_details_for_rendering(dct),
});
}
}
}