mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-21 23:44:20 +01:00
Refactor EventStore to use a more Set-like interface
This commit is contained in:
@@ -41,9 +41,9 @@ async function handleEvent(event: Event): Promise<void> {
|
||||
|
||||
/** Encounter the event, and return whether it has already been encountered. */
|
||||
async function encounterEvent(event: Event): Promise<boolean> {
|
||||
const preexisting = (await memorelay.countEvents([{ ids: [event.id] }])) > 0;
|
||||
memorelay.storeEvent(event);
|
||||
reqmeister.storeEvent(event);
|
||||
const preexisting = (await memorelay.count([{ ids: [event.id] }])) > 0;
|
||||
memorelay.add(event);
|
||||
reqmeister.add(event);
|
||||
return preexisting;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ async function storeEvent(event: Event, data: EventData, opts: StoreEventOpts =
|
||||
const { force = false } = opts;
|
||||
|
||||
if (force || data.user || isAdminEvent(event) || await isLocallyFollowed(event.pubkey)) {
|
||||
const [deletion] = await eventsDB.getEvents(
|
||||
const [deletion] = await eventsDB.filter(
|
||||
[{ kinds: [5], authors: [event.pubkey], '#e': [event.id], limit: 1 }],
|
||||
{ limit: 1 },
|
||||
);
|
||||
@@ -75,7 +75,7 @@ async function storeEvent(event: Event, data: EventData, opts: StoreEventOpts =
|
||||
return Promise.reject(new RelayError('blocked', 'event was deleted'));
|
||||
} else {
|
||||
await Promise.all([
|
||||
eventsDB.storeEvent(event, { data }).catch(debug),
|
||||
eventsDB.add(event, { data }).catch(debug),
|
||||
updateStats(event).catch(debug),
|
||||
]);
|
||||
}
|
||||
@@ -88,13 +88,13 @@ async function storeEvent(event: Event, data: EventData, opts: StoreEventOpts =
|
||||
async function processDeletions(event: Event): Promise<void> {
|
||||
if (event.kind === 5) {
|
||||
const ids = getTagSet(event.tags, 'e');
|
||||
const events = await eventsDB.getEvents([{ ids: [...ids] }]);
|
||||
const events = await eventsDB.filter([{ ids: [...ids] }]);
|
||||
|
||||
const deleteIds = events
|
||||
.filter(({ pubkey, id }) => pubkey === event.pubkey && ids.has(id))
|
||||
.map((event) => event.id);
|
||||
|
||||
await eventsDB.deleteEvents([{ ids: deleteIds }]);
|
||||
await eventsDB.deleteFilters([{ ids: deleteIds }]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ function fetchRelatedEvents(event: Event, data: EventData) {
|
||||
reqmeister.req({ kinds: [0], authors: [event.pubkey] }).catch(() => {});
|
||||
}
|
||||
for (const [name, id, relay] of event.tags) {
|
||||
if (name === 'e' && !memorelay.countEvents([{ ids: [id] }])) {
|
||||
if (name === 'e' && !memorelay.count([{ ids: [id] }])) {
|
||||
reqmeister.req({ ids: [id] }, { relays: [relay] }).catch(() => {});
|
||||
}
|
||||
}
|
||||
@@ -173,7 +173,7 @@ function broadcast(event: Event, data: EventData) {
|
||||
if (!data.user || !isFresh(event)) return;
|
||||
|
||||
if (event.kind === 5) {
|
||||
client.storeEvent(event);
|
||||
client.add(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user