Skip to content

Commit

Permalink
re-added in-We notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
matthme committed Dec 20, 2023
1 parent 8c29d59 commit 0928e00
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 62 deletions.
7 changes: 6 additions & 1 deletion example/ui/src/example-applet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './elements/create-post.js';
import './elements/post-detail.js';
import './elements/posts-context.js';

import { WeClient, weLinkFromAppletHash } from '@lightningrodlabs/we-applet';
import { WeClient, WeNotification, weLinkFromAppletHash } from '@lightningrodlabs/we-applet';
import { weClientContext } from '@lightningrodlabs/we-elements';

import '@lightningrodlabs/we-elements/dist/elements/we-client-context.js';
Expand Down Expand Up @@ -43,6 +43,10 @@ export class ExampleApplet extends LitElement {
}
}

async notifyWe(notifications: WeNotification[]) {
this.weClient.notifyWe(notifications);
}

render() {
if (!this.weClient.renderInfo) return html`loading...`;
switch (this.weClient.renderInfo.type) {
Expand All @@ -56,6 +60,7 @@ export class ExampleApplet extends LitElement {
<applet-main
.client=${this.weClient.renderInfo.appletClient}
.weClient=${this.weClient}
@notification=${(e: CustomEvent) => this.notifyWe(e.detail)}
@post-selected=${async (e: CustomEvent) => {
const appInfo = await client.appInfo();
const dnaHash = (appInfo.cell_info.forum[0] as any)[CellType.Provisioned]
Expand Down
2 changes: 1 addition & 1 deletion example/ui/src/posts-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export class PostsStore {
allPosts = lazyLoadAndPoll(async () => {
const records = await this.client.getAllPosts();
return records;
}, 15000);
}, 2000);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "org.lightningrodlabs.we-electron-alpha",
"version": "0.8.4",
"version": "0.8.5",
"private": true,
"description": "lightningrodlabs We (Alhpa)",
"main": "./out/main/index.js",
Expand All @@ -18,7 +18,7 @@
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
"typecheck": "yarn typecheck:node && yarn typecheck:web",
"start": "electron-vite preview",
"applet-dev": "BPORT=46263 SPORT=46262; concurrently \"hc run-local-services -b $BPORT -s $SPORT\" \"electron-vite dev -- --dev-config we.dev.config.json --agent-num 1 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\" \"sleep 10 && electron-vite dev -- --dev-config we.dev.config.json --agent-num 2 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\"",
"applet-dev": "BPORT=46263 SPORT=46262; concurrently \"hc run-local-services -b $BPORT -s $SPORT\" \"UI_PORT=8888 yarn workspace example-applet start\" \"electron-vite dev -- --dev-config we.dev.config.json --agent-num 1 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\" \"sleep 10 && electron-vite dev -- --dev-config we.dev.config.json --agent-num 2 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\"",
"applet-dev-example": "BPORT=46263 SPORT=46262; concurrently \"hc run-local-services -b $BPORT -s $SPORT\" \"UI_PORT=8888 yarn workspace example-applet start\" \"electron-vite dev -- --dev-config we.dev.config.example.json --agent-num 1 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\" \"sleep 10 && electron-vite dev -- --dev-config we.dev.config.example.json --agent-num 2 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\"",
"dev": "BPORT=46263 SPORT=46262; concurrently \"hc run-local-services -b $BPORT -s $SPORT\" \"UI_PORT=8888 yarn workspace example-applet start\" \"electron-vite dev -- --dev-config we.dev.config.json --agent-num 1 -b http://127.0.0.1:$BPORT -s ws://127.0.0.1:$SPORT\"",
"dev:electron": "electron-vite dev",
Expand Down
2 changes: 1 addition & 1 deletion src/main/devSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export async function devSetup(

const networkSeed = randomUUID();
const applet: Applet = {
custom_name: appletConfig.name,
custom_name: appletInstallConfig.instanceName,
description: appletConfig.description,
sha256_happ: happHash,
sha256_ui: maybeUiHash,
Expand Down
120 changes: 63 additions & 57 deletions src/renderer/src/applets/applet-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ import { getAppletIframeScript, signZomeCallElectron } from '../electron-api.js'
import { WeStore } from '../we-store.js';
// import { AppletNotificationSettings } from './types.js';
import { AppletHash, AppletId } from '../types.js';
import { appEntryIdFromDistInfo, toOriginalCaseB64 } from '../utils.js';
import {
appEntryIdFromDistInfo,
getNotificationState,
storeAppletNotifications,
toOriginalCaseB64,
validateNotifications,
} from '../utils.js';
// import {
// getAppletNotificationSettings,
// getNotificationState,
Expand Down Expand Up @@ -307,62 +313,62 @@ export async function handleAppletIframeMessage(
return openViews.userSelectHrl();
case 'toggle-clipboard':
return openViews.toggleClipboard();
case 'notify-we':
throw new Error('Notifications not implemented.');
// const appletId: AppletId = encodeHashToBase64(appletHash);
// if (!message.notifications) {
// throw new Error(
// `Got notification message without notifications attribute: ${JSON.stringify(message)}`,
// );
// }

// const appletStore2 = await toPromise(weStore.appletStores.get(appletHash));

// const mainWindowFocused = await isMainWindowFocused();
// const windowFocused = await appWindow.isFocused();
// const windowVisible = await appWindow.isVisible();

// If the applet that the notification is coming from is already open, and the We main window
// itself is also open, don't do anything
// const selectedAppletHash = get(weStore.selectedAppletHash());
// if (
// selectedAppletHash &&
// selectedAppletHash.toString() === appletHash.toString() &&
// windowFocused
// ) {
// return;
// }

// // add notifications to unread messages and store them in the persisted notifications log
// const notifications: Array<WeNotification> = message.notifications;
// validateNotifications(notifications); // validate notifications to ensure not to corrupt localStorage
// const unreadNotifications = storeAppletNotifications(notifications, appletId);

// // update the notifications store
// appletStore2.setUnreadNotifications(getNotificationState(unreadNotifications));

// // trigger OS notification if allowed by the user and notification is fresh enough (less than 10 minutes old)
// const appletNotificationSettings: AppletNotificationSettings =
// getAppletNotificationSettings(appletId);

// await Promise.all(
// notifications.map(async (notification) => {
// // check whether it's actually a new event or not. Events older than 5 minutes won't trigger an OS notification
// // because it is assumed that they are emitted by the Applet UI upon startup of We and occurred while the
// // user was offline
// if (Date.now() - notification.timestamp < 300000) {
// console.log('notifying tauri');
// await notifyElectron(
// notification,
// appletNotificationSettings.showInSystray && !windowVisible,
// appletNotificationSettings.allowOSNotification && notification.urgency === 'high',
// // appletStore ? encodeHashToBase64(appletStore.applet.appstore_app_hash) : undefined,
// appletStore ? appletStore.applet.custom_name : undefined,
// );
// }
// }),
// );
// return;
case 'notify-we': {
const appletId: AppletId = encodeHashToBase64(appletHash);
if (!message.notifications) {
throw new Error(
`Got notification message without notifications attribute: ${JSON.stringify(message)}`,
);
}
const appletStore = await toPromise(weStore.appletStores.get(appletHash));

// const mainWindowFocused = await isMainWindowFocused();
// const windowFocused = await appWindow.isFocused();
// const windowVisible = await appWindow.isVisible();

// If the applet that the notification is coming from is already open, and the We main window
// itself is also open, don't do anything
// const selectedAppletHash = get(weStore.selectedAppletHash());
// if (
// selectedAppletHash &&
// selectedAppletHash.toString() === appletHash.toString() &&
// windowFocused
// ) {
// return;
// }

// add notifications to unread messages and store them in the persisted notifications log
const notifications: Array<WeNotification> = message.notifications;
validateNotifications(notifications); // validate notifications to ensure not to corrupt localStorage
const unreadNotifications = storeAppletNotifications(notifications, appletId);

// update the notifications store
appletStore.setUnreadNotifications(getNotificationState(unreadNotifications));

// // trigger OS notification if allowed by the user and notification is fresh enough (less than 10 minutes old)
// const appletNotificationSettings: AppletNotificationSettings =
// getAppletNotificationSettings(appletId);

// await Promise.all(
// notifications.map(async (notification) => {
// // check whether it's actually a new event or not. Events older than 5 minutes won't trigger an OS notification
// // because it is assumed that they are emitted by the Applet UI upon startup of We and occurred while the
// // user was offline
// if (Date.now() - notification.timestamp < 300000) {
// console.log('notifying electron main process');
// await notifyElectron(
// notification,
// appletNotificationSettings.showInSystray && !windowVisible,
// appletNotificationSettings.allowOSNotification && notification.urgency === 'high',
// // appletStore ? encodeHashToBase64(appletStore.applet.appstore_app_hash) : undefined,
// appletStore ? appletStore.applet.custom_name : undefined,
// );
// }
// }),
// );
return;
}

case 'get-applet-info':
return weServices.appletInfo(message.appletHash);
case 'get-group-profile':
Expand Down

0 comments on commit 0928e00

Please sign in to comment.