Implement mention of all users – first approach

This commit is contained in:
Wolfgang Huß 2023-07-25 13:52:18 +02:00
parent e465e59afe
commit 4e21afb8ff
2 changed files with 32 additions and 5 deletions

View File

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

View File

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