Fixed some problems linking messages with their threads in the UI.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3811 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-02-01 01:47:18 +00:00
parent bf7d5c34f6
commit c57b0a2f2f
4 changed files with 40 additions and 33 deletions

View File

@ -3,6 +3,7 @@ var g_data = {
whoami: null,
connections: [],
messages: [],
messages_by_id: {},
users: {},
broadcasts: [],
show_connect_dialog: false,
@ -42,45 +43,43 @@ function processMessages() {
new_message.children = [];
var found = false;
var root = JSON.parse(new_message.content).root;
if (root) {
for (let message of g_data.messages) {
if (root == message.id) {
message.children.push(new_message);
message.children.sort((x, y) => x.timestamp - y.timestamp);
found = true;
}
/* If we had inserted a fake root, replace it if we see the real message. */
if (g_data.messages_by_id[new_message.id]) {
var old_message = g_data.messages_by_id[new_message.id];
new_message.children = old_message.children;
for (let child of new_message.children) {
child.parent = new_message;
}
if (!found) {
if (old_message.parent) {
old_message.parent.children = old_message.parent.children.filter(x => x != old_message);
} else {
g_data.messages = g_data.messages.filter(x => x != old_message);
}
}
Vue.set(g_data.messages_by_id, new_message.id, new_message);
if (root) {
/* If we don't know of the message's root, add it. */
if (!g_data.messages_by_id[root]) {
var fake_root = {
id: root,
children: [new_message],
children: [],
timestamp: new_message.timestamp,
content: '{}',
};
Vue.set(g_data.messages_by_id, root, fake_root);
g_data.messages.push(fake_root);
g_data.messages.sort((x, y) => y.timestamp - x.timestamp);
found = true;
}
}
var removed = {};
for (let message of g_data.messages) {
if (JSON.parse(message.content).root == new_message.id) {
new_message.children.push(message);
new_message.children.sort((x, y) => x.timestamp - y.timestamp);
removed[message.id] = true;
}
}
g_data.messages = g_data.messages.filter(x => !removed[x.id]);
if (!found) {
for (let message of g_data.messages) {
if (message.id == new_message.id) {
new_message.children = message.children;
Vue.set(g_data.messages, g_data.messages.indexOf(message), new_message);
found = true;
}
}
}
if (!found) {
var message = g_data.messages_by_id[root];
new_message.parent = message;
message.children.push(new_message);
message.children.sort((x, y) => x.timestamp - y.timestamp);
} else {
/* This is just a new message with no root. Add it. */
g_data.messages.push(new_message);
g_data.messages.sort((x, y) => y.timestamp - x.timestamp);
}
@ -136,6 +135,14 @@ function processMessages() {
g_data.load_time = (new Date() - g_load_start) / 1000;
g_data.loading = false;
g_data.times = event.data.times;
function dump(messages, indent) {
for (let m of messages) {
console.log(indent + m.id);
dump(m.children, indent + ' ');
}
}
dump(g_data.messages, '');
} else if (key == 'unread') {
g_data.unread += event.data.unread;
} else if (key == 'hash') {
@ -186,7 +193,7 @@ window.addEventListener('load', function() {
message.mentions = Object.values(g_data.mentions);
}
window.parent.postMessage({appendMessage: message}, '*');
document.getElementById('post_text').value = '';
g_data.post_text = null;
Vue.set(g_data, 'mentions', {});
g_data.reply_root = null;
g_data.reply_branch = null;