diff --git a/src/app/push/adapters/capacitor.ts b/src/app/push/adapters/capacitor.ts index 0cbd5a74..725e46a7 100644 --- a/src/app/push/adapters/capacitor.ts +++ b/src/app/push/adapters/capacitor.ts @@ -109,6 +109,7 @@ export class CapacitorNotifications implements IPushAdapter { ["d", identifier], ["relay", relay], ["callback", subscription.callback], + ["include_event"], ...ignore.map(filter => ["ignore", JSON.stringify(filter)]), ...filters.map(filter => ["filter", JSON.stringify(filter)]), ], diff --git a/src/app/push/adapters/common.ts b/src/app/push/adapters/common.ts index 424d13b7..253c8e9b 100644 --- a/src/app/push/adapters/common.ts +++ b/src/app/push/adapters/common.ts @@ -8,7 +8,7 @@ import { } from "@capacitor/push-notifications" import type {PluginListenerHandle} from "@capacitor/core" import {goto} from "$app/navigation" -import {assoc, call, now, on, poll, spec, throttle, uniq} from "@welshman/lib" +import {assoc, call, now, on, parseJson, poll, spec, throttle, uniq} from "@welshman/lib" import {LOCAL_RELAY_URL} from "@welshman/net" import type {RepositoryUpdate} from "@welshman/net" import { @@ -16,6 +16,7 @@ import { matchFilters, tagSpec, tagValue, + verifyEvent, MESSAGE, WRAP, type Filter, @@ -102,15 +103,27 @@ export const onNotification = call(() => { } }) -const loadNotificationEvent = (relay: string, id: string) => - network.get().request({ - filters: getIdFilters([id]), - relays: [relay, LOCAL_RELAY_URL], - // The local relay eoses immediately, so anything less waits on it alone - threshold: 1, - autoClose: true, - signal: AbortSignal.timeout(5000), - }) +const ingestNotification = async (relay: string, id: string, json?: string) => { + const pushed = parseJson(json) + + if (pushed && verifyEvent(pushed)) { + app.get().tracker.track(pushed.id, relay) + app.get().repository.publish(pushed) + + return pushed + } else { + const events = await network.get().request({ + filters: getIdFilters([id]), + relays: [relay, LOCAL_RELAY_URL], + // The local relay eoses immediately, so anything less waits on it alone + threshold: 1, + autoClose: true, + signal: AbortSignal.timeout(5000), + }) + + return events[0] + } +} // Wraps are ingested into the repository and unwrapped in the background, so wait for the rumor const loadNotificationRumor = async (wrap: TrustedEvent) => { @@ -122,8 +135,8 @@ const loadNotificationRumor = async (wrap: TrustedEvent) => { } export const onPushNotificationAction = async (action: ActionPerformed) => { - const {relay, id} = action.notification.data - const [event] = await loadNotificationEvent(relay, id) + const {relay, id, event: json} = action.notification.data + const event = await ingestNotification(relay, id, json) const target = event?.kind === WRAP ? await loadNotificationRumor(event) : event const path = target && makeEventPath(target, [relay])