Friendship management added
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user