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,15 +24,22 @@ import DOMPurify from 'dompurify';
import sharp from 'sharp';
import Diary from '../models/community/diary.js';
import Friendship from '../models/community/friendship.js';
import { getUserSession } from '../utils/redis.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class SocialNetworkService extends BaseService {
async searchUsers({ username, ageFrom, ageTo, genders }) {
async searchUsers({ hashedUserId, username, ageFrom, ageTo, genders }) {
const whereClause = this.buildSearchWhereClause(username);
const user = await this.loadUserByHash(hashedUserId);
console.log(hashedUserId, user);
if (!user) {
throw new Error('User not found');
}
whereClause.id = { [Op.ne]: user.id };
const users = await User.findAll({ where: whereClause, include: this.getUserParamsInclude() });
return this.filterUsersByCriteria(users, ageFrom, ageTo, genders);
return await this.filterUsersByCriteria(users, ageFrom, ageTo, genders);
}
async getProfile(hashedUserId, requestingUserId) {
@@ -48,16 +55,18 @@ class SocialNetworkService extends BaseService {
if (!user) {
throw new Error('User not found');
}
console.log('given data', data, folderId);
const parentFolder = data.parentId ? await Folder.findOne({
where: { id: data.parentId, userId: user.id }
}) : null;
if (data.parentId && !parentFolder) {
throw new Error('Parent folder not found');
}
console.log('parentFolder', parentFolder);
let newFolder;
if (folderId === 0) {
if (parentFolder) {
newFolder = await Folder.create({
parentId: data.parentId || null,
parentId: parentFolder.id || null,
userId: user.id,
name: data.name
});
@@ -267,7 +276,8 @@ class SocialNetworkService extends BaseService {
}
async loadUserByHash(hashedId) {
return await User.findOne({ hashedId });
console.log('Loading user by hashedId:', hashedId);
return await User.findOne({ where: { hashedId: hashedId } });
}
async loadUserByName(userName) {
@@ -300,10 +310,10 @@ class SocialNetworkService extends BaseService {
];
}
filterUsersByCriteria(users, ageFrom, ageTo, genders) {
async filterUsersByCriteria(users, ageFrom, ageTo, genders) {
const results = [];
for (const user of users) {
const userDetails = this.extractUserDetails(user);
const userDetails = await this.extractUserDetails(user);
if (this.isUserValid(userDetails, ageFrom, ageTo, genders)) {
results.push(userDetails);
}
@@ -311,11 +321,11 @@ class SocialNetworkService extends BaseService {
return results;
}
extractUserDetails(user) {
async extractUserDetails(user) {
const birthdateParam = user.user_params.find(param => param.paramType.description === 'birthdate');
const genderParam = user.user_params.find(param => param.paramType.description === 'gender');
const age = birthdateParam ? this.calculateAge(birthdateParam.value) : null;
const gender = genderParam ? this.getGenderValue(genderParam.value) : null;
const gender = genderParam ? await this.getGenderValue(genderParam.value) : null;
return {
id: user.hashedId,
username: user.username,
@@ -735,8 +745,9 @@ class SocialNetworkService extends BaseService {
}
async addFriend(hashedUserid, friendUserid) {
console.log('--------', friendUserid, hashedUserid);
const requestingUserId = await this.checkUserAccess(hashedUserid);
const friend = await this.loadUserByHash(friendUserid);
const friend = await User.findOne({ where: { hashedId: friendUserid } });
if (!friend) {
throw new Error('notfound');
}
@@ -748,10 +759,10 @@ class SocialNetworkService extends BaseService {
]
}
});
console.log('friendship', friend, requestingUserId);
if (friendship) {
if (friendship.withdrawn) {
friendship.withdrawn = false;
if (friendship.withdrawn && friendship.user1Id === requestingUserId) {
friendship.update({ withdrawn: false });
} else {
throw new Error('alreadyexists');
}
@@ -811,5 +822,37 @@ class SocialNetworkService extends BaseService {
throw new Error('notfound');
}
}
async getLoggedInFriends(hashedUserId) {
const userId = await this.checkUserAccess(hashedUserId);
const activeFriendships = await Friendship.findAll({
where: {
accepted: true,
denied: false,
withdrawn: false,
[Op.or]: [
{ user1Id: userId },
{ user2Id: userId }
]
}
});
const friendIds = activeFriendships.map(friendship =>
friendship.user1Id === userId ? friendship.user2Id : friendship.user1Id
);
const loggedInFriends = [];
for (const friendId of friendIds) {
const session = await getUserSession(friendId);
if (session && session.id) {
const friend = await User.findOne({ where: { hashedId: session.id } });
if (friend) {
loggedInFriends.push({
id: friend.hashedId,
username: friend.username,
});
}
}
}
return loggedInFriends;
}
}
export default SocialNetworkService;