refactoring email notification, closes #8
This commit is contained in:
@@ -186,7 +186,12 @@ const userController = {
|
||||
if (!recover_code) return res.sendStatus(400)
|
||||
const user = await User.findOne({ where: { recover_code: { [Op.eq]: recover_code } } })
|
||||
if (!user) return res.sendStatus(400)
|
||||
res.json(user)
|
||||
try {
|
||||
await user.update({ recover_code: ''})
|
||||
res.sendStatus(200)
|
||||
} catch (e) {
|
||||
res.sendStatus(400)
|
||||
}
|
||||
},
|
||||
|
||||
async updatePasswordWithRecoverCode(req, res) {
|
||||
@@ -195,6 +200,7 @@ const userController = {
|
||||
if (!recover_code || !password) return res.sendStatus(400)
|
||||
const user = await User.findOne({ where: { recover_code: { [Op.eq]: recover_code } } })
|
||||
if (!user) return res.sendStatus(400)
|
||||
user.recover_code = ''
|
||||
user.password = password
|
||||
try {
|
||||
await user.save()
|
||||
@@ -218,8 +224,8 @@ const userController = {
|
||||
async update(req, res) {
|
||||
const user = await User.findByPk(req.body.id)
|
||||
if (user) {
|
||||
if (!user.is_active && req.body.is_active) {
|
||||
await mail.send(user.email, 'confirm', { user, config })
|
||||
if (!user.is_active && req.body.is_active && user.recover_code) {
|
||||
mail.send(user.email, 'confirm', { user, config })
|
||||
}
|
||||
await user.update(req.body)
|
||||
res.json(user)
|
||||
@@ -240,9 +246,11 @@ const userController = {
|
||||
req.body.is_active = false
|
||||
}
|
||||
|
||||
req.body.recover_code = crypto.randomBytes(16).toString('hex')
|
||||
const user = await User.create(req.body)
|
||||
try {
|
||||
mail.send([user.email, config.admin], 'register', { user, config })
|
||||
mail.send(user.email, 'register', { user, config })
|
||||
mail.send(config.admin, 'admin_register', { user, config })
|
||||
} catch (e) {
|
||||
return res.status(400).json(e)
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@ const path = require('path')
|
||||
const moment = require('moment')
|
||||
const config = require('config')
|
||||
const settings = require('./controller/settings')
|
||||
const debug = require('debug')('email')
|
||||
|
||||
moment.locale('it')
|
||||
const mail = {
|
||||
send(addresses, template, locals) {
|
||||
debug(`Send ${template} email to ${addresses}`)
|
||||
const email = new Email({
|
||||
views: { root: path.join(__dirname, '..', 'emails') },
|
||||
htmlToText: false,
|
||||
@@ -18,15 +20,17 @@ const mail = {
|
||||
}
|
||||
},
|
||||
message: {
|
||||
from: `${config.title} <${config.smtp.auth.user}>`
|
||||
from: `${config.title} <${config.admin}>`
|
||||
},
|
||||
send: true,
|
||||
i18n: {
|
||||
directory: path.join(__dirname, '..', '..', 'locales', 'email'),
|
||||
objectNotation: true,
|
||||
syncFiles: false,
|
||||
updateFiles: false,
|
||||
defaultLocale: settings.locale,
|
||||
locales: ['en', 'it']
|
||||
locale: settings.locale,
|
||||
locales: ['it', 'es'],
|
||||
},
|
||||
transport: config.smtp
|
||||
})
|
||||
@@ -45,7 +49,7 @@ const mail = {
|
||||
}
|
||||
return email.send(msg)
|
||||
.catch(e => {
|
||||
console.error(e)
|
||||
debug('Error sending email =>', e)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,11 +36,13 @@ module.exports = (sequelize, DataTypes) => {
|
||||
event.hasMany(models.comment)
|
||||
}
|
||||
|
||||
//
|
||||
event.prototype.toAP = function (username, follower) {
|
||||
const tags = this.tags && '-' + this.tags.map(t => '#' + t.tag).join(' ')
|
||||
const content = `<b><a href='${config.baseurl}/event/${this.id}'>${this.title}</a></b> @${this.place.name}
|
||||
${moment.unix(this.start_datetime).format('dddd, D MMMM (HH:mm)')}<br/>
|
||||
${this.description.length > 200 ? this.description.substr(0, 200) + '...' : this.description} ${tags} <br/>`
|
||||
|
||||
return {
|
||||
id: `${config.baseurl}/federation/m/c_${this.id}`,
|
||||
type: 'Create',
|
||||
|
||||
4
server/emails/admin_register/html.pug
Normal file
4
server/emails/admin_register/html.pug
Normal file
@@ -0,0 +1,4 @@
|
||||
extends ../layout.pug
|
||||
|
||||
block content
|
||||
p !{t('admin_register.content', { user, config })}
|
||||
1
server/emails/admin_register/subject.pug
Normal file
1
server/emails/admin_register/subject.pug
Normal file
@@ -0,0 +1 @@
|
||||
| [#{config.title}] #{t('admin_register.subject')}
|
||||
6
server/emails/admin_register/text.pug
Normal file
6
server/emails/admin_register/text.pug
Normal file
@@ -0,0 +1,6 @@
|
||||
p= t('admin_register.content', { user, config })
|
||||
|
||||
hr
|
||||
small #{config.title} / #{config.description}
|
||||
br
|
||||
small <a href="#{config.baseurl}">#{config.baseurl}</a>
|
||||
@@ -1,12 +1,3 @@
|
||||
<!DOCTYPE html>
|
||||
html(lang="en")
|
||||
head
|
||||
meta(charset="UTF-8")
|
||||
meta(name="viewport", content="width=device-width, initial-scale=1.0")
|
||||
meta(http-equiv="X-UA-Compatible", content="ie=edge")
|
||||
title #{config.title}
|
||||
body
|
||||
p= t('email.confirm', { config })
|
||||
|
||||
hr
|
||||
<a href="#{config.baseurl}"> #{config.title} - #{config.description}</a>
|
||||
extends ../layout.pug
|
||||
block content
|
||||
p !{t('confirm.content', { config, user })}
|
||||
@@ -1 +1 @@
|
||||
| [#{config.title}] #{t('email.confirm.subject')}
|
||||
| [#{config.title}] #{t('confirm.subject')}
|
||||
1
server/emails/confirm/text.pug
Normal file
1
server/emails/confirm/text.pug
Normal file
@@ -0,0 +1 @@
|
||||
p !{t('confirm.content', { config })}
|
||||
@@ -1,22 +1,21 @@
|
||||
h3 #{event.title}
|
||||
p Dove: #{event.place.name} - #{event.place.address}
|
||||
p Quando: #{datetime(event.start_datetime)}
|
||||
br
|
||||
if event.image_path
|
||||
<center>
|
||||
<img style="height: 89vh; margin: 0 auto;" src="#{config.baseurl}/media/#{event.image_path}" />
|
||||
</center>
|
||||
p #{event.description}
|
||||
extends ../layout.pug
|
||||
block content
|
||||
h3 #{event.title}
|
||||
p Dove: #{event.place.name} - #{event.place.address}
|
||||
p Quando: #{datetime(event.start_datetime)}
|
||||
br
|
||||
if event.image_path
|
||||
<center>
|
||||
<img style="height: 89vh; margin: 0 auto;" src="#{config.baseurl}/media/#{event.image_path}" />
|
||||
</center>
|
||||
p #{event.description}
|
||||
|
||||
each tag in event.tags
|
||||
span ##{tag.tag}
|
||||
br
|
||||
<a href="#{config.baseurl}/event/#{event.id}">#{config.baseurl}/event/#{event.id}</a>
|
||||
hr
|
||||
if to_confirm
|
||||
p Puoi confermare questo evento <a href="#{config.baseurl}/admin/confirm/#{event.id}">qui</a>
|
||||
else
|
||||
p Puoi eliminare queste notifiche <a href="#{config.baseurl}/del_notification/#{notification.remove_code}">qui</a>
|
||||
|
||||
hr
|
||||
<a href="#{config.baseurl}">#{config.title} - #{config.description}</a>
|
||||
each tag in event.tags
|
||||
span ##{tag.tag}
|
||||
br
|
||||
<a href="#{config.baseurl}/event/#{event.id}">#{config.baseurl}/event/#{event.id}</a>
|
||||
hr
|
||||
if to_confirm
|
||||
p Puoi confermare questo evento <a href="#{config.baseurl}/admin/confirm/#{event.id}">qui</a>
|
||||
else
|
||||
p Puoi eliminare queste notifiche <a href="#{config.baseurl}/del_notification/#{notification.remove_code}">qui</a>
|
||||
|
||||
21
server/emails/event/text.pug
Normal file
21
server/emails/event/text.pug
Normal file
@@ -0,0 +1,21 @@
|
||||
extends ../layout.pug
|
||||
block content
|
||||
h3 #{event.title}
|
||||
p Dove: #{event.place.name} - #{event.place.address}
|
||||
p Quando: #{datetime(event.start_datetime)}
|
||||
br
|
||||
if event.image_path
|
||||
<center>
|
||||
<img style="height: 89vh; margin: 0 auto;" src="#{config.baseurl}/media/#{event.image_path}" />
|
||||
</center>
|
||||
p #{event.description}
|
||||
|
||||
each tag in event.tags
|
||||
span ##{tag.tag}
|
||||
br
|
||||
<a href="#{config.baseurl}/event/#{event.id}">#{config.baseurl}/event/#{event.id}</a>
|
||||
hr
|
||||
if to_confirm
|
||||
p Puoi confermare questo evento <a href="#{config.baseurl}/admin/confirm/#{event.id}">qui</a>
|
||||
else
|
||||
p Puoi eliminare queste notifiche <a href="#{config.baseurl}/del_notification/#{notification.remove_code}">qui</a>
|
||||
13
server/emails/layout.pug
Normal file
13
server/emails/layout.pug
Normal file
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
html(lang="en")
|
||||
head
|
||||
meta(charset="UTF-8")
|
||||
meta(name="viewport", content="width=device-width, initial-scale=1.0")
|
||||
meta(http-equiv="X-UA-Compatible", content="ie=edge")
|
||||
title #{config.title}
|
||||
body
|
||||
block content
|
||||
hr
|
||||
small #{config.title} / #{config.description}
|
||||
br
|
||||
<a href='#{config.baseurl}'> #{config.baseurl}</a>
|
||||
@@ -1,12 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
html(lang="en")
|
||||
head
|
||||
meta(charset="UTF-8")
|
||||
meta(name="viewport", content="width=device-width, initial-scale=1.0")
|
||||
meta(http-equiv="X-UA-Compatible", content="ie=edge")
|
||||
title #{config.title}
|
||||
body
|
||||
p= t('email.recover', {config})
|
||||
extends ../layout.pug
|
||||
|
||||
hr
|
||||
<a href="#{config.baseurl}/recover/#{user.recover_code}">#{t('email.press_here')}</a>
|
||||
block content
|
||||
p !{t('recover.content', {config, user})}
|
||||
@@ -1 +1 @@
|
||||
= `[#{config.title}] ${t('email.recover')}`
|
||||
| [#{config.title}] #{t('recover.subject')}
|
||||
|
||||
1
server/emails/recover/text.pug
Normal file
1
server/emails/recover/text.pug
Normal file
@@ -0,0 +1 @@
|
||||
p= !t('recover.content', {config, user})
|
||||
@@ -1,6 +1,4 @@
|
||||
p= t('email.register')
|
||||
extends ../layout.pug
|
||||
|
||||
hr
|
||||
small #{config.title} / #{config.description}
|
||||
br
|
||||
<a href='#{config.baseurl}'> #{config.baseurl}</a>
|
||||
block content
|
||||
p !{t('register.content', { user, config })}
|
||||
@@ -1 +1 @@
|
||||
| [#{config.title}] #{t('email.register.subject')}
|
||||
| [#{config.title}] #{t('register.subject')}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
p= t('email.register')
|
||||
p= t('register.content', { config, user })
|
||||
|
||||
hr
|
||||
small #{config.title} / #{config.description}
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
<!DOCTYPE html>
|
||||
html(lang="en")
|
||||
head
|
||||
meta(charset="UTF-8")
|
||||
meta(name="viewport", content="width=device-width, initial-scale=1.0")
|
||||
meta(http-equiv="X-UA-Compatible", content="ie=edge")
|
||||
title #{config.title}
|
||||
body
|
||||
|
||||
p !{t('email.user_confirm', { config, user })}
|
||||
extends ../layout.pug
|
||||
block content
|
||||
p !{t('confirm.content', { config, user })}
|
||||
|
||||
@@ -1 +1 @@
|
||||
= `[Gancio] Richiesta password recovery`
|
||||
| [#{config.title}] #{t('confirm.subject')}
|
||||
1
server/emails/user_confirm/text.pug
Normal file
1
server/emails/user_confirm/text.pug
Normal file
@@ -0,0 +1 @@
|
||||
p !{t('confirm.content', { config, user })}
|
||||
Reference in New Issue
Block a user