flotilla/src/routes/+layout.svelte

201 lines
5.3 KiB
Svelte
Raw Normal View History

2024-08-05 21:45:16 +00:00
<script lang="ts">
2024-08-06 20:10:10 +00:00
import "@src/app.css"
2024-08-16 17:50:38 +00:00
import {onMount} from "svelte"
2025-01-28 21:04:37 +00:00
import * as nip19 from "nostr-tools/nip19"
2024-11-14 00:14:05 +00:00
import {get, derived} from "svelte/store"
2025-01-24 17:36:24 +00:00
import {App} from "@capacitor/app"
2024-10-23 16:45:06 +00:00
import {dev} from "$app/environment"
2025-01-28 21:04:37 +00:00
import {goto} from "$app/navigation"
2024-11-16 16:04:09 +00:00
import {bytesToHex, hexToBytes} from "@noble/hashes/utils"
2025-04-11 15:41:50 +00:00
import {identity, memoize, sleep, defer, ago, WEEK, TaskQueue} from "@welshman/lib"
2025-04-09 22:32:18 +00:00
import type {TrustedEvent, StampedEvent} from "@welshman/util"
import {WRAP} from "@welshman/util"
2025-01-28 21:04:37 +00:00
import {Nip46Broker, getPubkey, makeSecret} from "@welshman/signer"
2025-04-09 22:32:18 +00:00
import type {Socket} from "@welshman/net"
import {request, defaultSocketPolicies, makeSocketPolicyAuth} from "@welshman/net"
2024-08-30 23:25:41 +00:00
import {
loadRelay,
db,
2024-08-30 23:25:41 +00:00
initStorage,
repository,
pubkey,
2025-04-09 22:32:18 +00:00
defaultStorageAdapters,
2024-10-23 00:29:43 +00:00
session,
signer,
dropSession,
2024-11-14 00:14:05 +00:00
getRelayUrls,
userInboxRelaySelections,
2025-01-28 21:04:37 +00:00
addSession,
2024-08-30 23:25:41 +00:00
} from "@welshman/app"
import * as lib from "@welshman/lib"
import * as util from "@welshman/util"
2024-11-15 22:03:54 +00:00
import * as welshmanSigner from "@welshman/signer"
2024-11-14 00:14:05 +00:00
import * as net from "@welshman/net"
2024-09-11 18:09:25 +00:00
import * as app from "@welshman/app"
import AppContainer from "@app/components/AppContainer.svelte"
import ModalContainer from "@app/components/ModalContainer.svelte"
import {setupTracking} from "@app/tracking"
import {setupAnalytics} from "@app/analytics"
2025-01-28 21:04:37 +00:00
import {nsecDecode} from "@lib/util"
2024-08-14 23:25:36 +00:00
import {theme} from "@app/theme"
2024-12-11 00:38:22 +00:00
import {INDEXER_RELAYS, userMembership, ensureUnwrapped, canDecrypt} from "@app/state"
2025-02-14 19:12:19 +00:00
import {loadUserData, listenForNotifications} from "@app/requests"
import {loginWithNip46} from "@app/commands"
2024-11-15 22:03:54 +00:00
import * as commands from "@app/commands"
2024-12-11 00:38:22 +00:00
import * as requests from "@app/requests"
2024-11-14 18:53:13 +00:00
import * as notifications from "@app/notifications"
2025-02-04 00:37:14 +00:00
import * as appState from "@app/state"
2024-08-05 21:45:16 +00:00
2024-10-23 00:29:43 +00:00
// Migration: old nostrtalk instance used different sessions
if ($session && !$signer) {
dropSession($session.pubkey)
}
const {children} = $props()
2025-02-04 00:37:14 +00:00
const ready = $state(defer<void>())
2024-08-09 00:22:17 +00:00
2024-11-14 00:14:05 +00:00
onMount(async () => {
2024-11-15 22:03:54 +00:00
Object.assign(window, {
get,
nip19,
2024-11-16 16:04:09 +00:00
bytesToHex,
hexToBytes,
2024-11-15 22:03:54 +00:00
...lib,
...welshmanSigner,
...util,
...net,
...app,
2025-02-04 00:37:14 +00:00
...appState,
2024-11-15 22:03:54 +00:00
...commands,
2024-12-11 00:38:22 +00:00
...requests,
2024-11-15 22:03:54 +00:00
...notifications,
})
2024-08-23 20:33:36 +00:00
2025-01-28 21:04:37 +00:00
// Nstart login
if (window.location.hash?.startsWith("#nostr-login")) {
const params = new URLSearchParams(window.location.hash.slice(1))
const login = params.get("nostr-login")
let success = false
try {
if (login?.startsWith("bunker://")) {
success = await loginWithNip46({
clientSecret: makeSecret(),
...Nip46Broker.parseBunkerUrl(login),
})
} else if (login) {
const secret = nsecDecode(login)
addSession({method: "nip01", secret, pubkey: getPubkey(secret)})
success = true
}
} catch (e) {
console.error(e)
}
if (success) {
goto("/home")
}
}
2024-10-08 20:20:55 +00:00
if (!db) {
setupTracking()
2024-10-25 21:15:35 +00:00
setupAnalytics()
2025-01-24 17:36:24 +00:00
App.addListener("backButton", () => {
if (window.history.length > 1) {
window.history.back()
} else {
App.exitApp()
}
})
2025-04-09 22:32:18 +00:00
initStorage("flotilla", 6, defaultStorageAdapters).then(async () => {
2025-02-04 00:37:14 +00:00
await sleep(300)
ready.resolve()
})
2024-08-14 19:46:25 +00:00
2025-04-09 22:32:18 +00:00
defaultSocketPolicies.push(
makeSocketPolicyAuth({
sign: (event: StampedEvent) => signer.get()?.sign(event),
shouldAuth: (socket: Socket) => true,
}),
)
2024-11-14 00:14:05 +00:00
2025-04-09 22:32:18 +00:00
// Unwrap gift wraps as they come in, but throttled
const unwrapper = new TaskQueue<TrustedEvent>({batchSize: 10, processItem: ensureUnwrapped})
2024-11-14 00:14:05 +00:00
repository.on("update", ({added}) => {
if (!$canDecrypt) {
return
}
2024-11-14 00:14:05 +00:00
for (const event of added) {
if (event.kind === WRAP) {
unwrapper.push(event)
}
}
})
// Load relay info
for (const url of INDEXER_RELAYS) {
loadRelay(url)
}
// Load user data
if ($pubkey) {
await loadUserData($pubkey)
}
2024-11-14 18:53:13 +00:00
// Listen for space data, populate space-based notifications
2024-12-17 01:07:52 +00:00
let unsubSpaces: any
2024-11-13 01:44:29 +00:00
2025-04-11 15:41:50 +00:00
userMembership.subscribe(
memoize($membership => {
unsubSpaces?.()
unsubSpaces = listenForNotifications()
}),
)
2024-11-13 01:44:29 +00:00
2024-11-14 18:53:13 +00:00
// Listen for chats, populate chat-based notifications
2025-04-09 22:32:18 +00:00
let chatsReq: any
2024-11-13 01:44:29 +00:00
2025-01-02 23:08:16 +00:00
derived([pubkey, canDecrypt, userInboxRelaySelections], identity).subscribe(
([$pubkey, $canDecrypt, $userInboxRelaySelections]) => {
2025-04-09 22:32:18 +00:00
chatsReq?.close()
2024-11-14 00:14:05 +00:00
2025-01-02 23:08:16 +00:00
if ($pubkey && $canDecrypt) {
2025-04-09 22:32:18 +00:00
chatsReq = request({
2024-11-14 18:53:13 +00:00
filters: [
{kinds: [WRAP], "#p": [$pubkey], since: ago(WEEK, 2)},
{kinds: [WRAP], "#p": [$pubkey], limit: 100},
],
2024-11-14 00:14:05 +00:00
relays: getRelayUrls($userInboxRelaySelections),
})
}
},
)
2024-10-15 21:44:21 +00:00
}
2024-08-09 00:22:17 +00:00
})
2024-08-05 21:45:16 +00:00
</script>
2024-10-23 16:45:06 +00:00
<svelte:head>
{#if !dev}
<link rel="manifest" href="/manifest.webmanifest" />
{/if}
</svelte:head>
2024-08-14 19:46:25 +00:00
{#await ready}
2025-02-03 23:01:42 +00:00
<div data-theme={$theme}></div>
2024-08-14 19:46:25 +00:00
{:then}
2024-08-14 23:25:36 +00:00
<div data-theme={$theme}>
<AppContainer>
2025-02-04 00:37:14 +00:00
{@render children()}
</AppContainer>
<ModalContainer />
2025-02-03 23:01:42 +00:00
<div class="tippy-target"></div>
2024-08-05 21:45:16 +00:00
</div>
2024-08-14 19:46:25 +00:00
{/await}