0.11.4 : change on feed and save/hide

This commit is contained in:
2025-10-05 22:06:23 +02:00
parent f12adf603c
commit bfb630109f
4 changed files with 100 additions and 56 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "harmony-back", "name": "harmony-back",
"version": "0.11.3", "version": "0.11.4",
"private": true, "private": true,
"description": "A Strapi application", "description": "A Strapi application",
"scripts": { "scripts": {

View File

@@ -177,20 +177,15 @@ export default factories.createCoreController(
.findMany({ .findMany({
where: { where: {
$or: [ $or: [
// Posts réguliers (amis, follows, groupes, système, mes propres posts) - exclure les cachés // Posts réguliers (amis, follows, groupes, système, mes propres posts)
{ {
$and: [ $or: [
{ { author: { id: friendIds } }, // Posts de mes amis
$or: [ { author: { id: followIds } }, // Posts des contacts que je suis
{ author: { id: friendIds } }, // Posts de mes amis { author: { id: parseInt(userId) }, relation: "owner" }, // Mes propres posts
{ author: { id: followIds } }, // Posts des contacts que je suis { contextType: "group", contextId: groupIds }, // Posts de mes groupes
{ author: { id: parseInt(userId) }, relation: "owner" }, // Mes propres posts { contextType: "group", contextId: friendsGroupIds }, // Posts des groupes de mes amis
{ contextType: "group", contextId: groupIds }, // Posts de mes groupes { contextType: "system" }, // Posts système
{ contextType: "group", contextId: friendsGroupIds }, // Posts des groupes de mes amis
{ contextType: "system" }, // Posts système
],
},
{ relation: { $ne: "hidden" } }, // exclure les posts masqués pour les posts réguliers
], ],
}, },
// Posts sauvés par l'utilisateur // Posts sauvés par l'utilisateur
@@ -199,17 +194,43 @@ export default factories.createCoreController(
contextId: parseInt(userId), contextId: parseInt(userId),
relation: "saved", relation: "saved",
}, },
// Posts cachés par l'utilisateur
{
contextType: "user",
contextId: parseInt(userId),
relation: "hidden",
},
], ],
}, },
...queryParams, ...queryParams,
}); });
// 4.1️⃣ Récupérer tous les posts cachés par l'utilisateur
const hiddenPosts = await strapi.db
.query("api::post-ownership.post-ownership")
.findMany({
where: {
contextType: "user",
contextId: parseInt(userId),
relation: "hidden",
},
populate: {
post: true,
},
});
const hiddenPostIds = hiddenPosts
.map((hp) => hp.post?.id)
.filter(Boolean);
console.log("🔍 Hidden posts found:", hiddenPostIds);
console.log("📊 Feed before filtering:", feed.length);
// 4.2️⃣ Filtrer le feed pour exclure les posts cachés
const filteredFeed = feed.filter((postOwnership) => {
const postId = postOwnership.post?.id;
const isHidden = hiddenPostIds.includes(postId);
if (isHidden) {
console.log(`🚫 Filtering out hidden post ID: ${postId}`);
}
return !isHidden;
});
console.log("📊 Feed after filtering:", filteredFeed.length);
// 5⃣ Récupérer tous les groupes mentionnés dans le feed pour les populer // 5⃣ Récupérer tous les groupes mentionnés dans le feed pour les populer
const allGroupIds = [...new Set([...groupIds, ...friendsGroupIds])]; const allGroupIds = [...new Set([...groupIds, ...friendsGroupIds])];
const allGroups = await strapi.db.query("api::group.group").findMany({ const allGroups = await strapi.db.query("api::group.group").findMany({
@@ -220,7 +241,7 @@ export default factories.createCoreController(
const groupsMap = new Map(allGroups.map((group) => [group.id, group])); const groupsMap = new Map(allGroups.map((group) => [group.id, group]));
// 6⃣ Enrichir le feed avec les propriétés friend, member, contactFollow et group // 6⃣ Enrichir le feed avec les propriétés friend, member, contactFollow et group
const enrichedFeed = feed.map((postOwnership) => { const enrichedFeed = filteredFeed.map((postOwnership) => {
const authorId = postOwnership.author?.id; const authorId = postOwnership.author?.id;
const contextType = postOwnership.contextType; const contextType = postOwnership.contextType;
const contextId = postOwnership.contextId; const contextId = postOwnership.contextId;
@@ -346,16 +367,22 @@ export default factories.createCoreController(
} }
// Check if post is already saved by this user // Check if post is already saved by this user
const whereClause: any = {
post: postId,
contextType: "user",
contextId: user.id,
relation: "saved",
};
// Only add author filter if authorId is valid (not 0 for system posts)
if (authorId && authorId !== 0) {
whereClause.author = authorId;
}
const existingSavedPost = await strapi.db const existingSavedPost = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.findOne({ .findOne({
where: { where: whereClause,
post: postId,
author: authorId,
contextType: "user",
contextId: user.id,
relation: "saved",
},
}); });
if (existingSavedPost) { if (existingSavedPost) {
@@ -364,26 +391,31 @@ export default factories.createCoreController(
} }
// Create postOwnership entry to mark post as saved // Create postOwnership entry to mark post as saved
const savedPostData: any = {
post: postId,
contextType: "user",
contextId: user.id,
relation: "saved",
};
// Only add author if authorId is valid (not 0 for system posts)
if (authorId && authorId !== 0) {
savedPostData.author = authorId;
}
const savedPostOwnership = await strapi.db const savedPostOwnership = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.create({ .create({
data: { data: savedPostData,
post: postId,
author: authorId,
contextType: "user",
contextId: user.id,
relation: "saved",
},
}); });
console.log("✅ Post marked as saved:", savedPostOwnership); console.log("✅ Post marked as saved:", savedPostOwnership);
// Get count of saved posts for this user // Get count of saved posts for this user (all saved posts, regardless of author)
const savedPostsCount = await strapi.db const savedPostsCount = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.count({ .count({
where: { where: {
author: authorId,
contextType: "user", contextType: "user",
contextId: user.id, contextId: user.id,
relation: "saved", relation: "saved",
@@ -444,12 +476,11 @@ export default factories.createCoreController(
console.log("✅ Post removed from saved posts successfully"); console.log("✅ Post removed from saved posts successfully");
// Get updated count of saved posts for this user // Get updated count of saved posts for this user (all saved posts, regardless of author)
const savedPostsCount = await strapi.db const savedPostsCount = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.count({ .count({
where: { where: {
author: user.id,
contextType: "user", contextType: "user",
contextId: user.id, contextId: user.id,
relation: "saved", relation: "saved",
@@ -475,6 +506,7 @@ export default factories.createCoreController(
console.log("✅ User authenticated:", user.id); console.log("✅ User authenticated:", user.id);
const postId = parseInt(String(ctx.params.id)); const postId = parseInt(String(ctx.params.id));
const authorId = parseInt(String(ctx.query.authorId));
if (isNaN(postId)) { if (isNaN(postId)) {
return ctx.badRequest("Invalid post ID"); return ctx.badRequest("Invalid post ID");
@@ -487,16 +519,22 @@ export default factories.createCoreController(
} }
// Check if post is already hidden by this user // Check if post is already hidden by this user
const whereClauseHidden: any = {
post: postId,
contextType: "user",
contextId: user.id,
relation: "hidden",
};
// Only add author filter if authorId is valid (not 0 for system posts)
if (authorId && authorId !== 0) {
whereClauseHidden.author = authorId;
}
const existingHiddenPost = await strapi.db const existingHiddenPost = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.findOne({ .findOne({
where: { where: whereClauseHidden,
post: postId,
author: user.id,
contextType: "user",
contextId: user.id,
relation: "hidden",
},
}); });
if (existingHiddenPost) { if (existingHiddenPost) {
@@ -505,16 +543,22 @@ export default factories.createCoreController(
} }
// Create postOwnership entry to mark post as hidden // Create postOwnership entry to mark post as hidden
const hiddenPostData: any = {
post: postId,
contextType: "user",
contextId: user.id,
relation: "hidden",
};
// Only add author if authorId is valid (not 0 for system posts)
if (authorId && authorId !== 0) {
hiddenPostData.author = authorId;
}
const hiddenPostOwnership = await strapi.db const hiddenPostOwnership = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.create({ .create({
data: { data: hiddenPostData,
post: postId,
author: user.id,
contextType: "user",
contextId: user.id,
relation: "hidden",
},
}); });
console.log("✅ Post marked as hidden:", hiddenPostOwnership); console.log("✅ Post marked as hidden:", hiddenPostOwnership);
@@ -550,12 +594,12 @@ export default factories.createCoreController(
} }
// Find the hidden post ownership entry // Find the hidden post ownership entry
// Note: We search by contextId (the user who hid the post), not by author (the post's author)
const hiddenPostOwnership = await strapi.db const hiddenPostOwnership = await strapi.db
.query("api::post-ownership.post-ownership") .query("api::post-ownership.post-ownership")
.findOne({ .findOne({
where: { where: {
post: postId, post: postId,
author: user.id,
contextType: "user", contextType: "user",
contextId: user.id, contextId: user.id,
relation: "hidden", relation: "hidden",

View File

@@ -20,7 +20,7 @@ export default {
handler: "post.hidePost", handler: "post.hidePost",
}, },
{ {
method: "DELETE", method: "POST",
path: "/posts/:id/hide", path: "/posts/:id/hide",
handler: "post.removeHiddenPost", handler: "post.removeHiddenPost",
}, },

View File

@@ -14,7 +14,7 @@
"name": "Apache 2.0", "name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html" "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
}, },
"x-generation-date": "2025-10-03T20:42:58.084Z" "x-generation-date": "2025-10-05T19:46:56.298Z"
}, },
"x-strapi-config": { "x-strapi-config": {
"plugins": [ "plugins": [