diff --git a/apps/cory/ssb.json b/apps/cory/ssb.json index 4361417b..7ea8cc8a 100644 --- a/apps/cory/ssb.json +++ b/apps/cory/ssb.json @@ -1 +1 @@ -{"type":"tildefriends-app","files":{"app.js":"&/KEl8MyPA3758dy9OHuLWMsU7PCbrxWm8B2Lp6D6VdA=.sha256","index.html":"&1A20L0gv3iOk6hqTdxrRX3yM1jNnUlSn9+NOC+HMBSk=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&BwircO1usZwNmgxReG3COtztqg6bVYnVenTKsLhOeLI=.sha256","tf-message.js":"&qYRsJfdVWpS/ZSAQ0+jeP4n61qziThP5GlhCeOtDYhQ=.sha256","tf.js":"&iy81728sf718+imn/XrU+T3zjJKbbuo+tlFU7wubrcw=.sha256","commonmark.min.js":"&EP0OeR9zyLwZannz+0ga4s9AGES2RLvvIIQYHqqV6+k=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256","tf-shared.js":"&g17mCj0bfk0tw0sYk2VMU67Bt8Hdznjl44E01TxQF5Y=.sha256","style.css":"&qegBNCrVUihxffRUxGFuG/6u+0Y6d18zHtfNHBZtZ04=.sha256"}} \ No newline at end of file +{"type":"tildefriends-app","files":{"app.js":"&7v8vYyzZmo/SFkcyrTPnlv/jZc9xt36zv7N9ZTdEt7s=.sha256","index.html":"&TGAqOmpnOigx4XQbFix82ugZEOfR1Gdc2zMWUQMSvwI=.sha256","vue-material.js":"&K5cdLqXYCENPak/TCINHQhyJhpS4G9DlZHGwoh/LF2g=.sha256","tf-user.js":"&smJOHGgIsKwQXeEJD3VbHX4A+vhr1Se54WlSHUP/Km8=.sha256","tf-message.js":"&nf978NOZPz1i97/nYMA2kHOm0HTlF4pm5UgpqE8+JSE=.sha256","tf.js":"&ZXBUsG/ZZWxd1RtrdcPPvpH4Qa2XvUUgL0x9KNqqSe0=.sha256","commonmark.min.js":"&EP0OeR9zyLwZannz+0ga4s9AGES2RLvvIIQYHqqV6+k=.sha256","vue.js":"&g1wvA+yHl1sVC+eufTsg9If7ZeVyMTBU+h0tks7ZNzE=.sha256","vue-material-theme-default-dark.css":"&RP2nr+2CR18BpHHw5ST9a5GJUCOG9n0G2kuGkcQioWE=.sha256","vue-material.min.css":"&kGbUM2QgFSyHZRzqQb0b+0S3EVIlZ0AXpdiAVjIhou8=.sha256","roboto.css":"&jJv43Om673mQO5JK0jj7714s5E+5Yrf82H6LcDx7wUs=.sha256","material-icons.css":"&a28PdcVvgq/DxyIvJAx/e+ZOEtOuHnr3kjLWKyzH11M=.sha256","tf-shared.js":"&MPINm55jkpz2rrNbwsYl09PKGvbgL3nwgBy6CMQkSnw=.sha256","style.css":"&qegBNCrVUihxffRUxGFuG/6u+0Y6d18zHtfNHBZtZ04=.sha256"}} \ No newline at end of file diff --git a/apps/cory/ssb/app.js b/apps/cory/ssb/app.js index 1ff37167..55f6d0a5 100644 --- a/apps/cory/ssb/app.js +++ b/apps/cory/ssb/app.js @@ -1,4 +1,4 @@ -"use strict"; +import * as tfrpc from '/tfrpc.js'; const k_posts_max = 40; const k_votes_max = 20; @@ -393,10 +393,17 @@ async function getPosts(db, ids) { return posts; } -async function ready() { +tfrpc.register(async function ready() { g_ready = true; return refresh(g_selected); -} +}); + +tfrpc.register(async function store_blob(blob) { + if (Array.isArray(blob)) { + blob = Uint8Array.from(blob); + } + return await ssb.blobStore(blob); +}); ssb.addEventListener('broadcasts', async function() { await app.postMessage({broadcasts: await ssb.getBroadcasts()}); @@ -568,9 +575,7 @@ async function addAppSources(message) { } core.register('message', async function(m) { - if (m.message == 'ready') { - await ready(); - } else if (m.message) { + if (m.message) { if (m.message.connect) { await ssb.connect(m.message.connect); } else if (m.message.appendMessage) { diff --git a/apps/cory/ssb/index.html b/apps/cory/ssb/index.html index 643dc053..963cd0df 100644 --- a/apps/cory/ssb/index.html +++ b/apps/cory/ssb/index.html @@ -9,10 +9,10 @@ - - - - + + + + diff --git a/apps/cory/ssb/tf-message.js b/apps/cory/ssb/tf-message.js index 41736e89..7fe9164c 100644 --- a/apps/cory/ssb/tf-message.js +++ b/apps/cory/ssb/tf-message.js @@ -1,4 +1,5 @@ -"use strict"; +import * as tfshared from './tf-shared.js'; + Vue.component('tf-message', { props: ['message', 'messages', 'votes'], data: function() { return { showRaw: false } }, @@ -39,7 +40,7 @@ Vue.component('tf-message', { }, }, methods: { - markdown: markdown, + markdown: tfshared.markdown, set_reply: function() { g_data.reply_root = this.content_json.root || this.message.id; g_data.reply_branch = this.message.id; diff --git a/apps/cory/ssb/tf-shared.js b/apps/cory/ssb/tf-shared.js index 3436e5ce..887d3658 100644 --- a/apps/cory/ssb/tf-shared.js +++ b/apps/cory/ssb/tf-shared.js @@ -1,6 +1,4 @@ -"use strict"; - -function markdown(md) { +export function markdown(md) { var reader = new commonmark.Parser({safe: true}); var writer = new commonmark.HtmlRenderer(); var parsed = reader.parse(md || ''); diff --git a/apps/cory/ssb/tf-user.js b/apps/cory/ssb/tf-user.js index df1344ba..4a0f21a8 100644 --- a/apps/cory/ssb/tf-user.js +++ b/apps/cory/ssb/tf-user.js @@ -1,17 +1,15 @@ -"use strict"; +import * as tf from './tf.js'; + Vue.component('tf-user', { props: ['id'], computed: { following: { get: function() { - return g_data.users && - g_data.users[g_data.whoami] && - g_data.users[g_data.whoami].following && - g_data.users[g_data.whoami].following[this.id]; + return tf.g_data.users?.[tf.g_data.whoami]?.following?.[this.id]; }, }, - whoami: { get: function() { return g_data.whoami; } }, - users: { get: function() { return g_data.users; } }, + whoami: { get: function() { return tf.g_data.whoami; } }, + users: { get: function() { return tf.g_data.users; } }, }, methods: { show_user: function() { diff --git a/apps/cory/ssb/tf.js b/apps/cory/ssb/tf.js index d5a51e6b..5e78a2fb 100644 --- a/apps/cory/ssb/tf.js +++ b/apps/cory/ssb/tf.js @@ -1,5 +1,7 @@ -"use strict"; -var g_data = { +import * as tfrpc from '/static/tfrpc.js'; +import * as tfshared from './tf-shared.js'; + +export var g_data = { whoami: null, connections: [], messages: [], @@ -176,6 +178,9 @@ function processMessages() { } window.addEventListener('message', function(event) { + if (event.data.message === 'tfrpc') { + return; + } g_message_queue.push(event); if (!g_process_pending) { g_process_pending = true; @@ -217,7 +222,7 @@ window.addEventListener('load', function() { return undefined; } }, - markdown: markdown, + markdown: tfshared.markdown, refresh: function() { window.parent.postMessage({refresh: true}, '*'); }, @@ -272,14 +277,15 @@ window.addEventListener('load', function() { 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, - }, - context: context, - }, '*'); + let bin = Array.from(new Uint8Array(buffer)); + return tfrpc.rpc.store_blob(bin); + }).then(function(id) { + g_data.post_text = `${g_data.post_text || ''}\n![${file.name}](${id})`; + Vue.set(g_data.mentions, id, { + link: id, + name: file.name, + type: file.type, + }); }).catch(function(e) { console.log('error', e); }); @@ -324,5 +330,5 @@ window.addEventListener('load', function() { } } }); - window.parent.postMessage('ready', '*'); + tfrpc.rpc.ready(); }); \ No newline at end of file diff --git a/core/tfrpc.js b/core/tfrpc.js index c8cc3058..9b0f4f82 100644 --- a/core/tfrpc.js +++ b/core/tfrpc.js @@ -26,6 +26,14 @@ function make_rpc(target, prop, receiver) { } } +function send(response) { + if (k_is_browser) { + window.parent.postMessage(response, '*'); + } else { + app.postMessage(response); + } +} + function call_rpc(message) { if (message && message.message === 'tfrpc') { if (message.method) { @@ -33,14 +41,16 @@ function call_rpc(message) { if (method) { let response = {message: 'tfrpc', id: message.id}; try { - response.result = method(...message.params); + Promise.resolve(method(...message.params)).then(function(result) { + response.result = result; + send(response); + }).catch(function(error) { + response.error = error; + send(response); + }); } catch (error) { response.error = error; - } - if (k_is_browser) { - window.parent.postMessage(response, '*'); - } else { - app.postMessage(response); + send(response); } } else { throw new Error(message.method + ' not found.');