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

@@ -24,6 +24,7 @@ class AuthController {
const { username, password } = req.body;
try {
const result = await userService.loginUser({ username, password });
console.log('User logged in successfully', result);
res.status(200).json(result);
} catch (error) {
if (error.message === 'credentialsinvalid') {
@@ -37,6 +38,7 @@ class AuthController {
async logout(req, res) {
const { userid: hashedUserId } = req.headers;
await userService.logoutUser(hashedUserId);
res.status(200).json({ result: 'loggedout' });
}
async forgotPassword(req, res) {

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;

View File

@@ -0,0 +1,18 @@
import * as falukantService from '../services/falukantService.js';
class FalukantController {
constructor() {
this.exampleMethod = this.exampleMethod.bind(this);
}
async exampleMethod(req, res) {
try {
const result = await falukantService.exampleMethod();
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}
export default FalukantController;

View File

@@ -0,0 +1,69 @@
import friendshipService from '../services/friendshipService.js';
const friendshipController = {
async endFriendship(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { friendUserId } = req.body;
await friendshipService.endFriendship(hashedUserId, friendUserId);
res.status(200).json({ message: 'Friendship ended successfully' });
} catch (error) {
console.error('Error in endFriendship:', error);
res.status(400).json({ error: error.message });
}
},
async acceptFriendship(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { friendUserId } = req.body;
await friendshipService.acceptFriendship(hashedUserId, friendUserId);
res.status(200).json({ message: 'Friendship accepted successfully' });
} catch (error) {
console.error('Error in acceptFriendship:', error);
res.status(400).json({ error: error.message });
}
},
async rejectFriendship(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { friendUserId } = req.body;
await friendshipService.rejectFriendship(hashedUserId, friendUserId);
res.status(200).json({ message: 'Friendship rejected successfully' });
} catch (error) {
console.error('Error in rejectFriendship:', error);
res.status(400).json({ error: error.message });
}
},
async withdrawRequest(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { friendUserId } = req.body;
await friendshipService.withdrawRequest(hashedUserId, friendUserId);
res.status(200).json({ message: 'Friendship request withdrawn successfully' });
} catch (error) {
console.error('Error in withdrawRequest:', error);
res.status(400).json({ error: error.message });
}
},
async getFriendships(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { acceptedOnly } = req.query;
console.log('Friendships:', acceptedOnly);
const friendships = await friendshipService.getFriendships(hashedUserId, acceptedOnly === 'true');
res.status(200).json(friendships);
} catch (error) {
console.error('Error in getFriendships:', error);
res.status(400).json({ error: error.message });
}
},
};
export default friendshipController;

View File

@@ -16,7 +16,7 @@ const menuStructure = {
children: {
manageFriends: {
visible: ["all"],
path: "/socialnetwork/friends",
path: "/friends",
icon: "friends24.png"
}
},

View File

@@ -26,12 +26,14 @@ class SocialNetworkController {
this.addFriend = this.addFriend.bind(this);
this.removeFriend = this.removeFriend.bind(this);
this.acceptFriendship = this.acceptFriendship.bind(this);
this.getLoggedInFriends = this.getLoggedInFriends.bind(this);
}
async userSearch(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { username, ageFrom, ageTo, genders } = req.body;
const users = await this.socialNetworkService.searchUsers({ username, ageFrom, ageTo, genders });
const users = await this.socialNetworkService.searchUsers({ hashedUserId, username, ageFrom, ageTo, genders });
res.status(200).json(users);
} catch (error) {
console.error('Error in userSearch:', error);
@@ -295,6 +297,7 @@ class SocialNetworkController {
try {
const { userid: hashedUserid } = req.headers;
const { friendUserid } = req.body;
console.log('--------', friendUserid, hashedUserid);
await this.socialNetworkService.addFriend(hashedUserid, friendUserid);
res.status(201).json({ message: 'added' });
} catch (error) {
@@ -326,6 +329,20 @@ class SocialNetworkController {
res.status(500).json({ error: error.message });
}
}
async getLoggedInFriends(req, res) {
try {
const { userid: userId } = req.headers;
if (!userId) {
return res.status(400).json({ error: 'Missing user ID' });
}
const loggedInFriends = await this.socialNetworkService.getLoggedInFriends(userId);
res.status(200).json(loggedInFriends);
} catch (error) {
console.error('Error in getLoggedInFriends:', error);
res.status(500).json({ error: error.message });
}
}
}
export default SocialNetworkController;