Send mentions of all users in chunks of user id pages

This commit is contained in:
Wolfgang Huß 2023-07-25 16:22:03 +02:00
parent 56d9c6facd
commit 3a2f8b3bb5
3 changed files with 25 additions and 15 deletions

View File

@ -1,4 +1,4 @@
import extractMentionedUsers from './extractMentionedUsers'
import { extractMentionedUsers } from './extractMentionedUsers'
const contentWithMentions =
'<p>Something inspirational about <a href="/profile/u2" class="not-a-mention" data-mention-id="bobs-id" target="_blank">@bob-der-baumeister</a> and <a href="/profile/u3/jenny-rostock" class="mention" data-mention-id="u3" target="_blank">@jenny-rostock</a>.</p>'

View File

@ -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
}

View File

@ -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)