diff --git a/src/server/model/feed/event.ts b/src/server/model/feed/event.ts new file mode 100644 index 0000000..9266cca --- /dev/null +++ b/src/server/model/feed/event.ts @@ -0,0 +1,18 @@ +import { Prisma } from '@prisma/client'; +import { subscribeEventBus } from '../../ws/shared'; +import { prisma } from '../_client'; +import { serializeJSON } from '../../utils/json'; + +export async function createFeedEvent( + workspaceId: string, + eventData: Prisma.FeedEventCreateArgs['data'] +) { + const event = await prisma.feedEvent.create({ + data: eventData, + }); + subscribeEventBus.emit( + 'onReceiveFeedEvent', + workspaceId, + serializeJSON(event) + ); +} diff --git a/src/server/trpc/routers/feed/integration.ts b/src/server/trpc/routers/feed/integration.ts index e3671d0..e64e4f9 100644 --- a/src/server/trpc/routers/feed/integration.ts +++ b/src/server/trpc/routers/feed/integration.ts @@ -2,10 +2,9 @@ import { z } from 'zod'; import { OpenApiMetaInfo, publicProcedure, router } from '../../trpc'; import { prisma } from '../../../model/_client'; import _ from 'lodash'; -import { subscribeEventBus } from '../../../ws/shared'; import { OpenApiMeta } from 'trpc-openapi'; import { OPENAPI_TAG } from '../../../utils/const'; -import { serializeJSON } from '../../../utils/json'; +import { createFeedEvent } from '../../../model/feed/event'; export const feedIntegrationRouter = router({ github: publicProcedure @@ -55,24 +54,18 @@ export const feedIntegrationRouter = router({ const senderId = String(_.get(data, 'sender.id')); const senderName = String(_.get(data, 'sender.login')); const url = String(_.get(data, 'compare')); - const event = await prisma.feedEvent.create({ - data: { - channelId: channelId, - eventName: eventType, - eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit **${commits}** to **${ref}** in [${fullName}](${repoUrl})`, - tags: [], - source: 'github', - senderId, - senderName, - important: false, - url, - }, + + await createFeedEvent(workspaceId, { + channelId: channelId, + eventName: eventType, + eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit **${commits}** to **${ref}** in [${fullName}](${repoUrl})`, + tags: [], + source: 'github', + senderId, + senderName, + important: false, + url, }); - subscribeEventBus.emit( - 'onReceiveFeedEvent', - workspaceId, - serializeJSON(event) - ); return 'ok'; } else if (eventType === 'star') { @@ -83,24 +76,18 @@ export const feedIntegrationRouter = router({ const senderName = String(_.get(data, 'sender.login')); const senderUrl = String(_.get(data, 'sender.html_url')); const url = String(_.get(data, 'compare')); - const event = await prisma.feedEvent.create({ - data: { - channelId: channelId, - eventName: eventType, - eventContent: `[${senderName}](${senderUrl}) star repo [${fullName}](${repoUrl}), now is ${starCount}.`, - tags: [], - source: 'github', - senderId, - senderName, - important: false, - url, - }, + + await createFeedEvent(workspaceId, { + channelId: channelId, + eventName: eventType, + eventContent: `[${senderName}](${senderUrl}) star repo [${fullName}](${repoUrl}), now is ${starCount}.`, + tags: [], + source: 'github', + senderId, + senderName, + important: false, + url, }); - subscribeEventBus.emit( - 'onReceiveFeedEvent', - workspaceId, - serializeJSON(event) - ); return 'ok'; } else if (eventType === 'issues') { @@ -124,24 +111,17 @@ export const feedIntegrationRouter = router({ } if (eventContent) { - const event = await prisma.feedEvent.create({ - data: { - channelId: channelId, - eventName: eventName, - eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`, - tags: [], - source: 'github', - senderId, - senderName, - important: false, - url, - }, + await createFeedEvent(workspaceId, { + channelId: channelId, + eventName: eventName, + eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`, + tags: [], + source: 'github', + senderId, + senderName, + important: false, + url, }); - subscribeEventBus.emit( - 'onReceiveFeedEvent', - workspaceId, - serializeJSON(event) - ); return 'ok'; }