Fixing bugs with push notifications

This commit is contained in:
Jon Staab 2026-01-28 13:35:50 -08:00
parent bf6abd301c
commit 000344a942
6 changed files with 74 additions and 71 deletions

View file

@ -4,13 +4,13 @@ const config: CapacitorConfig = {
appId: "social.flotilla", appId: "social.flotilla",
appName: "Flotilla", appName: "Flotilla",
webDir: "build", webDir: "build",
server: {
androidScheme: "https",
},
android: { android: {
adjustMarginsForEdgeToEdge: false, adjustMarginsForEdgeToEdge: false,
}, },
plugins: { plugins: {
CapacitorHttp: {
enabled: true,
},
SystemBars: { SystemBars: {
insetsHandling: "disable", insetsHandling: "disable",
}, },
@ -26,10 +26,10 @@ const config: CapacitorConfig = {
autoClear: true, autoClear: true,
}, },
}, },
// Use this for live reload https://capacitorjs.com/docs/guides/live-reload
server: { server: {
url: "http://192.168.1.148:1847", // Use this for live reload https://capacitorjs.com/docs/guides/live-reload
cleartext: true, // url: "http://192.168.1.17:1847",
// cleartext: true,
}, },
} }

Binary file not shown.

View file

@ -26,7 +26,7 @@
<link rel="apple-touch-icon" sizes="180x180" href="/icons/apple-touch-icon-180x180.png" /> <link rel="apple-touch-icon" sizes="180x180" href="/icons/apple-touch-icon-180x180.png" />
%sveltekit.head% %sveltekit.head%
</head> </head>
<body data-sveltekit-preload-data="hover"> <body data-sveltekit-preload-data="hover" data-sveltekit-preload-code="eager">
<div style="display: contents">%sveltekit.body%</div> <div style="display: contents">%sveltekit.body%</div>
<script <script
defer defer

View file

