Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions __tests__/workers/cdc/primary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectType> = {
...base,
trending: null,
};
const after: ChangeObject<ObjectType> = {
...before,
trending: 42,
lastTrending: trendingAt.getTime() * 1000,
};
await expectSuccessfulBackground(
worker,
mockChangeMessage<ObjectType>({
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 = {
Expand Down
5 changes: 5 additions & 0 deletions src/common/typedPubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ export type PubSubSchema = {
};
'skadi.v2.campaign-updated': CampaignUpdateEventArgs;
'api.v1.post-metrics-updated': z.infer<typeof postMetricsUpdatedTopic>;
'api.v1.post-trending': {
postId: string;
trending: number;
trendingAt: string;
};
'api.v1.post-highlighted': PostHighlightedMessage;
'api.v1.reputation-event': {
op: ChangeMessage<unknown>['payload']['op'];
Expand Down
17 changes: 17 additions & 0 deletions src/workers/cdc/primary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Loading