From 8007e71e1dcde51ac4c2ea1e186af1f1a59579d1 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 13 Apr 2024 19:52:40 -0400 Subject: [PATCH] Make the ssb app use the global identity picker. --- apps/ssb.json | 2 +- apps/ssb/app.js | 6 +++++ apps/ssb/script.js | 1 - apps/ssb/tf-app.js | 29 +++------------------ apps/ssb/tf-id-picker.js | 54 ---------------------------------------- core/core.js | 26 +++++++++++++++++++ 6 files changed, 37 insertions(+), 81 deletions(-) delete mode 100644 apps/ssb/tf-id-picker.js diff --git a/apps/ssb.json b/apps/ssb.json index 2a2cd7ca..d646c48b 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🐌", - "previous": "&kHFHpwFhnRVEbFQxuKlOUXPs/eJ2cOvWgS8poXm8WNM=.sha256" + "previous": "&CEeuaRddTxMQkVqXi1GTN9AyqzdyWTAjs3mqSHVTBWY=.sha256" } diff --git a/apps/ssb/app.js b/apps/ssb/app.js index 3316bace..c0f7a8cc 100644 --- a/apps/ssb/app.js +++ b/apps/ssb/app.js @@ -100,6 +100,9 @@ tfrpc.register(async function try_decrypt(id, content) { tfrpc.register(async function encrypt(id, recipients, content) { return await ssb.privateMessageEncrypt(id, recipients, content); }); +tfrpc.register(async function getActiveIdentity() { + return await ssb.getActiveIdentity(); +}); ssb.addEventListener('broadcasts', async function () { await tfrpc.rpc.set('broadcasts', await ssb.getBroadcasts()); }); @@ -107,6 +110,9 @@ ssb.addEventListener('broadcasts', async function () { core.register('onConnectionsChanged', async function () { await tfrpc.rpc.set('connections', await ssb.connections()); }); +core.register('setActiveIdentity', async function (id) { + await tfrpc.rpc.set('identity', id); +}); async function main() { if (typeof database !== 'undefined') { diff --git a/apps/ssb/script.js b/apps/ssb/script.js index 03c4858f..71cb39c2 100644 --- a/apps/ssb/script.js +++ b/apps/ssb/script.js @@ -1,7 +1,6 @@ import {LitElement, html} from './lit-all.min.js'; import * as tfrpc from '/static/tfrpc.js'; -import * as tf_id_picker from './tf-id-picker.js'; import * as tf_app from './tf-app.js'; import * as tf_message from './tf-message.js'; import * as tf_user from './tf-user.js'; diff --git a/apps/ssb/tf-app.js b/apps/ssb/tf-app.js index 7064d617..72ef8201 100644 --- a/apps/ssb/tf-app.js +++ b/apps/ssb/tf-app.js @@ -52,13 +52,15 @@ class TfElement extends LitElement { self.broadcasts = value; } else if (name === 'connections') { self.connections = value; + } else if (name === 'identity') { + self.whoami = value; } }); this.initial_load(); } async initial_load() { - let whoami = await tfrpc.rpc.localStorageGet('whoami'); + let whoami = await tfrpc.rpc.getActiveIdentity(); let ids = (await tfrpc.rpc.getIdentities()) || []; this.whoami = whoami ?? (ids.length ? ids[0] : undefined); this.ids = ids; @@ -193,29 +195,6 @@ class TfElement extends LitElement { } } - render_id_picker() { - return html` -
- - -
- `; - } - async load_recent_tags() { let start = new Date(); this.tags = await tfrpc.rpc.query( @@ -380,7 +359,7 @@ class TfElement extends LitElement { class="w3-theme-dark" >
- ${this.render_id_picker()} ${tabs} + ${tabs} ${this.tags.map( (x) => html`` )} diff --git a/apps/ssb/tf-id-picker.js b/apps/ssb/tf-id-picker.js deleted file mode 100644 index b9a8adf9..00000000 --- a/apps/ssb/tf-id-picker.js +++ /dev/null @@ -1,54 +0,0 @@ -import {LitElement, html} from './lit-all.min.js'; -import * as tfrpc from '/static/tfrpc.js'; -import {styles} from './tf-styles.js'; - -/* - ** Provide a list of IDs, and this lets the user pick one. - */ -class TfIdentityPickerElement extends LitElement { - static get properties() { - return { - ids: {type: Array}, - selected: {type: String}, - users: {type: Object}, - }; - } - - static styles = styles; - - constructor() { - super(); - this.ids = []; - this.users = {}; - } - - changed(event) { - this.selected = event.srcElement.value; - this.dispatchEvent( - new Event('change', { - srcElement: this, - }) - ); - } - - render() { - return html` - - `; - } -} - -customElements.define('tf-id-picker', TfIdentityPickerElement); diff --git a/core/core.js b/core/core.js index fe3f6a2c..438ea44d 100644 --- a/core/core.js +++ b/core/core.js @@ -244,6 +244,7 @@ function broadcastEvent(eventName, argv) { } return Promise.all(promises); } + /** * TODOC * @param {*} message @@ -265,6 +266,28 @@ function broadcast(message) { return Promise.all(promises); } +/** + * TODOC + * @param {String} eventName + * @param {*} argv + * @returns + */ +function broadcastAppEventToUser(user, packageOwner, packageName, eventName, argv) { + let promises = []; + for (let process of Object.values(gProcesses)) { + if ( + process.credentials?.session?.name === user && + process.packageOwner == packageOwner && + process.packageName == packageName + ) { + if (process.eventHandlers[eventName]) { + promises.push(invoke(process.eventHandlers[eventName], argv)); + } + } + } + return Promise.all(promises); +} + /** * TODOC * @param {*} caller @@ -360,6 +383,8 @@ async function getProcessBlob(blobId, key, options) { process.key = key; process.credentials = options.credentials || {}; process.task = new Task(); + process.packageOwner = options.packageOwner; + process.packageName = options.packageName; process.eventHandlers = {}; if (!options?.script || options?.script === 'app.js') { process.app = new app.App(); @@ -520,6 +545,7 @@ async function getProcessBlob(blobId, key, options) { await new Database(process?.credentials?.session?.name).set(`id:${options.packageOwner}:${options.packageName}`, identity); } process.sendIdentities(); + broadcastAppEventToUser(process?.credentials?.session?.name, options.packageOwner, options.packageName, 'setActiveIdentity', [identity]); }; process.createIdentity = async function() { if (