Browse Source

Update NDK and ensure feed and article read works

master
buttercat1791 2 years ago committed by limina1
parent
commit
a341e1adc5
  1. 4
      package.json
  2. 82
      pnpm-lock.yaml
  3. 17
      src/lib/Article.svelte
  4. 21
      src/lib/components/Login.svelte
  5. 2
      src/lib/consts.ts
  6. 10
      src/lib/ndk.ts
  7. 66
      src/routes/+page.svelte

4
package.json

@ -13,8 +13,8 @@ @@ -13,8 +13,8 @@
"format": "prettier --plugin-search-dir . --write ."
},
"dependencies": {
"@nostr-dev-kit/ndk": "^2.3.3",
"@nostr-dev-kit/ndk-cache-dexie": "^2.2.4",
"@nostr-dev-kit/ndk": "^2.10.0",
"@nostr-dev-kit/ndk-cache-dexie": "^2.5.1",
"@popperjs/core": "^2.11.8",
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@tailwindcss/forms": "^0.5.7",

82
pnpm-lock.yaml

@ -9,11 +9,11 @@ importers: @@ -9,11 +9,11 @@ importers:
.:
dependencies:
'@nostr-dev-kit/ndk':
specifier: ^2.3.3
version: 2.3.3(typescript@5.3.3)
specifier: ^2.10.0
version: 2.10.0(typescript@5.3.3)
'@nostr-dev-kit/ndk-cache-dexie':
specifier: ^2.2.4
version: 2.2.4(typescript@5.3.3)
specifier: ^2.5.1
version: 2.5.1(typescript@5.3.3)
'@popperjs/core':
specifier: ^2.11.8
version: 2.11.8
@ -327,12 +327,18 @@ packages: @@ -327,12 +327,18 @@ packages:
'@noble/ciphers@0.2.0':
resolution: {integrity: sha512-6YBxJDAapHSdd3bLDv6x2wRPwq4QFMUaB3HvljNBUTThDd12eSm7/3F+2lnfzx2jvM+S6Nsy0jEt9QbPqSwqRw==}
'@noble/ciphers@0.5.3':
resolution: {integrity: sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==}
'@noble/curves@1.1.0':
resolution: {integrity: sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==}
'@noble/curves@1.2.0':
resolution: {integrity: sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==}
'@noble/curves@1.5.0':
resolution: {integrity: sha512-J5EKamIHnKPyClwVrzmaf5wSdQXgdHcPZIZLu3bwnbeCx8/7NPK5q2ZBWF+5FvYGByjiQQsJYX6jfgB2wDPn3A==}
'@noble/hashes@1.3.1':
resolution: {integrity: sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==}
engines: {node: '>= 16'}
@ -345,6 +351,10 @@ packages: @@ -345,6 +351,10 @@ packages:
resolution: {integrity: sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==}
engines: {node: '>= 16'}
'@noble/hashes@1.4.0':
resolution: {integrity: sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==}
engines: {node: '>= 16'}
'@noble/secp256k1@2.0.0':
resolution: {integrity: sha512-rUGBd95e2a45rlmFTqQJYEFA4/gdIARFfuTuTqLglz0PZ6AKyzyXsEZZq7UZn8hZsvaBgpCzKKBJizT2cJERXw==}
@ -360,11 +370,12 @@ packages: @@ -360,11 +370,12 @@ packages:
resolution: {integrity: sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==}
engines: {node: '>= 8'}
'@nostr-dev-kit/ndk-cache-dexie@2.2.4':
resolution: {integrity: sha512-HPdA+YnGrbNuXuqvGf1KpRoDo2avaqvJLlRE1iH8W8Yp45T8efJV9/UmT/OU7YgIoeL6k0y5F1zOCLu4P3pJSg==}
'@nostr-dev-kit/ndk-cache-dexie@2.5.1':
resolution: {integrity: sha512-tUwEy68bd9GL5JVuZIjcpdwuDEBnaXen3WJ64/GRDtbyE1RB01Y6hHC7IQC9bcQ6SC7XBGyPd+2nuTyR7+Mffg==}
'@nostr-dev-kit/ndk@2.3.3':
resolution: {integrity: sha512-R2r6U1Xt4B7yygQFgTEexNqhuQQrbJ0Kxh4GvcCgNgSjMI+cPJQPWg4g4noWGRnaWf4epqLNCblfo5UfMuijTw==}
'@nostr-dev-kit/ndk@2.10.0':
resolution: {integrity: sha512-TqCAAo6ylORraAXrzRkCGFN2xTMiFbdER8Y8CtUT0HwOpFG/Wn+PBNeDeDmqkl/6LaPdeyXmVwCWj2KcUjIwYA==}
engines: {node: '>=16'}
'@pkgjs/parseargs@0.11.0':
resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==}
@ -753,9 +764,8 @@ packages: @@ -753,9 +764,8 @@ packages:
devalue@4.3.2:
resolution: {integrity: sha512-KqFl6pOgOW+Y6wJgu80rHpo2/3H07vr8ntR9rkkFIRETewbf5GaYYcakYfiKz89K+sLsuPkQIZaXDMjUObZwWg==}
dexie@3.2.4:
resolution: {integrity: sha512-VKoTQRSv7+RnffpOJ3Dh6ozknBqzWw/F3iqMdsZg958R0AS8AnY9x9d1lbwENr0gzeGJHXKcGhAMRaqys6SxqA==}
engines: {node: '>=6.0'}
dexie@4.0.8:
resolution: {integrity: sha512-1G6cJevS17KMDK847V3OHvK2zei899GwpDiqfEXHP1ASvme6eWJmAp9AU4s1son2TeGkWmC0g3y8ezOBPnalgQ==}
didyoumean@1.2.2:
resolution: {integrity: sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==}
@ -1329,16 +1339,16 @@ packages: @@ -1329,16 +1339,16 @@ packages:
resolution: {integrity: sha512-bdok/XvKII3nUpklnV6P2hxtMNrCboOjAcyBuQnWEhO665FwrSNRxU+AqpsyvO6LgGYPspN+lu5CLtw4jPRKNA==}
engines: {node: '>=0.10.0'}
nostr-tools@1.17.0:
resolution: {integrity: sha512-LZmR8GEWKZeElbFV5Xte75dOeE9EFUW/QLI1Ncn3JKn0kFddDKEfBbFN8Mu4TMs+L4HR/WTPha2l+PPuRnJcMw==}
nostr-tools@2.1.4:
resolution: {integrity: sha512-MX4gU1gJYP2PKdlLwveHhZIJi/k0qp+ZKwGB7gMFkUodJs4V1OX18Cteb+vxFngtD0B5LlCzLn+ml6tzwlKXFA==}
peerDependencies:
typescript: '>=5.0.0'
peerDependenciesMeta:
typescript:
optional: true
nostr-tools@2.1.4:
resolution: {integrity: sha512-MX4gU1gJYP2PKdlLwveHhZIJi/k0qp+ZKwGB7gMFkUodJs4V1OX18Cteb+vxFngtD0B5LlCzLn+ml6tzwlKXFA==}
nostr-tools@2.7.2:
resolution: {integrity: sha512-Bq3Ug0SZFtgtL1+0wCnAe8AJtI7yx/00/a2nUug9SkhfOwlKS92Tef12iCK9FdwXw+oFZWMtRnSwcLayQso+xA==}
peerDependencies:
typescript: '>=5.0.0'
peerDependenciesMeta:
@ -2131,6 +2141,8 @@ snapshots: @@ -2131,6 +2141,8 @@ snapshots:
'@noble/ciphers@0.2.0': {}
'@noble/ciphers@0.5.3': {}
'@noble/curves@1.1.0':
dependencies:
'@noble/hashes': 1.3.1
@ -2139,12 +2151,18 @@ snapshots: @@ -2139,12 +2151,18 @@ snapshots:
dependencies:
'@noble/hashes': 1.3.2
'@noble/curves@1.5.0':
dependencies:
'@noble/hashes': 1.4.0
'@noble/hashes@1.3.1': {}
'@noble/hashes@1.3.2': {}
'@noble/hashes@1.3.3': {}
'@noble/hashes@1.4.0': {}
'@noble/secp256k1@2.0.0': {}
'@nodelib/fs.scandir@2.1.5':
@ -2159,26 +2177,27 @@ snapshots: @@ -2159,26 +2177,27 @@ snapshots:
'@nodelib/fs.scandir': 2.1.5
fastq: 1.17.1
'@nostr-dev-kit/ndk-cache-dexie@2.2.4(typescript@5.3.3)':
'@nostr-dev-kit/ndk-cache-dexie@2.5.1(typescript@5.3.3)':
dependencies:
'@nostr-dev-kit/ndk': 2.3.3(typescript@5.3.3)
'@nostr-dev-kit/ndk': 2.10.0(typescript@5.3.3)
debug: 4.3.4
dexie: 3.2.4
nostr-tools: 1.17.0(typescript@5.3.3)
dexie: 4.0.8
nostr-tools: 2.7.2(typescript@5.3.3)
typescript-lru-cache: 2.0.0
transitivePeerDependencies:
- supports-color
- typescript
'@nostr-dev-kit/ndk@2.3.3(typescript@5.3.3)':
'@nostr-dev-kit/ndk@2.10.0(typescript@5.3.3)':
dependencies:
'@noble/hashes': 1.3.3
'@noble/curves': 1.5.0
'@noble/hashes': 1.4.0
'@noble/secp256k1': 2.0.0
'@scure/base': 1.1.5
debug: 4.3.4
light-bolt11-decoder: 3.0.0
node-fetch: 3.3.2
nostr-tools: 1.17.0(typescript@5.3.3)
nostr-tools: 2.7.2(typescript@5.3.3)
tseep: 1.2.1
typescript-lru-cache: 2.0.0
utf8-buffer: 1.0.0
@ -2240,13 +2259,13 @@ snapshots: @@ -2240,13 +2259,13 @@ snapshots:
'@scure/bip32@1.3.1':
dependencies:
'@noble/curves': 1.1.0
'@noble/hashes': 1.3.1
'@scure/base': 1.1.1
'@noble/hashes': 1.3.3
'@scure/base': 1.1.5
'@scure/bip39@1.2.1':
dependencies:
'@noble/hashes': 1.3.1
'@scure/base': 1.1.1
'@noble/hashes': 1.3.3
'@scure/base': 1.1.5
'@sveltejs/adapter-auto@3.1.1(@sveltejs/kit@2.4.3(@sveltejs/vite-plugin-svelte@3.0.1(svelte@4.2.9)(vite@5.0.12))(svelte@4.2.9)(vite@5.0.12))':
dependencies:
@ -2565,7 +2584,7 @@ snapshots: @@ -2565,7 +2584,7 @@ snapshots:
devalue@4.3.2: {}
dexie@3.2.4: {}
dexie@4.0.8: {}
didyoumean@1.2.2: {}
@ -3168,20 +3187,21 @@ snapshots: @@ -3168,20 +3187,21 @@ snapshots:
normalize-range@0.1.2: {}
nostr-tools@1.17.0(typescript@5.3.3):
nostr-tools@2.1.4(typescript@5.3.3):
dependencies:
'@noble/ciphers': 0.2.0
'@noble/curves': 1.1.0
'@noble/curves': 1.2.0
'@noble/hashes': 1.3.1
'@scure/base': 1.1.1
'@scure/bip32': 1.3.1
'@scure/bip39': 1.2.1
optionalDependencies:
nostr-wasm: 0.1.0
typescript: 5.3.3
nostr-tools@2.1.4(typescript@5.3.3):
nostr-tools@2.7.2(typescript@5.3.3):
dependencies:
'@noble/ciphers': 0.2.0
'@noble/ciphers': 0.5.3
'@noble/curves': 1.2.0
'@noble/hashes': 1.3.1
'@scure/base': 1.1.1

