flotilla/src/routes/+layout.svelte

227 lines
6.7 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"
2024-11-14 21:17:47 +00:00
import {nip19} from "nostr-tools"
2024-11-14 00:14:05 +00:00
import {get, derived} from "svelte/store"
2024-10-23 16:45:06 +00:00
import {dev} from "$app/environment"
2024-11-16 16:04:09 +00:00
import {bytesToHex, hexToBytes} from "@noble/hashes/utils"
2025-01-02 23:08:16 +00:00
import {identity, sleep, take, sortBy, 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"
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,
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"
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"
import {loadUserData} 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"
2024-08-23 20:33:36 +00:00
import * as state 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)
}
2024-10-15 21:44:21 +00:00
let ready: Promise<unknown> = Promise.resolve()
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,
...state,
...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
2024-10-08 20:20:55 +00:00
if (!db) {
setupTracking()
2024-10-25 21:15:35 +00:00
setupAnalytics()
2024-10-08 22:30:18 +00:00
ready = initStorage("flotilla", 4, {
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-02 20:03:16 +00:00
events: 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]
const $sessionKeys = new Set(Object.keys(app.sessions.get()))
const $userFollows = new Set(getPubkeyTagValues(getListTags(get(app.userFollows))))
const $maxWot = get(app.maxWot)
const scoreEvent = (e: TrustedEvent) => {
const isFollowing = $userFollows.has(e.pubkey)
// 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
if ($sessionKeys.has(e.pubkey)) return ALWAYS_KEEP
if (e.tags.some(t => $sessionKeys.has(t[1]))) return ALWAYS_KEEP
// 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
let score = isFollowing ? $maxWot : app.getUserWotScore(e.pubkey)
// 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
}),
2024-10-08 22:30:18 +00:00
}).then(() => sleep(300))
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}
2024-08-14 23:25:36 +00:00
<div data-theme={$theme} />
2024-08-14 19:46:25 +00:00
{:then}
2024-08-14 23:25:36 +00:00
<div data-theme={$theme}>
<AppContainer>
<slot />
</AppContainer>
<ModalContainer />
2024-10-16 18:27:27 +00:00
<div class="tippy-target" />
2024-08-05 21:45:16 +00:00
</div>
2024-08-14 19:46:25 +00:00
{/await}