forked from cory/tildefriends
Cory McWilliams
3026443c1e
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3726 ed5197a5-7fde-0310-b194-c3ffbd925b24
165 lines
4.9 KiB
JavaScript
165 lines
4.9 KiB
JavaScript
"use strict";
|
|
var g_data = {
|
|
whoami: null,
|
|
connections: [],
|
|
messages: [],
|
|
users: {},
|
|
broadcasts: [],
|
|
show_connect_dialog: false,
|
|
show_user_dialog: null,
|
|
connect: null,
|
|
pubs: [],
|
|
votes: {},
|
|
apps: {},
|
|
reply_root: null,
|
|
reply_branch: null,
|
|
mentions: {},
|
|
unread: 0,
|
|
};
|
|
|
|
var g_data_initial = JSON.parse(JSON.stringify(g_data));
|
|
var g_message_queue = [];
|
|
var g_process_pending = false;
|
|
|
|
function processMessages() {
|
|
for (let event of g_message_queue) {
|
|
var key = Object.keys(event.data)[0];
|
|
if (key == 'message') {
|
|
event.data[key].children = [];
|
|
var found = false;
|
|
var root = JSON.parse(event.data[key].content).root;
|
|
if (root) {
|
|
for (let message of g_data.messages) {
|
|
if (root == message.id) {
|
|
message.children.push(event.data[key]);
|
|
message.children.sort((x, y) => y.timestamp - x.timestamp);
|
|
found = true;
|
|
}
|
|
}
|
|
}
|
|
for (let message of g_data.messages) {
|
|
if (JSON.parse(message.content).root == event.data[key].id) {
|
|
event.data[key].children.push(message);
|
|
event.data[key].children.sort((x, y) => y.timestamp - x.timestamp);
|
|
g_data.messages.splice(g_data.messages.indexOf(message), 1);
|
|
break;
|
|
}
|
|
}
|
|
if (!found) {
|
|
g_data.messages.push(event.data[key]);
|
|
g_data.messages.sort((x, y) => y.timestamp - x.timestamp);
|
|
}
|
|
} 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));
|
|
} else if (key == 'followers') {
|
|
if (!g_data.users[event.data.followers.id]) {
|
|
Vue.set(g_data.users, event.data.followers.id, {});
|
|
}
|
|
if (!g_data.users[event.data.followers.id].followers) {
|
|
Vue.set(g_data.users[event.data.followers.id], 'followers', {});
|
|
}
|
|
for (let user of event.data.followers.users) {
|
|
Vue.set(g_data.users[event.data.followers.id].followers, user, true);
|
|
}
|
|
} else if (key == 'following') {
|
|
if (!g_data.users[event.data.following.id]) {
|
|
Vue.set(g_data.users, event.data.following.id, {});
|
|
}
|
|
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);
|
|
}
|
|
} 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') {
|
|
Object.keys(g_data_initial).forEach(function(key) {
|
|
Vue.set(g_data, key, JSON.parse(JSON.stringify(g_data_initial[key])));
|
|
});
|
|
} else if (key == 'unread') {
|
|
g_data.unread += event.data.unread;
|
|
} 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);
|
|
}
|
|
});
|
|
window.addEventListener('load', function() {
|
|
Vue.use(VueMaterial.default);
|
|
var vue = new Vue({
|
|
el: '#app',
|
|
data: g_data,
|
|
methods: {
|
|
post_message: function() {
|
|
var message = {
|
|
type: 'post',
|
|
text: document.getElementById('post_text').value,
|
|
};
|
|
if (g_data.reply_root || g_data.reply_branch) {
|
|
message.root = g_data.reply_root;
|
|
message.branch = g_data.reply_branch;
|
|
}
|
|
if (Object.keys(g_data.mentions).length) {
|
|
message.mentions = Object.values(g_data.mentions);
|
|
}
|
|
window.parent.postMessage({appendMessage: message}, '*');
|
|
document.getElementById('post_text').value = '';
|
|
Vue.set(g_data, mentions, {});
|
|
g_data.reply_root = null;
|
|
g_data.reply_branch = null;
|
|
},
|
|
ssb_connect: function(connection) {
|
|
window.parent.postMessage({connect: connection}, '*');
|
|
},
|
|
content_json: function(message) {
|
|
try {
|
|
return JSON.parse(message.content);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
},
|
|
refresh: function() {
|
|
window.parent.postMessage({refresh: true}, '*');
|
|
},
|
|
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);
|
|
},
|
|
}
|
|
});
|
|
window.parent.postMessage('ready', '*');
|
|
}); |