17
src/lib/Article.svelte

@ -18,11 +18,18 @@ @@ -18,11 +18,18 @@
}
const eventIds = index!.getMatchingTags('e').map((value) => value[1]);
const events = await $ndk.fetchEvents({
// @ts-ignore
kinds: zettelKinds,
ids: eventIds,
});
const events = await $ndk.fetchEvents(
{
// @ts-ignore
kinds: zettelKinds,
ids: eventIds,
},
{
groupable: false,
skipVerification: false,
skipValidation: false
}
);
console.debug(`Fetched ${events.size} events from ${eventIds.length} references.`);
return events;

21
src/lib/components/Login.svelte

@ -1,9 +1,18 @@ @@ -1,9 +1,18 @@
<script lang='ts'>
import { Avatar, Button, Popover } from 'flowbite-svelte';
import NDK, { NDKNip07Signer, type NDKUserProfile } from '@nostr-dev-kit/ndk';
import { NDKNip07Signer, type NDKUserProfile } from '@nostr-dev-kit/ndk';
import { signedIn, ndk } from '$lib/ndk';
let profile: NDKUserProfile | null = null;
let pfp: string | undefined = undefined;
let username: string | undefined = undefined;
let tag: string | undefined = undefined;
$: {
pfp = profile?.image;
username = profile?.name;
tag = profile?.name;
}
const signInWithExtension = async () => {
const signer = new NDKNip07Signer();
@ -14,7 +23,7 @@ @@ -14,7 +23,7 @@
$ndk.activeUser = user;
await $ndk.connect();
profile = await user.fetchProfile();
profile = await $ndk.activeUser?.fetchProfile();
console.debug('NDK signed in with extension and reconnected.');
@ -30,16 +39,16 @@ @@ -30,16 +39,16 @@
<Avatar
rounded
class='h-6 w-6 m-4 cursor-pointer'
src={profile?.image}
alt={profile?.displayName}
src={pfp}
alt={username}
/>
<Popover
class='popover-leather w-fit'
placement='bottom'
target='avatar'
>
<h3 class='text-lg font-bold'>{profile?.displayName}</h3>
<h4 class='text-base'>@{profile?.name}</h4>
<h3 class='text-lg font-bold'>{username}</h3>
<h4 class='text-base'>@{tag}</h4>
</Popover>
{:else}
<Avatar rounded class='h-6 w-6 m-4 cursor-pointer' id='avatar' />

2
src/lib/consts.ts

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
export const wikiKind = 30818;
export const indexKind = 30040;
export const zettelKinds = [ 1, 30024, 30041, 30818];
export const standardRelays = [ "wss://thecitadel.nostr1.com" ];
export const standardRelays = [ "wss://thecitadel.nostr1.com", "wss://relay.noswhere.com" ];
export enum FeedType {
Relays,

10
src/lib/ndk.ts

@ -9,17 +9,19 @@ export function getStoredNdkConfig() { @@ -9,17 +9,19 @@ export function getStoredNdkConfig() {
(browser && localStorage.getItem('alexandria_relays')) || JSON.stringify(standardRelays)
);
const dexieAdapter = new NDKCacheAdapterDexie({ dbName: 'alexandria-ndk-cache-db' });
// const dexieAdapter = new NDKCacheAdapterDexie({ dbName: 'alexandria-ndk-cache-db' });
return { relays, dexieAdapter };
return {
relays,
// dexieAdapter,
};
}
export function getNdkInstance() {
const { relays, dexieAdapter } = getStoredNdkConfig();
const { relays } = getStoredNdkConfig();
const ndk = new NDK({
autoConnectUserRelays: true,
cacheAdapter: dexieAdapter,
enableOutboxModel: true,
explicitRelayUrls: relays,
});

66
src/routes/+page.svelte

@ -1,39 +1,55 @@ @@ -1,39 +1,55 @@
<script lang="ts">
import ArticleHeader from "$lib/ArticleHeader.svelte";
import { FeedType, indexKind } from "$lib/consts";
import { FeedType, indexKind, standardRelays } from "$lib/consts";
import { ndk } from "$lib/ndk";
import { filterValidIndexEvents } from "$lib/utils";
import { NDKEvent, NDKRelayList, NDKRelaySet, type NDKUser } from "@nostr-dev-kit/ndk";
import { NDKEvent, NDKRelaySet, type NDKUser } from "@nostr-dev-kit/ndk";
import { Button, Dropdown, Radio, Skeleton } from "flowbite-svelte";
import { ChevronDownOutline } from "flowbite-svelte-icons";
const getEvents = (): Promise<Set<NDKEvent>> =>
// @ts-ignore
$ndk.fetchEvents({ kinds: [indexKind] }).then(filterValidIndexEvents);
const getEventsFromUserRelays = (userRelays: NDKRelayList): Promise<Set<NDKEvent>> => {
const relaySet = NDKRelaySet.fromRelayUrls(userRelays!.readRelayUrls, $ndk);
return $ndk.fetchEvents(
// @ts-ignore
$ndk.fetchEvents(
{ kinds: [indexKind] },
relaySet,
).then(filterValidIndexEvents);
};
const getEventsFromUserFollows = (follows: Set<NDKUser>, userRelays: NDKRelayList): Promise<Set<NDKEvent>> => {
const relaySet = NDKRelaySet.fromRelayUrls(userRelays?.readRelayUrls ?? [], $ndk);
const pubkeys = Array.from(follows ?? []).map(user => user.pubkey);
return $ndk.fetchEvents(
{
authors: pubkeys,
// @ts-ignore
kinds: [indexKind]
groupable: true,
skipVerification: false,
skipValidation: false
},
relaySet,
NDKRelaySet.fromRelayUrls(standardRelays, $ndk)
).then(filterValidIndexEvents);
};
const getEventsFromUserRelays = (userRelays: string[]): Promise<Set<NDKEvent>> => {
return $ndk
.fetchEvents(
// @ts-ignore
{ kinds: [indexKind] },
{
closeOnEose: true,
groupable: true,
skipVerification: false,
skipValidation: false,
},
)
.then(filterValidIndexEvents);
}
const getEventsFromUserFollows = (follows: Set<NDKUser>, userRelays?: string[]): Promise<Set<NDKEvent>> => {
return $ndk
.fetchEvents(
{
authors: Array.from(follows ?? []).map(user => user.pubkey),
// @ts-ignore
kinds: [indexKind]
},
{
groupable: true,
skipVerification: false,
skipValidation: false
},
)
.then(filterValidIndexEvents);
}
const getFeedTypeFriendlyName = (feedType: FeedType): string => {
switch (feedType) {
@ -60,13 +76,13 @@ @@ -60,13 +76,13 @@
}
let user: NDKUser | null | undefined;
let readRelays: NDKRelayList | null | undefined;
let readRelays: string[] | null | undefined;
let userFollows: Set<NDKUser> | null | undefined;
let feedType: FeedType = FeedType.Relays;
$: {
user = $ndk.activeUser;
user?.relayList().then(relays => readRelays = relays);
readRelays = user?.relayUrls;
user?.follows().then(follows => userFollows = follows);
}
</script>

Loading…
Cancel
Save