Skip to content

Commit

Permalink
Trigger actual Weave notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Zequez committed Dec 24, 2024
1 parent 622a02c commit 3a7059c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
12 changes: 10 additions & 2 deletions ui/src/store/gameSpaceStore.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,7 +15,10 @@ type UiState = {

export type GameSpaceSyn = ReturnType<typeof createGameSpaceSynStore>;

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<GameSpace>;
const pubKey = synDoc.pubKey;
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 46 additions & 3 deletions ui/src/store/grammar.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 15 additions & 6 deletions ui/src/store/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3a7059c

Please sign in to comment.