2021-02-28 21:16:38 +01:00
|
|
|
import {format, isSameDay} from "date-fns";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-02-28 21:16:38 +01:00
|
|
|
|
2021-02-10 16:56:33 +01:00
|
|
|
import render_message_edit_history from "../templates/message_edit_history.hbs";
|
2021-06-16 15:20:20 +02:00
|
|
|
import render_message_history_modal from "../templates/message_history_modal.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
import * as channel from "./channel";
|
2021-07-07 11:47:18 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
2022-01-16 06:20:07 +01:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-06-16 09:20:53 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
|
|
|
import {page_params} from "./page_params";
|
2021-02-10 16:56:33 +01:00
|
|
|
import * as people from "./people";
|
2021-06-16 09:20:53 +02:00
|
|
|
import * as popovers from "./popovers";
|
2022-02-22 10:35:06 +01:00
|
|
|
import * as rendered_markdown from "./rendered_markdown";
|
2021-06-16 09:20:53 +02:00
|
|
|
import * as rows from "./rows";
|
2022-04-29 20:49:44 +02:00
|
|
|
import * as spectators from "./spectators";
|
2022-01-11 07:26:00 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
|
|
|
import * as sub_store from "./sub_store";
|
2021-02-28 01:14:36 +01:00
|
|
|
import * as timerender from "./timerender";
|
2021-02-28 00:58:55 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2020-06-07 04:50:31 +02:00
|
|
|
|
2021-02-10 16:56:33 +01:00
|
|
|
export function fetch_and_render_message_history(message) {
|
2020-06-07 04:50:31 +02:00
|
|
|
channel.get({
|
|
|
|
url: "/json/messages/" + message.id + "/history",
|
|
|
|
data: {message_id: JSON.stringify(message.id)},
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2020-06-07 04:50:31 +02:00
|
|
|
const content_edit_history = [];
|
2021-02-05 21:20:14 +01:00
|
|
|
let prev_time = null;
|
2022-02-01 22:58:46 +01:00
|
|
|
let prev_stream_item = null;
|
2020-06-07 04:50:31 +02:00
|
|
|
|
|
|
|
for (const [index, msg] of data.message_history.entries()) {
|
2020-06-02 13:28:33 +02:00
|
|
|
// Format times and dates nicely for display
|
2021-02-05 21:20:14 +01:00
|
|
|
const time = new Date(msg.timestamp * 1000);
|
2020-06-07 04:50:31 +02:00
|
|
|
const item = {
|
2020-06-02 14:52:06 +02:00
|
|
|
timestamp: timerender.stringify_time(time),
|
2021-02-05 21:20:14 +01:00
|
|
|
display_date: format(time, "MMMM d, yyyy"),
|
|
|
|
show_date_row: prev_time === null || !isSameDay(time, prev_time),
|
2020-06-07 04:50:31 +02:00
|
|
|
};
|
2020-06-02 13:28:33 +02:00
|
|
|
|
2022-02-01 23:09:14 +01:00
|
|
|
if (!msg.user_id) {
|
|
|
|
continue;
|
2020-06-07 04:50:31 +02:00
|
|
|
}
|
|
|
|
|
2022-02-01 23:09:14 +01:00
|
|
|
const person = people.get_by_user_id(msg.user_id);
|
|
|
|
const full_name = person.full_name;
|
|
|
|
|
2020-06-07 04:50:31 +02:00
|
|
|
if (index === 0) {
|
2022-02-01 23:09:14 +01:00
|
|
|
item.edited_by_notice = $t(
|
|
|
|
{defaultMessage: "Posted by {full_name}"},
|
|
|
|
{full_name},
|
|
|
|
);
|
2020-06-07 04:50:31 +02:00
|
|
|
item.body_to_render = msg.rendered_content;
|
|
|
|
} else if (msg.prev_topic && msg.prev_content) {
|
2022-02-01 23:09:14 +01:00
|
|
|
item.edited_by_notice = $t(
|
|
|
|
{defaultMessage: "Edited by {full_name}"},
|
|
|
|
{full_name},
|
|
|
|
);
|
2020-06-07 04:50:31 +02:00
|
|
|
item.body_to_render = msg.content_html_diff;
|
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
2022-01-11 07:26:00 +01:00
|
|
|
} else if (msg.prev_topic && msg.prev_stream) {
|
|
|
|
const sub = sub_store.get(msg.prev_stream);
|
2022-02-01 23:09:14 +01:00
|
|
|
item.edited_by_notice = $t(
|
|
|
|
{defaultMessage: "Moved by {full_name}"},
|
|
|
|
{full_name},
|
|
|
|
);
|
2022-01-11 07:26:00 +01:00
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
|
|
|
item.stream_changed = true;
|
|
|
|
if (!sub) {
|
2022-01-16 06:20:07 +01:00
|
|
|
item.prev_stream = $t({defaultMessage: "Unknown stream"});
|
2022-01-11 07:26:00 +01:00
|
|
|
} else {
|
|
|
|
item.prev_stream = stream_data.maybe_get_stream_name(msg.prev_stream);
|
|
|
|
}
|
2022-02-01 22:58:46 +01:00
|
|
|
if (prev_stream_item !== null) {
|
|
|
|
prev_stream_item.new_stream = stream_data.maybe_get_stream_name(
|
|
|
|
msg.prev_stream,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
prev_stream_item = item;
|
2020-06-07 04:50:31 +02:00
|
|
|
} else if (msg.prev_topic) {
|
2022-02-01 23:09:14 +01:00
|
|
|
item.edited_by_notice = $t(
|
|
|
|
{defaultMessage: "Moved by {full_name}"},
|
|
|
|
{full_name},
|
|
|
|
);
|
2020-06-07 04:50:31 +02:00
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
2022-01-11 07:26:00 +01:00
|
|
|
} else if (msg.prev_stream) {
|
|
|
|
const sub = sub_store.get(msg.prev_stream);
|
2022-02-01 23:09:14 +01:00
|
|
|
item.edited_by_notice = $t(
|
|
|
|
{defaultMessage: "Moved by {full_name}"},
|
|
|
|
{full_name},
|
|
|
|
);
|
2022-01-11 07:26:00 +01:00
|
|
|
item.stream_changed = true;
|
|
|
|
if (!sub) {
|
2022-01-16 06:20:07 +01:00
|
|
|
item.prev_stream = $t({defaultMessage: "Unknown stream"});
|
2022-01-11 07:26:00 +01:00
|
|
|
} else {
|
|
|
|
item.prev_stream = stream_data.maybe_get_stream_name(msg.prev_stream);
|
|
|
|
}
|
2022-02-01 22:58:46 +01:00
|
|
|
if (prev_stream_item !== null) {
|
|
|
|
prev_stream_item.new_stream = stream_data.maybe_get_stream_name(
|
|
|
|
msg.prev_stream,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
prev_stream_item = item;
|
2020-06-07 04:50:31 +02:00
|
|
|
} else {
|
|
|
|
// just a content edit
|
2022-02-01 23:09:14 +01:00
|
|
|
item.edited_by_notice = $t(
|
|
|
|
{defaultMessage: "Edited by {full_name}"},
|
|
|
|
{full_name},
|
|
|
|
);
|
2020-06-07 04:50:31 +02:00
|
|
|
item.body_to_render = msg.content_html_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
content_edit_history.push(item);
|
2021-02-05 21:20:14 +01:00
|
|
|
prev_time = time;
|
2020-06-07 04:50:31 +02:00
|
|
|
}
|
2022-02-01 22:58:46 +01:00
|
|
|
if (prev_stream_item !== null) {
|
|
|
|
prev_stream_item.new_stream = stream_data.maybe_get_stream_name(message.stream_id);
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#message-history").attr("data-message-id", message.id);
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#message-history").html(
|
|
|
|
render_message_edit_history({
|
|
|
|
edited_messages: content_edit_history,
|
|
|
|
}),
|
|
|
|
);
|
2022-02-22 10:35:06 +01:00
|
|
|
// Pass the history through rendered_markdown.js
|
|
|
|
// to update dynamic_elements in the content.
|
|
|
|
$("#message-history")
|
|
|
|
.find(".rendered_markdown")
|
|
|
|
.each(function () {
|
|
|
|
rendered_markdown.update_elements($(this));
|
|
|
|
});
|
2020-06-07 04:50:31 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 00:34:28 +02:00
|
|
|
ui_report.error(
|
2021-04-13 05:18:25 +02:00
|
|
|
$t_html({defaultMessage: "Error fetching message edit history"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
xhr,
|
2021-07-07 11:47:18 +02:00
|
|
|
$("#dialog_error"),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2020-06-07 04:50:31 +02:00
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:56:33 +01:00
|
|
|
}
|
2020-06-07 04:50:31 +02:00
|
|
|
|
2021-02-10 16:56:33 +01:00
|
|
|
export function show_history(message) {
|
2021-06-16 15:20:20 +02:00
|
|
|
const rendered_message_history = render_message_history_modal();
|
|
|
|
|
2021-07-07 11:47:18 +02:00
|
|
|
dialog_widget.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Message edit history"}),
|
|
|
|
html_body: rendered_message_history,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Close"}),
|
|
|
|
id: "message-edit-history",
|
|
|
|
on_click: () => {},
|
|
|
|
close_on_submit: true,
|
|
|
|
focus_submit_on_open: true,
|
|
|
|
single_footer_button: true,
|
|
|
|
post_render: () => {
|
|
|
|
fetch_and_render_message_history(message);
|
|
|
|
},
|
|
|
|
});
|
2021-02-10 16:56:33 +01:00
|
|
|
}
|
2021-06-14 12:27:26 +02:00
|
|
|
|
|
|
|
export function initialize() {
|
2021-06-16 09:20:53 +02:00
|
|
|
$("body").on("mouseenter", ".message_edit_notice", (e) => {
|
|
|
|
if (page_params.realm_allow_edit_history) {
|
|
|
|
$(e.currentTarget).addClass("message_edit_notice_hover");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("body").on("mouseleave", ".message_edit_notice", (e) => {
|
|
|
|
if (page_params.realm_allow_edit_history) {
|
|
|
|
$(e.currentTarget).removeClass("message_edit_notice_hover");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("body").on("click", ".message_edit_notice", (e) => {
|
2022-04-29 20:49:44 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2021-06-16 09:20:53 +02:00
|
|
|
popovers.hide_all();
|
2022-04-29 20:49:44 +02:00
|
|
|
|
2021-06-16 09:20:53 +02:00
|
|
|
const message_id = rows.id($(e.currentTarget).closest(".message_row"));
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = message_lists.current.get_row(message_id);
|
|
|
|
const message = message_lists.current.get(rows.id($row));
|
2021-06-16 09:20:53 +02:00
|
|
|
|
2022-04-29 20:49:44 +02:00
|
|
|
if (page_params.is_spectator) {
|
|
|
|
spectators.login_to_access();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:20:53 +02:00
|
|
|
if (page_params.realm_allow_edit_history) {
|
|
|
|
show_history(message);
|
2021-06-16 15:20:20 +02:00
|
|
|
$("#message-history-cancel").trigger("focus");
|
2021-06-16 09:20:53 +02:00
|
|
|
}
|
|
|
|
});
|
2021-06-14 12:27:26 +02:00
|
|
|
}
|