flotilla/src/routes/+layout.svelte

285 lines
8 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-02-04 00:37:14 +00:00
import {
identity,
sleep,
take,
sortBy,
defer,
ago,
now,
HOUR,
WEEK,
MONTH,
Worker,
} from "@welshman/lib"
2024-10-10 20:27:39 +00:00
import type {TrustedEvent} from "@welshman/util"
2024-10-11 00:15:20 +00:00
import {
2025-01-02 23:08:16 +00:00
MESSAGE,
2024-10-11 00:15:20 +00:00
PROFILE,
2025-01-02 23:08:16 +00:00
DELETE,
2024-10-11 00:15:20 +00:00
REACTION,
ZAP_RESPONSE,
FOLLOWS,
RELAYS,
INBOX_RELAYS,
WRAP,
getPubkeyTagValues,
getListTags,
} from "@welshman/util"
2025-01-28 21:04:37 +00:00
import {Nip46Broker, getPubkey, makeSecret} from "@welshman/signer"
2024-08-30 23:25:41 +00:00
import {
relays,
handles,
loadRelay,
db,
2024-08-30 23:25:41 +00:00
initStorage,
repository,
pubkey,
plaintext,
freshness,
2024-09-11 03:19:57 +00:00
storageAdapters,
2024-09-11 21:08:04 +00:00
tracker,
2024-10-23 00:29:43 +00:00
session,
signer,
dropSession,
2024-11-14 00:14:05 +00:00
getRelayUrls,
2024-12-17 00:14:51 +00:00
subscribe,
2024-11-14 00:14:05 +00:00
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-01-28 21:04:37 +00:00
import {loadUserData, loginWithNip46} from "@app/commands"
2024-12-17 00:14:51 +00:00
import {listenForNotifications} from "@app/requests"
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-02-04 00:37:14 +00:00
initStorage("flotilla", 5, {
2025-01-02 18:04:28 +00:00
relays: storageAdapters.fromCollectionStore("url", relays, {throttle: 3000}),
handles: storageAdapters.fromCollectionStore("nip05", handles, {throttle: 3000}),
2024-10-11 00:15:20 +00:00
freshness: storageAdapters.fromObjectStore(freshness, {
throttle: 3000,
2025-01-02 20:03:16 +00:00
migrate: (data: {key: string; value: number}[]) => {
const cutoff = ago(HOUR)
return data.filter(({value}) => value > cutoff)
},
2024-10-11 00:15:20 +00:00
}),
plaintext: storageAdapters.fromObjectStore(plaintext, {
throttle: 3000,
2025-01-02 20:03:16 +00:00
migrate: (data: {key: string; value: number}[]) => data.slice(0, 10_000),
2024-10-11 00:15:20 +00:00
}),
2025-01-14 23:03:36 +00:00
events2: storageAdapters.fromRepositoryAndTracker(repository, tracker, {
2024-12-09 22:03:59 +00:00
throttle: 3000,
2025-01-02 20:03:16 +00:00
migrate: (events: TrustedEvent[]) => {
2025-01-02 23:08:16 +00:00
if (events.length < 15_000) {
2025-01-02 20:03:16 +00:00
return events
}
const NEVER_KEEP = 0
const ALWAYS_KEEP = Infinity
2025-01-02 23:08:16 +00:00
const reactionKinds = [REACTION, ZAP_RESPONSE, DELETE]
2025-01-02 20:03:16 +00:00
const metaKinds = [PROFILE, FOLLOWS, RELAYS, INBOX_RELAYS]
2025-02-04 00:37:14 +00:00
const sessionKeys = new Set(Object.keys(app.sessions.get()))
const userFollows = new Set(getPubkeyTagValues(getListTags(get(app.userFollows))))
const maxWot = get(app.maxWot)
2025-01-02 20:03:16 +00:00
const scoreEvent = (e: TrustedEvent) => {
2025-02-04 00:37:14 +00:00
const isFollowing = userFollows.has(e.pubkey)
2025-01-02 20:03:16 +00:00
// No need to keep a record of everyone who follows the current user
if (e.kind === FOLLOWS && !isFollowing) return NEVER_KEEP
2025-01-02 23:08:16 +00:00
// Drop room messages after a month, re-load on demand
if (e.kind === MESSAGE && e.created_at < ago(MONTH)) return NEVER_KEEP
2025-01-02 20:03:16 +00:00
// Always keep stuff by or tagging a signed in user
2025-02-04 00:37:14 +00:00
if (sessionKeys.has(e.pubkey)) return ALWAYS_KEEP
if (e.tags.some(t => sessionKeys.has(t[1]))) return ALWAYS_KEEP
2025-01-02 20:03:16 +00:00
// Get rid of irrelevant messages, reactions, and likes
if (e.wrap || e.kind === 4 || e.kind === WRAP) return NEVER_KEEP
if (reactionKinds.includes(e.kind)) return NEVER_KEEP
// If the user follows this person, use max wot score
2025-02-04 00:37:14 +00:00
let score = isFollowing ? maxWot : app.getUserWotScore(e.pubkey)
2025-01-02 20:03:16 +00:00
// Inflate the score for profiles/relays/follows to avoid redundant fetches
// Demote non-metadata type events, and introduce recency bias
score *= metaKinds.includes(e.kind) ? 2 : e.created_at / now()
return score
}
return take(
2025-01-02 23:08:16 +00:00
10_000,
2025-01-02 20:03:16 +00:00
sortBy(e => -scoreEvent(e), events),
)
},
2024-12-09 22:03:59 +00:00
}),
2025-02-04 00:37:14 +00:00
}).then(async () => {
await sleep(300)
ready.resolve()
})
2024-08-14 19:46:25 +00:00
2024-11-14 00:14:05 +00:00
// Unwrap gift wraps as they come in, but throttled
const unwrapper = new Worker<TrustedEvent>({chunkSize: 10})
unwrapper.addGlobalHandler(ensureUnwrapped)
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
userMembership.subscribe($membership => {
2024-12-17 01:07:52 +00:00
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
2024-12-17 00:14:51 +00:00
let chatsSub: 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]) => {
2024-12-17 00:14:51 +00:00
chatsSub?.close()
2024-11-14 00:14:05 +00:00
2025-01-02 23:08:16 +00:00
if ($pubkey && $canDecrypt) {
2024-12-17 00:14:51 +00:00
chatsSub = subscribe({
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}