2021-02-17 21:58:06 +01:00
|
|
|
function transformMessageText(message) {
|
2021-04-26 17:40:07 +02:00
|
|
|
return message
|
|
|
|
.split(/(?<=:|\W|^)(:[a-z0-9_-]+:)(?=:|\W|$)/)
|
|
|
|
.map((x, i) => i % 2
|
|
|
|
? emoji[x.slice(1, -1)] || x
|
|
|
|
: x)
|
|
|
|
.join("");
|
2021-02-17 21:58:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-11 17:25:12 +02:00
|
|
|
function shouldShowTime(lastShownTime, time) {
|
|
|
|
const sim = dateSimilarity(lastShownTime, time);
|
|
|
|
if (sim < DATE_SIMILARITY.date) {
|
|
|
|
return {date: true, time: true};
|
|
|
|
}
|
|
|
|
const minutesSinceLastShown = (time - lastShownTime) / 1000 / 60;
|
|
|
|
if (minutesSinceLastShown >= 15) {
|
|
|
|
return {date: false, time: true};
|
|
|
|
}
|
|
|
|
return {date: false, time: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
async function createMessageElement(client, post, lastTime, lastAuthor) {
|
2022-06-11 17:25:12 +02:00
|
|
|
let nodes = [];
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
const users = await client.getUsers();
|
|
|
|
const isThreadReply = !!post.parent_id;
|
|
|
|
|
2022-06-11 17:25:12 +02:00
|
|
|
const createAt = new Date(post.create_at);
|
|
|
|
let createAtLocalized = createAt;
|
|
|
|
let lastTimeLocalized = lastTime;
|
|
|
|
try {
|
|
|
|
if (client.me.timezone.useAutomaticTimezone !== "true") {
|
|
|
|
createAtLocalized = toTimeZone(createAt, client.me.timezone.manualTimezone);
|
|
|
|
lastTimeLocalized = toTimeZone(lastTime, client.me.timezone.manualTimezone);
|
|
|
|
}
|
|
|
|
} catch(e) {}
|
|
|
|
const showTime = shouldShowTime(lastTimeLocalized, createAtLocalized);
|
|
|
|
if (showTime.time) {
|
|
|
|
lastTime = createAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const postDiv = document.createElement("div");
|
|
|
|
postDiv.className = "post";
|
|
|
|
if (post.type !== "") {
|
|
|
|
postDiv.className += " special";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showTime.date) {
|
|
|
|
const dateElement = document.createElement("div");
|
|
|
|
dateElement.className = "date_separator";
|
|
|
|
dateElement.innerText = formatYyyyMmDdWeekday(createAtLocalized);
|
|
|
|
nodes.push(dateElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showTime.time) {
|
|
|
|
postDiv.className += " show_time";
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
const messageDiv = document.createElement("div");
|
|
|
|
messageDiv.className = "message";
|
2021-02-17 21:58:06 +01:00
|
|
|
messageDiv.innerText = transformMessageText(post.message);
|
2021-02-17 16:07:15 +01:00
|
|
|
|
|
|
|
const createAtDiv = document.createElement("time");
|
|
|
|
createAtDiv.className = "create_at";
|
2022-06-11 17:25:12 +02:00
|
|
|
let createAtText = formatHhMm(createAtLocalized);
|
2021-02-17 16:07:15 +01:00
|
|
|
createAtDiv.title = createAt.toString();
|
|
|
|
createAtDiv.innerText = createAtText;
|
|
|
|
createAtDiv.dateTime = createAt.toISOString();
|
|
|
|
|
|
|
|
const authorName = users[post.user_id] ? users[post.user_id].username : post.user_id;
|
|
|
|
const authorDiv = document.createElement("div");
|
|
|
|
authorDiv.className = "author";
|
|
|
|
authorDiv.innerText = authorName;
|
|
|
|
|
|
|
|
if (lastAuthor === post.user_id) {
|
|
|
|
postDiv.className += " same_author";
|
|
|
|
}
|
|
|
|
lastAuthor = post.user_id;
|
|
|
|
|
|
|
|
postDiv.dataset["id"] = post.id;
|
|
|
|
postDiv.appendChild(authorDiv);
|
|
|
|
postDiv.appendChild(createAtDiv);
|
|
|
|
postDiv.appendChild(messageDiv);
|
|
|
|
|
2021-02-17 18:15:57 +01:00
|
|
|
if (post.metadata && (post.metadata.files || []).length > 0) {
|
2021-02-17 16:07:15 +01:00
|
|
|
const attachmentsUl = document.createElement("ul");
|
|
|
|
attachmentsUl.className = "attachments";
|
|
|
|
for (let file of post.metadata.files || []) {
|
|
|
|
const attachmentLi = document.createElement("li");
|
|
|
|
attachmentLi.dataset["id"] = file.id;
|
|
|
|
|
|
|
|
const attachmentA = document.createElement("a");
|
|
|
|
client.filePublicLink(file.id).then(link => attachmentA.href = link);
|
|
|
|
attachmentA.target = "_blank";
|
|
|
|
attachmentA.innerText = file.name;
|
|
|
|
|
|
|
|
if (file.mini_preview) {
|
|
|
|
const attachmentImg = document.createElement("img");
|
|
|
|
attachmentImg.src = `data:image/jpeg;base64,${file.mini_preview}`;
|
|
|
|
attachmentA.appendChild(attachmentImg);
|
|
|
|
}
|
|
|
|
|
|
|
|
attachmentLi.appendChild(attachmentA);
|
|
|
|
attachmentsUl.appendChild(attachmentLi);
|
|
|
|
}
|
|
|
|
postDiv.appendChild(attachmentsUl);
|
|
|
|
}
|
|
|
|
|
2022-06-11 17:25:12 +02:00
|
|
|
nodes.push(postDiv);
|
|
|
|
|
|
|
|
return {nodes, lastTime, lastAuthor};
|
2021-02-17 16:07:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function populateChannelContents(client, channel, contents) {
|
2020-03-31 12:09:33 +02:00
|
|
|
byId("channel_contents").innerHTML = "";
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
let result = {lastAuthor: null, lastTime: null, postDiv: null};
|
2021-02-07 21:47:59 +01:00
|
|
|
|
2020-03-31 12:09:33 +02:00
|
|
|
let nodes = [];
|
|
|
|
for (let id of contents.order) {
|
2021-02-17 16:07:15 +01:00
|
|
|
result = await createMessageElement(client, contents.posts[id], result.lastTime, result.lastAuthor);
|
2022-06-11 17:25:12 +02:00
|
|
|
nodes.push(...result.nodes);
|
2020-03-31 12:09:33 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
byId("channel_contents").dataset["server"] = client.endpoint;
|
|
|
|
byId("channel_contents").dataset["id"] = channel.id;
|
|
|
|
byId("channel_contents").dataset["lastTime"] = result.lastTime.getTime();
|
|
|
|
byId("channel_contents").dataset["lastAuthor"] = result.lastAuthor;
|
2020-03-31 12:09:33 +02:00
|
|
|
byId("channel_contents").append(...nodes);
|
2021-02-07 21:10:36 +01:00
|
|
|
scrollToBottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
async function addMessage(client, post) {
|
|
|
|
const shouldScroll = isScrolledToBottom();
|
|
|
|
|
|
|
|
const result = await createMessageElement(
|
|
|
|
client, post,
|
|
|
|
new Date(1 * (byId("channel_contents").dataset["lastTime"])),
|
|
|
|
byId("channel_contents").dataset["lastAuthor"]
|
|
|
|
);
|
|
|
|
byId("channel_contents").dataset["lastTime"] = result.lastTime.getTime();
|
|
|
|
byId("channel_contents").dataset["lastAuthor"] = result.lastAuthor;
|
2022-06-11 17:25:12 +02:00
|
|
|
byId("channel_contents").append(...result.nodes);
|
2021-02-17 16:07:15 +01:00
|
|
|
|
|
|
|
if (shouldScroll) {
|
|
|
|
scrollToBottom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-07 21:10:36 +01:00
|
|
|
function scrollToBottom() {
|
|
|
|
const el = byId("channel_contents_wrapper");
|
|
|
|
el.scrollTop = el.scrollHeight;
|
|
|
|
el.className = "";
|
2020-03-31 12:09:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function updateComposeHeight() {
|
|
|
|
byId("compose").style.height = "";
|
|
|
|
byId("compose").style.height = (byId("compose").scrollHeight + 1) + "px";
|
|
|
|
}
|
|
|
|
byId("compose").addEventListener("input", updateComposeHeight);
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
function isScrolledToBottom() {
|
2020-03-31 12:09:33 +02:00
|
|
|
const el = byId("channel_contents_wrapper");
|
|
|
|
|
|
|
|
const scrolledTo = el.clientHeight + el.scrollTop;
|
2022-06-17 14:03:27 +02:00
|
|
|
return scrolledTo + 1 >= el.scrollHeight;
|
2021-02-17 16:07:15 +01:00
|
|
|
}
|
|
|
|
function checkScrolledToBottom() {
|
2022-06-09 00:48:59 +02:00
|
|
|
if (isScrolledToBottom()) {
|
|
|
|
removeClass(byId("channel_contents_wrapper"), "not-at-bottom");
|
|
|
|
} else {
|
|
|
|
addClass(byId("channel_contents_wrapper"), "not-at-bottom");
|
|
|
|
}
|
2020-03-31 12:09:33 +02:00
|
|
|
}
|
|
|
|
byId("channel_contents_wrapper").addEventListener("scroll", checkScrolledToBottom);
|
2021-02-17 16:07:15 +01:00
|
|
|
|
|
|
|
|
2022-06-08 17:57:06 +02:00
|
|
|
function currentChannel() {
|
|
|
|
return {
|
|
|
|
endpoint: byId("channel_contents").dataset["server"],
|
|
|
|
channel_id: byId("channel_contents").dataset["id"]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 16:07:15 +01:00
|
|
|
pubsub.subscribe("MESSAGES_NEW", post => {
|
2022-06-08 17:57:06 +02:00
|
|
|
const chan = currentChannel();
|
|
|
|
if (post.endpoint === chan.endpoint && post.channel_id === chan.channel_id) {
|
2022-06-09 14:35:49 +02:00
|
|
|
addMessage(mm_client.getOrCreate(post.endpoint), post);
|
2021-02-17 16:07:15 +01:00
|
|
|
}
|
|
|
|
});
|