Fix relationships

This commit is contained in:
Rory& 2025-10-18 14:00:22 +02:00
parent 72ac6c4c3e
commit 1b21988617

View File

@ -56,16 +56,7 @@ router.get(
select: ["id", "relationships"], select: ["id", "relationships"],
}); });
//TODO DTO const related_users = user.relationships.map((r) => r.toPublicRelationship());
const related_users = user.relationships.map((r) => {
return {
id: r.to.id,
type: r.type,
nickname: null,
user: r.to.toPublicUser(),
};
});
return res.json(related_users); return res.json(related_users);
}, },
); );
@ -146,8 +137,8 @@ router.delete(
}, },
}), }),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { id } = req.params; const { user_id } = req.params;
if (id === req.user_id) if (user_id === req.user_id)
throw new HTTPError("You can't remove yourself as a friend"); throw new HTTPError("You can't remove yourself as a friend");
const user = await User.findOneOrFail({ const user = await User.findOneOrFail({
@ -156,21 +147,21 @@ router.delete(
relations: ["relationships"], relations: ["relationships"],
}); });
const friend = await User.findOneOrFail({ const friend = await User.findOneOrFail({
where: { id: id }, where: { id: user_id },
select: userProjection, select: userProjection,
relations: ["relationships"], relations: ["relationships"],
}); });
const relationship = user.relationships.find((x) => x.to_id === id); const relationship = user.relationships.find((x) => x.to_id === user_id);
const friendRequest = friend.relationships.find( const friendRequest = friend.relationships.find(
(x) => x.to_id === req.user_id, (x) => x.to_id === req.user_id,
); );
if (!relationship) if (!relationship)
throw new HTTPError("You are not friends with the user", 404); throw new HTTPError("You are not friends with the user", 404);
if (relationship?.type === RelationshipType.blocked) { if (relationship?.type === RelationshipType.blocked) {
// unblock user // unblock user
await Promise.all([ await Promise.all([
Relationship.delete({ id: relationship.id }), Relationship.delete({ id: relationship.id }),
emitEvent({ emitEvent({
@ -187,7 +178,7 @@ router.delete(
await emitEvent({ await emitEvent({
event: "RELATIONSHIP_REMOVE", event: "RELATIONSHIP_REMOVE",
data: friendRequest.toPublicRelationship(), data: friendRequest.toPublicRelationship(),
user_id: id, user_id: user_id,
} as RelationshipRemoveEvent), } as RelationshipRemoveEvent),
]); ]);
} }