feat: message.ts service
This commit is contained in:
@@ -82,10 +82,10 @@ model Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model Reply {
|
model Reply {
|
||||||
message Message @relation("MessageToReply", fields: [messageId], references: [id])
|
message Message @relation("MessageToReply", fields: [messageId], references: [id]) //message text
|
||||||
messageId String @unique
|
messageId String @unique //message id of the reply
|
||||||
repliesTo Message @relation("ReplyToMessage", fields: [repliesToId], references: [id])
|
repliesTo Message @relation("ReplyToMessage", fields: [repliesToId], references: [id]) //message id that this message replies to
|
||||||
repliesToId String @unique
|
repliesToId String @unique //replies to this message id
|
||||||
|
|
||||||
@@unique([messageId, repliesToId])
|
@@unique([messageId, repliesToId])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { sendMessageToChannel, removeMessageFromChannel } from "../services/real
|
|||||||
import { success } from "zod";
|
import { success } from "zod";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function postMessageToChannel(
|
export async function postMessageToChannel(
|
||||||
io: any,
|
io: any,
|
||||||
c: Context
|
c: Context
|
||||||
|
|||||||
28
concord-server/src/routes/realtime.ts
Normal file
28
concord-server/src/routes/realtime.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { Hono } from "hono";
|
||||||
|
import { zValidator } from "@hono/zod-validator";
|
||||||
|
import { describeRoute, resolver } from "hono-openapi";
|
||||||
|
import { postMessageToChannel,
|
||||||
|
deleteMessageFromChannel
|
||||||
|
} from "../controller/realtime";
|
||||||
|
|
||||||
|
const app = new Hono();
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
"message/",
|
||||||
|
zValidator({
|
||||||
|
body: z.object({
|
||||||
|
content: z.string().min(1).max(500)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
async (c) => {
|
||||||
|
const { instanceId, categoryId, channelId } = c.req.params;
|
||||||
|
const { content } = c.req.body;
|
||||||
|
|
||||||
|
return postMessageToChannel(c.get("io"), {
|
||||||
|
instanceId,
|
||||||
|
categoryId,
|
||||||
|
channelId,
|
||||||
|
content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
77
concord-server/src/services/message.ts
Normal file
77
concord-server/src/services/message.ts
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import {
|
||||||
|
Message,
|
||||||
|
MessagePing,
|
||||||
|
PrismaClient,
|
||||||
|
Role,
|
||||||
|
Reply
|
||||||
|
|
||||||
|
} from "@prisma/client";
|
||||||
|
import { CreateUserInput } from '../validators/userValidator';
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
class MessageService {
|
||||||
|
public async function sendMessageToChannel(
|
||||||
|
channelId: string,
|
||||||
|
userId: string,
|
||||||
|
content: string,
|
||||||
|
repliedMessageId: string | null
|
||||||
|
): Promise<{
|
||||||
|
id: string,
|
||||||
|
channelId: string,
|
||||||
|
userId: string,
|
||||||
|
text: string,
|
||||||
|
deleted: boolean,
|
||||||
|
replies: null | {
|
||||||
|
messageId: string,
|
||||||
|
repliesToId: string,
|
||||||
|
repliesToText: string
|
||||||
|
}
|
||||||
|
} | null> {
|
||||||
|
try {
|
||||||
|
|
||||||
|
|
||||||
|
const newMessage = await prisma.message.create({
|
||||||
|
data: {
|
||||||
|
channelId: channelId,
|
||||||
|
userId: userId,
|
||||||
|
text: content,
|
||||||
|
deleted: false,
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let origMessage;
|
||||||
|
if(repliedMessageId){
|
||||||
|
origMessage = await prisma.message.findUnique({
|
||||||
|
where: {
|
||||||
|
id: repliedMessageId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if(!origMessage){
|
||||||
|
throw new Error("could not find original message to reply to");
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.reply.create({
|
||||||
|
data: {
|
||||||
|
messageId: newMessage.id,
|
||||||
|
repliesToId: origMessage.id
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...newMessage,
|
||||||
|
replies: repliedMessageId ? {
|
||||||
|
messageId: newMessage.id,
|
||||||
|
repliesToId: origMessage?.id,
|
||||||
|
repliesToText: origMessage?.text
|
||||||
|
} : null
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
0
concord-server/src/validators/messageValidator.ts
Normal file
0
concord-server/src/validators/messageValidator.ts
Normal file
Reference in New Issue
Block a user