From 3a2f8b3bb5b5ac18b9eb6e2675c272d7201dd71d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Tue, 25 Jul 2023 16:22:03 +0200 Subject: [PATCH] Send mentions of all users in chunks of user id pages --- .../mentions/extractMentionedUsers.spec.ts | 2 +- .../mentions/extractMentionedUsers.ts | 12 ++++----- .../notifications/notificationsMiddleware.ts | 26 ++++++++++++++----- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/backend/src/middleware/notifications/mentions/extractMentionedUsers.spec.ts b/backend/src/middleware/notifications/mentions/extractMentionedUsers.spec.ts index 9865eab0d..84559003f 100644 --- a/backend/src/middleware/notifications/mentions/extractMentionedUsers.spec.ts +++ b/backend/src/middleware/notifications/mentions/extractMentionedUsers.spec.ts @@ -1,4 +1,4 @@ -import extractMentionedUsers from './extractMentionedUsers' +import { extractMentionedUsers } from './extractMentionedUsers' const contentWithMentions = '

Something inspirational about @bob-der-baumeister and @jenny-rostock.

' diff --git a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts index 9f67d6abf..6d7333710 100644 --- a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts +++ b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts @@ -1,10 +1,12 @@ import cheerio from 'cheerio' -export const queryAllUserIds = async (context) => { +export const queryAllUserIds = async (context, offset = -1, pageSize = -1) => { + const offsetCypher = offset >= 0 ? ` SKIP ${offset}` : '' + const pageSizeCypher = pageSize >= 0 ? ` LIMIT ${pageSize}` : '' const allUserIdCypher = ` MATCH (user: User) // blocked users are not filtered out - RETURN user {.id} + RETURN user {.id}${offsetCypher}${pageSizeCypher} ` const session = context.driver.session() const writeTxResultPromise = session.readTransaction(async (transaction) => { @@ -21,7 +23,7 @@ export const queryAllUserIds = async (context) => { } } -export const extractMentionedUsers = async (context, content?) => { +export const extractMentionedUsers = async (content?) => { if (!content) return [] console.log('extractMentionedUsers – content: ', content) const $ = cheerio.load(content) @@ -34,9 +36,5 @@ export const extractMentionedUsers = async (context, content?) => { .filter((id) => !!id) .filter((id, index, allIds) => allIds.indexOf(id) === index) console.log('extractMentionedUsers – userIds: ', userIds) - // Wolle if (context.user.role === 'admin' && 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 b83d7804a..b33cd05cb 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.ts +++ b/backend/src/middleware/notifications/notificationsMiddleware.ts @@ -57,11 +57,23 @@ const notifyPublishUsersOfMentionInclAll = async (label, id, idsOfUsers, reason, if (context.user.role !== 'admin') throw new AuthenticationError('You are not allowed to use the "@all" mention!') - idsOfUsers = await queryAllUserIds(context) - console.log('handleContentDataOfPost – on @all – idsOfUsers: ', idsOfUsers) - await publishNotifications(context, [ - notifyUsersOfMention(label, id, idsOfUsers, reason, context), - ]) + let offset = 0 + const pageSize = 100 + let pageOfUserIds = await queryAllUserIds(context, offset, pageSize) + + while (pageOfUserIds.length > 0) { + console.log('handleContentDataOfPost – on @all – idsOfUsers: ', pageOfUserIds) + await publishNotifications(context, [ + notifyUsersOfMention(label, id, pageOfUserIds, reason, context), + ]) + + if (pageOfUserIds.length < pageSize) { + pageOfUserIds = [] + } else { + offset += pageSize + pageOfUserIds = await queryAllUserIds(context, offset, pageSize) + } + } } else { await publishNotifications(context, [ notifyUsersOfMention(label, id, idsOfUsers, reason, context), @@ -114,7 +126,7 @@ const handleRemoveUserFromGroup = async (resolve, root, args, context, resolveIn } const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => { - const idsOfUsers = await extractMentionedUsers(context, args.content) + const idsOfUsers = await extractMentionedUsers(args.content) const post = await resolve(root, args, context, resolveInfo) if (post) { await notifyPublishUsersOfMentionInclAll( @@ -130,7 +142,7 @@ const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => { const { content } = args - let idsOfUsers = await extractMentionedUsers(context, content) + let idsOfUsers = await extractMentionedUsers(content) const comment = await resolve(root, args, context, resolveInfo) const [postAuthor] = await postAuthorOfComment(comment.id, { context }) idsOfUsers = idsOfUsers.filter((id) => id !== postAuthor.id)