From 6e68a8044dbda7a391800589ebb4cc2c828a8dbc Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 20 Jul 2024 01:41:58 +0800 Subject: [PATCH] feat: add date range and improve report display --- src/server/cronjob/index.ts | 2 +- src/server/model/feed/event.ts | 68 +++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/server/cronjob/index.ts b/src/server/cronjob/index.ts index 37f5e9f..6901b13 100644 --- a/src/server/cronjob/index.ts +++ b/src/server/cronjob/index.ts @@ -368,7 +368,7 @@ async function checkFeedEventsNotify( }, }); - sendFeedEventsNotify(channel.notifications, events); + sendFeedEventsNotify(channel, events); }, { concurrency: 5, diff --git a/src/server/model/feed/event.ts b/src/server/model/feed/event.ts index 7e2bf76..abf7ce0 100644 --- a/src/server/model/feed/event.ts +++ b/src/server/model/feed/event.ts @@ -1,16 +1,17 @@ -import { - FeedChannelNotifyFrequency, - FeedEvent, - Notification, - Prisma, -} from '@prisma/client'; +import { FeedChannelNotifyFrequency, FeedEvent, Prisma } from '@prisma/client'; import { subscribeEventBus } from '../../ws/shared'; import { prisma } from '../_client'; import { serializeJSON } from '../../utils/json'; import { buildQueryWithCache } from '../../cache'; import { sendNotification } from '../notification'; -import { token } from '../notification/token'; +import { ContentToken, token } from '../notification/token'; import { logger } from '../../utils/logger'; +import { + FeedChannelModelSchema, + NotificationModelSchema, +} from '../../prisma/zod'; +import dayjs from 'dayjs'; +import { z } from 'zod'; const { get: getFeedEventNotify, del: delFeedEventNotifyCache } = buildQueryWithCache(async (channelId: string) => { @@ -24,10 +25,10 @@ const { get: getFeedEventNotify, del: delFeedEventNotifyCache } = }); if (!channel) { - return [null, []] as const; + return null; } - return [channel.notifyFrequency, channel.notifications] as const; + return channel; }); export { delFeedEventNotifyCache }; @@ -46,30 +47,53 @@ export async function createFeedEvent( ); if (event.channelId) { - const [notify, notifications] = await getFeedEventNotify(event.channelId); + const channel = await getFeedEventNotify(event.channelId); - if (notify === FeedChannelNotifyFrequency.event) { + if (channel?.notifyFrequency === FeedChannelNotifyFrequency.event) { // send notify every event - sendFeedEventsNotify(notifications, [event]); + sendFeedEventsNotify(channel, [event]); } } } export async function sendFeedEventsNotify( - notifications: Notification[], + channel: Pick< + z.infer, + 'name' | 'notifyFrequency' + > & { + notifications: z.infer[]; + }, events: FeedEvent[] ) { - const eventTokens = events - .map((event) => [ - token.text( - `[${event.eventName}] ${event.senderName}: ${event.eventContent}` - ), - token.newline(), - ]) - .flat(); + let frequencyToken = token.paragraph('Range: Every Event'); + if (channel.notifyFrequency === FeedChannelNotifyFrequency.day) { + frequencyToken = token.paragraph( + `Range: Daily | ${dayjs().subtract(1, 'day').toISOString()} - ${dayjs().toISOString()}` + ); + } else if (channel.notifyFrequency === FeedChannelNotifyFrequency.week) { + frequencyToken = token.paragraph( + `Range: Weekly | ${dayjs().subtract(1, 'week').toISOString()} - ${dayjs().toISOString()}` + ); + } else if (channel.notifyFrequency === FeedChannelNotifyFrequency.month) { + frequencyToken = token.paragraph( + `Range: Monthly | ${dayjs().subtract(1, 'month').toISOString()} - ${dayjs().toISOString()}` + ); + } + + const eventTokens: ContentToken[] = [ + token.title('Feed Report from Channel: ' + channel.name, 2), + frequencyToken, + token.list( + events.map((event) => + token.text( + `[${event.eventName}] ${event.senderName}: ${event.eventContent}` + ) + ) + ), + ]; await Promise.all( - notifications.map((notification) => + channel.notifications.map((notification) => sendNotification(notification, 'Feed Report', eventTokens).catch((err) => logger.error('[Notification] sendFeedEventsNotify', err) )