mirror of
https://github.com/luminous-devs/lume.git
synced 2024-09-18 11:13:30 +00:00
wip: replace ndk cache with metadata table
This commit is contained in:
parent
413571ee7f
commit
18a9ba3fb0
@ -0,0 +1,9 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
CREATE TABLE
|
||||||
|
metadata (
|
||||||
|
id TEXT NOT NULL PRIMARY KEY,
|
||||||
|
event TEXT NOT NULL,
|
||||||
|
author TEXT NOT NULL,
|
||||||
|
kind NUMBER NOT NULL DEFAULt 0,
|
||||||
|
created_at INTEGER NOT NULL
|
||||||
|
);
|
@ -217,6 +217,12 @@ fn main() {
|
|||||||
sql: include_str!("../migrations/20230918235335_add_uniq_to_relay.sql"),
|
sql: include_str!("../migrations/20230918235335_add_uniq_to_relay.sql"),
|
||||||
kind: MigrationKind::Up,
|
kind: MigrationKind::Up,
|
||||||
},
|
},
|
||||||
|
Migration {
|
||||||
|
version: 20230921085234,
|
||||||
|
description: "add metadata",
|
||||||
|
sql: include_str!("../migrations/20230921085234_add_metadata_table.sql"),
|
||||||
|
kind: MigrationKind::Up,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.build(),
|
.build(),
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
// inspire by: https://github.com/nostr-dev-kit/ndk-react/
|
|
||||||
import NDK from '@nostr-dev-kit/ndk';
|
import NDK from '@nostr-dev-kit/ndk';
|
||||||
import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
|
import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
|
||||||
import { message } from '@tauri-apps/api/dialog';
|
import { message } from '@tauri-apps/api/dialog';
|
||||||
@ -6,7 +5,6 @@ import { fetch } from '@tauri-apps/api/http';
|
|||||||
import { NostrFetcher } from 'nostr-fetch';
|
import { NostrFetcher } from 'nostr-fetch';
|
||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
import TauriAdapter from '@libs/ndk/cache';
|
|
||||||
import { useStorage } from '@libs/storage/provider';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
export const NDKInstance = () => {
|
export const NDKInstance = () => {
|
||||||
@ -15,7 +13,6 @@ export const NDKInstance = () => {
|
|||||||
const [ndk, setNDK] = useState<NDK | undefined>(undefined);
|
const [ndk, setNDK] = useState<NDK | undefined>(undefined);
|
||||||
const [relayUrls, setRelayUrls] = useState<string[]>([]);
|
const [relayUrls, setRelayUrls] = useState<string[]>([]);
|
||||||
|
|
||||||
const cacheAdapter = useMemo(() => new TauriAdapter(), [ndk]);
|
|
||||||
const fetcher = useMemo(
|
const fetcher = useMemo(
|
||||||
() => (ndk ? NostrFetcher.withCustomPool(ndkAdapter(ndk)) : null),
|
() => (ndk ? NostrFetcher.withCustomPool(ndkAdapter(ndk)) : null),
|
||||||
[ndk]
|
[ndk]
|
||||||
@ -61,7 +58,6 @@ export const NDKInstance = () => {
|
|||||||
const explicitRelayUrls = await getExplicitRelays();
|
const explicitRelayUrls = await getExplicitRelays();
|
||||||
const instance = new NDK({
|
const instance = new NDK({
|
||||||
explicitRelayUrls,
|
explicitRelayUrls,
|
||||||
cacheAdapter,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -79,10 +75,6 @@ export const NDKInstance = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ndk) initNDK();
|
if (!ndk) initNDK();
|
||||||
|
|
||||||
return () => {
|
|
||||||
cacheAdapter.saveCache();
|
|
||||||
};
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -6,6 +6,7 @@ import { Stronghold } from 'tauri-plugin-stronghold-api';
|
|||||||
|
|
||||||
import { FULL_RELAYS } from '@stores/constants';
|
import { FULL_RELAYS } from '@stores/constants';
|
||||||
|
|
||||||
|
import { toRawEvent } from '@utils/rawEvent';
|
||||||
import { Account, DBEvent, Relays, Widget } from '@utils/types';
|
import { Account, DBEvent, Relays, Widget } from '@utils/types';
|
||||||
|
|
||||||
export class LumeStorage {
|
export class LumeStorage {
|
||||||
@ -282,6 +283,31 @@ export class LumeStorage {
|
|||||||
return results.length < 1;
|
return results.length < 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async createMetadata(event: NDKEvent) {
|
||||||
|
const rawEvent = toRawEvent(event);
|
||||||
|
|
||||||
|
return await this.db.execute(
|
||||||
|
'INSERT OR IGNORE INTO metadata (id, event, author, kind, created_at) VALUES ($1, $2, $3, $4, $5);',
|
||||||
|
[
|
||||||
|
rawEvent.id,
|
||||||
|
JSON.stringify(rawEvent),
|
||||||
|
rawEvent.pubkey,
|
||||||
|
rawEvent.kind,
|
||||||
|
rawEvent.created_at,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getMetadataByPubkey(pubkey: string) {
|
||||||
|
const results: DBEvent[] = await this.db.select(
|
||||||
|
'SELECT * FROM metadata WHERE author = $1 AND kind = "0" LIMIT 1;',
|
||||||
|
[pubkey]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (results.length < 1) return null;
|
||||||
|
return JSON.parse(results[0].event as string) as NDKEvent;
|
||||||
|
}
|
||||||
|
|
||||||
public async getExplicitRelayUrls() {
|
public async getExplicitRelayUrls() {
|
||||||
if (!this.account) return FULL_RELAYS;
|
if (!this.account) return FULL_RELAYS;
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
|
import { NDKKind, NDKUserProfile } from '@nostr-dev-kit/ndk';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { useNDK } from '@libs/ndk/provider';
|
import { useNDK } from '@libs/ndk/provider';
|
||||||
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
export function useProfile(pubkey: string, embed?: string) {
|
export function useProfile(pubkey: string, embed?: string) {
|
||||||
|
const { db } = useStorage();
|
||||||
const { ndk } = useNDK();
|
const { ndk } = useNDK();
|
||||||
const {
|
const {
|
||||||
status,
|
status,
|
||||||
@ -14,14 +16,24 @@ export function useProfile(pubkey: string, embed?: string) {
|
|||||||
async () => {
|
async () => {
|
||||||
if (!embed) {
|
if (!embed) {
|
||||||
const cleanPubkey = pubkey.replace('-', '');
|
const cleanPubkey = pubkey.replace('-', '');
|
||||||
const user = ndk.getUser({ hexpubkey: cleanPubkey });
|
const dbEvent = await db.getMetadataByPubkey(cleanPubkey);
|
||||||
await user.fetchProfile();
|
if (dbEvent) {
|
||||||
if (user.profile) {
|
return JSON.parse(dbEvent.content) as NDKUserProfile;
|
||||||
user.profile.display_name = user.profile.displayName;
|
|
||||||
return user.profile;
|
|
||||||
} else {
|
|
||||||
throw new Error(`User not found: ${pubkey}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const events = await ndk.fetchEvents(
|
||||||
|
{
|
||||||
|
kinds: [NDKKind.Metadata],
|
||||||
|
authors: [cleanPubkey],
|
||||||
|
limit: 1,
|
||||||
|
},
|
||||||
|
{ closeOnEose: true }
|
||||||
|
);
|
||||||
|
if (!events) throw new Error(`User not found: ${pubkey}`);
|
||||||
|
const latestEvent = [...events].sort((a, b) => b.created_at - a.created_at)[0];
|
||||||
|
await db.createMetadata(latestEvent);
|
||||||
|
|
||||||
|
return JSON.parse(latestEvent.content) as NDKUserProfile;
|
||||||
} else {
|
} else {
|
||||||
const profile: NDKUserProfile = JSON.parse(embed);
|
const profile: NDKUserProfile = JSON.parse(embed);
|
||||||
return profile;
|
return profile;
|
||||||
|
Loading…
Reference in New Issue
Block a user