fix mention in composer and improve error handling

This commit is contained in:
Ren Amamiya
2023-09-01 15:57:31 +07:00
parent cc315a190a
commit e6d35bc635
10 changed files with 156 additions and 124 deletions

View File

@@ -19,36 +19,34 @@ export function useEvent(id: string, embed?: string) {
}
// get event from db
const dbEvent = await db.getEventByID(id);
if (dbEvent) {
return dbEvent;
if (dbEvent) return dbEvent;
// get event from relay if event in db not present
const event = await ndk.fetchEvent(id);
if (!event) throw new Error(`Event not found: ${id}`);
let root: string;
let reply: string;
if (event.tags?.[0]?.[0] === 'e' && !event.tags?.[0]?.[3]) {
root = event.tags[0][1];
} else {
// get event from relay if event in db not present
const event = await ndk.fetchEvent(id);
if (!event) throw new Error(`Event not found: ${id}`);
let root: string;
let reply: string;
if (event.tags?.[0]?.[0] === 'e' && !event.tags?.[0]?.[3]) {
root = event.tags[0][1];
} else {
root = event.tags.find((el) => el[3] === 'root')?.[1];
reply = event.tags.find((el) => el[3] === 'reply')?.[1];
}
const rawEvent = toRawEvent(event);
await db.createEvent(
event.id,
JSON.stringify(rawEvent),
event.pubkey,
event.kind,
root,
reply,
event.created_at
);
return event;
root = event.tags.find((el) => el[3] === 'root')?.[1];
reply = event.tags.find((el) => el[3] === 'reply')?.[1];
}
const rawEvent = toRawEvent(event);
await db.createEvent(
event.id,
JSON.stringify(rawEvent),
event.pubkey,
event.kind,
root,
reply,
event.created_at
);
return event;
},
{
enabled: !!ndk,

View File

@@ -5,8 +5,6 @@ import { Event, parseReferences } from 'nostr-tools';
import { RichContent } from '@utils/types';
export function parser(event: NDKEvent) {
if (event.kind !== 1) return;
const references = parseReferences(event as unknown as Event);
const urls = getUrls(event.content as unknown as string);