mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2026-01-20 20:01:22 +00:00
add helper function to filter invisible posts
This commit is contained in:
parent
4a12cdf9e2
commit
069eefde17
44
backend/src/schema/resolvers/helpers/filterInvisiblePosts.js
Normal file
44
backend/src/schema/resolvers/helpers/filterInvisiblePosts.js
Normal file
@ -0,0 +1,44 @@
|
||||
import { mergeWith, isArray } from 'lodash'
|
||||
|
||||
const getInvisiblePosts = async (context) => {
|
||||
const session = context.driver.session()
|
||||
const readTxResultPromise = await session.readTransaction(async (transaction) => {
|
||||
let cypher = ''
|
||||
if (context.user) {
|
||||
cypher = `
|
||||
MATCH (post:Post)<-[:CANNOT_SEE]-(user:User { id: $userId })
|
||||
RETURN collect(post.id) AS invisiblePostIds`
|
||||
} else {
|
||||
cypher = `
|
||||
MATCH (post:Post)-[:IN]->(group:Group)
|
||||
WHERE NOT group.groupType = 'public'
|
||||
RETURN collect(post.id) AS invisiblePostIds`
|
||||
}
|
||||
const invisiblePostIdsResponse = await transaction.run(cypher, { userId: context.user.id })
|
||||
return invisiblePostIdsResponse.records.map((record) => record.get('invisiblePostIds'))
|
||||
})
|
||||
try {
|
||||
const [invisiblePostIds] = readTxResultPromise
|
||||
return invisiblePostIds
|
||||
} finally {
|
||||
session.close()
|
||||
}
|
||||
}
|
||||
|
||||
export const filterInvisiblePosts = async (params, context) => {
|
||||
const invisiblePostIds = await getInvisiblePosts(context)
|
||||
if (!invisiblePostIds.length) return params
|
||||
|
||||
params.filter = mergeWith(
|
||||
params.filter,
|
||||
{
|
||||
id_not_in: invisiblePostIds,
|
||||
},
|
||||
(objValue, srcValue) => {
|
||||
if (isArray(objValue)) {
|
||||
return objValue.concat(srcValue)
|
||||
}
|
||||
},
|
||||
)
|
||||
return params
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user