refactor: change create feed event to local

This commit is contained in:
moonrailgun 2024-07-16 00:53:54 +08:00
parent b6bca6c250
commit ab179e9af6
2 changed files with 51 additions and 53 deletions

View File

@ -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)
);
}

View File

@ -2,10 +2,9 @@ import { z } from 'zod';
import { OpenApiMetaInfo, publicProcedure, router } from '../../trpc'; import { OpenApiMetaInfo, publicProcedure, router } from '../../trpc';
import { prisma } from '../../../model/_client'; import { prisma } from '../../../model/_client';
import _ from 'lodash'; import _ from 'lodash';
import { subscribeEventBus } from '../../../ws/shared';
import { OpenApiMeta } from 'trpc-openapi'; import { OpenApiMeta } from 'trpc-openapi';
import { OPENAPI_TAG } from '../../../utils/const'; import { OPENAPI_TAG } from '../../../utils/const';
import { serializeJSON } from '../../../utils/json'; import { createFeedEvent } from '../../../model/feed/event';
export const feedIntegrationRouter = router({ export const feedIntegrationRouter = router({
github: publicProcedure github: publicProcedure
@ -55,8 +54,8 @@ export const feedIntegrationRouter = router({
const senderId = String(_.get(data, 'sender.id')); const senderId = String(_.get(data, 'sender.id'));
const senderName = String(_.get(data, 'sender.login')); const senderName = String(_.get(data, 'sender.login'));
const url = String(_.get(data, 'compare')); const url = String(_.get(data, 'compare'));
const event = await prisma.feedEvent.create({
data: { await createFeedEvent(workspaceId, {
channelId: channelId, channelId: channelId,
eventName: eventType, eventName: eventType,
eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit **${commits}** to **${ref}** in [${fullName}](${repoUrl})`, eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit **${commits}** to **${ref}** in [${fullName}](${repoUrl})`,
@ -66,13 +65,7 @@ export const feedIntegrationRouter = router({
senderName, senderName,
important: false, important: false,
url, url,
},
}); });
subscribeEventBus.emit(
'onReceiveFeedEvent',
workspaceId,
serializeJSON(event)
);
return 'ok'; return 'ok';
} else if (eventType === 'star') { } else if (eventType === 'star') {
@ -83,8 +76,8 @@ export const feedIntegrationRouter = router({
const senderName = String(_.get(data, 'sender.login')); const senderName = String(_.get(data, 'sender.login'));
const senderUrl = String(_.get(data, 'sender.html_url')); const senderUrl = String(_.get(data, 'sender.html_url'));
const url = String(_.get(data, 'compare')); const url = String(_.get(data, 'compare'));
const event = await prisma.feedEvent.create({
data: { await createFeedEvent(workspaceId, {
channelId: channelId, channelId: channelId,
eventName: eventType, eventName: eventType,
eventContent: `[${senderName}](${senderUrl}) star repo [${fullName}](${repoUrl}), now is ${starCount}.`, eventContent: `[${senderName}](${senderUrl}) star repo [${fullName}](${repoUrl}), now is ${starCount}.`,
@ -94,13 +87,7 @@ export const feedIntegrationRouter = router({
senderName, senderName,
important: false, important: false,
url, url,
},
}); });
subscribeEventBus.emit(
'onReceiveFeedEvent',
workspaceId,
serializeJSON(event)
);
return 'ok'; return 'ok';
} else if (eventType === 'issues') { } else if (eventType === 'issues') {
@ -124,8 +111,7 @@ export const feedIntegrationRouter = router({
} }
if (eventContent) { if (eventContent) {
const event = await prisma.feedEvent.create({ await createFeedEvent(workspaceId, {
data: {
channelId: channelId, channelId: channelId,
eventName: eventName, eventName: eventName,
eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`, eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`,
@ -135,13 +121,7 @@ export const feedIntegrationRouter = router({
senderName, senderName,
important: false, important: false,
url, url,
},
}); });
subscribeEventBus.emit(
'onReceiveFeedEvent',
workspaceId,
serializeJSON(event)
);
return 'ok'; return 'ok';
} }