flotilla/src/routes/+layout.svelte

235 lines
6.6 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"
2024-12-11 00:38:22 +00:00
import {identity, sleep, take, sortBy, ago, now, HOUR, WEEK, 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 {
PROFILE,
REACTION,
ZAP_RESPONSE,
FOLLOWS,
RELAYS,
INBOX_RELAYS,
WRAP,
getPubkeyTagValues,
getListTags,
} from "@welshman/util"
2024-09-20 18:00:41 +00:00
import {throttled} from "@welshman/store"
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,
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"
import {subscribePersistent, 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 {checked} from "@app/notifications"
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-10 20:27:39 +00:00
const getScoreEvent = () => {
const ALWAYS_KEEP = Infinity
const NEVER_KEEP = 0
const reactionKinds = [REACTION, ZAP_RESPONSE]
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)
return (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
// 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
2024-10-11 00:15:20 +00:00
score *= metaKinds.includes(e.kind) ? 2 : e.created_at / now()
2024-10-10 20:27:39 +00:00
return score
}
}
const migrateFreshness = (data: {key: string; value: number}[]) => {
const cutoff = ago(HOUR)
return data.filter(({value}) => value > cutoff)
2024-10-10 20:27:39 +00:00
}
2024-10-11 00:15:20 +00:00
const migratePlaintext = (data: {key: string; value: number}[]) => data.slice(0, 10_000)
2024-10-10 20:27:39 +00:00
const migrateEvents = (events: TrustedEvent[]) => {
if (events.length < 50_000) {
return events
}
const scoreEvent = getScoreEvent()
2024-10-11 00:15:20 +00:00
return take(
30_000,
sortBy(e => -scoreEvent(e), events),
)
2024-10-10 20:27:39 +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, {
relays: {keyPath: "url", store: throttled(3000, relays)},
handles: {keyPath: "nip05", store: throttled(3000, handles)},
checked: storageAdapters.fromObjectStore(checked, {throttle: 3000}),
2024-10-11 00:15:20 +00:00
freshness: storageAdapters.fromObjectStore(freshness, {
throttle: 3000,
2024-10-11 00:15:20 +00:00
migrate: migrateFreshness,
}),
plaintext: storageAdapters.fromObjectStore(plaintext, {
throttle: 3000,
2024-10-11 00:15:20 +00:00
migrate: migratePlaintext,
}),
2024-12-09 22:03:59 +00:00
events: storageAdapters.fromRepositoryAndTracker(repository, tracker, {
throttle: 3000,
migrate: migrateEvents,
}),
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
let unsubSpaces: any
2024-11-13 01:44:29 +00:00
userMembership.subscribe($membership => {
unsubSpaces?.()
2024-12-11 00:38:22 +00:00
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-11-13 01:44:29 +00:00
let unsubChats: any
2024-11-14 00:14:05 +00:00
derived([pubkey, userInboxRelaySelections], identity).subscribe(
([$pubkey, $userInboxRelaySelections]) => {
unsubChats?.()
if ($pubkey) {
unsubChats = subscribePersistent({
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}