flotilla/src/routes/+layout.svelte

273 lines
7.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"
import "@capacitor-community/safe-area"
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"
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"
2025-04-16 21:08:58 +00:00
import {
WRAP,
EVENT_TIME,
THREAD,
MESSAGE,
INBOX_RELAYS,
2025-05-05 17:11:02 +00:00
DIRECT_MESSAGE,
2025-06-03 23:46:30 +00:00
DIRECT_MESSAGE_FILE,
2025-04-16 21:08:58 +00:00
MUTES,
FOLLOWS,
PROFILE,
RELAYS,
BLOSSOM_SERVERS,
2025-04-25 17:41:38 +00:00
getRelaysFromList,
2025-04-16 21:08:58 +00:00
} from "@welshman/util"
2025-04-11 18:51:15 +00:00
import {Nip46Broker, 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
userInboxRelaySelections,
2025-04-11 18:51:15 +00:00
loginWithNip01,
loginWithNip46,
2025-04-16 21:08:58 +00:00
EventsStorageAdapter,
loadRelaySelections,
2024-08-30 23:25:41 +00:00
} from "@welshman/app"
import * as lib from "@welshman/lib"
import * as util from "@welshman/util"
2025-06-19 17:22:17 +00:00
import * as feeds from "@welshman/feeds"
import * as router from "@welshman/router"
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"
2025-05-13 21:31:34 +00:00
import {
INDEXER_RELAYS,
userMembership,
userSettingValues,
ensureUnwrapped,
canDecrypt,
} from "@app/state"
2025-02-14 19:12:19 +00:00
import {loadUserData, listenForNotifications} from "@app/requests"
import {theme} from "@app/theme"
2025-06-24 21:15:36 +00:00
import {initializePushNotifications} from "@app/push"
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)
}
2025-06-24 21:15:36 +00:00
// Initialize push notification handler asap
initializePushNotifications()
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,
...lib,
...welshmanSigner,
...router,
2024-11-15 22:03:54 +00:00
...util,
2025-06-19 17:22:17 +00:00
...feeds,
2024-11-15 22:03:54 +00:00
...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-06-19 17:22:17 +00:00
// Listen for navigation messages from service worker
2025-06-24 21:15:36 +00:00
navigator.serviceWorker?.addEventListener("message", event => {
if (event.data && event.data.type === "NAVIGATE") {
2025-06-19 17:22:17 +00:00
goto(event.data.url)
}
})
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://")) {
2025-04-11 18:51:15 +00:00
const clientSecret = makeSecret()
const {signerPubkey, connectSecret, relays} = Nip46Broker.parseBunkerUrl(login)
2025-05-07 20:53:58 +00:00
const broker = new Nip46Broker({relays, clientSecret, signerPubkey})
2025-04-11 18:51:15 +00:00
const result = await broker.connect(connectSecret, appState.NIP46_PERMS)
const pubkey = await broker.getPublicKey()
// TODO: remove ack result
if (pubkey && ["ack", connectSecret].includes(result)) {
await loadUserData(pubkey)
2025-01-28 21:04:37 +00:00
2025-04-11 18:51:15 +00:00
loginWithNip46(pubkey, clientSecret, signerPubkey, relays)
2025-05-07 20:53:58 +00:00
broker.cleanup()
2025-04-11 18:51:15 +00:00
success = true
}
} else if (login) {
loginWithNip01(nsecDecode(login))
2025-01-28 21:04:37 +00:00
success = true
}
} catch (e) {
console.error(e)
}
if (success) {
goto("/home")
}
}
// Sync theme
theme.subscribe($theme => {
document.body.setAttribute("data-theme", $theme)
})
2025-05-13 21:31:34 +00:00
// Sync font size
userSettingValues.subscribe($userSettingValues => {
// @ts-ignore
document.documentElement.style["font-size"] = `${$userSettingValues.font_size}rem`
})
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-05-05 17:11:02 +00:00
// Unwrap gift wraps as they come in, but throttled
const unwrapper = new TaskQueue<TrustedEvent>({batchSize: 10, processItem: ensureUnwrapped})
repository.on("update", ({added}) => {
for (const event of added) {
loadRelaySelections(event.pubkey)
if ($canDecrypt && event.kind === WRAP) {
2025-05-05 17:11:02 +00:00
unwrapper.push(event)
}
}
})
2025-04-23 18:05:28 +00:00
await initStorage("flotilla", 8, {
2025-04-16 21:08:58 +00:00
...defaultStorageAdapters,
events: new EventsStorageAdapter({
name: "events",
limit: 10_000,
repository,
rankEvent: (e: TrustedEvent) => {
2025-05-28 23:46:41 +00:00
if ([PROFILE, FOLLOWS, MUTES, RELAYS, BLOSSOM_SERVERS, INBOX_RELAYS].includes(e.kind)) {
return 1
2025-05-05 17:11:02 +00:00
}
2025-06-03 23:46:30 +00:00
if (
[EVENT_TIME, THREAD, MESSAGE, DIRECT_MESSAGE, DIRECT_MESSAGE_FILE].includes(e.kind)
) {
2025-05-05 17:11:02 +00:00
return 0.9
}
2025-04-16 21:08:58 +00:00
return 0
},
}),
2025-02-04 00:37:14 +00:00
})
2024-08-14 19:46:25 +00:00
2025-04-23 18:05:28 +00:00
sleep(300).then(() => ready.resolve())
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
// 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-11 16:27:19 +00:00
let controller: AbortController
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-11 16:27:19 +00:00
controller?.abort()
controller = new AbortController()
2024-11-14 00:14:05 +00:00
2025-01-02 23:08:16 +00:00
if ($pubkey && $canDecrypt) {
2025-04-11 16:27:19 +00:00
request({
signal: controller.signal,
2024-11-14 18:53:13 +00:00
filters: [
{kinds: [WRAP], "#p": [$pubkey], since: ago(WEEK, 2)},
{kinds: [WRAP], "#p": [$pubkey], limit: 100},
],
2025-04-25 17:41:38 +00:00
relays: getRelaysFromList($userInboxRelaySelections),
2024-11-14 00:14:05 +00:00
})
}
},
)
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}
<div></div>
2024-08-14 19:46:25 +00:00
{:then}
<div>
<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}