Generally just refactor alerts, upgrade some deps

This commit is contained in:
Jon Staab
2026-01-22 14:23:46 -08:00
parent 63fee653e8
commit ca3d661830
16 changed files with 1208 additions and 1058 deletions
+1
View File
@@ -3,6 +3,7 @@
--ignore-dir=build --ignore-dir=build
--ignore-dir=ios/DerivedData --ignore-dir=ios/DerivedData
--ignore-dir=ios/App/App/public --ignore-dir=ios/App/App/public
--ignore-dir=ios/App/Pods
--ignore-file=match:.svg --ignore-file=match:.svg
--ignore-file=match:package-lock.json --ignore-file=match:package-lock.json
+10 -10
View File
@@ -1,30 +1,30 @@
// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN // DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
include ':capacitor-android' include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capacitor+android@7.4.3_@capacitor+core@7.4.3/node_modules/@capacitor/android/capacitor') project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capacitor+android@8.0.1_@capacitor+core@8.0.1/node_modules/@capacitor/android/capacitor')
include ':capacitor-community-safe-area' include ':capacitor-community-safe-area'
project(':capacitor-community-safe-area').projectDir = new File('../node_modules/.pnpm/@capacitor-community+safe-area@7.0.0-alpha.1_@capacitor+core@7.4.3/node_modules/@capacitor-community/safe-area/android') project(':capacitor-community-safe-area').projectDir = new File('../node_modules/.pnpm/@capacitor-community+safe-area@8.0.1_@capacitor+core@8.0.1/node_modules/@capacitor-community/safe-area/android')
include ':capacitor-app' include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@capacitor+app@7.1.0_@capacitor+core@7.4.3/node_modules/@capacitor/app/android') project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@capacitor+app@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/app/android')
include ':capacitor-filesystem' include ':capacitor-filesystem'
project(':capacitor-filesystem').projectDir = new File('../node_modules/.pnpm/@capacitor+filesystem@7.1.4_@capacitor+core@7.4.3/node_modules/@capacitor/filesystem/android') project(':capacitor-filesystem').projectDir = new File('../node_modules/.pnpm/@capacitor+filesystem@8.1.0_@capacitor+core@8.0.1/node_modules/@capacitor/filesystem/android')
include ':capacitor-keyboard' include ':capacitor-keyboard'
project(':capacitor-keyboard').projectDir = new File('../node_modules/.pnpm/@capacitor+keyboard@7.0.3_@capacitor+core@7.4.3/node_modules/@capacitor/keyboard/android') project(':capacitor-keyboard').projectDir = new File('../node_modules/.pnpm/@capacitor+keyboard@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/keyboard/android')
include ':capacitor-preferences' include ':capacitor-preferences'
project(':capacitor-preferences').projectDir = new File('../node_modules/.pnpm/@capacitor+preferences@7.0.2_@capacitor+core@7.4.3/node_modules/@capacitor/preferences/android') project(':capacitor-preferences').projectDir = new File('../node_modules/.pnpm/@capacitor+preferences@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/preferences/android')
include ':capacitor-push-notifications' include ':capacitor-push-notifications'
project(':capacitor-push-notifications').projectDir = new File('../node_modules/.pnpm/@capacitor+push-notifications@7.0.3_@capacitor+core@7.4.3/node_modules/@capacitor/push-notifications/android') project(':capacitor-push-notifications').projectDir = new File('../node_modules/.pnpm/@capacitor+push-notifications@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/push-notifications/android')
include ':capawesome-capacitor-android-dark-mode-support' include ':capawesome-capacitor-android-dark-mode-support'
project(':capawesome-capacitor-android-dark-mode-support').projectDir = new File('../node_modules/.pnpm/@capawesome+capacitor-android-dark-mode-support@7.0.0_@capacitor+core@7.4.3/node_modules/@capawesome/capacitor-android-dark-mode-support/android') project(':capawesome-capacitor-android-dark-mode-support').projectDir = new File('../node_modules/.pnpm/@capawesome+capacitor-android-dark-mode-support@8.0.0_@capacitor+core@8.0.1/node_modules/@capawesome/capacitor-android-dark-mode-support/android')
include ':capawesome-capacitor-badge' include ':capawesome-capacitor-badge'
project(':capawesome-capacitor-badge').projectDir = new File('../node_modules/.pnpm/@capawesome+capacitor-badge@7.0.1_@capacitor+core@7.4.3/node_modules/@capawesome/capacitor-badge/android') project(':capawesome-capacitor-badge').projectDir = new File('../node_modules/.pnpm/@capawesome+capacitor-badge@8.0.0_@capacitor+core@8.0.1/node_modules/@capawesome/capacitor-badge/android')
include ':nostr-signer-capacitor-plugin' include ':nostr-signer-capacitor-plugin'
project(':nostr-signer-capacitor-plugin').projectDir = new File('../node_modules/.pnpm/nostr-signer-capacitor-plugin@0.0.4_@capacitor+core@7.4.3/node_modules/nostr-signer-capacitor-plugin/android') project(':nostr-signer-capacitor-plugin').projectDir = new File('../node_modules/.pnpm/nostr-signer-capacitor-plugin@0.0.4_@capacitor+core@8.0.1/node_modules/nostr-signer-capacitor-plugin/android')
+2 -2
View File
@@ -1,7 +1,7 @@
ext { ext {
minSdkVersion = 23 minSdkVersion = 23
compileSdkVersion = 35 compileSdkVersion = 36
targetSdkVersion = 35 targetSdkVersion = 36
androidxActivityVersion = '1.9.2' androidxActivityVersion = '1.9.2'
//https://github.com/ionic-team/capacitor/issues/7866 //https://github.com/ionic-team/capacitor/issues/7866
// androidxAppCompatVersion = '1.7.0' // androidxAppCompatVersion = '1.7.0'
+11 -11
View File
@@ -1,4 +1,4 @@
require_relative '../../node_modules/.pnpm/@capacitor+ios@7.4.3_@capacitor+core@7.4.3/node_modules/@capacitor/ios/scripts/pods_helpers' require_relative '../../node_modules/.pnpm/@capacitor+ios@8.0.1_@capacitor+core@8.0.1/node_modules/@capacitor/ios/scripts/pods_helpers'
platform :ios, '14.0' platform :ios, '14.0'
use_frameworks! use_frameworks!
@@ -9,16 +9,16 @@ use_frameworks!
install! 'cocoapods', :disable_input_output_paths => true install! 'cocoapods', :disable_input_output_paths => true
def capacitor_pods def capacitor_pods
pod 'Capacitor', :path => '../../node_modules/.pnpm/@capacitor+ios@7.4.3_@capacitor+core@7.4.3/node_modules/@capacitor/ios' pod 'Capacitor', :path => '../../node_modules/.pnpm/@capacitor+ios@8.0.1_@capacitor+core@8.0.1/node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../node_modules/.pnpm/@capacitor+ios@7.4.3_@capacitor+core@7.4.3/node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/.pnpm/@capacitor+ios@8.0.1_@capacitor+core@8.0.1/node_modules/@capacitor/ios'
pod 'CapacitorCommunitySafeArea', :path => '../../node_modules/.pnpm/@capacitor-community+safe-area@7.0.0-alpha.1_@capacitor+core@7.4.3/node_modules/@capacitor-community/safe-area' pod 'CapacitorCommunitySafeArea', :path => '../../node_modules/.pnpm/@capacitor-community+safe-area@8.0.1_@capacitor+core@8.0.1/node_modules/@capacitor-community/safe-area'
pod 'CapacitorApp', :path => '../../node_modules/.pnpm/@capacitor+app@7.1.0_@capacitor+core@7.4.3/node_modules/@capacitor/app' pod 'CapacitorApp', :path => '../../node_modules/.pnpm/@capacitor+app@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/app'
pod 'CapacitorFilesystem', :path => '../../node_modules/.pnpm/@capacitor+filesystem@7.1.4_@capacitor+core@7.4.3/node_modules/@capacitor/filesystem' pod 'CapacitorFilesystem', :path => '../../node_modules/.pnpm/@capacitor+filesystem@8.1.0_@capacitor+core@8.0.1/node_modules/@capacitor/filesystem'
pod 'CapacitorKeyboard', :path => '../../node_modules/.pnpm/@capacitor+keyboard@7.0.3_@capacitor+core@7.4.3/node_modules/@capacitor/keyboard' pod 'CapacitorKeyboard', :path => '../../node_modules/.pnpm/@capacitor+keyboard@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/keyboard'
pod 'CapacitorPreferences', :path => '../../node_modules/.pnpm/@capacitor+preferences@7.0.2_@capacitor+core@7.4.3/node_modules/@capacitor/preferences' pod 'CapacitorPreferences', :path => '../../node_modules/.pnpm/@capacitor+preferences@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/preferences'
pod 'CapacitorPushNotifications', :path => '../../node_modules/.pnpm/@capacitor+push-notifications@7.0.3_@capacitor+core@7.4.3/node_modules/@capacitor/push-notifications' pod 'CapacitorPushNotifications', :path => '../../node_modules/.pnpm/@capacitor+push-notifications@8.0.0_@capacitor+core@8.0.1/node_modules/@capacitor/push-notifications'
pod 'CapawesomeCapacitorBadge', :path => '../../node_modules/.pnpm/@capawesome+capacitor-badge@7.0.1_@capacitor+core@7.4.3/node_modules/@capawesome/capacitor-badge' pod 'CapawesomeCapacitorBadge', :path => '../../node_modules/.pnpm/@capawesome+capacitor-badge@8.0.0_@capacitor+core@8.0.1/node_modules/@capawesome/capacitor-badge'
pod 'NostrSignerCapacitorPlugin', :path => '../../node_modules/.pnpm/nostr-signer-capacitor-plugin@0.0.4_@capacitor+core@7.4.3/node_modules/nostr-signer-capacitor-plugin' pod 'NostrSignerCapacitorPlugin', :path => '../../node_modules/.pnpm/nostr-signer-capacitor-plugin@0.0.4_@capacitor+core@8.0.1/node_modules/nostr-signer-capacitor-plugin'
end end
target 'Flotilla Chat' do target 'Flotilla Chat' do
+12 -12
View File
@@ -37,18 +37,18 @@
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@capacitor-community/safe-area": "7.0.0-alpha.1", "@capacitor-community/safe-area": "^8.0.1",
"@capacitor/android": "^7.4.3", "@capacitor/android": "^8.0.1",
"@capacitor/app": "^7.1.0", "@capacitor/app": "^8.0.0",
"@capacitor/cli": "^7.4.3", "@capacitor/cli": "^8.0.1",
"@capacitor/core": "^7.4.3", "@capacitor/core": "^8.0.1",
"@capacitor/filesystem": "^7.1.4", "@capacitor/filesystem": "^8.1.0",
"@capacitor/ios": "^7.4.3", "@capacitor/ios": "^8.0.1",
"@capacitor/keyboard": "^7.0.3", "@capacitor/keyboard": "^8.0.0",
"@capacitor/preferences": "^7.0.2", "@capacitor/preferences": "^8.0.0",
"@capacitor/push-notifications": "^7.0.3", "@capacitor/push-notifications": "^8.0.0",
"@capawesome/capacitor-android-dark-mode-support": "^7.0.0", "@capawesome/capacitor-android-dark-mode-support": "^8.0.0",
"@capawesome/capacitor-badge": "^7.0.1", "@capawesome/capacitor-badge": "^8.0.0",
"@getalby/lightning-tools": "^6.0.0", "@getalby/lightning-tools": "^6.0.0",
"@getalby/sdk": "^5.1.2", "@getalby/sdk": "^5.1.2",
"@pomade/core": "^0.0.12", "@pomade/core": "^0.0.12",
+916 -815
View File
File diff suppressed because it is too large Load Diff
+9 -17
View File
@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import {onMount} from "svelte" import {onMount} from "svelte"
import {userSettingsValues} from "@app/core/state" import {userSettingsValues} from "@app/core/state"
import {notifications} from "../util/notifications" import {onNotification} from "@app/util/notifications"
let audioElement: HTMLAudioElement let audioElement: HTMLAudioElement
@@ -15,24 +15,16 @@
} }
}) })
let notificationCount = $state($notifications.size)
const playSound = () => {
if (enabled && $userSettingsValues.alerts_sound) {
audioElement?.play()
}
}
$effect(() => {
if ($notifications.size > notificationCount) {
playSound()
}
notificationCount = $notifications.size
})
onMount(() => { onMount(() => {
audioElement.load() audioElement.load()
const unsubscribe = onNotification(() => {
if (enabled && $userSettingsValues.alerts_sound) {
audioElement?.play()
}
})
return unsubscribe
}) })
</script> </script>
+1 -1
View File
@@ -47,7 +47,7 @@
{/if} {/if}
</span> </span>
</div> </div>
<div class="flex justify-between text-sm opacity-75"> <div class="flex flex-col justify-between text-sm opacity-75 sm:flex-row">
<p> <p>
Logged in with Logged in with
{#if $session.method === SessionMethod.Nip01} {#if $session.method === SessionMethod.Nip01}
+3 -3
View File
@@ -103,7 +103,7 @@
</script> </script>
<div bind:this={element} class="flex h-full flex-col justify-between"> <div bind:this={element} class="flex h-full flex-col justify-between">
<SecondaryNavSection> <SecondaryNavSection class="pb-0">
<div> <div>
<Button <Button
class="flex w-full flex-col rounded-xl p-3 transition-all hover:bg-base-100" class="flex w-full flex-col rounded-xl p-3 transition-all hover:bg-base-100"
@@ -172,7 +172,7 @@
</Popover> </Popover>
{/if} {/if}
</div> </div>
<div class="flex max-h-[calc(100vh-250px)] min-h-0 flex-col gap-1 overflow-auto"> <div class="flex max-h-[calc(100vh-150px)] min-h-0 flex-col gap-1 overflow-auto">
{#if hasNip29($relay)} {#if hasNip29($relay)}
<SecondaryNavItem {replaceState} href={makeSpacePath(url, "recent")}> <SecondaryNavItem {replaceState} href={makeSpacePath(url, "recent")}>
<Icon icon={History} /> Recent Activity <Icon icon={History} /> Recent Activity
@@ -239,7 +239,7 @@
{/if} {/if}
</div> </div>
</SecondaryNavSection> </SecondaryNavSection>
<div class="flex flex-col gap-2 p-4"> <div class="flex flex-col gap-2 p-4 pt-0">
<Button class="btn btn-neutral btn-sm" onclick={showDetail}> <Button class="btn btn-neutral btn-sm" onclick={showDetail}>
<SocketStatusIndicator {url} /> <SocketStatusIndicator {url} />
</Button> </Button>
-5
View File
@@ -15,11 +15,6 @@ import {
import { import {
EVENT_TIME, EVENT_TIME,
RELAY_INVITE, RELAY_INVITE,
ALERT_EMAIL,
ALERT_WEB,
ALERT_IOS,
ALERT_ANDROID,
ALERT_STATUS,
matchFilters, matchFilters,
getTagValue, getTagValue,
getAddress, getAddress,
+8 -8
View File
@@ -1,7 +1,7 @@
import twColors from "tailwindcss/colors" import twColors from "tailwindcss/colors"
import {context as pomadeContext} from "@pomade/core" import {context as pomadeContext} from "@pomade/core"
import {Capacitor} from "@capacitor/core" import {Capacitor} from "@capacitor/core"
import {get, derived, readable, writable} from "svelte/store" import {derived, readable, writable} from "svelte/store"
import * as nip19 from "nostr-tools/nip19" import * as nip19 from "nostr-tools/nip19"
import { import {
on, on,
@@ -323,11 +323,9 @@ export const settingsByPubkey = deriveItemsByKey({
export const getSettingsByPubkey = getter(settingsByPubkey) export const getSettingsByPubkey = getter(settingsByPubkey)
export const getSettings = (pubkey: string) => getSettingsByPubkey().get(pubkey)
export const loadSettings = makeLoadItem( export const loadSettings = makeLoadItem(
makeOutboxLoader(APP_DATA, {"#d": [SETTINGS]}), makeOutboxLoader(APP_DATA, {"#d": [SETTINGS]}),
getSettings, (pubkey: string) => getSettingsByPubkey().get(pubkey),
) )
export const userSettings = makeUserData(settingsByPubkey, loadSettings) export const userSettings = makeUserData(settingsByPubkey, loadSettings)
@@ -336,7 +334,9 @@ export const loadUserSettings = makeUserLoader(loadSettings)
export const userSettingsValues = derived(userSettings, $s => $s?.values || defaultSettings) export const userSettingsValues = derived(userSettings, $s => $s?.values || defaultSettings)
export const getSetting = <T>(key: keyof Settings["values"]) => get(userSettingsValues)[key] as T export const getSettings = getter(userSettingsValues)
export const getSetting = <T>(key: keyof Settings["values"]) => getSettings()[key] as T
// Relays sending events with empty signatures that the user has to choose to trust // Relays sending events with empty signatures that the user has to choose to trust
@@ -346,11 +346,11 @@ export const relaysPendingTrust = writable<string[]>([])
export const relaysMostlyRestricted = writable<Record<string, string>>({}) export const relaysMostlyRestricted = writable<Record<string, string>>({})
// Alerts // Push notifications
export const alertToken = writable<string | undefined>() export const pushToken = writable<string | undefined>()
export const alertSecret = writable<string | undefined>() export const pushSecret = writable<string | undefined>()
// Chats // Chats
+186 -143
View File
@@ -1,4 +1,4 @@
import type {Unsubscriber} from "svelte/store" import type {Unsubscriber, Subscriber} from "svelte/store"
import {derived, get} from "svelte/store" import {derived, get} from "svelte/store"
import {Capacitor} from "@capacitor/core" import {Capacitor} from "@capacitor/core"
import {Badge} from "@capawesome/capacitor-badge" import {Badge} from "@capawesome/capacitor-badge"
@@ -14,17 +14,19 @@ import {
publishThunk, publishThunk,
getPubkeyRelays, getPubkeyRelays,
loadRelay, loadRelay,
waitForThunkError,
} from "@welshman/app" } from "@welshman/app"
import type {Maybe
} from "@welshman/lib"
import { import {
on,
call,
assoc,
poll, poll,
prop, prop,
sha256, sha256,
textEncoder, textEncoder,
parseJson, parseJson,
ms,
maybe,
int,
MINUTE,
flatten, flatten,
find, find,
spec, spec,
@@ -32,18 +34,12 @@ import {
identity, identity,
now, now,
groupBy, groupBy,
hash,
tryCatch, tryCatch,
postJson, postJson,
fetchJson, fetchJson,
} from "@welshman/lib" } from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util" import type {TrustedEvent, Filter} from "@welshman/util"
import { import {deriveEventsByIdByUrl} from "@welshman/store"
deriveEventsByIdByUrl,
deriveEventsById,
deriveEventsDesc,
deriveDeduplicated,
} from "@welshman/store"
import { import {
ZAP_GOAL, ZAP_GOAL,
EVENT_TIME, EVENT_TIME,
@@ -73,16 +69,18 @@ import {
MESSAGE_KINDS, MESSAGE_KINDS,
PUSH_BRIDGE, PUSH_BRIDGE,
PUSH_SERVER, PUSH_SERVER,
alertToken, pushToken,
alertSecret, pushSecret,
chatsById, chatsById,
hasNip29, hasNip29,
getSetting, getSettings,
userSettingsValues,
userGroupList, userGroupList,
getSpaceUrlsFromGroupList, getSpaceUrlsFromGroupList,
getSpaceRoomsFromGroupList, getSpaceRoomsFromGroupList,
makeCommentFilter, makeCommentFilter,
userSpaceUrls, userSpaceUrls,
makeRoomId,
} from "@app/core/state" } from "@app/core/state"
import {kv} from "@app/core/storage" import {kv} from "@app/core/storage"
import {goto} from "$app/navigation" import {goto} from "$app/navigation"
@@ -104,7 +102,7 @@ export const setChecked = (key: string) => checked.update(state => ({...state, [
const goalCommentFilters = [{kinds: [COMMENT], "#K": [String(ZAP_GOAL)]}] const goalCommentFilters = [{kinds: [COMMENT], "#K": [String(ZAP_GOAL)]}]
const threadCommentFilters = [{kinds: [COMMENT], "#K": [String(THREAD)]}] const threadCommentFilters = [{kinds: [COMMENT], "#K": [String(THREAD)]}]
const calendarCommentFilters = [{kinds: [COMMENT], "#K": [String(EVENT_TIME)]}] const calendarCommentFilters = [{kinds: [COMMENT], "#K": [String(EVENT_TIME)]}]
const messageFilters = [{kinds: CONTENT_KINDS}] const messageFilters = [{kinds: MESSAGE_KINDS}]
const dmFilters = [{kinds: DM_KINDS}] const dmFilters = [{kinds: DM_KINDS}]
const allFilters = flatten([ const allFilters = flatten([
goalCommentFilters, goalCommentFilters,
@@ -114,11 +112,6 @@ const allFilters = flatten([
dmFilters, dmFilters,
]) ])
export const latestNotification = deriveDeduplicated(
deriveEventsDesc(deriveEventsById({repository, filters: allFilters})),
first,
)
export const notifications = derived( export const notifications = derived(
throttled( throttled(
1000, 1000,
@@ -268,55 +261,107 @@ export const notifications = derived(
}, },
) )
// Badges export const onNotification = call(() => {
const filters = allFilters.map(assoc("since", now()))
const subscribers: Subscriber<TrustedEvent>[] = []
export const badgeCount = derived(notifications, notifications => { let unsubscribe: Unsubscriber | undefined
return notifications.size
return (f: (event: TrustedEvent) => void) => {
subscribers.push(f)
if (!unsubscribe) {
unsubscribe = on(repository, "update", ({added}) => {
const $pubkey = pubkey.get()
const {muted_rooms} = getSettings()
for (const event of added) {
if (event.pubkey == $pubkey) {
continue
}
const h = getTagValue("h", event.tags)
const muted = Array.from(tracker.getRelays(event.id)).every(
url => h && muted_rooms.includes(makeRoomId(url, h)),
)
if (muted) {
continue
}
if (matchFilters(filters, event)) {
for (const f of subscribers) {
f(event)
}
}
}
})
}
return () => {
subscribers.splice(subscribers.indexOf(f), 1)
if (subscribers.length === 0) {
unsubscribe?.()
unsubscribe = undefined
}
}
}
}) })
export const handleBadgeCountChanges = async (count: number) => { // Badges
if (getSetting<boolean>("alerts_badge")) {
try { export const syncBadges = () =>
await Badge.set({count}) derived([notifications, userSettingsValues], identity).subscribe(
} catch (err) { async ([$notifications, {alerts_badge}]) => {
// failed to set badge if (alerts_badge) {
} try {
} else { await Badge.set({count: $notifications.size})
await clearBadges() } catch (err) {
} // pass - firefox doesn't support badges
} }
} else {
await clearBadges()
}
},
)
export const clearBadges = async () => { export const clearBadges = async () => {
try { try {
await Badge.clear() await Badge.clear()
} catch (e) { } catch (e) {
// Pass - firefox doesn't support this // pass - firefox doesn't support this
} }
} }
// Local notifications // Local notifications
interface IAlertsAdapter { interface IPushAdapter {
request: () => Promise<string> request: () => Promise<string>
start: () => Unsubscriber start: () => Unsubscriber
} }
class CapacitorNotifications implements IAlertsAdapter { class CapacitorNotifications implements IPushAdapter {
async ensureSubscription() { async sync() {
const token = get(alertToken) const token = get(pushToken)
if (!token) { if (!token) {
return return
} }
const info = await tryCatch(async () => { const info: Maybe<{
const secret = get(alertSecret) secret: string
callback: string
}> = await tryCatch(async () => {
const secret = get(pushSecret)
if (secret) { if (secret) {
const {callback} = await fetchJson(`${PUSH_SERVER}/subscription/${secret}`) const {callback} = await fetchJson(`${PUSH_SERVER}/subscription/${secret}`)
if (callback) { if (callback) {
return {secret, callback} return {secret, callback}
} else {
pushSecret.set(undefined)
} }
} }
@@ -328,92 +373,85 @@ class CapacitorNotifications implements IAlertsAdapter {
const json = await postJson(`${PUSH_SERVER}/subscription/${channel}`, data) const json = await postJson(`${PUSH_SERVER}/subscription/${channel}`, data)
if (json) { if (json) {
return { pushSecret.set(json.sk)
secret: json.sk,
callback: json.callback, return {secret: json.sk, callback: json.callback}
} as {
secret: string
callback: string
}
} }
}) })
if (info) { if (!info) {
alertSecret.set(info.secret) return
}
const getPushStuff = async (url: string) => { const getPushStuff = async (url: string) => {
let relay = await loadRelay(url) let relay = await loadRelay(url)
if (!relay?.self || !relay?.supported_nips?.includes("9a")) { if (!relay?.self || !relay?.supported_nips?.includes("9a")) {
relay = await loadRelay(PUSH_BRIDGE) relay = await loadRelay(PUSH_BRIDGE)
}
if (relay?.self) {
return {url: relay.url, pubkey: relay.self}
}
} }
for (const url of get(userSpaceUrls)) { if (relay?.self) {
const stuff = await getPushStuff(url) return {url: relay.url, pubkey: relay.self}
}
}
if (!stuff) { const syncSubscription = async (
console.warn(`Failed to subscribe ${url} to space notifications`) key: string,
continue relay: string,
} filters: Filter[],
ignore: Filter[] = [],
) => {
const stuff = await getPushStuff(relay)
const filters = [{kinds: MESSAGE_KINDS}, makeCommentFilter(CONTENT_KINDS)] if (!stuff) {
// const ignore = [] todo - muted rooms console.warn(`Failed to subscribe ${relay} to ${key} notifications: unsupported`)
} else {
const {url, pubkey} = stuff
const identifier = await sha256(textEncoder.encode(info.callback + relay + key))
publishThunk({ const thunk = publishThunk({
relays: [stuff.url], relays: [url],
event: makeEvent(30390, { event: makeEvent(30390, {
content: await signer.get().nip44.encrypt( content: await signer
stuff.pubkey, .get()
JSON.stringify([ .nip44.encrypt(
["relay", url], pubkey,
["callback", info.callback], JSON.stringify([
// ...ignore.map(filter => ["ignore", JSON.stringify(filter)]), ["relay", relay],
...filters.map(filter => ["filter", JSON.stringify(filter)]), ["callback", info.callback],
]), ...ignore.map(filter => ["ignore", JSON.stringify(filter)]),
), ...filters.map(filter => ["filter", JSON.stringify(filter)]),
]),
),
tags: [ tags: [
["d", await sha256(textEncoder.encode(info.callback + url + "spaces"))], ["d", identifier],
["p", stuff.pubkey], ["p", pubkey],
], ],
}), }),
}) })
}
const $pubkey = pubkey.get()! const error = await waitForThunkError(thunk)
for (const url of getPubkeyRelays($pubkey, RelayMode.Messaging)) { if (error) {
const stuff = await getPushStuff(url) console.warn(`Failed to subscribe ${relay} to ${key} notifications:`, error)
if (!stuff) {
console.warn(`Failed to subscribe ${url} to messaging notifications`)
continue
} }
publishThunk({
relays: [stuff.url],
event: makeEvent(30390, {
content: await signer.get().nip44.encrypt(
stuff.pubkey,
JSON.stringify([
["relay", url],
["callback", info.callback],
["filter", JSON.stringify({kinds: DM_KINDS, "#p": [$pubkey]})],
]),
),
tags: [
["d", await sha256(textEncoder.encode(info.callback + url + "messages"))],
["p", stuff.pubkey],
],
}),
})
} }
} else { }
alertSecret.set(undefined)
for (const relay of get(userSpaceUrls)) {
const {muted_rooms} = getSettings()
const filters = [{kinds: MESSAGE_KINDS}, makeCommentFilter(CONTENT_KINDS)]
const ignore = [{"#h": [muted_rooms]}]
syncSubscription("spaces", relay, filters, ignore)
}
const $pubkey = pubkey.get()!
for (const relay of getPubkeyRelays($pubkey, RelayMode.Messaging)) {
const filters = [{kinds: DM_KINDS, "#p": [$pubkey]}]
syncSubscription("messages", relay, filters)
} }
} }
@@ -445,18 +483,18 @@ class CapacitorNotifications implements IAlertsAdapter {
}) })
if (token) { if (token) {
alertToken.set(token) pushToken.set(token)
return "granted" return "granted"
} }
alertToken.set(undefined) pushToken.set(undefined)
return "denied" return "denied"
} }
start() { start() {
this.ensureSubscription().then(() => { this.sync().then(() => {
PushNotifications.addListener( PushNotifications.addListener(
"pushNotificationActionPerformed", "pushNotificationActionPerformed",
async (action: ActionPerformed) => { async (action: ActionPerformed) => {
@@ -472,7 +510,7 @@ class CapacitorNotifications implements IAlertsAdapter {
} }
} }
class WebNotifications implements IAlertsAdapter { class WebNotifications implements IPushAdapter {
async request() { async request() {
if (Notification?.permission === "default") { if (Notification?.permission === "default") {
await Notification.requestPermission() await Notification.requestPermission()
@@ -506,21 +544,19 @@ class WebNotifications implements IAlertsAdapter {
} }
start() { start() {
let initialized = false return onNotification(event => {
const {alerts_messages, alerts_mentions, alerts_spaces} = getSettings()
return latestNotification.subscribe(event => { if (document.hidden && Notification?.permission === "granted") {
if (!initialized) { if (alerts_messages && matchFilters(dmFilters, event)) {
initialized = true
} else if (event && document.hidden && Notification?.permission === "granted") {
if (getSetting<boolean>("alerts_messages") && matchFilters(dmFilters, event)) {
this.notify(event, "New direct message", "Someone sent you a direct message.") this.notify(event, "New direct message", "Someone sent you a direct message.")
} else if ( } else if (
alerts_mentions &&
event.pubkey !== pubkey.get() && event.pubkey !== pubkey.get() &&
getSetting<boolean>("alerts_mentions") &&
getPubkeyTagValues(event.tags).includes(pubkey.get()!) getPubkeyTagValues(event.tags).includes(pubkey.get()!)
) { ) {
this.notify(event, "Someone mentioned you", "Someone tagged you in a message.") this.notify(event, "Someone mentioned you", "Someone tagged you in a message.")
} else if (getSetting<boolean>("alerts_spaces")) { } else if (alerts_spaces) {
this.notify(event, "New activity", "Someone posted a new message.") this.notify(event, "New activity", "Someone posted a new message.")
} }
} }
@@ -528,49 +564,56 @@ class WebNotifications implements IAlertsAdapter {
} }
} }
export class Alerts { export class Push {
static _adapter: IAlertsAdapter | undefined static _adapter: IPushAdapter | undefined
static _unsubscriber: Unsubscriber | undefined static _unsubscriber: Unsubscriber | undefined
static _getAdapter() { static _getAdapter() {
if (!Alerts._adapter) { if (!Push._adapter) {
if (Capacitor.isNativePlatform()) { if (Capacitor.isNativePlatform()) {
Alerts._adapter = new CapacitorNotifications() Push._adapter = new CapacitorNotifications()
} else { } else {
Alerts._adapter = new WebNotifications() Push._adapter = new WebNotifications()
} }
} }
return Alerts._adapter return Push._adapter
} }
static start() { static start() {
return Alerts._getAdapter().start() return Push._getAdapter().start()
} }
static request() { static request() {
return Alerts._getAdapter().request() return Push._getAdapter().request()
} }
static resume() { static resume() {
if (getSetting<boolean>("alerts_push")) { const unsubscribe = userSettingsValues.subscribe(({alerts_push}) => {
const promise = Alerts.request() if (alerts_push) {
const controller = new AbortController() const promise = Push.request()
const controller = new AbortController()
promise.then(permissions => { promise.then(permissions => {
if (permissions === "granted" && !controller.signal.aborted) { if (permissions === "granted" && !controller.signal.aborted) {
controller.signal.addEventListener("abort", Alerts.start()) controller.signal.addEventListener("abort", Push.start())
} }
}) })
Alerts._unsubscriber = () => controller.abort() Push._unsubscriber = () => controller.abort()
} else {
Push.stop()
}
})
return () => {
unsubscribe()
Push.stop()
} }
return Alerts.stop
} }
static stop() { static stop() {
Alerts._unsubscriber?.() Push._unsubscriber?.()
Alerts._unsubscriber = undefined Push._unsubscriber = undefined
} }
} }
+1 -1
View File
@@ -16,7 +16,7 @@
replaceState?: boolean replaceState?: boolean
disabled?: boolean disabled?: boolean
class?: string class?: string
'data-tip'?: string "data-tip"?: string
} = $props() } = $props()
const go = (e: Event) => { const go = (e: Event) => {
+2 -2
View File
@@ -123,13 +123,13 @@
unsubscribers.push(setupHistory(), setupAnalytics(), syncApplicationData()) unsubscribers.push(setupHistory(), setupAnalytics(), syncApplicationData())
// Subscribe to badge count for changes // Subscribe to badge count for changes
unsubscribers.push(notifications.badgeCount.subscribe(notifications.handleBadgeCountChanges)) unsubscribers.push(notifications.syncBadges)
// Initialize keyboard state tracking // Initialize keyboard state tracking
unsubscribers.push(syncKeyboard()) unsubscribers.push(syncKeyboard())
// Initialize background notifications // Initialize background notifications
unsubscribers.push(notifications.Alerts.resume()) unsubscribers.push(notifications.Push.resume())
// Listen for signer errors, report to user via toast // Listen for signer errors, report to user via toast
unsubscribers.push( unsubscribers.push(
+34 -22
View File
@@ -2,13 +2,15 @@
import cx from "classnames" import cx from "classnames"
import {sleep, remove} from "@welshman/lib" import {sleep, remove} from "@welshman/lib"
import {displayRelayUrl} from "@welshman/util" import {displayRelayUrl} from "@welshman/util"
import {Capacitor} from "@capacitor/core"
import {Badge} from "@capawesome/capacitor-badge"
import CloseCircle from "@assets/icons/close-circle.svg?dataurl" import CloseCircle from "@assets/icons/close-circle.svg?dataurl"
import {preventDefault} from "@lib/html" import {preventDefault} from "@lib/html"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import RoomName from "@app/components/RoomName.svelte" import RoomName from "@app/components/RoomName.svelte"
import {pushToast} from "@app/util/toast" import {pushToast} from "@app/util/toast"
import {Alerts, clearBadges} from "@app/util/notifications" import {Push, clearBadges} from "@app/util/notifications"
import {userSettingsValues, splitRoomId} from "@app/core/state" import {userSettingsValues, splitRoomId} from "@app/core/state"
import {publishSettings} from "@app/core/commands" import {publishSettings} from "@app/core/commands"
@@ -24,7 +26,7 @@
const onAlertsPushChange = () => { const onAlertsPushChange = () => {
if (settings.alerts_push) { if (settings.alerts_push) {
Alerts.request().then(permissions => { Push.request().then(permissions => {
if (permissions !== "granted") { if (permissions !== "granted") {
sleep(300).then(() => { sleep(300).then(() => {
settings.alerts_push = false settings.alerts_push = false
@@ -46,12 +48,6 @@
const onsubmit = preventDefault(async () => { const onsubmit = preventDefault(async () => {
await publishSettings($state.snapshot(settings)) await publishSettings($state.snapshot(settings))
if (settings.alerts_push) {
await Alerts.start()
} else {
await Alerts.stop()
}
pushToast({message: "Your settings have been saved!"}) pushToast({message: "Your settings have been saved!"})
}) })
@@ -61,18 +57,26 @@
<form class="content column gap-4" {onsubmit}> <form class="content column gap-4" {onsubmit}>
<div class="card2 bg-alt col-4 shadow-md"> <div class="card2 bg-alt col-4 shadow-md">
<strong class="text-lg">Alert Settings</strong> <strong class="text-lg">Alert Settings</strong>
<div class="flex justify-between"> {#await Badge.isSupported()}
<p>Show badge for unread alerts</p> <!-- pass -->
<input {:then { isSupported }}
type="checkbox" {#if isSupported}
class="toggle toggle-primary" <div class="flex justify-between">
bind:checked={settings.alerts_badge} <p>Show badge for unread alerts</p>
onchange={onAlertsBadgeChange} /> <input
</div> type="checkbox"
<div class="flex justify-between"> class="toggle toggle-primary"
<p>Play sound for new activity</p> bind:checked={settings.alerts_badge}
<input type="checkbox" class="toggle toggle-primary" bind:checked={settings.alerts_sound} /> onchange={onAlertsBadgeChange} />
</div> </div>
{/if}
{/await}
{#if !Capacitor.isNativePlatform()}
<div class="flex justify-between">
<p>Play sound for new activity</p>
<input type="checkbox" class="toggle toggle-primary" bind:checked={settings.alerts_sound} />
</div>
{/if}
<div class="flex justify-between"> <div class="flex justify-between">
<p>Enable push notifications</p> <p>Enable push notifications</p>
<input <input
@@ -82,7 +86,11 @@
onchange={onAlertsPushChange} /> onchange={onAlertsPushChange} />
</div> </div>
</div> </div>
<div class="card2 bg-alt col-4 shadow-md"> <div
class={cx("card2 bg-alt col-4 shadow-md", {
"pointer-events-none opacity-50":
!settings.alerts_badge && !settings.alerts_sound && !settings.alerts_push,
})}>
<strong class="text-lg">Alert Types</strong> <strong class="text-lg">Alert Types</strong>
<div class="flex justify-between"> <div class="flex justify-between">
<p>Notify me about new activity</p> <p>Notify me about new activity</p>
@@ -100,7 +108,11 @@
bind:checked={settings.alerts_messages} /> bind:checked={settings.alerts_messages} />
</div> </div>
</div> </div>
<div class="card2 bg-alt col-4 shadow-md"> <div
class={cx("card2 bg-alt col-4 shadow-md", {
"pointer-events-none opacity-50":
!settings.alerts_badge && !settings.alerts_sound && !settings.alerts_push,
})}>
<strong class="text-lg">Muted Rooms</strong> <strong class="text-lg">Muted Rooms</strong>
{#each settings.muted_rooms as id (id)} {#each settings.muted_rooms as id (id)}
{@const [url, h] = splitRoomId(id)} {@const [url, h] = splitRoomId(id)}
+12 -6
View File
@@ -1,5 +1,4 @@
<script lang="ts"> <script lang="ts">
import cx from "classnames"
import {readable, derived as _derived} from "svelte/store" import {readable, derived as _derived} from "svelte/store"
import {onMount, onDestroy} from "svelte" import {onMount, onDestroy} from "svelte"
import {page} from "$app/stores" import {page} from "$app/stores"
@@ -76,7 +75,14 @@
const isFavorite = $derived($userRooms.includes(h)) const isFavorite = $derived($userRooms.includes(h))
const membershipStatus = deriveUserRoomMembershipStatus(url, h) const membershipStatus = deriveUserRoomMembershipStatus(url, h)
const isMuted = $derived($userSettingsValues.muted_rooms.includes($room.id)) const isMuted = $derived($userSettingsValues.muted_rooms.includes($room.id))
const hasAlerts = throttled(800, _derived(userSettingsValues, ({alerts_spaces, alerts_push, alerts_sound, alerts_badge}) => alerts_spaces && (alerts_push || alerts_sound || alerts_badge))) const hasAlerts = throttled(
800,
_derived(
userSettingsValues,
({alerts_spaces, alerts_push, alerts_sound, alerts_badge}) =>
alerts_spaces && (alerts_push || alerts_sound || alerts_badge),
),
)
const showRoomDetail = () => pushModal(RoomDetail, {url, h}) const showRoomDetail = () => pushModal(RoomDetail, {url, h})
@@ -371,22 +377,22 @@
class="btn btn-neutral btn-sm tooltip tooltip-left" class="btn btn-neutral btn-sm tooltip tooltip-left"
data-tip="Notifications are turned off" data-tip="Notifications are turned off"
onclick={unmuteRoom}> onclick={unmuteRoom}>
<Icon size={4} icon={VolumeLoud} /> <Icon size={4} icon={VolumeCross} />
</Button> </Button>
{:else} {:else}
<Button <Button
class="btn btn-neutral btn-sm tooltip tooltip-left" class="btn btn-neutral btn-sm tooltip tooltip-left"
data-tip="Notifications are turned on" data-tip="Notifications are turned on"
onclick={muteRoom}> onclick={muteRoom}>
<Icon size={4} icon={VolumeLoud} class="text-primary" /> <Icon size={4} icon={VolumeLoud} />
</Button> </Button>
{/if} {/if}
{#if isFavorite} {#if isFavorite}
<Button <Button
class="btn btn-neutral btn-sm tooltip tooltip-left" class="btn btn-neutral btn-sm tooltip tooltip-left text-secondary"
data-tip="Remove Favorite" data-tip="Remove Favorite"
onclick={removeFavorite}> onclick={removeFavorite}>
<Icon size={4} icon={Bookmark} class="text-primary" /> <Icon size={4} icon={Bookmark} />
</Button> </Button>
{:else} {:else}
<Button <Button