Only recommend page if the user is allowed to post

This commit is contained in:
Hongbo Wu 2022-12-13 19:00:49 +08:00
parent e5abb14c52
commit 943b20f21e
2 changed files with 24 additions and 8 deletions

View file

@ -26,6 +26,7 @@ import {
import {
createGroup,
createLabelAndRuleForGroup,
getGroupsWhereUserCanPost,
getInviteUrl,
getRecommendationGroups,
joinGroup,
@ -187,18 +188,16 @@ export const recommendResolver = authorized<
}
}
const groups = await getRepository(Group).find({
where: { id: In(input.groupIds) },
relations: ['members', 'members.user'],
})
if (groups.length === 0) {
const page = await getPageByParam({ _id: input.pageId, userId: uid })
if (!page) {
return {
errorCodes: [RecommendErrorCode.NotFound],
}
}
const page = await getPageByParam({ _id: input.pageId, userId: uid })
if (!page) {
// find groups where id is in the groupIds and the user is a member of the group and the user is allowed to post
const groups = await getGroupsWhereUserCanPost(uid, input.groupIds)
if (groups.length === 0) {
return {
errorCodes: [RecommendErrorCode.NotFound],
}
@ -211,7 +210,7 @@ export const recommendResolver = authorized<
const exp = Math.floor(Date.now() / 1000) + 60 * 60 * 24 // 1 day
const auth = (await signToken({ uid, exp }, env.server.jwtSecret)) as string
await Promise.all(
const taskNames = await Promise.all(
groups
.map((group) =>
group.members.map((member) =>
@ -237,6 +236,7 @@ export const recommendResolver = authorized<
)
.flat()
)
console.log('taskNames', taskNames)
return {
success: true,

View file

@ -269,3 +269,19 @@ export const createLabelAndRuleForGroup = async (
await Promise.all([addLabelPromise, sendNotificationPromise])
}
// find groups where id is in the groupIds and the user is a member of the group and the user is allowed to post
export const getGroupsWhereUserCanPost = async (
userId: string,
groupIds: string[]
): Promise<Group[]> => {
return getRepository(Group)
.createQueryBuilder('group')
.innerJoin('group.members', 'members1')
.whereInIds(groupIds)
.andWhere('members1.user_id = :userId', { userId })
.andWhere('(members1.is_admin = true OR group.only_admin_can_post = false)')
.innerJoinAndSelect('group.members', 'members')
.innerJoinAndSelect('members.user', 'user')
.getMany()
}