diff --git a/apps/ssb/app.js b/apps/ssb/app.js index 1a61fe79..922b554b 100644 --- a/apps/ssb/app.js +++ b/apps/ssb/app.js @@ -88,6 +88,9 @@ tfrpc.register(function apps() { tfrpc.register(async function try_decrypt(id, content) { return await ssb.privateMessageDecrypt(id, content); }); +tfrpc.register(async function encrypt(id, recipients, content) { + return await ssb.privateMessageEncrypt(id, recipients, content); +}); ssb.addEventListener('broadcasts', async function() { await tfrpc.rpc.set('broadcasts', await ssb.getBroadcasts()); }); diff --git a/apps/ssb/tf-compose.js b/apps/ssb/tf-compose.js index cd100dad..48ac81ac 100644 --- a/apps/ssb/tf-compose.js +++ b/apps/ssb/tf-compose.js @@ -176,7 +176,7 @@ class TfComposeElement extends LitElement { } } - submit() { + async submit() { let self = this; let draft = this.get_draft(); let edit = this.renderRoot.getElementById('edit'); @@ -195,14 +195,24 @@ class TfComposeElement extends LitElement { message.contentWarning = draft.content_warning; } console.log('Would post:', message); - tfrpc.rpc.appendMessage(this.whoami, message).then(function() { - edit.value = ''; - self.change(); - self.notify(undefined); - self.requestUpdate(); - }).catch(function(error) { + if (draft.encrypt_to) { + let to = new Set(draft.encrypt_to); + to.add(this.whoami); + to = [...to]; + console.log('encrypting to', to); + message = await tfrpc.rpc.encrypt(this.whoami, to, JSON.stringify(message)); + console.log('encrypted as', message); + } + try { + await tfrpc.rpc.appendMessage(this.whoami, message).then(function() { + edit.value = ''; + self.change(); + self.notify(undefined); + self.requestUpdate(); + }); + } catch (error) { alert(error.message); - }); + } } discard() { @@ -244,6 +254,16 @@ class TfComposeElement extends LitElement { preview.innerHTML = this.process_text(edit.value); this.last_updated_text = edit.value; } + let encrypt = this.renderRoot.getElementById('encrypt_to'); + if (encrypt) { + let tribute = new Tribute({ + values: Object.entries(this.users).map(x => ({key: x[1].name, value: x[0]})), + selectTemplate: function(item) { + return item.original.value; + }, + }); + tribute.attach(encrypt); + } } remove_mention(id) { @@ -354,6 +374,45 @@ class TfComposeElement extends LitElement { return this.drafts[this.branch || ''] || {}; } + update_encrypt(event) { + let input = event.srcElement; + let matches = input.value.match(/@.*?\.ed25519/g); + if (matches) { + let draft = this.get_draft(); + let to = [...new Set(matches.concat(draft.encrypt_to))]; + this.set_encrypt(to); + input.value = ''; + } + } + + render_encrypt() { + let draft = this.get_draft(); + if (draft.encrypt_to === undefined) { + return; + } + return html` +