Friendship management added

This commit is contained in:
Torsten Schulz
2024-10-27 13:14:05 +01:00
parent f74a16e58e
commit 7f8709516d
13 changed files with 406 additions and 31 deletions

View File

@@ -23,6 +23,7 @@ import { JSDOM } from 'jsdom';
import DOMPurify from 'dompurify';
import sharp from 'sharp';
import Diary from '../models/community/diary.js';
import Friendship from '../models/community/friendship.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -347,7 +348,15 @@ class SocialNetworkService extends BaseService {
{ model: UserParamType, as: 'paramType' },
{ model: UserParamVisibility, as: 'param_visibilities', include: [{ model: UserParamVisibilityType, as: 'visibility_type' }] }
],
order: [['order_id', 'asc']]
order: [['order_id', 'asc']],
},
{
model: Friendship,
as: 'friendSender',
},
{
model: Friendship,
as: 'friendReceiver',
}
]
});
@@ -370,9 +379,26 @@ class SocialNetworkService extends BaseService {
};
}
}
let friendship = null;
if (user.friendSender && user.friendSender.length > 0) {
friendship = {
isSender: true,
accepted: user.friendSender[0].dataValues.accepted,
denied: user.friendSender[0].dataValues.denied,
withdrawn: user.friendSender[0].dataValues.withdrawn,
}
} else if (user.friendReceiver && user.friendReceiver.length > 0) {
friendship = {
isSender: false,
accepted: user.friendReceiver[0].dataValues.accepted,
denied: user.friendReceiver[0].dataValues.denied,
withdrawn: user.friendReceiver[0].dataValues.withdrawn,
}
}
return {
username: user.username,
registrationDate: user.registrationDate,
friendship: friendship,
params: userParams
};
}
@@ -707,5 +733,83 @@ class SocialNetworkService extends BaseService {
});
return { entries: entries.rows, totalPages: Math.ceil(entries.count / 20) };
}
async addFriend(hashedUserid, friendUserid) {
const requestingUserId = await this.checkUserAccess(hashedUserid);
const friend = await this.loadUserByHash(friendUserid);
if (!friend) {
throw new Error('notfound');
}
const friendship = await Friendship.findOne({
where: {
[Op.or]: [
{ user1Id: requestingUserId, user2Id: friend.id },
{ user1Id: friend.id, user2Id: requestingUserId }
]
}
});
if (friendship) {
if (friendship.withdrawn) {
friendship.withdrawn = false;
} else {
throw new Error('alreadyexists');
}
} else {
await Friendship.create({ user1Id: requestingUserId, user2Id: friend.id });
}
return { accepted: false, withdrawn: false, denied: false };
}
async removeFriend(hashedUserid, friendUserid) {
const requestingUserId = await this.checkUserAccess(hashedUserid);
const friend = await this.loadUserByHash(friendUserid);
if (!friend) {
throw new Error('notfound');
}
const friendship = await Friendship.findOne({
where: {
[Op.or]: [
{ user1Id: requestingUserId, user2Id: friend.id },
{ user1Id: friend.id, user2Id: requestingUserId }
]
}
});
if (!friendship) {
throw new Error('notfound');
}
if (friendship.user1Id === requestingUserId) {
friendship.update({ withdrawn: true })
} else {
friendship.update({ denied: true });
}
return true;
}
async acceptFriendship(hashedUserid, friendUserid) {
const requestingUserId = await this.checkUserAccess(hashedUserid);
const friend = await this.loadUserByHash(friendUserid);
if (!friend) {
throw new Error('notfound');
}
const friendship = await Friendship.findOne({
where: {
[Op.or]: [
{ user1Id: requestingUserId, user2Id: friend.id },
{ user1Id: friend.id, user2Id: requestingUserId }
]
}
});
if (!friendship) {
throw new Error('notfound');
}
if (friendship.user1Id === requestingUserId && friendship.withdrawn) {
friendship.update({ withdrawn: false });
} else if (friendship.user2Id === requestingUserId && friendship.denied) {
friendship.update({ denied: false, accepted: true });
} else {
throw new Error('notfound');
}
}
}
export default SocialNetworkService;