fix: follow people

create a new migration that replaces the function to NOTIFY only the event id
This commit is contained in:
P. Reis 2024-10-29 11:07:51 -03:00
parent 8ff4a0bdbd
commit 6fd0c273cb
2 changed files with 36 additions and 2 deletions

View file

@ -0,0 +1,27 @@
import { Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await sql`DROP TRIGGER IF EXISTS nostr_event_trigger ON nostr_events`.execute(db);
await sql`
CREATE OR REPLACE FUNCTION notify_nostr_event()
RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('nostr_event', NEW.id::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
`.execute(db);
await sql`
CREATE TRIGGER nostr_event_trigger
AFTER INSERT OR UPDATE ON nostr_events
FOR EACH ROW EXECUTE FUNCTION notify_nostr_event()
`.execute(db);
}
export async function down(db: Kysely<any>): Promise<void> {
await sql`DROP TRIGGER nostr_event_trigger ON nostr_events`.execute(db);
await sql`DROP FUNCTION notify_nostr_event()`.execute(db);
}

View file

@ -1,5 +1,6 @@
import { Semaphore } from '@lambdalisue/async'; import { Semaphore } from '@lambdalisue/async';
import { Conf } from '@/config.ts';
import * as pipeline from '@/pipeline.ts'; import * as pipeline from '@/pipeline.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
@ -7,12 +8,18 @@ const sem = new Semaphore(1);
export async function startNotify(): Promise<void> { export async function startNotify(): Promise<void> {
const { listen } = await Storages.database(); const { listen } = await Storages.database();
const store = await Storages.db();
listen('nostr_event', (payload) => { listen('nostr_event', (payload) => {
sem.lock(async () => { sem.lock(async () => {
try { try {
const event = JSON.parse(payload); const id = payload;
await pipeline.handleEvent(event, AbortSignal.timeout(5000)); const timeout = Conf.db.timeouts.default;
const [event] = await store.query([{ ids: [id], limit: 1 }], { signal: AbortSignal.timeout(timeout) });
if (event) {
await pipeline.handleEvent(event, AbortSignal.timeout(timeout));
}
} catch (e) { } catch (e) {
console.warn(e); console.warn(e);
} }