diff --git a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts index 00c2c5351..9f67d6abf 100644 --- a/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts +++ b/backend/src/middleware/notifications/mentions/extractMentionedUsers.ts @@ -1,6 +1,6 @@ import cheerio from 'cheerio' -const queryAllUserIds = async (context) => { +export const queryAllUserIds = async (context) => { const allUserIdCypher = ` MATCH (user: User) // blocked users are not filtered out @@ -21,11 +21,11 @@ const queryAllUserIds = async (context) => { } } -export default async (context, content?) => { +export const extractMentionedUsers = async (context, content?) => { if (!content) return [] console.log('extractMentionedUsers – content: ', content) const $ = cheerio.load(content) - let userIds = $('a.mention[data-mention-id]') + const userIds = $('a.mention[data-mention-id]') .map((_, el) => { return $(el).attr('data-mention-id') }) @@ -34,9 +34,9 @@ export default async (context, content?) => { .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) - } + // 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 030a985b7..b83d7804a 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.ts +++ b/backend/src/middleware/notifications/notificationsMiddleware.ts @@ -1,5 +1,6 @@ import { pubsub, NOTIFICATION_ADDED } from '../../server' -import extractMentionedUsers from './mentions/extractMentionedUsers' +import { AuthenticationError } from 'apollo-server' +import { queryAllUserIds, extractMentionedUsers } from './mentions/extractMentionedUsers' import { validateNotifyUsers } from '../validation/validationMiddleware' import { sendMail } from '../helpers/email/sendMail' import { notificationTemplate } from '../helpers/email/templateBuilder' @@ -51,6 +52,23 @@ const publishNotifications = async (context, promises) => { }) } +const notifyPublishUsersOfMentionInclAll = async (label, id, idsOfUsers, reason, context) => { + if (idsOfUsers.find((id) => id === 'all')) { + 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), + ]) + } else { + await publishNotifications(context, [ + notifyUsersOfMention(label, id, idsOfUsers, reason, context), + ]) + } +} + const handleJoinGroup = async (resolve, root, args, context, resolveInfo) => { const { groupId, userId } = args const user = await resolve(root, args, context, resolveInfo) @@ -99,9 +117,13 @@ const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo const idsOfUsers = await extractMentionedUsers(context, args.content) const post = await resolve(root, args, context, resolveInfo) if (post) { - await publishNotifications(context, [ - notifyUsersOfMention('Post', post.id, idsOfUsers, 'mentioned_in_post', context), - ]) + await notifyPublishUsersOfMentionInclAll( + 'Post', + post.id, + idsOfUsers, + 'mentioned_in_post', + context, + ) } return post } @@ -113,9 +135,15 @@ const handleContentDataOfComment = async (resolve, root, args, context, resolveI const [postAuthor] = await postAuthorOfComment(comment.id, { context }) idsOfUsers = idsOfUsers.filter((id) => id !== postAuthor.id) await publishNotifications(context, [ - notifyUsersOfMention('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context), notifyUsersOfComment('Comment', comment.id, postAuthor.id, 'commented_on_post', context), ]) + await notifyPublishUsersOfMentionInclAll( + 'Comment', + comment.id, + idsOfUsers, + 'mentioned_in_comment', + context, + ) return comment }