AJout du likePost et des taggedContacts
This commit is contained in:
@@ -19,12 +19,7 @@
|
||||
"type": "media",
|
||||
"multiple": true,
|
||||
"required": false,
|
||||
"allowedTypes": [
|
||||
"images",
|
||||
"files",
|
||||
"videos",
|
||||
"audios"
|
||||
]
|
||||
"allowedTypes": ["images", "files", "videos", "audios"]
|
||||
},
|
||||
"likes": {
|
||||
"type": "relation",
|
||||
@@ -33,11 +28,7 @@
|
||||
},
|
||||
"category": {
|
||||
"type": "enumeration",
|
||||
"enum": [
|
||||
"photo",
|
||||
"video",
|
||||
"event"
|
||||
]
|
||||
"enum": ["photo", "video", "event"]
|
||||
},
|
||||
"comments": {
|
||||
"type": "relation",
|
||||
@@ -64,6 +55,11 @@
|
||||
"relation": "oneToMany",
|
||||
"target": "api::post-ownership.post-ownership",
|
||||
"mappedBy": "post"
|
||||
},
|
||||
"taggedContacts": {
|
||||
"type": "relation",
|
||||
"relation": "oneToMany",
|
||||
"target": "plugin::users-permissions.user"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,13 @@ export default factories.createCoreController(
|
||||
});
|
||||
data.media = asset[0].id;
|
||||
}
|
||||
|
||||
// Transformer taggedContacts en format de relation Strapi
|
||||
if (data.taggedContacts && Array.isArray(data.taggedContacts)) {
|
||||
data.taggedContacts = data.taggedContacts.map((id) => ({ id }));
|
||||
}
|
||||
|
||||
delete data.author;
|
||||
ctx.request.body = { data };
|
||||
const result = await super.create(ctx);
|
||||
|
||||
@@ -38,8 +45,8 @@ export default factories.createCoreController(
|
||||
data: {
|
||||
post: result.data.id,
|
||||
author: userId,
|
||||
contextType: data.contextType || "user",
|
||||
contextId: data.contextId || null,
|
||||
contextType: "user",
|
||||
contextId: userId,
|
||||
relation: "owner",
|
||||
},
|
||||
});
|
||||
@@ -82,6 +89,11 @@ export default factories.createCoreController(
|
||||
},
|
||||
},
|
||||
media: true,
|
||||
taggedContacts: {
|
||||
populate: {
|
||||
avatar: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
author: {
|
||||
@@ -165,13 +177,14 @@ export default factories.createCoreController(
|
||||
.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
// Posts réguliers (amis, follows, groupes, système) - exclure les cachés
|
||||
// Posts réguliers (amis, follows, groupes, système, mes propres posts) - exclure les cachés
|
||||
{
|
||||
$and: [
|
||||
{
|
||||
$or: [
|
||||
{ author: { id: friendIds } }, // Posts de mes amis
|
||||
{ author: { id: followIds } }, // Posts des contacts que je suis
|
||||
{ author: { id: parseInt(userId) }, relation: "owner" }, // Mes propres posts
|
||||
{ contextType: "group", contextId: groupIds }, // Posts de mes groupes
|
||||
{ contextType: "group", contextId: friendsGroupIds }, // Posts des groupes de mes amis
|
||||
{ contextType: "system" }, // Posts système
|
||||
@@ -245,9 +258,68 @@ export default factories.createCoreController(
|
||||
|
||||
const filteredFeedBlocked = enrichedFeed.filter((post) => !post.blocked);
|
||||
|
||||
ctx.send(filteredFeedBlocked);
|
||||
// Trier par createdAt du post (le plus récent en premier)
|
||||
const sortedFeed = filteredFeedBlocked.sort((a, b) => {
|
||||
const dateA = a.post?.createdAt
|
||||
? new Date(a.post.createdAt).getTime()
|
||||
: 0;
|
||||
const dateB = b.post?.createdAt
|
||||
? new Date(b.post.createdAt).getTime()
|
||||
: 0;
|
||||
return dateB - dateA; // Ordre décroissant (plus récent en premier)
|
||||
});
|
||||
|
||||
ctx.send(sortedFeed);
|
||||
},
|
||||
|
||||
async likePost(ctx) {
|
||||
const user = ctx.state.user; // Utilisateur actuellement connecté
|
||||
if (!user) {
|
||||
return ctx.unauthorized("You must be logged in to like a post.");
|
||||
}
|
||||
const postId = parseInt(String(ctx.params.id));
|
||||
|
||||
if (isNaN(postId)) {
|
||||
return ctx.badRequest("Invalid post ID");
|
||||
}
|
||||
// Verify the post exists
|
||||
const existingPost = await strapi.db.query("api::post.post").findOne({
|
||||
where: {
|
||||
id: postId,
|
||||
},
|
||||
populate: {
|
||||
likes: true,
|
||||
},
|
||||
});
|
||||
if (!existingPost) {
|
||||
return ctx.notFound("Post not found");
|
||||
}
|
||||
// Check if the user has already liked the post
|
||||
const existingLike = existingPost.likes.find(
|
||||
(like) => like.id === user.id
|
||||
);
|
||||
if (existingLike) {
|
||||
await strapi.db.query("api::post.post").update({
|
||||
where: { id: postId },
|
||||
data: {
|
||||
likes: {
|
||||
disconnect: [{ id: user.id }],
|
||||
},
|
||||
},
|
||||
});
|
||||
return ctx.send({ message: "Post unliked successfully" });
|
||||
}
|
||||
// Update the postLikes field on the post entity
|
||||
await strapi.db.query("api::post.post").update({
|
||||
where: { id: postId },
|
||||
data: {
|
||||
likes: {
|
||||
connect: [{ id: user.id }],
|
||||
},
|
||||
},
|
||||
});
|
||||
ctx.send({ message: "Post liked successfully" });
|
||||
},
|
||||
async savePost(ctx) {
|
||||
console.log("🎯 savePost called with params:", ctx.params);
|
||||
console.log("🎯 Request method:", ctx.request.method);
|
||||
|
||||
@@ -29,5 +29,10 @@ export default {
|
||||
path: "/posts/feed",
|
||||
handler: "post.feed",
|
||||
},
|
||||
{
|
||||
method: "PUT",
|
||||
path: "/posts/:id/like",
|
||||
handler: "post.likePost",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user