Update stats before storing event
This commit is contained in:
parent
a3597edb90
commit
eef349f1e9
|
@ -107,10 +107,8 @@ async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise<void
|
||||||
if (deletion) {
|
if (deletion) {
|
||||||
return Promise.reject(new RelayError('blocked', 'event was deleted'));
|
return Promise.reject(new RelayError('blocked', 'event was deleted'));
|
||||||
} else {
|
} else {
|
||||||
await Promise.all([
|
await updateStats(event).catch(debug);
|
||||||
Storages.db.event(event, { signal }).catch(debug),
|
await Storages.db.event(event, { signal }).catch(debug);
|
||||||
updateStats(event).catch(debug),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/stats.ts
10
src/stats.ts
|
@ -1,4 +1,4 @@
|
||||||
import { NostrEvent } from '@nostrify/nostrify';
|
import { NKinds, NostrEvent } from '@nostrify/nostrify';
|
||||||
import Debug from '@soapbox/stickynotes/debug';
|
import Debug from '@soapbox/stickynotes/debug';
|
||||||
import { InsertQueryBuilder } from 'kysely';
|
import { InsertQueryBuilder } from 'kysely';
|
||||||
|
|
||||||
|
@ -16,14 +16,14 @@ type StatDiff = AuthorStatDiff | EventStatDiff;
|
||||||
|
|
||||||
const debug = Debug('ditto:stats');
|
const debug = Debug('ditto:stats');
|
||||||
|
|
||||||
/** Store stats for the event in LMDB. */
|
/** Store stats for the event. */
|
||||||
async function updateStats(event: NostrEvent) {
|
async function updateStats(event: NostrEvent) {
|
||||||
let prev: NostrEvent | undefined;
|
let prev: NostrEvent | undefined;
|
||||||
const queries: InsertQueryBuilder<DittoTables, any, unknown>[] = [];
|
const queries: InsertQueryBuilder<DittoTables, any, unknown>[] = [];
|
||||||
|
|
||||||
// Kind 3 is a special case - replace the count with the new list.
|
// Kind 3 is a special case - replace the count with the new list.
|
||||||
if (event.kind === 3) {
|
if (event.kind === 3) {
|
||||||
prev = await maybeGetPrev(event);
|
prev = await getPrevEvent(event);
|
||||||
if (!prev || event.created_at >= prev.created_at) {
|
if (!prev || event.created_at >= prev.created_at) {
|
||||||
queries.push(updateFollowingCountQuery(event));
|
queries.push(updateFollowingCountQuery(event));
|
||||||
}
|
}
|
||||||
|
@ -153,13 +153,15 @@ function eventStatsQuery(diffs: EventStatDiff[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the last version of the event, if any. */
|
/** Get the last version of the event, if any. */
|
||||||
async function maybeGetPrev(event: NostrEvent): Promise<NostrEvent> {
|
async function getPrevEvent(event: NostrEvent): Promise<NostrEvent | undefined> {
|
||||||
|
if (NKinds.replaceable(event.kind) || NKinds.parameterizedReplaceable(event.kind)) {
|
||||||
const [prev] = await Storages.db.query([
|
const [prev] = await Storages.db.query([
|
||||||
{ kinds: [event.kind], authors: [event.pubkey], limit: 1 },
|
{ kinds: [event.kind], authors: [event.pubkey], limit: 1 },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return prev;
|
return prev;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Set the following count to the total number of unique "p" tags in the follow list. */
|
/** Set the following count to the total number of unique "p" tags in the follow list. */
|
||||||
function updateFollowingCountQuery({ pubkey, tags }: NostrEvent) {
|
function updateFollowingCountQuery({ pubkey, tags }: NostrEvent) {
|
||||||
|
|
Loading…
Reference in New Issue