@@ -177,9 +177,7 @@ export default factories.createCoreController(
. findMany ( {
where : {
$or : [
// Posts réguliers (amis, follows, groupes, système, mes propres posts) - exclure les cachés
{
$and : [
// Posts réguliers (amis, follows, groupes, système, mes propres posts)
{
$or : [
{ author : { id : friendIds } } , // Posts de mes amis
@@ -190,26 +188,49 @@ export default factories.createCoreController(
{ contextType : "system" } , // Posts système
] ,
} ,
{ relation : { $ne : "hidden" } } , // exclure les posts masqués pour les posts réguliers
] ,
} ,
// Posts sauvés par l'utilisateur
{
contextType : "user" ,
contextId : parseInt ( userId ) ,
relation : "saved" ,
} ,
// Posts cachés par l'utilisateur
{
contextType : "user" ,
contextId : parseInt ( userId ) ,
relation : "hidden" ,
} ,
] ,
} ,
. . . 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
const allGroupIds = [ . . . new Set ( [ . . . groupIds , . . . friendsGroupIds ] ) ] ;
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 ] ) ) ;
// 6️ ⃣ Enrichir le feed avec les propriétés friend, member, contactFollow et group
const enrichedFeed = feed . map ( ( postOwnership ) = > {
const enrichedFeed = filteredF eed . map ( ( postOwnership ) = > {
const authorId = postOwnership . author ? . id ;
const contextType = postOwnership . contextType ;
const contextId = postOwnership . contextId ;
@@ -346,16 +367,22 @@ export default factories.createCoreController(
}
// Check if post is already saved by this user
const existingSavedPost = await strapi . db
. query ( "api::post-ownership.post-ownership" )
. findOne ( {
where : {
const whereClause : any = {
post : postId ,
author : authorId ,
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
. query ( "api::post-ownership.post-ownership" )
. findOne ( {
where : whereClause ,
} ) ;
if ( existingSavedPost ) {
@@ -364,26 +391,31 @@ export default factories.createCoreController(
}
// Create postOwnership entry to mark post as saved
const savedPostOwnership = await strapi . db
. query ( "api::post-ownership.post-ownership" )
. create ( {
data : {
const savedPostData : any = {
post : postId ,
author : authorId ,
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
. query ( "api::post-ownership.post-ownership" )
. create ( {
data : savedPostData ,
} ) ;
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
. query ( "api::post-ownership.post-ownership" )
. count ( {
where : {
author : authorId ,
contextType : "user" ,
contextId : user.id ,
relation : "saved" ,
@@ -444,12 +476,11 @@ export default factories.createCoreController(
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
. query ( "api::post-ownership.post-ownership" )
. count ( {
where : {
author : user.id ,
contextType : "user" ,
contextId : user.id ,
relation : "saved" ,
@@ -475,6 +506,7 @@ export default factories.createCoreController(
console . log ( "✅ User authenticated:" , user . id ) ;
const postId = parseInt ( String ( ctx . params . id ) ) ;
const authorId = parseInt ( String ( ctx . query . authorId ) ) ;
if ( isNaN ( postId ) ) {
return ctx . badRequest ( "Invalid post ID" ) ;
@@ -487,16 +519,22 @@ export default factories.createCoreController(
}
// Check if post is already hidden by this user
const existingHiddenPost = await strapi . db
. query ( "api::post-ownership.post-ownership" )
. findOne ( {
where : {
const whereClauseHidden : any = {
post : postId ,
author : user.id ,
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
. query ( "api::post-ownership.post-ownership" )
. findOne ( {
where : whereClauseHidden ,
} ) ;
if ( existingHiddenPost ) {
@@ -505,16 +543,22 @@ export default factories.createCoreController(
}
// Create postOwnership entry to mark post as hidden
const hiddenPostOwnership = await strapi . db
. query ( "api::post-ownership.post-ownership" )
. create ( {
data : {
const hiddenPostData : any = {
post : postId ,
author : user.id ,
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
. query ( "api::post-ownership.post-ownership" )
. create ( {
data : hiddenPostData ,
} ) ;
console . log ( "✅ Post marked as hidden:" , hiddenPostOwnership ) ;
@@ -550,12 +594,12 @@ export default factories.createCoreController(
}
// 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
. query ( "api::post-ownership.post-ownership" )
. findOne ( {
where : {
post : postId ,
author : user.id ,
contextType : "user" ,
contextId : user.id ,
relation : "hidden" ,