diff --git a/client/src/components/notifications/NotificationsStep/Item.jsx b/client/src/components/notifications/NotificationsStep/Item.jsx index 892a75cf..e5896991 100644 --- a/client/src/components/notifications/NotificationsStep/Item.jsx +++ b/client/src/components/notifications/NotificationsStep/Item.jsx @@ -13,7 +13,7 @@ import { Button } from 'semantic-ui-react'; import selectors from '../../../selectors'; import entryActions from '../../../entry-actions'; -import { formatTextWithMentions } from '../../../utils/formatters'; +import { formatTextWithMentions } from '../../../utils/mentions'; import Paths from '../../../constants/Paths'; import { StaticUserIds } from '../../../constants/StaticUsers'; import { NotificationTypes } from '../../../constants/Enums'; diff --git a/client/src/configs/markdown-plugins/mention.js b/client/src/configs/markdown-plugins/mention.js index ec775c42..511efbd4 100644 --- a/client/src/configs/markdown-plugins/mention.js +++ b/client/src/configs/markdown-plugins/mention.js @@ -3,7 +3,7 @@ * Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md */ -const MENTION_REGEX = /@\[(.*?)\]\((.*?)\)/g; +import { MENTION_REGEX } from '../../utils/mentions'; export default (md) => { md.core.ruler.push('mention', ({ tokens }) => { diff --git a/client/src/utils/formatters.js b/client/src/utils/formatters.js deleted file mode 100644 index 3c6a81ca..00000000 --- a/client/src/utils/formatters.js +++ /dev/null @@ -1,4 +0,0 @@ -const MENTIONS_REGEX = /@\[(.*?)\]\(.*?\)/g; - -// eslint-disable-next-line import/prefer-default-export -export const formatTextWithMentions = (text) => text.replace(MENTIONS_REGEX, '@$1'); diff --git a/client/src/utils/mentions.js b/client/src/utils/mentions.js new file mode 100644 index 00000000..ad4e5f21 --- /dev/null +++ b/client/src/utils/mentions.js @@ -0,0 +1,9 @@ +/*! + * Copyright (c) 2024 PLANKA Software GmbH + * Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md + */ + +export const MENTION_REGEX = /@\[(.*?)\]\((.*?)\)/g; +export const MENTION_NAME_REGEX = /@\[(.*?)\]\(.*?\)/g; + +export const formatTextWithMentions = (text) => text.replace(MENTION_NAME_REGEX, '@$1'); diff --git a/server/api/helpers/comments/create-one.js b/server/api/helpers/comments/create-one.js index 67f5b7c1..eb0936c1 100644 --- a/server/api/helpers/comments/create-one.js +++ b/server/api/helpers/comments/create-one.js @@ -6,13 +6,7 @@ const escapeMarkdown = require('escape-markdown'); const escapeHtml = require('escape-html'); -const { formatTextWithMentions } = require('../../../utils/formatters'); - -const extractMentionedUserIds = (text) => { - const mentionRegex = /@\[.*?\]\((.*?)\)/g; - const matches = [...text.matchAll(mentionRegex)]; - return matches.map((match) => match[1]); -}; +const { extractMentionIds, formatTextWithMentions } = require('../../../utils/mentions'); const buildAndSendNotifications = async (services, board, card, comment, actorUser, t) => { const markdownCardLink = `[${escapeMarkdown(card.name)}](${sails.config.custom.baseUrl}/cards/${card.id})`; @@ -99,18 +93,18 @@ module.exports = { user: values.user, }); - let mentionedUserIds = extractMentionedUserIds(values.text); + let mentionUserIds = extractMentionIds(comment.text); - if (mentionedUserIds.length > 0) { + if (mentionUserIds.length > 0) { const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(inputs.board.id); - mentionedUserIds = _.difference( - _.intersection(mentionedUserIds, boardMemberUserIds), + mentionUserIds = _.difference( + _.intersection(mentionUserIds, boardMemberUserIds), comment.userId, ); } - const mentionedUserIdsSet = new Set(mentionedUserIds); + const mentionUserIdsSet = new Set(mentionUserIds); const cardSubscriptionUserIds = await sails.helpers.cards.getSubscriptionUserIds( comment.cardId, @@ -123,7 +117,7 @@ module.exports = { ); const notifiableUserIds = _.union( - mentionedUserIds, + mentionUserIds, cardSubscriptionUserIds, boardSubscriptionUserIds, ); @@ -134,7 +128,7 @@ module.exports = { values: { userId, comment, - type: mentionedUserIdsSet.has(userId) + type: mentionUserIdsSet.has(userId) ? Notification.Types.MENTION_IN_COMMENT : Notification.Types.COMMENT_CARD, data: { diff --git a/server/api/helpers/notifications/create-one.js b/server/api/helpers/notifications/create-one.js index 33a49e64..3b26a6cb 100644 --- a/server/api/helpers/notifications/create-one.js +++ b/server/api/helpers/notifications/create-one.js @@ -6,7 +6,7 @@ const escapeMarkdown = require('escape-markdown'); const escapeHtml = require('escape-html'); -const { formatTextWithMentions } = require('../../../utils/formatters'); +const { formatTextWithMentions } = require('../../../utils/mentions'); const buildTitle = (notification, t) => { switch (notification.type) { diff --git a/server/utils/formatters.js b/server/utils/formatters.js deleted file mode 100644 index 6c266c6b..00000000 --- a/server/utils/formatters.js +++ /dev/null @@ -1,7 +0,0 @@ -const MENTIONS_REGEX = /@\[(.*?)\]\(.*?\)/g; - -const formatTextWithMentions = (text) => text.replace(MENTIONS_REGEX, '@$1'); - -module.exports = { - formatTextWithMentions, -}; diff --git a/server/utils/mentions.js b/server/utils/mentions.js new file mode 100644 index 00000000..cab09dfb --- /dev/null +++ b/server/utils/mentions.js @@ -0,0 +1,22 @@ +/*! + * Copyright (c) 2024 PLANKA Software GmbH + * Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md + */ + +const MENTION_ID_REGEX = /@\[.*?\]\((.*?)\)/g; +const MENTION_NAME_REGEX = /@\[(.*?)\]\(.*?\)/g; + +const extractMentionIds = (text) => { + const matches = [...text.matchAll(MENTION_ID_REGEX)]; + return matches.map((match) => match[1]); +}; + +const formatTextWithMentions = (text) => text.replace(MENTION_NAME_REGEX, '@$1'); + +module.exports = { + MENTION_ID_REGEX, + MENTION_NAME_REGEX, + + extractMentionIds, + formatTextWithMentions, +};