From 0a8a2c6dc15b4630111b679aa8b96914d612e808 Mon Sep 17 00:00:00 2001 From: Viktor Poluksht Date: Fri, 15 May 2026 15:13:48 +0200 Subject: [PATCH] Publish post trending events --- __tests__/workers/cdc/primary.ts | 32 ++++++++++++++++++++++++++++++++ src/common/typedPubsub.ts | 5 +++++ src/workers/cdc/primary.ts | 17 +++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/__tests__/workers/cdc/primary.ts b/__tests__/workers/cdc/primary.ts index 24f9367ded..a5abcb0a5d 100644 --- a/__tests__/workers/cdc/primary.ts +++ b/__tests__/workers/cdc/primary.ts @@ -1771,6 +1771,38 @@ describe('post', () => { ]); }); + it('should notify when post becomes trending', async () => { + const trendingAt = new Date('2026-05-15T12:30:00.000Z'); + const before: ChangeObject = { + ...base, + trending: null, + }; + const after: ChangeObject = { + ...before, + trending: 42, + lastTrending: trendingAt.getTime() * 1000, + }; + await expectSuccessfulBackground( + worker, + mockChangeMessage({ + after, + before, + op: 'u', + table: 'post', + }), + ); + + expect(triggerTypedEvent).toHaveBeenCalledTimes(2); + expect(jest.mocked(triggerTypedEvent).mock.calls[1].slice(1)).toEqual([ + 'api.v1.post-trending', + { + postId: 'p1', + trending: 42, + trendingAt: trendingAt.toISOString(), + }, + ]); + }); + describe('collection', () => { it('should notify when collection content is updated', async () => { const before = { diff --git a/src/common/typedPubsub.ts b/src/common/typedPubsub.ts index 7bd072b458..a54222e537 100644 --- a/src/common/typedPubsub.ts +++ b/src/common/typedPubsub.ts @@ -190,6 +190,11 @@ export type PubSubSchema = { }; 'skadi.v2.campaign-updated': CampaignUpdateEventArgs; 'api.v1.post-metrics-updated': z.infer; + 'api.v1.post-trending': { + postId: string; + trending: number; + trendingAt: string; + }; 'api.v1.post-highlighted': PostHighlightedMessage; 'api.v1.reputation-event': { op: ChangeMessage['payload']['op']; diff --git a/src/workers/cdc/primary.ts b/src/workers/cdc/primary.ts index 4bd00cd035..9abe9066af 100644 --- a/src/workers/cdc/primary.ts +++ b/src/workers/cdc/primary.ts @@ -1122,6 +1122,23 @@ const onPostChange = async ( }, }); } + + const trending = data.payload.after!.trending; + if ( + data.payload.before!.trending == null && + typeof trending === 'number' && + trending > 0 + ) { + const trendingAt = data.payload.after!.lastTrending + ? debeziumTimeToDate(data.payload.after!.lastTrending).toISOString() + : new Date().toISOString(); + + await triggerTypedEvent(logger, 'api.v1.post-trending', { + postId: data.payload.after!.id, + trending, + trendingAt, + }); + } } else if (data.payload.op === 'd') { await notifyPostBannedOrRemoved(logger, data.payload.before!, 'hard'); }