From 3a7059cd8363db2795fdb0391108ab35d6c391f3 Mon Sep 17 00:00:00 2001 From: Ezequiel Adrian Schwartzman Date: Tue, 24 Dec 2024 14:46:23 -0300 Subject: [PATCH] Trigger actual Weave notifications --- ui/src/store/gameSpaceStore.ts | 12 +++++++-- ui/src/store/grammar.ts | 49 +++++++++++++++++++++++++++++++--- ui/src/store/rootStore.ts | 21 ++++++++++----- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/ui/src/store/gameSpaceStore.ts b/ui/src/store/gameSpaceStore.ts index 3cc6d26..9eae27d 100644 --- a/ui/src/store/gameSpaceStore.ts +++ b/ui/src/store/gameSpaceStore.ts @@ -1,3 +1,4 @@ +import type { WAL, WeaveClient } from '@theweave/api'; import { derived, get, type Readable, type Writable, writable } from 'svelte/store'; import { type SynDoc } from '~/lib/SimplerSyn'; @@ -14,7 +15,10 @@ type UiState = { export type GameSpaceSyn = ReturnType; -export function createGameSpaceSynStore(synDoc: SynDoc) { +export function createGameSpaceSynStore( + synDoc: SynDoc, + context: { weaveClient?: WeaveClient; toAsset: (gameSpaceHash: string) => WAL }, +) { console.log('SYN DOC', synDoc); const state = synDoc.state as Writable; const pubKey = synDoc.pubKey; @@ -141,7 +145,11 @@ export function createGameSpaceSynStore(synDoc: SynDoc) { await synDoc.change((state, _eph) => { console.time('Running deltas'); for (const delta of deltas) { - applyDelta(delta, state, { pubKey }); + applyDelta(delta, state, { + pubKey, + weaveClient: context.weaveClient, + asAsset: () => context.toAsset(hash), + }); } console.timeEnd('Running deltas'); }, force); diff --git a/ui/src/store/grammar.ts b/ui/src/store/grammar.ts index 588d6fa..9ed3f5b 100644 --- a/ui/src/store/grammar.ts +++ b/ui/src/store/grammar.ts @@ -1,13 +1,20 @@ +import type { WAL, WeaveClient } from '@theweave/api'; import { cloneDeep } from 'lodash'; import { v1 as uuidv1 } from 'uuid'; -import { type AgentPubKeyB64, encodeHashToBase64 } from '@holochain/client'; +import { type AgentPubKeyB64, decodeHashFromBase64, encodeHashToBase64 } from '@holochain/client'; import { colorSequence } from '~/lib/util'; import * as elements from '../GameSpace/elements'; import { LIBRARY } from './library'; -import type { GameSpace, GElement, LogType, NotificationsConfig } from './types'; +import { + DEFAULT_NOTIFICATIONS_CONFIG, + type GameSpace, + type GElement, + type LogType, + type NotificationsConfig, +} from './types'; export type Delta = | { type: 'set-is-archived'; value: boolean } @@ -52,7 +59,11 @@ export function initialState(pubKey: string): GameSpace { }; } -export const applyDelta = (delta: Delta, $state: GameSpace, context: { pubKey: string }) => { +export const applyDelta = ( + delta: Delta, + $state: GameSpace, + context: { pubKey: string; weaveClient?: WeaveClient; asAsset: () => WAL }, +) => { switch (delta.type) { case 'set-is-archived': $state.isArchived = delta.value; @@ -230,6 +241,38 @@ export const applyDelta = (delta: Delta, $state: GameSpace, context: { pubKey: s agentKey: log.pubKey || context.pubKey, elRef: log.elRef || null, }); + const nConfig = { + ...DEFAULT_NOTIFICATIONS_CONFIG, + ...($state.notificationsConfigOverride[context.pubKey] || {}), + }; + if (context.weaveClient) { + const players = $state.playersSlots + .filter((p) => p.pubKey !== context.pubKey && p.pubKey) + .filter((p) => { + const nConfig = { + ...DEFAULT_NOTIFICATIONS_CONFIG, + ...($state.notificationsConfigOverride[p.pubKey] || {}), + }; + return nConfig[log.type]; + }) + .map((p) => decodeHashFromBase64(p.pubKey)); + console.log('SENDING NOTIFICATIONS TO', players); + if (players) { + context.weaveClient.notifyFrame([ + { + title: log.message, + body: '', + notification_type: log.type, + urgency: 'medium', + fromAgent: decodeHashFromBase64(context.pubKey), + forAgents: players, + icon_src: undefined, + timestamp: Date.now(), + aboutWal: context.asAsset(), + }, + ]); + } + } } for (let e in elements) { diff --git a/ui/src/store/rootStore.ts b/ui/src/store/rootStore.ts index f5cfbb1..62cd2f0 100644 --- a/ui/src/store/rootStore.ts +++ b/ui/src/store/rootStore.ts @@ -43,7 +43,9 @@ export function createRootStore( (synDocs, deletedDocs) => { gameDocs.update((val) => { const newVal = { ...val }; - synDocs.forEach((doc) => (newVal[doc.hash] = createGameSpaceSynStore(doc))); + synDocs.forEach( + (doc) => (newVal[doc.hash] = createGameSpaceSynStore(doc, { weaveClient, toAsset })), + ); deletedDocs.forEach((hash) => delete newVal[hash]); return newVal; }); @@ -143,16 +145,23 @@ export function createRootStore( } function addToPocket(gameSpace: GameSpaceSyn) { + const asset = toAsset(gameSpace.hash); + if (asset) { + weaveClient.assets.assetToPocket(asset); + } else { + console.log('Tried adding to pocket before the DNA hash was loaded'); + } + } + + function toAsset(gameSpaceHash: string): WAL { const $dnaHash = get(dnaHash); if ($dnaHash && weaveClient) { - const attachment: WAL = { - hrl: [$dnaHash, decodeHashFromBase64(gameSpace.hash)], + return { + hrl: [$dnaHash, decodeHashFromBase64(gameSpaceHash)], context: {}, }; - // weaveClient.walToPocket(attachment); - } else { - console.log('Tried adding to pocket before the DNA hash was loaded'); } + return null; } return {