2021-02-10 16:56:33 +01:00
|
|
|
import render_message_edit_history from "../templates/message_edit_history.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
import * as channel from "./channel";
|
2021-02-10 16:56:33 +01:00
|
|
|
import * as people from "./people";
|
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
|
|
|
const {format, isSameDay} = require("date-fns");
|
2020-08-20 21:24:06 +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;
|
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
|
|
|
|
2020-06-07 04:50:31 +02:00
|
|
|
if (msg.user_id) {
|
|
|
|
const person = people.get_by_user_id(msg.user_id);
|
|
|
|
item.edited_by = person.full_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
item.posted_or_edited = "Posted by";
|
|
|
|
item.body_to_render = msg.rendered_content;
|
|
|
|
} else if (msg.prev_topic && msg.prev_content) {
|
|
|
|
item.posted_or_edited = "Edited by";
|
|
|
|
item.body_to_render = msg.content_html_diff;
|
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
|
|
|
} else if (msg.prev_topic) {
|
|
|
|
item.posted_or_edited = "Topic edited by";
|
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
|
|
|
} else {
|
|
|
|
// just a content edit
|
|
|
|
item.posted_or_edited = "Edited by";
|
|
|
|
item.body_to_render = msg.content_html_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
content_edit_history.push(item);
|
2020-06-03 09:34:18 +02:00
|
|
|
|
2021-02-05 21:20:14 +01:00
|
|
|
prev_time = time;
|
2020-06-07 04:50:31 +02:00
|
|
|
}
|
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,
|
|
|
|
}),
|
|
|
|
);
|
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(
|
|
|
|
i18n.t("Error fetching message edit history"),
|
|
|
|
xhr,
|
|
|
|
$("#message-history-error"),
|
|
|
|
);
|
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) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#message-history").html("");
|
|
|
|
$("#message-edit-history").modal("show");
|
2021-02-10 16:56:33 +01:00
|
|
|
fetch_and_render_message_history(message);
|
|
|
|
}
|