2021-01-13 21:59:11 -05:00
|
|
|
"use strict";
|
|
|
|
var g_data = {
|
|
|
|
whoami: null,
|
|
|
|
connections: [],
|
|
|
|
messages: [],
|
2022-01-31 20:47:18 -05:00
|
|
|
messages_by_id: {},
|
2021-01-13 21:59:11 -05:00
|
|
|
users: {},
|
|
|
|
broadcasts: [],
|
|
|
|
show_connect_dialog: false,
|
|
|
|
show_user_dialog: null,
|
|
|
|
connect: null,
|
|
|
|
pubs: [],
|
2021-01-19 21:20:40 -05:00
|
|
|
votes: {},
|
2021-01-13 21:59:11 -05:00
|
|
|
apps: {},
|
2021-12-31 14:01:53 -05:00
|
|
|
reply_root: null,
|
|
|
|
reply_branch: null,
|
2021-12-31 10:12:46 -05:00
|
|
|
mentions: {},
|
2021-12-29 09:57:10 -05:00
|
|
|
unread: 0,
|
2022-01-06 20:52:47 -05:00
|
|
|
loading: true,
|
2022-01-08 15:54:02 -05:00
|
|
|
selected: null,
|
|
|
|
edit_profile_name: null,
|
|
|
|
edit_profile_description: null,
|
2022-01-09 15:36:15 -05:00
|
|
|
load_time: null,
|
2022-01-15 17:11:05 -05:00
|
|
|
post_text: null,
|
2022-01-26 20:17:22 -05:00
|
|
|
times: {},
|
2021-01-13 21:59:11 -05:00
|
|
|
};
|
|
|
|
|
2022-01-09 15:36:15 -05:00
|
|
|
var g_load_start = new Date();
|
2021-01-13 21:59:11 -05:00
|
|
|
var g_data_initial = JSON.parse(JSON.stringify(g_data));
|
2021-12-29 14:46:55 -05:00
|
|
|
var g_message_queue = [];
|
|
|
|
var g_process_pending = false;
|
|
|
|
|
2022-01-08 15:54:02 -05:00
|
|
|
function updateEditUser() {
|
|
|
|
g_data.edit_profile_name = g_data.users[g_data.whoami] ? g_data.users[g_data.whoami].name : null;
|
|
|
|
g_data.edit_profile_description = g_data.users[g_data.whoami] ? g_data.users[g_data.whoami].description : null;
|
|
|
|
}
|
|
|
|
|
2021-12-29 14:46:55 -05:00
|
|
|
function processMessages() {
|
|
|
|
for (let event of g_message_queue) {
|
|
|
|
var key = Object.keys(event.data)[0];
|
|
|
|
if (key == 'message') {
|
2022-01-05 07:16:44 -05:00
|
|
|
var new_message = event.data.message;
|
|
|
|
new_message.children = [];
|
2021-12-29 14:46:55 -05:00
|
|
|
var found = false;
|
2022-01-05 07:16:44 -05:00
|
|
|
var root = JSON.parse(new_message.content).root;
|
2022-01-31 20:47:18 -05:00
|
|
|
|
|
|
|
/* 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;
|
2021-12-29 11:01:34 -05:00
|
|
|
}
|
2022-01-31 20:47:18 -05:00
|
|
|
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]) {
|
2022-01-14 19:29:51 -05:00
|
|
|
var fake_root = {
|
|
|
|
id: root,
|
2022-01-31 20:47:18 -05:00
|
|
|
children: [],
|
2022-01-14 19:29:51 -05:00
|
|
|
timestamp: new_message.timestamp,
|
|
|
|
content: '{}',
|
|
|
|
};
|
2022-01-31 20:47:18 -05:00
|
|
|
Vue.set(g_data.messages_by_id, root, fake_root);
|
2022-01-14 19:29:51 -05:00
|
|
|
g_data.messages.push(fake_root);
|
|
|
|
g_data.messages.sort((x, y) => y.timestamp - x.timestamp);
|
|
|
|
found = true;
|
|
|
|
}
|
2022-01-31 20:47:18 -05:00
|
|
|
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. */
|
2022-01-05 07:16:44 -05:00
|
|
|
g_data.messages.push(new_message);
|
2021-12-29 14:46:55 -05:00
|
|
|
g_data.messages.sort((x, y) => y.timestamp - x.timestamp);
|
2022-02-13 16:49:53 -05:00
|
|
|
g_data.messages = g_data.messages.slice(0, 32);
|
2021-12-29 11:01:34 -05:00
|
|
|
}
|
2021-12-29 14:46:55 -05:00
|
|
|
} else if (key + 's' in g_data && Array.isArray(g_data[key + 's'])) {
|
|
|
|
g_data[key + 's'].push(event.data[key]);
|
|
|
|
} else if (key == 'user') {
|
|
|
|
Vue.set(g_data.users, event.data.user.user, Object.assign({}, g_data.users[event.data.user.user] || {}, event.data.user.about));
|
2022-01-08 15:54:02 -05:00
|
|
|
if (event.data.user.user == g_data.whoami) {
|
|
|
|
updateEditUser();
|
|
|
|
}
|
2021-12-29 14:46:55 -05:00
|
|
|
} else if (key == 'following') {
|
|
|
|
if (!g_data.users[event.data.following.id]) {
|
|
|
|
Vue.set(g_data.users, event.data.following.id, {});
|
|
|
|
}
|
2021-12-29 16:00:03 -05:00
|
|
|
if (!g_data.users[event.data.following.id].following) {
|
|
|
|
Vue.set(g_data.users[event.data.following.id], 'following', {});
|
|
|
|
}
|
|
|
|
for (let user of event.data.following.users) {
|
|
|
|
Vue.set(g_data.users[event.data.following.id].following, user, true);
|
2022-01-23 14:15:18 -05:00
|
|
|
if (!g_data.users[user]) {
|
|
|
|
Vue.set(g_data.users, user, {});
|
|
|
|
}
|
|
|
|
if (!g_data.users[user].followers) {
|
|
|
|
Vue.set(g_data.users[user], 'followers', {});
|
|
|
|
}
|
|
|
|
Vue.set(g_data.users[user].followers, event.data.following.id, true);
|
2021-12-29 16:00:03 -05:00
|
|
|
}
|
2021-12-29 14:46:55 -05:00
|
|
|
} else if (key == 'broadcasts') {
|
|
|
|
g_data.broadcasts = event.data.broadcasts;
|
|
|
|
} else if (key == 'pubs') {
|
|
|
|
g_data.pubs = event.data.pubs;
|
|
|
|
} else if (key == 'apps') {
|
|
|
|
g_data.apps = event.data.apps;
|
|
|
|
} else if (key == 'votes') {
|
|
|
|
event.data.votes.forEach(function(vote) {
|
|
|
|
var content = JSON.parse(vote.content);
|
|
|
|
var link = content.vote.link;
|
|
|
|
if (!g_data.votes[link]) {
|
|
|
|
Vue.set(g_data.votes, link, {});
|
|
|
|
}
|
|
|
|
if (!g_data.votes[link][content.vote.expression]) {
|
|
|
|
Vue.set(g_data.votes[link], content.vote.expression, []);
|
|
|
|
}
|
|
|
|
g_data.votes[link][content.vote.expression].push({author: vote.author, value: content.vote.value});
|
|
|
|
});
|
|
|
|
} else if (key == 'clear') {
|
2022-01-09 15:36:15 -05:00
|
|
|
g_load_start = new Date();
|
2022-01-06 20:52:47 -05:00
|
|
|
g_data.loading = true;
|
2021-12-29 14:46:55 -05:00
|
|
|
Object.keys(g_data_initial).forEach(function(key) {
|
|
|
|
Vue.set(g_data, key, JSON.parse(JSON.stringify(g_data_initial[key])));
|
|
|
|
});
|
2022-01-06 20:52:47 -05:00
|
|
|
} else if (key == 'ready') {
|
2022-01-09 15:36:15 -05:00
|
|
|
g_data.load_time = (new Date() - g_load_start) / 1000;
|
2022-01-06 20:52:47 -05:00
|
|
|
g_data.loading = false;
|
2022-01-26 20:17:22 -05:00
|
|
|
g_data.times = event.data.times;
|
2021-12-29 14:46:55 -05:00
|
|
|
} else if (key == 'unread') {
|
|
|
|
g_data.unread += event.data.unread;
|
2022-01-06 20:52:47 -05:00
|
|
|
} else if (key == 'hash') {
|
2022-01-08 15:54:02 -05:00
|
|
|
g_data.selected = event.data.hash;
|
|
|
|
if (g_data.selected == g_data.whoami) {
|
|
|
|
updateEditUser();
|
|
|
|
}
|
2022-01-27 22:11:09 -05:00
|
|
|
} else if (key == 'storeBlobComplete') {
|
|
|
|
var blob = event.data.storeBlobComplete;
|
|
|
|
g_data.post_text = (g_data.post_text || '') + `\n![${blob.name}](${blob.path.substring(1)})`;
|
|
|
|
Vue.set(g_data.mentions, blob.path.substring(1), {
|
|
|
|
link: blob.path.substring(1),
|
|
|
|
name: blob.name,
|
|
|
|
type: blob.type,
|
|
|
|
});
|
2021-12-29 14:46:55 -05:00
|
|
|
} else {
|
|
|
|
g_data[key] = event.data[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_message_queue = [];
|
|
|
|
g_process_pending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('message', function(event) {
|
|
|
|
g_message_queue.push(event);
|
|
|
|
if (!g_process_pending) {
|
|
|
|
g_process_pending = true;
|
|
|
|
setTimeout(processMessages, 250);
|
2021-01-13 21:59:11 -05:00
|
|
|
}
|
|
|
|
});
|
2022-01-06 20:52:47 -05:00
|
|
|
|
2021-01-13 21:59:11 -05:00
|
|
|
window.addEventListener('load', function() {
|
|
|
|
Vue.use(VueMaterial.default);
|
|
|
|
var vue = new Vue({
|
|
|
|
el: '#app',
|
|
|
|
data: g_data,
|
|
|
|
methods: {
|
|
|
|
post_message: function() {
|
2021-12-31 10:12:46 -05:00
|
|
|
var message = {
|
|
|
|
type: 'post',
|
|
|
|
text: document.getElementById('post_text').value,
|
|
|
|
};
|
2021-12-31 14:01:53 -05:00
|
|
|
if (g_data.reply_root || g_data.reply_branch) {
|
|
|
|
message.root = g_data.reply_root;
|
|
|
|
message.branch = g_data.reply_branch;
|
|
|
|
}
|
2021-12-31 10:12:46 -05:00
|
|
|
if (Object.keys(g_data.mentions).length) {
|
|
|
|
message.mentions = Object.values(g_data.mentions);
|
2021-01-13 21:59:11 -05:00
|
|
|
}
|
2021-12-31 10:12:46 -05:00
|
|
|
window.parent.postMessage({appendMessage: message}, '*');
|
2022-01-31 20:47:18 -05:00
|
|
|
g_data.post_text = null;
|
2022-01-14 20:34:11 -05:00
|
|
|
Vue.set(g_data, 'mentions', {});
|
2021-12-31 14:01:53 -05:00
|
|
|
g_data.reply_root = null;
|
|
|
|
g_data.reply_branch = null;
|
2021-01-13 21:59:11 -05:00
|
|
|
},
|
|
|
|
ssb_connect: function(connection) {
|
|
|
|
window.parent.postMessage({connect: connection}, '*');
|
|
|
|
},
|
|
|
|
content_json: function(message) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(message.content);
|
|
|
|
} catch {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
},
|
2022-01-18 21:37:39 -05:00
|
|
|
markdown: markdown,
|
2021-01-13 21:59:11 -05:00
|
|
|
refresh: function() {
|
|
|
|
window.parent.postMessage({refresh: true}, '*');
|
|
|
|
},
|
2021-12-31 10:12:46 -05:00
|
|
|
add_app_to_mentions: function(app) {
|
|
|
|
Vue.set(g_data.mentions, g_data.apps[app], {
|
|
|
|
link: g_data.apps[app],
|
|
|
|
name: app,
|
|
|
|
type: 'application/tildefriends',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
remove_from_mentions: function(link) {
|
|
|
|
Vue.delete(g_data.mentions, link);
|
|
|
|
},
|
2022-01-08 15:54:02 -05:00
|
|
|
save_profile: function() {
|
|
|
|
var message = {appendMessage: {
|
|
|
|
type: 'about',
|
|
|
|
about: g_data.selected,
|
|
|
|
name: g_data.edit_profile_name,
|
|
|
|
description: g_data.edit_profile_description,
|
|
|
|
}};
|
|
|
|
window.parent.postMessage(message, '*');
|
|
|
|
},
|
|
|
|
follow: function(id) {
|
|
|
|
if (confirm('Are you sure you want to follow ' + id + '?')) {
|
|
|
|
window.parent.postMessage({appendMessage: {type: "contact", following: true, contact: id}}, '*');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unfollow: function(id) {
|
|
|
|
if (confirm('Are you sure you want to unfollow ' + id + '?')) {
|
|
|
|
window.parent.postMessage({appendMessage: {type: "contact", following: false, contact: id}}, '*');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set_hash(hash) {
|
|
|
|
window.parent.postMessage({action: 'setHash', hash: hash ? hash : '#'}, '*');
|
|
|
|
},
|
2022-01-27 22:11:09 -05:00
|
|
|
attach() {
|
|
|
|
var input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.onchange = function(event) {
|
|
|
|
var file = event.target.files[0];
|
|
|
|
file.arrayBuffer().then(function(buffer) {
|
|
|
|
window.parent.postMessage({action: 'storeBlob',
|
|
|
|
blob: {
|
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
buffer: buffer,
|
|
|
|
}
|
|
|
|
}, '*');
|
|
|
|
}).catch(function(e) {
|
|
|
|
console.log('error', e);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
input.click();
|
|
|
|
},
|
2022-01-29 14:49:01 -05:00
|
|
|
paste(event) {
|
|
|
|
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
|
|
|
|
for (let item of items) {
|
|
|
|
var file = item.getAsFile();
|
|
|
|
if (file) {
|
|
|
|
file.arrayBuffer().then(function(buffer) {
|
|
|
|
window.parent.postMessage({
|
|
|
|
action: 'storeBlob',
|
|
|
|
blob: {
|
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
buffer: buffer,
|
|
|
|
},
|
|
|
|
}, '*');
|
|
|
|
});
|
|
|
|
event.preventDefault();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-13 21:59:11 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
window.parent.postMessage('ready', '*');
|
|
|
|
});
|