Skip to content

Commit

Permalink
feat: SMTP integration and email notifications (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
vied12 authored Mar 21, 2024
1 parent 60ab097 commit 9f0fce0
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ services:
# - DEFAULT_ADMIN_NAME=Demo Demo
# - DEFAULT_ADMIN_USERNAME=demo

# Email Notifications (https://nodemailer.com/smtp/)
# - SMTP_HOST=
# - SMTP_PORT=587
# - SMTP_SECURE=true
# - SMTP_USER=
# - SMTP_PASSWORD=
# - SMTP_FROM="Demo Demo" <[email protected]>

# - OIDC_ISSUER=
# - OIDC_CLIENT_ID=
# - OIDC_CLIENT_SECRET=
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ services:
# - DEFAULT_ADMIN_NAME=Demo Demo
# - DEFAULT_ADMIN_USERNAME=demo

# Email Notifications (https://nodemailer.com/smtp/)
# - SMTP_HOST=
# - SMTP_PORT=587
# - SMTP_SECURE=true
# - SMTP_USER=
# - SMTP_PASSWORD=
# - SMTP_FROM="Demo Demo" <[email protected]>

# - OIDC_ISSUER=
# - OIDC_CLIENT_ID=
# - OIDC_CLIENT_SECRET=
Expand Down
8 changes: 8 additions & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ SECRET_KEY=notsecretkey
# DEFAULT_ADMIN_NAME=Demo Demo
# DEFAULT_ADMIN_USERNAME=demo

# Email Notifications (https://nodemailer.com/smtp/)
# SMTP_HOST=
# SMTP_PORT=587
# SMTP_SECURE=true
# SMTP_USER=
# SMTP_PASSWORD=
# SMTP_FROM="Demo Demo" <[email protected]>

# OIDC_ISSUER=
# OIDC_CLIENT_ID=
# OIDC_CLIENT_SECRET=
Expand Down
5 changes: 3 additions & 2 deletions server/api/controllers/cards/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;

const { list } = await sails.helpers.lists
const { board, list } = await sails.helpers.lists
.getProjectPath(inputs.listId)
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);

const boardMembership = await BoardMembership.findOne({
boardId: list.boardId,
boardId: board.id,
userId: currentUser.id,
});

Expand All @@ -99,6 +99,7 @@ module.exports = {

const card = await sails.helpers.cards.createOne
.with({
board,
values: {
...values,
list,
Expand Down
5 changes: 3 additions & 2 deletions server/api/controllers/comment-actions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;

const { card } = await sails.helpers.cards
const { board, card } = await sails.helpers.cards
.getProjectPath(inputs.cardId)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);

const boardMembership = await BoardMembership.findOne({
boardId: card.boardId,
boardId: board.id,
userId: currentUser.id,
});

Expand All @@ -55,6 +55,7 @@ module.exports = {
};

const action = await sails.helpers.actions.createOne.with({
board,
values: {
...values,
card,
Expand Down
7 changes: 7 additions & 0 deletions server/api/helpers/actions/create-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module.exports = {
custom: valuesValidator,
required: true,
},
board: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
Expand Down Expand Up @@ -56,6 +60,9 @@ module.exports = {
userId,
action,
},
user: values.user,
board: inputs.board,
card: values.card,
}),
),
);
Expand Down
5 changes: 5 additions & 0 deletions server/api/helpers/cards/create-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ module.exports = {
custom: valuesValidator,
required: true,
},
board: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
Expand Down Expand Up @@ -104,6 +108,7 @@ module.exports = {
},
user: values.creatorUser,
},
board: inputs.board,
});

return card;
Expand Down
1 change: 1 addition & 0 deletions server/api/helpers/cards/update-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ module.exports = {
toList: _.pick(values.list, ['id', 'name']),
},
},
board: inputs.board,
});
}

Expand Down
57 changes: 57 additions & 0 deletions server/api/helpers/notifications/create-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,59 @@ const valuesValidator = (value) => {
return true;
};

// TODO: use templates (views) to build html
const buildAndSendEmail = async (user, board, card, action, 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}`,
html:
`<p>${user.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>`,
};
break;
case Action.Types.COMMENT_CARD:
emailData = {
subject: `${user.name} left a new comment to ${card.name} on ${board.name}`,
html:
`<p>${user.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>`,
};
break;
default:
return;
}

await sails.helpers.utils.sendEmail.with({
...emailData,
to: notifiableUser.email,
});
};

module.exports = {
inputs: {
values: {
type: 'ref',
custom: valuesValidator,
required: true,
},
user: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
card: {
type: 'ref',
required: true,
},
},

async fn(inputs) {
Expand All @@ -40,6 +86,17 @@ module.exports = {
item: notification,
});

if (sails.hooks.smtp.isActive()) {
let notifiableUser;
if (values.user) {
notifiableUser = values.user;
} else {
notifiableUser = await sails.helpers.users.getOne(notification.userId);
}

buildAndSendEmail(inputs.user, inputs.board, inputs.card, values.action, notifiableUser);
}

return notification;
},
};
31 changes: 31 additions & 0 deletions server/api/helpers/utils/send-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
inputs: {
to: {
type: 'string',
required: true,
},
subject: {
type: 'string',
required: true,
},
html: {
type: 'string',
required: true,
},
},

async fn(inputs) {
const transporter = sails.hooks.smtp.getTransporter(); // TODO: check if active?

try {
const info = await transporter.sendMail({
...inputs,
from: sails.config.custom.smtpFrom,
});

sails.log.info('Email sent: %s', info.messageId);
} catch (error) {
sails.log.error(error);
}
},
};
35 changes: 35 additions & 0 deletions server/api/hooks/smtp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const nodemailer = require('nodemailer');

module.exports = function smtpServiceHook(sails) {
let transporter = null;

return {
/**
* Runs when this Sails app loads/lifts.
*/

async initialize() {
if (sails.config.custom.smtpHost) {
transporter = nodemailer.createTransport({
pool: true,
host: sails.config.custom.smtpHost,
port: sails.config.custom.smtpPort,
secure: sails.config.custom.smtpSecure,
auth: sails.config.custom.smtpUser && {
user: sails.config.custom.smtpUser,
pass: sails.config.custom.smtpPassword,
},
});
sails.log.info('SMTP hook has been loaded successfully');
}
},

getTransporter() {
return transporter;
},

isActive() {
return transporter !== null;
},
};
};
7 changes: 7 additions & 0 deletions server/config/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ module.exports.custom = {
defaultAdminEmail:
process.env.DEFAULT_ADMIN_EMAIL && process.env.DEFAULT_ADMIN_EMAIL.toLowerCase(),

smtpHost: process.env.SMTP_HOST,
smtpPort: process.env.SMTP_PORT || 587,
smtpSecure: process.env.SMTP_SECURE === 'true',
smtpUser: process.env.SMTP_USER,
smtpPassword: process.env.SMTP_PASSWORD,
smtpFrom: process.env.SMTP_FROM,

oidcIssuer: process.env.OIDC_ISSUER,
oidcClientId: process.env.OIDC_CLIENT_ID,
oidcClientSecret: process.env.OIDC_CLIENT_SECRET,
Expand Down
14 changes: 14 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"lodash": "^4.17.21",
"moment": "^2.29.4",
"move-file": "^2.1.0",
"nodemailer": "^6.9.12",
"openid-client": "^5.6.1",
"rimraf": "^5.0.5",
"sails": "^1.5.7",
Expand Down

0 comments on commit 9f0fce0

Please sign in to comment.