websockets implemented

This commit is contained in:
Torsten Schulz
2024-12-04 19:08:26 +01:00
parent d46a51db38
commit 069c97fa90
64 changed files with 2488 additions and 562 deletions

View File

@@ -1,11 +1,4 @@
import {
getMessages as getMessagesService,
findMatch,
registerUser as registerUserService,
addMessage,
endChat,
removeUser as removeUserService
} from '../services/chatService.js';
import chatService from '../services/chatService.js';
class ChatController {
constructor() {
@@ -15,12 +8,15 @@ class ChatController {
this.sendMessage = this.sendMessage.bind(this);
this.stopChat = this.stopChat.bind(this);
this.removeUser = this.removeUser.bind(this);
this.initOneToOne = this.initOneToOne.bind(this);
this.sendOneToOneMessage = this.sendOneToOneMessage.bind(this);
this.getOneToOneMessageHistory = this.getOneToOneMessageHistory.bind(this);
}
async getMessages(req, res) {
const { to, from } = req.body;
try {
const messages = await getMessagesService(to, from);
const messages = await chatService.getMessages(to, from);
res.status(200).json(messages);
} catch (error) {
res.status(500).json({ error: error.message });
@@ -30,7 +26,7 @@ class ChatController {
async findRandomChatMatch(req, res) {
const { genders, age, id } = req.body;
try {
const match = await findMatch(genders, age, id);
const match = await chatService.findMatch(genders, age, id);
if (match) {
res.status(200).json({ status: 'matched', user: match });
} else {
@@ -44,7 +40,7 @@ class ChatController {
async registerUser(req, res) {
const { gender, age } = req.body;
try {
const userId = await registerUserService(gender, age);
const userId = await chatService.registerUser(gender, age);
res.status(200).json({ id: userId });
} catch (error) {
res.status(500).json({ error: error.message });
@@ -54,7 +50,7 @@ class ChatController {
async sendMessage(req, res) {
const { from, to, text } = req.body;
try {
const message = await addMessage(from, to, text);
const message = await chatService.addMessage(from, to, text);
res.status(200).json(message);
} catch (error) {
res.status(500).json({ error: error.message });
@@ -64,7 +60,7 @@ class ChatController {
async removeUser(req, res) {
const { id } = req.body;
try {
await removeUserService(id);
await chatService.removeUser(id);
res.sendStatus(200);
} catch (error) {
res.status(500).json({ error: error.message });
@@ -74,12 +70,43 @@ class ChatController {
async stopChat(req, res) {
const { id } = req.body;
try {
await endChat(id);
await chatService.endChat(id);
res.sendStatus(200);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async initOneToOne(req, res) {
const { userid: hashedUserId } = req.headers;
const { partnerHashId } = req.body;
try {
await chatService.initOneToOne(hashedUserId, partnerHashId);
res.status(200).json({ message: 'One-to-one chat initialization is pending implementation.' });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async sendOneToOneMessage(req, res) {
const { user1HashId, user2HashId, message } = req.body;
try {
await chatService.sendOneToOneMessage(user1HashId, user2HashId, message);
res.status(200).json({ status: 'message sent' });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async getOneToOneMessageHistory(req, res) {
const { user1HashId, user2HashId } = req.query;
try {
const history = await chatService.getOneToOneMessageHistory(user1HashId, user2HashId);
res.status(200).json({ history });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}
export default ChatController;