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 { 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,8 +54,8 @@ 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: {
await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: eventType,
eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit **${commits}** to **${ref}** in [${fullName}](${repoUrl})`,
@ -66,13 +65,7 @@ export const feedIntegrationRouter = router({
senderName,
important: false,
url,
},
});
subscribeEventBus.emit(
'onReceiveFeedEvent',
workspaceId,
serializeJSON(event)
);
return 'ok';
} else if (eventType === 'star') {
@ -83,8 +76,8 @@ 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: {
await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: eventType,
eventContent: `[${senderName}](${senderUrl}) star repo [${fullName}](${repoUrl}), now is ${starCount}.`,
@ -94,13 +87,7 @@ export const feedIntegrationRouter = router({
senderName,
important: false,
url,
},
});
subscribeEventBus.emit(
'onReceiveFeedEvent',
workspaceId,
serializeJSON(event)
);
return 'ok';
} else if (eventType === 'issues') {
@ -124,8 +111,7 @@ export const feedIntegrationRouter = router({
}
if (eventContent) {
const event = await prisma.feedEvent.create({
data: {
await createFeedEvent(workspaceId, {
channelId: channelId,
eventName: eventName,
eventContent: `${senderName} star repo [${fullName}], now is ${starCount}.`,
@ -135,13 +121,7 @@ export const feedIntegrationRouter = router({
senderName,
important: false,
url,
},
});
subscribeEventBus.emit(
'onReceiveFeedEvent',
workspaceId,
serializeJSON(event)
);
return 'ok';
}