109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.MessageService = void 0;
|
|
const database_js_1 = require("../config/database.js");
|
|
class MessageService {
|
|
static async sendMessage(userId, data) {
|
|
try {
|
|
const message = await database_js_1.prisma.message.create({
|
|
data: {
|
|
content: data.constent,
|
|
userId,
|
|
roomId: data.roomId,
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
avatar: true,
|
|
},
|
|
},
|
|
reactions: {
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
return {
|
|
success: true,
|
|
data: message,
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.error("Send message error:", error);
|
|
return {
|
|
success: false,
|
|
error: "Failed to send message",
|
|
};
|
|
}
|
|
}
|
|
static async reactToMessage(userId, data) {
|
|
try {
|
|
const existingReaction = await database_js_1.prisma.messageReaction.findUnique({
|
|
where: {
|
|
userId_messageId_type: {
|
|
userId,
|
|
messageId: data.messageId,
|
|
type: data.type,
|
|
},
|
|
},
|
|
});
|
|
if (existingReaction) {
|
|
await database_js_1.prisma.messageReaction.delete({
|
|
where: {
|
|
id: existingReaction.id,
|
|
},
|
|
});
|
|
}
|
|
else {
|
|
await database_js_1.prisma.messageReaction.create({
|
|
data: {
|
|
userId,
|
|
messageId: data.messageId,
|
|
type: data.type,
|
|
},
|
|
});
|
|
}
|
|
const message = await database_js_1.prisma.message.findUnique({
|
|
where: {
|
|
id: data.messageId,
|
|
},
|
|
include: {
|
|
reactions: {
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
return {
|
|
success: true,
|
|
data: {
|
|
messageId: data.messageId,
|
|
reactions: message?.reactions ?? [],
|
|
},
|
|
};
|
|
}
|
|
catch (error) {
|
|
console.error("React to message error:", error);
|
|
return {
|
|
success: false,
|
|
error: "Failed to react to message",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
exports.MessageService = MessageService;
|