event utils

This commit is contained in:
Martti Malmi 2023-08-17 18:31:20 +03:00
parent 60052d6833
commit f02968edc4
4 changed files with 18 additions and 19 deletions

View File

@ -5,9 +5,9 @@ import { Link, route } from 'preact-router';
import InfiniteScroll from '@/components/helpers/InfiniteScroll.tsx';
import PubSub from '@/nostr/PubSub.ts';
import { getEventReplyingTo, getEventRoot } from '@/nostr/utils.ts';
import SortedMap from '@/utils/SortedMap.tsx';
import Events from '../../../nostr/Events';
import Key from '../../../nostr/Key';
import { translate as t } from '../../../translations/Translation.mjs';
import Show from '../../helpers/Show';
@ -79,8 +79,9 @@ const Note = ({
const sortedRepliesMap = new SortedMap<string, Event>(comparator);
const callback = (event) => {
sortedRepliesMap.set(event.id, event);
const callback = (reply) => {
if (getEventReplyingTo(reply) !== event.id) return;
sortedRepliesMap.set(reply.id, reply);
const sortedReplies = Array.from(sortedRepliesMap.keys()).slice(0, showReplies);
setReplies(sortedReplies);
};
@ -92,7 +93,7 @@ const Note = ({
};
}, [event.id, showReplies]);
let rootMsg = Events.getEventRoot(event);
let rootMsg = getEventRoot(event);
if (!rootMsg) {
rootMsg = meta.replyingTo;
}

View File

@ -1,7 +1,6 @@
import debounce from 'lodash/debounce';
import {
Event,
Filter,
getEventHash,
getPublicKey,
nip04,
@ -13,6 +12,7 @@ import { EventTemplate } from 'nostr-tools';
import EventDB from '@/nostr/EventDB.ts';
import {
getEventReplyingTo,
getEventRoot,
getNoteReplyingTo,
getOriginalPostEventId,
getRepostedEventId,
@ -63,7 +63,7 @@ localState.get('dev').on((d) => {
const Events = {
DEFAULT_GLOBAL_FILTER,
getEventHash,
db: new EventDB(), // TODO gb2 own indexing with Maps. easier to evict & understand what it actually does
db: new EventDB(),
eventsMetaDb: new EventMetaStore(),
seen: new Set<string>(),
deletedEvents: new Set<string>(),
@ -432,9 +432,6 @@ const Events = {
}
return true;
},
find(filter: Filter, callback: (event: Event) => void) {
this.db.find(filter, callback);
},
handle(event: Event & { id: string }, force = false, saveToIdb = true, retries = 2): boolean {
if (!event) return false;
if (!force && this.seen.has(event.id)) {
@ -608,14 +605,6 @@ const Events = {
}
return muted;
},
getEventRoot(event: Event) {
const rootEvent = event?.tags?.find((t) => t[0] === 'e' && t[3] === 'root')?.[1];
if (rootEvent) {
return rootEvent;
}
// first e tag
return event?.tags?.find((t) => t[0] === 'e')?.[1];
},
maybeAddNotification(event: Event) {
// if we're mentioned in tags, add to notifications
if (event.tags?.filter((tag) => tag[0] === 'p').length > 10) {
@ -634,7 +623,7 @@ const Events = {
}
if (!this.isMuted(event)) {
this.db.insert(event);
const target = this.getEventRoot(event) || getEventReplyingTo(event) || event.id; // TODO get thread root instead
const target = getEventRoot(event) || getEventReplyingTo(event) || event.id; // TODO get thread root instead
const key = `${event.kind}-${target}`;
const existing = this.latestNotificationByTargetAndKind.get(key); // also latestNotificationByAuthor?
const existingEvent = existing && this.db.get(existing);

View File

@ -112,7 +112,7 @@ const PubSub = {
filter.authors.forEach((a) => this.subscribedAuthors.add(a));
}
callback && Events.find(filter, callback);
callback && Events.db.find(filter, callback);
if (dev.indexedDbLoad !== false) {
IndexedDB.subscribe(filter);

View File

@ -65,3 +65,12 @@ export function getZappingUser(event: Event, npub = true) {
}
return obj.pubkey;
}
export function getEventRoot(event: Event) {
const rootEvent = event?.tags?.find((t) => t[0] === 'e' && t[3] === 'root')?.[1];
if (rootEvent) {
return rootEvent;
}
// first e tag
return event?.tags?.find((t) => t[0] === 'e')?.[1];
}