@ -13,9 +13,9 @@
</script> </script>
<Link replaceState href={path}> <Link replaceState href={path}>
<CardButton class="btn-neutral shadow-md"> <CardButton class="btn-neutral shadow-md bg-alt">
{#snippet icon()} {#snippet icon()}
<RelayIcon {url} size={12} /> <RelayIcon {url} size={12} class="rounded-full" />
{/snippet} {/snippet}
{#snippet title()} {#snippet title()}
<div class="flex gap-1"> <div class="flex gap-1">

View file

@ -31,10 +31,10 @@ import {
identity, identity,
now, now,
groupBy, groupBy,
postJson,
nth, nth,
nthEq, nthEq,
maybe, maybe,
throttle,
} from "@welshman/lib" } from "@welshman/lib"
import type {TrustedEvent, Filter} from "@welshman/util" import type {TrustedEvent, Filter} from "@welshman/util"
import {deriveEventsByIdByUrl} from "@welshman/store" import {deriveEventsByIdByUrl} from "@welshman/store"
@ -349,7 +349,6 @@ interface IPushAdapter {
request: (prompt?: boolean) => Promise<string> request: (prompt?: boolean) => Promise<string>
disable: () => Promise<void> disable: () => Promise<void>
enable: () => Promise<void> enable: () => Promise<void>
stop: () => Promise<void>
} }
if (Capacitor.isNativePlatform()) { if (Capacitor.isNativePlatform()) {
@ -411,14 +410,32 @@ class CapacitorNotifications implements IPushAdapter {
} }
if (!subscription) { if (!subscription) {
try {
const channel = Capacitor.getPlatform() === "ios" ? "apns" : "fcm" const channel = Capacitor.getPlatform() === "ios" ? "apns" : "fcm"
const url = buildUrl(PUSH_SERVER, "subscription", channel) const url = buildUrl(PUSH_SERVER, "subscription", channel)
const json = await postJson(url, {token}, {signal}) const res = await fetch(url, {
signal,
method: "POST",
body: JSON.stringify({token}),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
if (!res.ok) {
console.warn(`Failed to register with push server (status ${res.status})`)
} else {
const json = await res.json()
if (json?.callback && json?.key) { if (json?.callback && json?.key) {
notificationState.update(assoc("subscription", json)) notificationState.update(assoc("subscription", json))
} else { } else {
console.warn("Failed to register with push server") console.warn("Failed to register with push server (bad response)")
}
}
} catch (e) {
console.warn("Failed to register with push server:", e)
} }
} }
} }
@ -439,7 +456,6 @@ class CapacitorNotifications implements IPushAdapter {
} }
_syncRelay = async (relay: string, key: string, filters: Filter[], ignore: Filter[] = []) => { _syncRelay = async (relay: string, key: string, filters: Filter[], ignore: Filter[] = []) => {
console.log(`=== syncing ${relay} ${key}`)
const {subscription} = notificationState.get() const {subscription} = notificationState.get()
if (!subscription) { if (!subscription) {
@ -486,8 +502,6 @@ class CapacitorNotifications implements IPushAdapter {
} }
_unsyncRelay = async (relay: string, keys: string[]) => { _unsyncRelay = async (relay: string, keys: string[]) => {
console.log(`=== unsyncing ${relay} ${keys.join(", ")}`)
const stuff = await this._getPushStuff(relay) const stuff = await this._getPushStuff(relay)
if (!stuff) { if (!stuff) {
@ -517,7 +531,7 @@ class CapacitorNotifications implements IPushAdapter {
signal.addEventListener( signal.addEventListener(
"abort", "abort",
merged([userSpaceUrls, notificationSettings, userSettingsValues]).subscribe( merged([userSpaceUrls, notificationSettings, userSettingsValues]).subscribe(
([$userSpaceUrls, {spaces, mentions}, {muted_rooms}]) => { throttle(3000, ([$userSpaceUrls, {spaces, mentions}, {muted_rooms}]) => {
const filters = [{kinds: MESSAGE_KINDS}, makeCommentFilter(CONTENT_KINDS)] const filters = [{kinds: MESSAGE_KINDS}, makeCommentFilter(CONTENT_KINDS)]
for (const url of $userSpaceUrls) { for (const url of $userSpaceUrls) {
@ -545,7 +559,7 @@ class CapacitorNotifications implements IPushAdapter {
} }
} }
} }
}, }),
), ),
) )
} }
@ -554,7 +568,7 @@ class CapacitorNotifications implements IPushAdapter {
signal.addEventListener( signal.addEventListener(
"abort", "abort",
merged([userMessagingRelayList, notificationSettings]).subscribe( merged([userMessagingRelayList, notificationSettings]).subscribe(
([$userMessagingRelayList, {messages}]) => { throttle(3000, ([$userMessagingRelayList, {messages}]) => {
for (const url of getRelaysFromList($userMessagingRelayList)) { for (const url of getRelaysFromList($userMessagingRelayList)) {
if (messages) { if (messages) {
this._syncRelay(url, "messages", [{kinds: DM_KINDS, "#p": [pubkey.get()!]}]) this._syncRelay(url, "messages", [{kinds: DM_KINDS, "#p": [pubkey.get()!]}])
@ -562,12 +576,13 @@ class CapacitorNotifications implements IPushAdapter {
this._unsyncRelay(url, ["messages"]) this._unsyncRelay(url, ["messages"])
} }
} }
}, }),
), ),
) )
} }
async enable() { async enable() {
if (!this._controller) {
this._controller = new AbortController() this._controller = new AbortController()
try { try {
@ -578,8 +593,12 @@ class CapacitorNotifications implements IPushAdapter {
console.error(e) console.error(e)
} }
} }
}
async disable() { async disable() {
this._controller?.abort()
this._controller = undefined
const {subscription} = notificationState.get() const {subscription} = notificationState.get()
if (subscription) { if (subscription) {
@ -602,11 +621,6 @@ class CapacitorNotifications implements IPushAdapter {
notificationState.set({}) notificationState.set({})
} }
async stop() {
this._controller?.abort()
this._controller = undefined
}
} }
class WebNotifications implements IPushAdapter { class WebNotifications implements IPushAdapter {
@ -645,6 +659,7 @@ class WebNotifications implements IPushAdapter {
} }
async enable() { async enable() {
if (!this._unsubscriber) {
this._unsubscriber = onNotification(event => { this._unsubscriber = onNotification(event => {
const {push, messages, mentions, spaces} = notificationSettings.get() const {push, messages, mentions, spaces} = notificationSettings.get()
@ -663,12 +678,9 @@ class WebNotifications implements IPushAdapter {
} }
}) })
} }
async disable() {
// pass
} }
async stop() { async disable() {
this._unsubscriber?.() this._unsubscriber?.()
this._unsubscriber = undefined this._unsubscriber = undefined
} }
@ -696,27 +708,18 @@ export class Push {
static disable() { static disable() {
return Push._getAdapter().disable() return Push._getAdapter().disable()
} }
static enable() { static enable() {
return Push._getAdapter().enable() return Push._getAdapter().enable()
} }
static stop() {
return Push._getAdapter().stop()
}
static sync() { static sync() {
const adapter = Push._getAdapter() return notificationSettings.subscribe(({push}) => {
if (push) {
const unsubscriber = notificationSettings.subscribe($notificationSettings => { Push.enable()
if ($notificationSettings.push) {
adapter.enable()
} else { } else {
adapter.disable() Push.disable()
} }
}) })
return () => {
unsubscriber()
adapter.stop()
}
} }
} }