2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
import render_message_reaction from "../templates/message_reaction.hbs";
|
2020-07-25 02:02:35 +02:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 01:25:00 +01:00
|
|
|
import * as channel from "./channel";
|
2022-03-25 14:47:51 +01:00
|
|
|
import * as emoji from "./emoji";
|
2021-02-28 01:26:16 +01:00
|
|
|
import * as emoji_picker from "./emoji_picker";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:25:00 +01:00
|
|
|
import * as message_store from "./message_store";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:25:00 +01:00
|
|
|
import * as people from "./people";
|
2021-09-07 04:09:12 +02:00
|
|
|
import * as spectators from "./spectators";
|
2021-07-28 16:00:58 +02:00
|
|
|
import {user_settings} from "./user_settings";
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export const view = {}; // function namespace
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function get_local_reaction_id(reaction_info) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return [reaction_info.reaction_type, reaction_info.emoji_code].join(",");
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function open_reactions_popover() {
|
2021-03-30 02:21:21 +02:00
|
|
|
const message = message_lists.current.selected_message();
|
2021-02-24 16:50:30 +01:00
|
|
|
let target;
|
|
|
|
|
|
|
|
// Use verbose style to ensure we test both sides of the condition.
|
|
|
|
if (message.sent_by_me) {
|
2021-03-30 02:21:21 +02:00
|
|
|
target = $(message_lists.current.selected_row()).find(".actions_hover")[0];
|
2021-02-24 16:50:30 +01:00
|
|
|
} else {
|
2021-03-30 02:21:21 +02:00
|
|
|
target = $(message_lists.current.selected_row()).find(".reaction_button")[0];
|
2017-09-18 22:06:39 +02:00
|
|
|
}
|
2021-02-24 16:50:30 +01:00
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
emoji_picker.toggle_emoji_popover(target, message_lists.current.selected_id());
|
2017-09-18 22:06:39 +02:00
|
|
|
return true;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-09-18 22:06:39 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function current_user_has_reacted_to_emoji(message, local_id) {
|
|
|
|
set_clean_reactions(message);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
const clean_reaction_object = message.clean_reactions.get(local_id);
|
|
|
|
return clean_reaction_object && clean_reaction_object.user_ids.includes(page_params.user_id);
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-10-08 17:02:14 +02:00
|
|
|
|
2017-10-31 22:33:28 +01:00
|
|
|
function get_message(message_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2017-10-31 22:33:28 +01:00
|
|
|
if (!message) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("reactions: Bad message id: " + message_id);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
set_clean_reactions(message);
|
2017-10-31 22:33:28 +01:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2017-12-19 00:55:40 +01:00
|
|
|
function create_reaction(message_id, reaction_info) {
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2020-04-22 23:24:28 +02:00
|
|
|
user_id: page_params.user_id,
|
2021-02-28 01:25:00 +01:00
|
|
|
local_id: get_local_reaction_id(reaction_info),
|
2017-12-19 00:55:40 +01:00
|
|
|
reaction_type: reaction_info.reaction_type,
|
|
|
|
emoji_name: reaction_info.emoji_name,
|
|
|
|
emoji_code: reaction_info.emoji_code,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_ui_and_send_reaction_ajax(message_id, reaction_info) {
|
2021-11-21 17:31:12 +01:00
|
|
|
if (page_params.is_spectator) {
|
|
|
|
// Spectators can't react, since they don't have accounts. We
|
|
|
|
// stop here to avoid a confusing reaction local echo.
|
|
|
|
spectators.login_to_access();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = get_message(message_id);
|
2021-02-28 01:25:00 +01:00
|
|
|
const local_id = get_local_reaction_id(reaction_info);
|
|
|
|
const has_reacted = current_user_has_reacted_to_emoji(message, local_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
const operation = has_reacted ? "remove" : "add";
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction = create_reaction(message_id, reaction_info);
|
2017-12-19 00:55:40 +01:00
|
|
|
|
|
|
|
if (operation === "add") {
|
2021-02-28 01:25:00 +01:00
|
|
|
add_reaction(reaction);
|
2017-12-19 00:55:40 +01:00
|
|
|
} else {
|
2021-02-28 01:25:00 +01:00
|
|
|
remove_reaction(reaction);
|
2017-12-19 00:55:40 +01:00
|
|
|
}
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/" + message_id + "/reactions",
|
2017-10-31 22:33:28 +01:00
|
|
|
data: reaction_info,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {},
|
|
|
|
error(xhr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const response = channel.xhr_error_message("Error sending reaction", xhr);
|
2020-03-28 01:25:56 +01:00
|
|
|
// Errors are somewhat common here, due to race conditions
|
2017-03-24 23:46:20 +01:00
|
|
|
// where the user tries to add/remove the reaction when there is already
|
|
|
|
// an in-flight request. We eventually want to make this a blueslip
|
|
|
|
// error, rather than a warning, but we need to implement either
|
|
|
|
// #4291 or #4295 first.
|
|
|
|
blueslip.warn(response);
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2016-12-02 13:23:23 +01:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
if (operation === "add") {
|
2017-10-31 22:33:28 +01:00
|
|
|
channel.post(args);
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (operation === "remove") {
|
2016-12-02 13:23:23 +01:00
|
|
|
channel.del(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function toggle_emoji_reaction(message_id, emoji_name) {
|
2018-03-11 18:55:20 +01:00
|
|
|
// This codepath doesn't support toggling a deactivated realm emoji.
|
|
|
|
// Since an user can interact with a deactivated realm emoji only by
|
|
|
|
// clicking on a reaction and that is handled by `process_reaction_click()`
|
|
|
|
// method. This codepath is to be used only where there is no chance of an
|
|
|
|
// user interacting with a deactivated realm emoji like emoji picker.
|
2017-05-11 17:00:24 +02:00
|
|
|
|
2021-07-26 12:43:55 +02:00
|
|
|
const reaction_info = emoji.get_emoji_details_by_name(emoji_name);
|
2017-12-19 00:55:40 +01:00
|
|
|
update_ui_and_send_reaction_ajax(message_id, reaction_info);
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2016-12-02 13:23:23 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function process_reaction_click(message_id, local_id) {
|
2020-03-09 14:26:17 +01:00
|
|
|
const message = get_message(message_id);
|
|
|
|
|
|
|
|
if (!message) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("message_id for reaction click is unknown: " + message_id);
|
2020-03-09 14:26:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
const clean_reaction_object = message.clean_reactions.get(local_id);
|
2020-03-09 14:26:17 +01:00
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
if (!clean_reaction_object) {
|
2020-03-09 14:26:17 +01:00
|
|
|
blueslip.error(
|
2020-07-15 00:34:28 +02:00
|
|
|
"Data integrity problem for reaction " + local_id + " (message " + message_id + ")",
|
|
|
|
);
|
2020-03-09 14:26:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const reaction_info = {
|
2022-09-29 02:23:22 +02:00
|
|
|
reaction_type: clean_reaction_object.reaction_type,
|
|
|
|
emoji_name: clean_reaction_object.emoji_name,
|
|
|
|
emoji_code: clean_reaction_object.emoji_code,
|
2020-03-09 14:26:17 +01:00
|
|
|
};
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2017-12-19 00:55:40 +01:00
|
|
|
update_ui_and_send_reaction_ajax(message_id, reaction_info);
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2020-10-21 15:09:10 +02:00
|
|
|
function generate_title(emoji_name, user_ids) {
|
2021-05-10 15:35:02 +02:00
|
|
|
const usernames = people.get_display_full_names(
|
|
|
|
user_ids.filter((user_id) => user_id !== page_params.user_id),
|
|
|
|
);
|
2020-10-21 15:09:10 +02:00
|
|
|
const current_user_reacted = user_ids.length !== usernames.length;
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
emoji_name: ":" + emoji_name + ":",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (user_ids.length === 1) {
|
|
|
|
if (current_user_reacted) {
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t({defaultMessage: "You (click to remove) reacted with {emoji_name}"}, context);
|
2020-10-21 15:09:10 +02:00
|
|
|
}
|
|
|
|
context.username = usernames[0];
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t({defaultMessage: "{username} reacted with {emoji_name}"}, context);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:09:10 +02:00
|
|
|
if (user_ids.length === 2 && current_user_reacted) {
|
|
|
|
context.other_username = usernames[0];
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"You (click to remove) and {other_username} reacted with {emoji_name}",
|
|
|
|
},
|
2020-10-21 15:09:10 +02:00
|
|
|
context,
|
|
|
|
);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2020-10-21 15:09:10 +02:00
|
|
|
|
2022-01-24 09:20:01 +01:00
|
|
|
context.comma_separated_usernames = usernames.slice(0, -1).join(", ");
|
|
|
|
context.last_username = usernames.at(-1);
|
2020-10-21 15:09:10 +02:00
|
|
|
if (current_user_reacted) {
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"You (click to remove), {comma_separated_usernames} and {last_username} reacted with {emoji_name}",
|
|
|
|
},
|
2020-10-21 15:09:10 +02:00
|
|
|
context,
|
|
|
|
);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"{comma_separated_usernames} and {last_username} reacted with {emoji_name}",
|
|
|
|
},
|
2020-10-21 15:09:10 +02:00
|
|
|
context,
|
|
|
|
);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 14:56:54 +02:00
|
|
|
// Add a tooltip showing who reacted to a message.
|
2021-02-28 01:25:00 +01:00
|
|
|
export function get_reaction_title_data(message_id, local_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = get_message(message_id);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
const clean_reaction_object = message.clean_reactions.get(local_id);
|
|
|
|
const user_list = clean_reaction_object.user_ids;
|
|
|
|
const emoji_name = clean_reaction_object.emoji_name;
|
2019-11-02 00:06:25 +01:00
|
|
|
const title = generate_title(emoji_name, user_list);
|
2019-04-04 14:56:54 +02:00
|
|
|
|
|
|
|
return title;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2019-04-04 14:56:54 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function get_reaction_section(message_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $message_element = $(".message_table").find(`[zid='${CSS.escape(message_id)}']`);
|
|
|
|
const $section = $message_element.find(".message_reactions");
|
|
|
|
return $section;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function find_reaction(message_id, local_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $reaction_section = get_reaction_section(message_id);
|
|
|
|
const $reaction = $reaction_section.find(`[data-reaction-id='${CSS.escape(local_id)}']`);
|
|
|
|
return $reaction;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function get_add_reaction_button(message_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $reaction_section = get_reaction_section(message_id);
|
|
|
|
const $add_button = $reaction_section.find(".reaction_button");
|
|
|
|
return $add_button;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2022-10-22 02:51:46 +02:00
|
|
|
export function set_reaction_vote_text($reaction, vote_text) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $count_element = $reaction.find(".message_reaction_count");
|
2022-10-22 02:51:46 +02:00
|
|
|
$count_element.text(vote_text);
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-05-29 19:24:31 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function add_reaction(event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = event.message_id;
|
|
|
|
const message = message_store.get(message_id);
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2017-01-17 09:10:41 +01:00
|
|
|
if (message === undefined) {
|
|
|
|
// If we don't have the message in cache, do nothing; if we
|
|
|
|
// ever fetch it from the server, it'll come with the
|
|
|
|
// latest reactions attached
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
set_clean_reactions(message);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
const local_id = get_local_reaction_id(event);
|
2020-04-22 23:24:28 +02:00
|
|
|
const user_id = event.user_id;
|
2022-10-20 02:16:48 +02:00
|
|
|
let clean_reaction_object = message.clean_reactions.get(local_id);
|
2022-09-29 02:23:22 +02:00
|
|
|
if (clean_reaction_object && clean_reaction_object.user_ids.includes(user_id)) {
|
2017-12-19 00:55:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
if (clean_reaction_object) {
|
|
|
|
clean_reaction_object.user_ids.push(user_id);
|
2022-10-22 02:51:46 +02:00
|
|
|
update_user_fields(clean_reaction_object, message.clean_reactions);
|
2022-10-22 02:42:51 +02:00
|
|
|
view.update_existing_reaction(clean_reaction_object, message, user_id);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
} else {
|
2022-10-20 02:16:48 +02:00
|
|
|
clean_reaction_object = make_clean_reaction({
|
2020-07-20 22:18:43 +02:00
|
|
|
local_id,
|
2022-10-20 02:16:48 +02:00
|
|
|
user_ids: [user_id],
|
|
|
|
reaction_type: event.reaction_type,
|
|
|
|
emoji_name: event.emoji_name,
|
|
|
|
emoji_code: event.emoji_code,
|
|
|
|
});
|
|
|
|
|
|
|
|
message.clean_reactions.set(local_id, clean_reaction_object);
|
2022-10-22 02:51:46 +02:00
|
|
|
update_user_fields(clean_reaction_object, message.clean_reactions);
|
2022-10-22 02:42:51 +02:00
|
|
|
view.insert_new_reaction(clean_reaction_object, message, user_id);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2016-12-02 13:23:23 +01:00
|
|
|
|
2022-10-22 02:42:51 +02:00
|
|
|
view.update_existing_reaction = function (clean_reaction_object, message, acting_user_id) {
|
2017-05-29 16:31:51 +02:00
|
|
|
// Our caller ensures that this message already has a reaction
|
|
|
|
// for this emoji and sets up our user_list. This function
|
|
|
|
// simply updates the DOM.
|
2022-10-20 02:16:48 +02:00
|
|
|
const local_id = get_local_reaction_id(clean_reaction_object);
|
2022-10-22 02:42:51 +02:00
|
|
|
const $reaction = find_reaction(message.id, local_id);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2022-10-20 02:16:48 +02:00
|
|
|
const new_label = generate_title(
|
|
|
|
clean_reaction_object.emoji_name,
|
|
|
|
clean_reaction_object.user_ids,
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$reaction.attr("aria-label", new_label);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2022-10-20 02:16:48 +02:00
|
|
|
if (acting_user_id === page_params.user_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$reaction.addClass("reacted");
|
2017-05-29 16:31:51 +02:00
|
|
|
}
|
2022-10-22 02:51:46 +02:00
|
|
|
|
|
|
|
update_vote_text_on_message(message);
|
2017-05-29 16:31:51 +02:00
|
|
|
};
|
|
|
|
|
2022-10-22 02:42:51 +02:00
|
|
|
view.insert_new_reaction = function (clean_reaction_object, message, user_id) {
|
2017-05-29 16:31:51 +02:00
|
|
|
// Our caller ensures we are the first user to react to this
|
2022-10-20 02:16:48 +02:00
|
|
|
// message with this emoji. We then render the emoji/title/count
|
|
|
|
// and insert it before the add button.
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const context = {
|
2022-10-22 02:42:51 +02:00
|
|
|
message_id: message.id,
|
2022-10-20 02:16:48 +02:00
|
|
|
...emoji.get_emoji_details_for_rendering(clean_reaction_object),
|
2017-06-28 21:07:26 +02:00
|
|
|
};
|
|
|
|
|
2022-10-20 02:16:48 +02:00
|
|
|
const new_label = generate_title(
|
|
|
|
clean_reaction_object.emoji_name,
|
|
|
|
clean_reaction_object.user_ids,
|
|
|
|
);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2017-06-28 21:07:26 +02:00
|
|
|
context.count = 1;
|
2019-04-04 14:56:54 +02:00
|
|
|
context.label = new_label;
|
2022-10-20 02:16:48 +02:00
|
|
|
context.local_id = get_local_reaction_id(clean_reaction_object);
|
2021-07-28 16:00:58 +02:00
|
|
|
context.emoji_alt_code = user_settings.emojiset === "text";
|
2021-11-20 20:13:59 +01:00
|
|
|
context.is_realm_emoji =
|
|
|
|
context.reaction_type === "realm_emoji" || context.reaction_type === "zulip_extra_emoji";
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2021-12-17 12:14:52 +01:00
|
|
|
if (user_id === page_params.user_id) {
|
2017-06-28 21:07:26 +02:00
|
|
|
context.class = "message_reaction reacted";
|
2017-05-29 16:31:51 +02:00
|
|
|
} else {
|
2017-06-28 21:07:26 +02:00
|
|
|
context.class = "message_reaction";
|
2017-05-29 16:31:51 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $new_reaction = $(render_message_reaction(context));
|
2017-05-29 16:31:51 +02:00
|
|
|
|
|
|
|
// Now insert it before the add button.
|
2022-10-22 02:42:51 +02:00
|
|
|
const $reaction_button_element = get_add_reaction_button(message.id);
|
2022-01-25 11:36:19 +01:00
|
|
|
$new_reaction.insertBefore($reaction_button_element);
|
2022-10-22 02:51:46 +02:00
|
|
|
|
|
|
|
update_vote_text_on_message(message);
|
2017-05-29 16:31:51 +02:00
|
|
|
};
|
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function remove_reaction(event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = event.message_id;
|
2020-04-22 23:24:28 +02:00
|
|
|
const user_id = event.user_id;
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2021-02-28 01:25:00 +01:00
|
|
|
const local_id = get_local_reaction_id(event);
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2017-01-17 09:10:41 +01:00
|
|
|
if (message === undefined) {
|
|
|
|
// If we don't have the message in cache, do nothing; if we
|
|
|
|
// ever fetch it from the server, it'll come with the
|
|
|
|
// latest reactions attached
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
set_clean_reactions(message);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
const clean_reaction_object = message.clean_reactions.get(local_id);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
if (!clean_reaction_object) {
|
2017-12-19 00:55:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
if (!clean_reaction_object.user_ids.includes(user_id)) {
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
return;
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2022-09-29 02:23:22 +02:00
|
|
|
clean_reaction_object.user_ids = clean_reaction_object.user_ids.filter((id) => id !== user_id);
|
2022-10-22 02:51:46 +02:00
|
|
|
if (clean_reaction_object.user_ids.length === 0) {
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
message.clean_reactions.delete(local_id);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2022-10-22 02:51:46 +02:00
|
|
|
const should_display_reactors = check_should_display_reactors(message.clean_reactions);
|
|
|
|
update_user_fields(clean_reaction_object, should_display_reactors);
|
|
|
|
|
2022-10-22 02:42:51 +02:00
|
|
|
view.remove_reaction(clean_reaction_object, message, user_id);
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
2017-06-28 21:41:35 +02:00
|
|
|
|
2022-10-22 02:42:51 +02:00
|
|
|
view.remove_reaction = function (clean_reaction_object, message, user_id) {
|
2022-10-20 02:16:48 +02:00
|
|
|
const local_id = get_local_reaction_id(clean_reaction_object);
|
2022-10-22 02:42:51 +02:00
|
|
|
const $reaction = find_reaction(message.id, local_id);
|
2022-10-20 02:16:48 +02:00
|
|
|
const reaction_count = clean_reaction_object.user_ids.length;
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2022-10-20 02:16:48 +02:00
|
|
|
if (reaction_count === 0) {
|
2017-05-29 16:56:13 +02:00
|
|
|
// If this user was the only one reacting for this emoji, we simply
|
|
|
|
// remove the reaction and exit.
|
2022-01-25 11:36:19 +01:00
|
|
|
$reaction.remove();
|
2022-10-22 02:51:46 +02:00
|
|
|
update_vote_text_on_message(message);
|
2017-05-29 16:56:13 +02:00
|
|
|
return;
|
2017-02-13 07:51:40 +01:00
|
|
|
}
|
2017-05-29 16:56:13 +02:00
|
|
|
|
|
|
|
// The emoji still has reactions from other users, so we need to update
|
|
|
|
// the title/count and, if the user is the current user, turn off the
|
|
|
|
// "reacted" class.
|
2022-10-20 02:16:48 +02:00
|
|
|
const new_label = generate_title(
|
|
|
|
clean_reaction_object.emoji_name,
|
|
|
|
clean_reaction_object.user_ids,
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$reaction.attr("aria-label", new_label);
|
2017-05-29 16:56:13 +02:00
|
|
|
if (user_id === page_params.user_id) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$reaction.removeClass("reacted");
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2022-10-22 02:51:46 +02:00
|
|
|
|
|
|
|
update_vote_text_on_message(message);
|
2016-12-02 13:23:23 +01:00
|
|
|
};
|
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function get_emojis_used_by_user_for_message_id(message_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = page_params.user_id;
|
|
|
|
const message = message_store.get(message_id);
|
2021-02-28 01:25:00 +01:00
|
|
|
set_clean_reactions(message);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
|
|
|
const names = [];
|
2022-09-29 02:23:22 +02:00
|
|
|
for (const clean_reaction_object of message.clean_reactions.values()) {
|
|
|
|
if (clean_reaction_object.user_ids.includes(user_id)) {
|
|
|
|
names.push(clean_reaction_object.emoji_name);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function get_message_reactions(message) {
|
|
|
|
set_clean_reactions(message);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
return Array.from(message.clean_reactions.values());
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2021-02-28 01:25:00 +01:00
|
|
|
export function set_clean_reactions(message) {
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
/*
|
2022-09-29 02:30:58 +02:00
|
|
|
set_clean_reactions processes the raw message.reactions object,
|
|
|
|
which will contain one object for each individual reaction, even
|
|
|
|
if two users react with the same emoji.
|
|
|
|
|
|
|
|
As output, it sets message.cleaned_reactions, which is a more
|
|
|
|
compressed format with one entry per reaction pill that should
|
|
|
|
be displayed visually to users.
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (message.clean_reactions) {
|
2022-10-22 02:51:46 +02:00
|
|
|
// Update display details for the reaction. In particular,
|
|
|
|
// user_settings.display_emoji_reaction_users or the names of
|
|
|
|
// the users appearing in the reaction may have changed since
|
|
|
|
// this reaction was first rendered.
|
|
|
|
const should_display_reactors = check_should_display_reactors(message.clean_reactions);
|
|
|
|
for (const clean_reaction of message.clean_reactions.values()) {
|
|
|
|
update_user_fields(clean_reaction, should_display_reactors);
|
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-29 02:30:58 +02:00
|
|
|
// This first loop creates a temporary distinct_reactions data
|
|
|
|
// structure, which will accumulate the set of users who have
|
|
|
|
// reacted with each distinct reaction.
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
const distinct_reactions = new Map();
|
|
|
|
const user_map = new Map();
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const reaction of message.reactions) {
|
2021-02-28 01:25:00 +01:00
|
|
|
const local_id = get_local_reaction_id(reaction);
|
2020-04-22 23:24:28 +02:00
|
|
|
const user_id = reaction.user_id;
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2017-03-26 20:38:47 +02:00
|
|
|
if (!people.is_known_user_id(user_id)) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.warn("Unknown user_id " + user_id + " in reaction for message " + message.id);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
continue;
|
2017-03-26 20:38:47 +02:00
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
if (!distinct_reactions.has(local_id)) {
|
|
|
|
distinct_reactions.set(local_id, reaction);
|
|
|
|
user_map.set(local_id, []);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
|
|
|
const user_ids = user_map.get(local_id);
|
|
|
|
|
|
|
|
if (user_ids.includes(user_id)) {
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.error(
|
|
|
|
"server sent duplicate reactions for user " + user_id + " (key=" + local_id + ")",
|
|
|
|
);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
continue;
|
2017-02-13 07:51:40 +01:00
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
|
|
|
user_ids.push(user_id);
|
|
|
|
}
|
|
|
|
|
2022-09-29 02:30:58 +02:00
|
|
|
// TODO: Rather than adding this field to the message object, it
|
|
|
|
// might be cleaner to create an independent map from message_id
|
|
|
|
// => clean_reactions data for the message, with care being taken
|
|
|
|
// to make sure reify_message_id moves the data structure
|
|
|
|
// properly.
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
message.clean_reactions = new Map();
|
|
|
|
for (const local_id of distinct_reactions.keys()) {
|
|
|
|
const reaction = distinct_reactions.get(local_id);
|
|
|
|
const user_ids = user_map.get(local_id);
|
|
|
|
|
2021-12-17 13:20:16 +01:00
|
|
|
message.clean_reactions.set(
|
2020-07-20 22:18:43 +02:00
|
|
|
local_id,
|
2021-12-19 11:41:45 +01:00
|
|
|
make_clean_reaction({local_id, user_ids, ...reaction}),
|
2021-12-17 13:20:16 +01:00
|
|
|
);
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
}
|
2022-09-29 02:30:58 +02:00
|
|
|
|
2022-10-22 02:51:46 +02:00
|
|
|
// We do update_user_fields in a separate loop, because doing so
|
|
|
|
// lets us avoid duplicating check_should_display_reactors to
|
|
|
|
// determine whether to store in the vote_text field a count or
|
|
|
|
// the names of reactors (users who reacted).
|
|
|
|
const should_display_reactors = check_should_display_reactors(message.clean_reactions);
|
|
|
|
for (const clean_reaction of message.clean_reactions.values()) {
|
|
|
|
update_user_fields(clean_reaction, should_display_reactors);
|
|
|
|
}
|
|
|
|
|
2022-09-29 02:30:58 +02:00
|
|
|
// We don't maintain message.reactions when users react to
|
|
|
|
// messages we already have a copy of, so it's safest to delete it
|
|
|
|
// after we've processed the reactions data for a message into the
|
|
|
|
// clean_reactions data structure, which we do maintain.
|
|
|
|
delete message.reactions;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2021-12-17 15:29:58 +01:00
|
|
|
function make_clean_reaction({local_id, user_ids, emoji_name, emoji_code, reaction_type}) {
|
2022-09-29 02:23:22 +02:00
|
|
|
const clean_reaction_object = {
|
2021-12-17 15:29:58 +01:00
|
|
|
local_id,
|
|
|
|
user_ids,
|
|
|
|
...emoji.get_emoji_details_for_rendering({emoji_name, emoji_code, reaction_type}),
|
|
|
|
};
|
2022-09-29 02:23:22 +02:00
|
|
|
clean_reaction_object.emoji_alt_code = user_settings.emojiset === "text";
|
|
|
|
clean_reaction_object.is_realm_emoji =
|
|
|
|
clean_reaction_object.reaction_type === "realm_emoji" ||
|
|
|
|
clean_reaction_object.reaction_type === "zulip_extra_emoji";
|
|
|
|
return clean_reaction_object;
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
|
2022-10-22 02:51:46 +02:00
|
|
|
export function update_user_fields(clean_reaction_object, should_display_reactors) {
|
|
|
|
// update_user_fields needs to be called whenever the set of users
|
|
|
|
// whor eacted on a message might have changed, including due to
|
|
|
|
// upvote/downvotes on ANY reaction in the message, because those
|
|
|
|
// can change the correct value of should_display_reactors to use.
|
2022-09-29 02:23:22 +02:00
|
|
|
clean_reaction_object.count = clean_reaction_object.user_ids.length;
|
|
|
|
clean_reaction_object.label = generate_title(
|
|
|
|
clean_reaction_object.emoji_name,
|
|
|
|
clean_reaction_object.user_ids,
|
|
|
|
);
|
|
|
|
if (clean_reaction_object.user_ids.includes(page_params.user_id)) {
|
|
|
|
clean_reaction_object.class = "message_reaction reacted";
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
} else {
|
2022-09-29 02:23:22 +02:00
|
|
|
clean_reaction_object.class = "message_reaction";
|
reactions: Rewrite code to use clean reactions.
Before this commit, the reactions code would
take the `message.reactions` structure from
the server and try to "collapse" all the reactions
for the same users into the same reactions,
but with each reaction having a list of user_ids.
It was a strangely denormalized structure that
was awkward to work with, and it made it really
hard to reason about whether the data was in
the original structure that the server sent or
the modified structure.
Now we use a cleaner, normalized Map to keep
each reaction (i.e. one per emoji), and we
write that to `message.clean_reactions`.
The `clean_reactions` structure is now the
authoritatize source for all reaction-related
operations. As soon as you try to do anything
with reactions, we build the `clean_reactions`
data on the fly from the server data.
In particular, when we process events, we just
directly manipulate the `clean_reactions` data,
which is much easier to work with, since it's
a Map and doesn't duplicate any data.
This rewrite should avoid some obscure bugs.
I use `r` as shorthand for the clean reaction
structures, so as not to confuse it with
data from the server's message.reactions.
It also avoids some confusion where we use
`reaction` as a var name for the reaction
elements.
2020-03-08 13:13:47 +01:00
|
|
|
}
|
2022-10-22 02:51:46 +02:00
|
|
|
|
|
|
|
clean_reaction_object.count = clean_reaction_object.user_ids.length;
|
|
|
|
clean_reaction_object.label = generate_title(
|
|
|
|
clean_reaction_object.emoji_name,
|
|
|
|
clean_reaction_object.user_ids,
|
|
|
|
);
|
|
|
|
|
|
|
|
// The vote_text field set here is used directly in the Handlebars
|
|
|
|
// template for rendering (or rerendering!) a message.
|
|
|
|
clean_reaction_object.vote_text = get_vote_text(clean_reaction_object, should_display_reactors);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function get_vote_text(clean_reaction_object, should_display_reactors) {
|
|
|
|
if (should_display_reactors) {
|
|
|
|
return comma_separated_usernames(clean_reaction_object.user_ids);
|
|
|
|
}
|
|
|
|
return `${clean_reaction_object.user_ids.length}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_should_display_reactors(cleaned_reactions) {
|
|
|
|
if (!user_settings.display_emoji_reaction_users) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let total_reactions = 0;
|
|
|
|
for (const r of cleaned_reactions.values()) {
|
|
|
|
// r.count is not yet initialized when this is called during
|
|
|
|
// set_clean_reactions.
|
|
|
|
total_reactions += r.count || r.user_ids.length;
|
|
|
|
}
|
|
|
|
return total_reactions <= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
function comma_separated_usernames(user_list) {
|
|
|
|
const usernames = people.get_display_full_names(user_list);
|
|
|
|
const current_user_has_reacted = user_list.includes(page_params.user_id);
|
|
|
|
|
|
|
|
if (current_user_has_reacted) {
|
|
|
|
const current_user_index = user_list.indexOf(page_params.user_id);
|
|
|
|
usernames[current_user_index] = $t({
|
|
|
|
defaultMessage: "You",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const comma_separated_usernames = usernames.join(", ");
|
|
|
|
return comma_separated_usernames;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function update_vote_text_on_message(message) {
|
|
|
|
// Because whether we display a count or the names of reacting
|
|
|
|
// users depends on total reactions on the message, we need to
|
|
|
|
// recalculate this whenever adjusting reaction rendering on a
|
|
|
|
// message.
|
|
|
|
const cleaned_reactions = get_message_reactions(message);
|
|
|
|
const should_display_reactors = check_should_display_reactors(cleaned_reactions);
|
|
|
|
for (const [reaction, clean_reaction] of message.clean_reactions.entries()) {
|
|
|
|
const reaction_elem = find_reaction(message.id, clean_reaction.local_id);
|
|
|
|
const vote_text = get_vote_text(clean_reaction, should_display_reactors);
|
|
|
|
message.clean_reactions.get(reaction).vote_text = vote_text;
|
|
|
|
set_reaction_vote_text(reaction_elem, vote_text);
|
|
|
|
}
|
2021-02-28 01:25:00 +01:00
|
|
|
}
|