2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
|
|
|
|
2020-07-25 00:12:17 +02:00
|
|
|
const emoji = require("../shared/js/emoji");
|
2020-07-15 01:29:15 +02:00
|
|
|
const render_message_reaction = require("../templates/message_reaction.hbs");
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
|
|
|
|
2017-06-28 21:29:45 +02:00
|
|
|
exports.view = {}; // function namespace
|
|
|
|
|
2017-10-31 22:33:28 +01:00
|
|
|
exports.get_local_reaction_id = function (reaction_info) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return [reaction_info.reaction_type, reaction_info.emoji_code].join(",");
|
2017-10-31 22:33:28 +01:00
|
|
|
};
|
|
|
|
|
2017-09-18 22:06:39 +02:00
|
|
|
exports.open_reactions_popover = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.selected_message();
|
|
|
|
let target = $(current_msg_list.selected_row()).find(".actions_hover")[0];
|
2017-09-18 22:06:39 +02:00
|
|
|
if (!message.sent_by_me) {
|
|
|
|
target = $(current_msg_list.selected_row()).find(".reaction_button")[0];
|
|
|
|
}
|
|
|
|
emoji_picker.toggle_emoji_popover(target, current_msg_list.selected_id());
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
exports.current_user_has_reacted_to_emoji = function (message, local_id) {
|
|
|
|
exports.set_clean_reactions(message);
|
|
|
|
|
|
|
|
const r = message.clean_reactions.get(local_id);
|
|
|
|
return r && r.user_ids.includes(page_params.user_id);
|
2017-10-31 22:33:28 +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
|
|
|
|
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
|
|
|
exports.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,
|
2017-12-19 00:55:40 +01:00
|
|
|
local_id: exports.get_local_reaction_id(reaction_info),
|
|
|
|
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) {
|
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
|
|
|
const local_id = exports.get_local_reaction_id(reaction_info);
|
2020-07-15 00:34:28 +02:00
|
|
|
const has_reacted = exports.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") {
|
|
|
|
exports.add_reaction(reaction);
|
|
|
|
} else {
|
|
|
|
exports.remove_reaction(reaction);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-29 21:18:59 +02:00
|
|
|
exports.toggle_emoji_reaction = function (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.
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction_info = {
|
2020-07-20 22:18:43 +02:00
|
|
|
emoji_name,
|
2017-10-31 22:33:28 +01:00
|
|
|
};
|
2017-05-11 17:00:24 +02:00
|
|
|
|
2020-02-06 00:17:30 +01:00
|
|
|
if (emoji.active_realm_emojis.has(emoji_name)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (emoji_name === "zulip") {
|
|
|
|
reaction_info.reaction_type = "zulip_extra_emoji";
|
2017-10-31 22:33:28 +01:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
reaction_info.reaction_type = "realm_emoji";
|
2017-10-31 22:33:28 +01:00
|
|
|
}
|
2020-02-06 00:17:30 +01:00
|
|
|
reaction_info.emoji_code = emoji.active_realm_emojis.get(emoji_name).id;
|
2017-10-31 22:33:28 +01:00
|
|
|
} else {
|
2020-05-27 04:17:29 +02:00
|
|
|
const codepoint = emoji.get_emoji_codepoint(emoji_name);
|
|
|
|
if (codepoint === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("Bad emoji name: " + emoji_name);
|
2020-05-27 04:17:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
reaction_info.reaction_type = "unicode_emoji";
|
2020-05-27 04:17:29 +02:00
|
|
|
reaction_info.emoji_code = codepoint;
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2017-05-11 17:00:24 +02:00
|
|
|
|
2017-12-19 00:55:40 +01:00
|
|
|
update_ui_and_send_reaction_ajax(message_id, reaction_info);
|
2016-12-02 13:23:23 +01:00
|
|
|
};
|
|
|
|
|
2017-10-31 22:33:28 +01:00
|
|
|
exports.process_reaction_click = function (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const r = message.clean_reactions.get(local_id);
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
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 = {
|
|
|
|
reaction_type: r.reaction_type,
|
|
|
|
emoji_name: r.emoji_name,
|
|
|
|
emoji_code: r.emoji_code,
|
|
|
|
};
|
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);
|
2017-10-31 22:33:28 +01:00
|
|
|
};
|
|
|
|
|
2020-10-21 15:09:10 +02:00
|
|
|
function generate_title(emoji_name, user_ids) {
|
|
|
|
const usernames = user_ids
|
|
|
|
.filter((user_id) => user_id !== page_params.user_id)
|
|
|
|
.map((user_id) => people.get_by_user_id(user_id).full_name);
|
|
|
|
const current_user_reacted = user_ids.length !== usernames.length;
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
emoji_name: ":" + emoji_name + ":",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (user_ids.length === 1) {
|
|
|
|
if (current_user_reacted) {
|
|
|
|
return i18n.t("You (click to remove) reacted with __emoji_name__", context);
|
|
|
|
}
|
|
|
|
context.username = usernames[0];
|
|
|
|
return i18n.t("__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];
|
|
|
|
return i18n.t(
|
|
|
|
"You (click to remove) and __other_username__ reacted with __emoji_name__",
|
|
|
|
context,
|
|
|
|
);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2020-10-21 15:09:10 +02:00
|
|
|
|
|
|
|
context.comma_separated_usernames = _.initial(usernames).join(", ");
|
|
|
|
context.last_username = _.last(usernames);
|
|
|
|
if (current_user_reacted) {
|
|
|
|
return i18n.t(
|
|
|
|
"You (click to remove), __comma_separated_usernames__ and __last_username__ reacted with __emoji_name__",
|
|
|
|
context,
|
|
|
|
);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2020-10-21 15:09:10 +02:00
|
|
|
return i18n.t(
|
|
|
|
"__comma_separated_usernames__ and __last_username__ reacted with __emoji_name__",
|
|
|
|
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.
|
|
|
|
exports.get_reaction_title_data = function (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
|
|
|
|
2020-03-09 14:26:17 +01:00
|
|
|
const r = message.clean_reactions.get(local_id);
|
|
|
|
const user_list = r.user_ids;
|
|
|
|
const emoji_name = r.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;
|
|
|
|
};
|
|
|
|
|
2017-05-29 16:31:51 +02:00
|
|
|
exports.get_reaction_section = function (message_id) {
|
2021-02-03 23:23:32 +01:00
|
|
|
const message_element = $(".message_table").find(`[zid='${CSS.escape(message_id)}']`);
|
2020-07-15 01:29:15 +02:00
|
|
|
const section = message_element.find(".message_reactions");
|
2017-05-29 16:31:51 +02:00
|
|
|
return section;
|
|
|
|
};
|
|
|
|
|
2017-10-31 22:33:28 +01:00
|
|
|
exports.find_reaction = function (message_id, local_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction_section = exports.get_reaction_section(message_id);
|
2021-02-03 23:23:32 +01:00
|
|
|
const reaction = reaction_section.find(`[data-reaction-id='${CSS.escape(local_id)}']`);
|
2017-05-29 16:31:51 +02:00
|
|
|
return reaction;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_add_reaction_button = function (message_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction_section = exports.get_reaction_section(message_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
const add_button = reaction_section.find(".reaction_button");
|
2017-05-29 16:31:51 +02:00
|
|
|
return add_button;
|
|
|
|
};
|
|
|
|
|
2017-05-29 19:24:31 +02:00
|
|
|
exports.set_reaction_count = function (reaction, count) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const count_element = reaction.find(".message_reaction_count");
|
2018-03-22 22:04:24 +01:00
|
|
|
count_element.text(count);
|
2017-05-29 19:24:31 +02:00
|
|
|
};
|
|
|
|
|
2016-12-02 13:23:23 +01:00
|
|
|
exports.add_reaction = function (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
|
|
|
|
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
|
|
|
exports.set_clean_reactions(message);
|
|
|
|
|
|
|
|
const local_id = exports.get_local_reaction_id(event);
|
2020-04-22 23:24:28 +02:00
|
|
|
const user_id = event.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
|
|
|
|
|
|
|
const r = message.clean_reactions.get(local_id);
|
|
|
|
|
|
|
|
if (r && r.user_ids.includes(user_id)) {
|
2017-12-19 00:55:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 (r) {
|
|
|
|
r.user_ids.push(user_id);
|
|
|
|
exports.update_user_fields(r);
|
|
|
|
} else {
|
|
|
|
exports.add_clean_reaction({
|
2020-07-20 22:18:43 +02:00
|
|
|
message,
|
|
|
|
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
|
|
|
user_ids: [user_id],
|
|
|
|
reaction_type: event.reaction_type,
|
|
|
|
emoji_name: event.emoji_name,
|
|
|
|
emoji_code: event.emoji_code,
|
|
|
|
});
|
|
|
|
}
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: event.reaction_type,
|
|
|
|
emoji_name: event.emoji_name,
|
|
|
|
emoji_code: event.emoji_code,
|
2020-07-20 22:18:43 +02:00
|
|
|
user_id,
|
2017-10-31 22:33:28 +01:00
|
|
|
};
|
2017-05-29 16:31:51 +02: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 (r) {
|
|
|
|
opts.user_list = r.user_ids;
|
2017-10-31 22:33:28 +01:00
|
|
|
exports.view.update_existing_reaction(opts);
|
2016-12-02 13:23:23 +01:00
|
|
|
} else {
|
2017-10-31 22:33:28 +01:00
|
|
|
exports.view.insert_new_reaction(opts);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-28 20:10:05 +02:00
|
|
|
exports.view.update_existing_reaction = function (opts) {
|
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.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = opts.message_id;
|
|
|
|
const emoji_name = opts.emoji_name;
|
|
|
|
const user_list = opts.user_list;
|
|
|
|
const user_id = opts.user_id;
|
|
|
|
const local_id = exports.get_local_reaction_id(opts);
|
|
|
|
const reaction = exports.find_reaction(message_id, local_id);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2017-05-29 19:24:31 +02:00
|
|
|
exports.set_reaction_count(reaction, user_list.length);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_label = generate_title(emoji_name, user_list);
|
2020-07-15 01:29:15 +02:00
|
|
|
reaction.attr("aria-label", new_label);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2017-06-28 20:10:05 +02:00
|
|
|
if (user_id === page_params.user_id) {
|
2017-05-29 16:31:51 +02:00
|
|
|
reaction.addClass("reacted");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-28 21:29:45 +02:00
|
|
|
exports.view.insert_new_reaction = function (opts) {
|
2017-05-29 16:31:51 +02:00
|
|
|
// Our caller ensures we are the first user to react to this
|
|
|
|
// message with this emoji, and it populates user_list for
|
|
|
|
// us. We then render the emoji/title/count and insert it
|
|
|
|
// before the add button.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = opts.message_id;
|
|
|
|
const emoji_name = opts.emoji_name;
|
|
|
|
const emoji_code = opts.emoji_code;
|
|
|
|
const user_id = opts.user_id;
|
|
|
|
const user_list = [user_id];
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const context = {
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
|
|
|
emoji_name,
|
|
|
|
emoji_code,
|
2017-06-28 21:07:26 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_label = generate_title(emoji_name, user_list);
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (opts.reaction_type !== "unicode_emoji") {
|
2017-06-28 21:07:26 +02:00
|
|
|
context.is_realm_emoji = true;
|
2020-10-06 23:57:16 +02:00
|
|
|
const emoji_info = emoji.all_realm_emojis.get(emoji_code);
|
|
|
|
if (!emoji_info) {
|
|
|
|
blueslip.error(`Cannot find/insert realm emoji for code '${emoji_code}'.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.url = emoji_info.emoji_url;
|
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;
|
2017-10-31 22:33:28 +01:00
|
|
|
context.local_id = exports.get_local_reaction_id(opts);
|
2020-07-15 01:29:15 +02:00
|
|
|
context.emoji_alt_code = page_params.emojiset === "text";
|
2017-05-29 16:31:51 +02:00
|
|
|
|
2017-06-28 21:29:45 +02:00
|
|
|
if (opts.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
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_reaction = $(render_message_reaction(context));
|
2017-05-29 16:31:51 +02:00
|
|
|
|
|
|
|
// Now insert it before the add button.
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction_button_element = exports.get_add_reaction_button(message_id);
|
2017-05-29 16:31:51 +02:00
|
|
|
new_reaction.insertBefore(reaction_button_element);
|
|
|
|
};
|
|
|
|
|
2016-12-02 13:23:23 +01:00
|
|
|
exports.remove_reaction = function (event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction_type = event.reaction_type;
|
|
|
|
const emoji_name = event.emoji_name;
|
|
|
|
const emoji_code = event.emoji_code;
|
|
|
|
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);
|
|
|
|
const local_id = exports.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
|
|
|
|
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
|
|
|
exports.set_clean_reactions(message);
|
|
|
|
|
|
|
|
const r = message.clean_reactions.get(local_id);
|
|
|
|
|
|
|
|
if (!r) {
|
2017-12-19 00:55:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 (!r.user_ids.includes(user_id)) {
|
|
|
|
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
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
r.user_ids = r.user_ids.filter((id) => id !== 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
|
|
|
if (r.user_ids.length > 0) {
|
|
|
|
exports.update_user_fields(r);
|
|
|
|
} else {
|
|
|
|
message.clean_reactions.delete(local_id);
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2017-06-28 21:41:35 +02:00
|
|
|
exports.view.remove_reaction({
|
2020-07-20 22:18:43 +02:00
|
|
|
message_id,
|
|
|
|
reaction_type,
|
|
|
|
emoji_name,
|
|
|
|
emoji_code,
|
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_list: r.user_ids,
|
2020-07-20 22:18:43 +02:00
|
|
|
user_id,
|
2017-06-28 21:41:35 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.view.remove_reaction = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = opts.message_id;
|
|
|
|
const emoji_name = opts.emoji_name;
|
|
|
|
const user_list = opts.user_list;
|
|
|
|
const user_id = opts.user_id;
|
|
|
|
const local_id = exports.get_local_reaction_id(opts);
|
|
|
|
const reaction = exports.find_reaction(message_id, local_id);
|
2017-05-29 16:56:13 +02:00
|
|
|
|
|
|
|
if (user_list.length === 0) {
|
|
|
|
// If this user was the only one reacting for this emoji, we simply
|
|
|
|
// remove the reaction and exit.
|
|
|
|
reaction.remove();
|
|
|
|
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.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_label = generate_title(emoji_name, user_list);
|
2020-07-15 01:29:15 +02:00
|
|
|
reaction.attr("aria-label", new_label);
|
2019-04-04 14:56:54 +02:00
|
|
|
|
|
|
|
// If the user is the current user, turn off the "reacted" class.
|
2017-05-29 16:56:13 +02:00
|
|
|
|
2017-05-29 19:24:31 +02:00
|
|
|
exports.set_reaction_count(reaction, user_list.length);
|
2017-05-29 16:56:13 +02:00
|
|
|
|
|
|
|
if (user_id === page_params.user_id) {
|
|
|
|
reaction.removeClass("reacted");
|
2016-12-02 13:23:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_emojis_used_by_user_for_message_id = function (message_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_id = page_params.user_id;
|
|
|
|
const message = message_store.get(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
|
|
|
exports.set_clean_reactions(message);
|
|
|
|
|
|
|
|
const names = [];
|
|
|
|
for (const r of message.clean_reactions.values()) {
|
|
|
|
if (r.user_ids.includes(user_id)) {
|
|
|
|
names.push(r.emoji_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names;
|
|
|
|
};
|
|
|
|
|
2016-12-02 13:23:23 +01:00
|
|
|
exports.get_message_reactions = function (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
|
|
|
exports.set_clean_reactions(message);
|
|
|
|
return Array.from(message.clean_reactions.values());
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_clean_reactions = function (message) {
|
|
|
|
/*
|
|
|
|
The server sends us a single structure for
|
|
|
|
each reaction, even if two users are reacting
|
|
|
|
with the same emoji. Our first loop creates
|
|
|
|
a map of distinct reactions and a map of
|
|
|
|
local_id -> user_ids. The `local_id` is
|
|
|
|
basically a key for the emoji name.
|
|
|
|
|
|
|
|
Then in our second loop we build a more compact
|
|
|
|
data structure that's easier for our message
|
|
|
|
list view templates to work with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (message.clean_reactions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
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 local_id = exports.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
It might feel a little janky to attach clean_reactions
|
|
|
|
directly to the message object, but this allows the
|
|
|
|
server to send us a new copy of the message, and then
|
|
|
|
the next time we try to get reactions from it, we
|
|
|
|
won't have `clean_reactions`, and we will re-process
|
|
|
|
the server's latest copy of the reactions.
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
exports.add_clean_reaction({
|
2020-07-20 22:18:43 +02:00
|
|
|
message,
|
|
|
|
local_id,
|
|
|
|
user_ids,
|
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
|
|
|
reaction_type: reaction.reaction_type,
|
|
|
|
emoji_name: reaction.emoji_name,
|
|
|
|
emoji_code: reaction.emoji_code,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.add_clean_reaction = function (opts) {
|
|
|
|
const r = {};
|
|
|
|
|
|
|
|
r.reaction_type = opts.reaction_type;
|
|
|
|
r.emoji_name = opts.emoji_name;
|
|
|
|
r.emoji_code = opts.emoji_code;
|
|
|
|
r.local_id = opts.local_id;
|
|
|
|
|
|
|
|
r.user_ids = opts.user_ids;
|
|
|
|
exports.update_user_fields(r);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
r.emoji_alt_code = page_params.emojiset === "text";
|
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
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (r.reaction_type !== "unicode_emoji") {
|
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
|
|
|
r.is_realm_emoji = true;
|
2020-10-06 23:57:16 +02:00
|
|
|
const emoji_info = emoji.all_realm_emojis.get(r.emoji_code);
|
|
|
|
if (!emoji_info) {
|
|
|
|
blueslip.error(`Cannot find/add realm emoji for code '${r.emoji_code}'.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r.url = emoji_info.emoji_url;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
opts.message.clean_reactions.set(opts.local_id, r);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_user_fields = function (r) {
|
|
|
|
r.count = r.user_ids.length;
|
|
|
|
r.label = generate_title(r.emoji_name, r.user_ids);
|
|
|
|
if (r.user_ids.includes(page_params.user_id)) {
|
|
|
|
r.class = "message_reaction reacted";
|
|
|
|
} else {
|
|
|
|
r.class = "message_reaction";
|
|
|
|
}
|
2016-12-02 13:23:23 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.reactions = exports;
|