1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-10 16:05:35 +02:00

refactor: notify mention

This commit is contained in:
Albert Buenaventura 2024-04-26 23:41:38 +08:00
parent c3add4fc92
commit 02a9528a77
3 changed files with 38 additions and 6 deletions

View file

@ -9,7 +9,8 @@ const replaceMentionsWithName = (text, users) => {
return text.replace(mentionRegex, function (matched) {
mentionRegex.lastIndex = 0;
const nameOrEmail = mentionRegex.exec(text)[1];
const mentionMatch = matched.match(mentionRegex)[0];
const nameOrEmail = mentionRegex.exec(mentionMatch)[1];
const member = findUserByUsernameOrEmail(nameOrEmail, users);
return member ? `[${member.user.name}](#)` : matched;

View file

@ -17,13 +17,19 @@ module.exports = {
async fn(inputs) {
const { comment, users } = inputs;
const mentionRegex = /\[@(.*?)\]/g;
const mentions = comment.match(mentionRegex);
return comment.match(mentionRegex).map((match) => {
if (!mentions) return [];
return mentions.map((match) => {
mentionRegex.lastIndex = 0;
const nameOrEmail = mentionRegex.exec(match)[1];
const member = findUserByUsernameOrEmail(nameOrEmail, users);
return member.id;
});
},
findUserByUsernameOrEmail,
};

View file

@ -1,3 +1,5 @@
const { findUserByUsernameOrEmail } = require('../mentions/get-mentions');
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
@ -14,6 +16,19 @@ const valuesValidator = (value) => {
return true;
};
const replaceMentionsWithLink = (text, users) => {
const mentionRegex = /\[@(.*?)\]/g;
return text.replace(mentionRegex, (matched) => {
mentionRegex.lastIndex = 0;
const mentionMatch = matched.match(mentionRegex)[0];
const nameOrEmail = mentionRegex.exec(mentionMatch)[1];
const user = findUserByUsernameOrEmail(nameOrEmail, users);
return user ? `<p style="color: blue; margin: 0;">${user.name}</p>` : matched;
});
};
// TODO: use templates (views) to build html
const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
let emailData;
@ -29,17 +44,27 @@ const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
};
break;
case Action.Types.COMMENT_CARD:
case Action.Types.COMMENT_CARD: {
const boardMemberships = await sails.helpers.boards.getBoardMemberships(board.id);
const userIds = sails.helpers.utils.mapRecords(boardMemberships, 'userId');
const users = await sails.helpers.users.getMany(userIds);
const comment = replaceMentionsWithLink(action.data.text, users);
const mentions = await sails.helpers.mentions.getMentions(action.data.text, users);
const isMentioned = mentions.includes(notifiableUser.id);
const subjectPrex = isMentioned ? `${user.name} mentioned you in` : `${user.name} left`;
emailData = {
subject: `${user.name} left a new comment to ${card.name} on ${board.name}`,
subject: `${subjectPrex} a new comment to ${card.name} on ${board.name}`,
html:
`<p>${user.name} left a new comment to ` +
`<p>${subjectPrex} a new comment to ` +
`<a href="${process.env.BASE_URL}/cards/${card.id}">${card.name}</a> ` +
`on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>` +
`<p>${action.data.text}</p>`,
`<div style="display: flex; gap: 5px;">${comment}</div>`,
};
break;
}
default:
return;
}