diff --git a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts index ccee18af9..00c2c5351 100644 --- a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts +++ b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts @@ -1,15 +1,42 @@ import cheerio from 'cheerio' -export default (content?) => { +const queryAllUserIds = async (context) => { + const allUserIdCypher = ` + MATCH (user: User) + // blocked users are not filtered out + RETURN user {.id} + ` + const session = context.driver.session() + const writeTxResultPromise = session.readTransaction(async (transaction) => { + const transactionResponse = await transaction.run(allUserIdCypher, {}) + return transactionResponse.records.map((record) => record.get('user').id) + }) + try { + const ids = await writeTxResultPromise + return ids + } catch (error) { + throw new Error(error) + } finally { + session.close() + } +} + +export default async (context, content?) => { if (!content) return [] + console.log('extractMentionedUsers – content: ', content) const $ = cheerio.load(content) - const userIds = $('a.mention[data-mention-id]') + let userIds = $('a.mention[data-mention-id]') .map((_, el) => { return $(el).attr('data-mention-id') }) .get() - return userIds .map((id) => id.trim()) .filter((id) => !!id) .filter((id, index, allIds) => allIds.indexOf(id) === index) + console.log('extractMentionedUsers – userIds: ', userIds) + if (userIds.find((id) => id === 'all')) { + userIds = await queryAllUserIds(context) + console.log('extractMentionedUsers – all userIds: ', userIds) + } + return userIds } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.ts b/backend/src/middleware/notifications/notificationsMiddleware.ts index 706e46c51..030a985b7 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.ts +++ b/backend/src/middleware/notifications/notificationsMiddleware.ts @@ -96,7 +96,7 @@ const handleRemoveUserFromGroup = async (resolve, root, args, context, resolveIn } const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => { - const idsOfUsers = extractMentionedUsers(args.content) + const idsOfUsers = await extractMentionedUsers(context, args.content) const post = await resolve(root, args, context, resolveInfo) if (post) { await publishNotifications(context, [ @@ -108,7 +108,7 @@ const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => { const { content } = args - let idsOfUsers = extractMentionedUsers(content) + let idsOfUsers = await extractMentionedUsers(context, content) const comment = await resolve(root, args, context, resolveInfo) const [postAuthor] = await postAuthorOfComment(comment.id, { context }) idsOfUsers = idsOfUsers.filter((id) => id !== postAuthor.id)