diff --git a/lib/app/notifications.js b/lib/app/notifications.js new file mode 100644 index 00000000..115f6f29 --- /dev/null +++ b/lib/app/notifications.js @@ -0,0 +1,9 @@ +const pull = require("pull-stream"); + +module.exports = function (notificationStreams) { + notificationStreams.forEach((stream) => { + pull(stream, pull.drain((n) => { + electron.ipcRenderer.send('send-notification', n) + })) + }); +} \ No newline at end of file diff --git a/lib/depject/page/html/render/chess.js b/lib/depject/page/html/render/chess.js index 68720cdd..66496003 100644 --- a/lib/depject/page/html/render/chess.js +++ b/lib/depject/page/html/render/chess.js @@ -3,57 +3,93 @@ const nest = require("depnest"); const SsbChessDataAccess = require("ssb-chess-data-access"); const SsbChessMithril = require("ssb-chess-mithril"); const { onceTrue, computed } = require("mutant"); +const defer = require('pull-defer'); +const pull = require('pull-stream'); exports.needs = nest({ "sbot.obs.connection": "first", "intl.sync.i18n": "first", }); -exports.gives = nest("page.html.render"); +exports.gives = nest({ + "page.html": ["render", "notifications"], +}); + +function createChess(ssb) { + const dataAccess = new SsbChessDataAccess.SbotClassic(ssb); + return SsbChessMithril(dataAccess); +} + +function waitForSbot(api) { + return new Promise((resolve, reject) => { + onceTrue(api.sbot.obs.connection, (ssb) => { + resolve(ssb) + }) + }) +} exports.create = function (api) { - return nest("page.html.render", function channel(path) { - if (path !== "/chess") return; - - let chessComponent = null; - let removed = false; - - const page = - h("div.wrapper", { className: 'Scroller' }, [ - h( - "section.content", - h("div", { id: "ssb-chess-mithril-plugin", style: { 'margin-top':'20px' } }), - ), - ]); - - onceTrue(api.sbot.obs.connection, (ssb) => { - const loadChess = () => { - const attachToElement = document.getElementById( - "ssb-chess-mithril-plugin", - ); - - if (!attachToElement && !removed) { - console.log("no element, loop"); - setTimeout(() => { - loadChess(); - }, 500); - return; - } else if (!removed) { - const dataAccess = new SsbChessDataAccess.SbotClassic(ssb); - chessComponent = SsbChessMithril(attachToElement, dataAccess); - } - }; + return nest({ + "page.html": { + notifications: function() { + const deferred = defer.source() + + waitForSbot(api) + .then(createChess) + .then(chess => pull( + chess.notifications.getNotificationStream(), + pull.map(notification => ({ + icon: null, + title: "Chess", + message: notification.message, + open: `/chess`, + anchor: { + gameId: notification.gameId + } + })) + ) + ).then(notificationStream => deferred.resolve(notificationStream)) - loadChess() - }) - + return deferred; + }, + render: function channel(path) { + if (!path.startsWith("/chess")) return; - return computed(page, p => p, {onUnlisten: () => { - removed = true; + const page = h("div.wrapper", { className: "Scroller" }, [ + h( + "section.content", + h("div", { + id: "ssb-chess-mithril-plugin", + style: { "margin-top": "20px" }, + }), + ), + ]); - if (chessComponent) { - chessComponent.destroy(); + const chessMountTarget = document.createElement('div') + + const chessComponent = waitForSbot(api) + .then(createChess) + .then(chess =>chess.mount(chessMountTarget)); + + const view = computed(page, (p) => { + const chessElem = p.querySelector('#ssb-chess-mithril-plugin'); + chessElem.appendChild(chessMountTarget) + return p + }, { + onUnlisten: () => { + chessComponent.then(x => x.destroy()); + }, + }); + + view.setAnchor = function(data) { + if (data && data.gameId) { + const gameId = data.gameId; + chessComponent.then(c => c.goToGame(gameId)) + } } - }}); + + return view; + }, + }, }); }; diff --git a/lib/main-window.js b/lib/main-window.js index 12ad9264..1f941546 100644 --- a/lib/main-window.js +++ b/lib/main-window.js @@ -16,6 +16,7 @@ const pull = require("pull-stream"); const moment = require("moment-timezone"); const fullscreen = require("./fullscreen.js"); const appViews = require("./app/views.js"); +const startViewNotifications = require("./app/notifications.js"); const getGitHandler = require("./app/sync/external-handler/git"); const path = require("path"); const fs = require("fs"); @@ -53,6 +54,7 @@ module.exports = function (config) { "sbot.async.get": "first", "blob.sync.url": "first", "page.html.render": "first", + "page.html.notifications": "map", "app.html.search": "first", "app.html.channels": "first", "app.html.progressNotifier": "first", @@ -178,6 +180,7 @@ module.exports = function (config) { ); const views = appViews(api.page.html.render, defaultViews); + startViewNotifications(api.page.html.notifications()); const pendingCount = views.get("/mentions").pendingUpdates; @@ -191,8 +194,8 @@ module.exports = function (config) { electron.ipcRenderer.on("goToSettings", () => api.app.navigate("/settings")); electron.ipcRenderer.on("goTostatus", () => api.app.navigate("/status")); - electron.ipcRenderer.on("navigate-to", (ev, target) => { - navigate(target); + electron.ipcRenderer.on("navigate-to", (ev, target, anchor) => { + navigate(target, anchor); }); // context menus are handled on the server. however, we need to know some diff --git a/lib/plugins/notifier.js b/lib/plugins/notifier.js index c945e1c8..a4a72d2b 100644 --- a/lib/plugins/notifier.js +++ b/lib/plugins/notifier.js @@ -11,7 +11,6 @@ const path = require("path"); const toPull = require("stream-to-pull-stream"); const getAvatar = require("ssb-avatar"); const electron = require("electron"); -const ref = require("ssb-ref"); const ssbUri = require("ssb-uri2"); const defaultIcon = "&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256"; diff --git a/multiple-identities-entrypoint.js b/multiple-identities-entrypoint.js index 0f61550a..02e612b5 100644 --- a/multiple-identities-entrypoint.js +++ b/multiple-identities-entrypoint.js @@ -261,7 +261,7 @@ electron.app.on("ready", () => { }; // Notification event - electron.ipcMain.on("send-notification", (ev, notif) => { + electron.ipcMain.on("send-notification", (sendNotificationEv, notif) => { const notificationsEnabled = preferences.get("notificationsEnabled", true); if (notificationsEnabled) { @@ -281,11 +281,13 @@ electron.app.on("ready", () => { }); notification.addListener("click", (ev) => { - console.log("notification clicked."); - console.log("ev", ev); - const id = windows.keys().next().value; + + const windowId = sendNotificationEv.sender.keyForWindowsMap || windows.keys().next().value; + const browserWindow = windows.get(windowId)?.renderer; + const url = notif.open; - const browserWindow = windows.get(id)?.renderer; + const anchor = notif.anchor; + console.log("browser window for open url", browserWindow); if (ssbUri.isClassicMessageSSBURI(url)) { const msgid = ssbUri.toMessageSigil(url); @@ -293,6 +295,8 @@ electron.app.on("ready", () => { } else if (ssbUri.isFeedSSBURI(url)) { const feedid = ssbUri.toFeedSigil(url); browserWindow.webContents.send("navigate-to", feedid); + } else { + browserWindow.webContents.send("navigate-to", url, anchor); } }); diff --git a/package-lock.json b/package-lock.json index 05e26172..93444231 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ssb-ponchowonky", - "version": "5.6.5", + "version": "5.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ssb-ponchowonky", - "version": "5.6.5", + "version": "5.7.0", "license": "AGPL-3.0", "dependencies": { "@electron/remote": "^2.1.3", @@ -79,7 +79,7 @@ "ssb-blobs": "^2.0.1", "ssb-book-schema": "^1.0.3", "ssb-chess-data-access": "^2.1.4", - "ssb-chess-mithril": "2.1.4", + "ssb-chess-mithril": "3.0.1", "ssb-client": "^4.9.0", "ssb-config": "^3.4.4", "ssb-conn": "~1.0.0", @@ -12238,9 +12238,9 @@ } }, "node_modules/ssb-chess-mithril": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/ssb-chess-mithril/-/ssb-chess-mithril-2.1.4.tgz", - "integrity": "sha512-lUY4eiinkzcypDUt/kFIHBAHM/oGupYxgyhuAQp/d8v5Jd6wNEsRbht2Gl/dnwdW2iMiJ/RIB2VntpTzmhnNtw==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/ssb-chess-mithril/-/ssb-chess-mithril-3.0.1.tgz", + "integrity": "sha512-AvZUoV3sfOpPLItYKserryzYUpyZ8ZmEQ+4LDWtenuLXPiN4cU+rLfr7VN9j3X/bdn6WTTaLCepE+xIR0Cl7SQ==", "license": "GPL-3.0", "dependencies": { "@lichess-org/chessground": "^10.4.2", diff --git a/package.json b/package.json index f182cd17..a50b2818 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "ssb-blobs": "^2.0.1", "ssb-book-schema": "^1.0.3", "ssb-chess-data-access": "^2.1.4", - "ssb-chess-mithril": "2.1.4", + "ssb-chess-mithril": "3.0.1", "ssb-client": "^4.9.0", "ssb-config": "^3.4.4", "ssb-conn": "~1.0.0",