1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

feat: Webhooks configuration, all events support, refactoring

This commit is contained in:
Maksim Eltyshev 2024-06-12 00:51:36 +02:00
parent 193daf6cfb
commit 87683fe523
96 changed files with 1280 additions and 509 deletions

View file

@ -15,14 +15,14 @@ const valuesValidator = (value) => {
};
// TODO: use templates (views) to build html
const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
const buildAndSendEmail = async (board, card, action, actorUser, notifiableUser) => {
let emailData;
switch (action.type) {
case Action.Types.MOVE_CARD:
emailData = {
subject: `${user.name} moved ${card.name} from ${action.data.fromList.name} to ${action.data.toList.name} on ${board.name}`,
subject: `${actorUser.name} moved ${card.name} from ${action.data.fromList.name} to ${action.data.toList.name} on ${board.name}`,
html:
`<p>${user.name} moved ` +
`<p>${actorUser.name} moved ` +
`<a href="${process.env.BASE_URL}/cards/${card.id}">${card.name}</a> ` +
`from ${action.data.fromList.name} to ${action.data.toList.name} ` +
`on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>`,
@ -31,9 +31,9 @@ const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
break;
case Action.Types.COMMENT_CARD:
emailData = {
subject: `${user.name} left a new comment to ${card.name} on ${board.name}`,
subject: `${actorUser.name} left a new comment to ${card.name} on ${board.name}`,
html:
`<p>${user.name} left a new comment to ` +
`<p>${actorUser.name} left 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>`,
@ -57,7 +57,7 @@ module.exports = {
custom: valuesValidator,
required: true,
},
user: {
project: {
type: 'ref',
required: true,
},
@ -65,10 +65,18 @@ module.exports = {
type: 'ref',
required: true,
},
list: {
type: 'ref',
required: true,
},
card: {
type: 'ref',
required: true,
},
actorUser: {
type: 'ref',
required: true,
},
},
async fn(inputs) {
@ -96,9 +104,24 @@ module.exports = {
notifiableUser = await sails.helpers.users.getOne(notification.userId);
}
buildAndSendEmail(inputs.user, inputs.board, inputs.card, values.action, notifiableUser);
buildAndSendEmail(inputs.board, inputs.card, values.action, inputs.actorUser, notifiableUser);
}
sails.helpers.utils.sendWebhooks.with({
event: 'notificationCreate',
data: {
item: notification,
included: {
projects: [inputs.project],
boards: [inputs.board],
lists: [inputs.list],
cards: [inputs.card],
actions: [values.action],
},
},
user: inputs.actorUser,
});
return notification;
},
};

View file

@ -12,8 +12,9 @@ module.exports = {
type: 'json',
required: true,
},
user: {
actorUser: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
@ -23,7 +24,9 @@ module.exports = {
async fn(inputs) {
const { values } = inputs;
const criteria = {};
const criteria = {
userId: inputs.actorUser.id,
};
if (_.every(inputs.recordsOrIds, _.isPlainObject)) {
criteria.id = sails.helpers.utils.mapRecords(inputs.recordsOrIds);
@ -31,10 +34,6 @@ module.exports = {
criteria.id = inputs.recordsOrIds;
}
if (inputs.user) {
criteria.userId = inputs.user.id;
}
const notifications = await Notification.update(criteria)
.set({ ...values })
.fetch();
@ -48,6 +47,14 @@ module.exports = {
},
inputs.request,
);
sails.helpers.utils.sendWebhooks.with({
event: 'notificationUpdate',
data: {
item: notification,
},
user: inputs.actorUser,
});
});
return notifications;