pm2 / cron worker to send reminder

This commit is contained in:
lesion
2019-03-10 01:01:23 +01:00
parent e41de7208c
commit 6ed639d94b
39 changed files with 264 additions and 105 deletions

View File

@@ -4,16 +4,10 @@ const eventController = require('./controller/event')
const exportController = require('./controller/export')
const userController = require('./controller/user')
// const botController = require('./controller/bot')
const path = require('path')
const multer = require('multer')
const crypto = require('crypto')
const storage = require('./storage')({
destination: 'uploads/',
filename: (req, file, cb) => {
cb(null, crypto.randomBytes(16).toString('hex') + path.extname(file.originalname))
}
destination: 'uploads/'
})
const upload = multer({ storage })
const api = express.Router()

View File

@@ -1,26 +1,24 @@
const jwt = require('jsonwebtoken')
const config = require('./config')
const User = require('./models/user')
const { Op } = require('sequelize')
const Auth = {
async fillUser (req, res, next) {
const token = req.body.token || req.params.token || req.headers['x-access-token']
console.log('[AUTH] ', token)
if (!token) next()
if (!token) return next()
jwt.verify(token, config.secret, async (err, decoded) => {
if (err) next()
req.user = await User.findOne({ where: { email: decoded.email, is_active: true } })
if (err) return next()
req.user = await User.findOne({ where: { email: { [Op.eq]: decoded.email }, is_active: true } })
next()
})
},
async isAuth (req, res, next) {
const token = req.body.token || req.params.token || req.headers['x-access-token']
console.log('[AUTH] ', token)
if (!token) return res.status(403).send({ message: 'Token not found' })
jwt.verify(token, config.secret, async (err, decoded) => {
if (err) return res.status(403).send({ message: 'Failed to authenticate token ' + err })
console.log('DECODED TOKEN', decoded)
req.user = await User.findOne({ where: { email: decoded.email, is_active: true } })
req.user = await User.findOne({ where: { email: { [Op.eq]: decoded.email }, is_active: true } })
if (!req.user) return res.status(403).send({ message: 'Failed to authenticate token ' + err })
next()
})

View File

@@ -20,9 +20,9 @@ if (process.env.NODE_ENV === 'production') {
}
module.exports = {
locale: 'en',
locale: 'it',
title: process.env.TITLE || 'Put here your site name',
title: process.env.TITLE || 'GANCIO',
description: process.env.DESCRIPTION || 'A calendar for radical communities',
baseurl: process.env.BASE_URL || 'http://localhost:8080',

View File

@@ -1,14 +1,14 @@
const { User, Event, Comment, Tag, Place, MailReminder } = require('../model')
const { User, Event, Comment, Tag, Place, Reminder } = require('../model')
const moment = require('moment')
const Sequelize = require('sequelize')
const { Op } = require('sequelize')
const lodash = require('lodash')
const eventController = {
async addComment (req, res) {
// comment could be added to an event or to another comment
let event = await Event.findOne({ where: { activitypub_id: req.body.id } })
let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
if (!event) {
const comment = await Comment.findOne({ where: { activitypub_id: req.body.id }, include: Event })
const comment = await Comment.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
event = comment.event
}
const comment = new Comment(req.body)
@@ -23,6 +23,26 @@ const eventController = {
res.json({ tags, places })
},
async getReminders (event) {
function match (event, filters) {
// matches if no filter specified
if (!filters.tags.length && !filters.places.length) return true
if (filters.tags.length) {
const m = lodash.intersection(event.tags.map(t => t.tag), filters.tags)
if (m.length > 0) return true
}
if (filters.places.length) {
if (filters.places.find(p => p === event.place.name)) {
return true
}
}
}
const reminders = await Reminder.findAll()
// get reminder that matches with selected event
return reminders.filter(reminder => match(event, reminder.filters))
},
async updateTag (req, res) {
const tag = await Tag.findByPk(req.body.tag)
console.log(tag)
@@ -68,8 +88,12 @@ const eventController = {
},
async addReminder (req, res) {
await MailReminder.create(req.body.reminder)
res.json(200)
try {
await Reminder.create(req.body)
res.sendStatus(200)
} catch (e) {
res.sendStatus(404)
}
},
async getAll (req, res) {
const start = moment().year(req.params.year).month(req.params.month).startOf('month').subtract(1, 'week')
@@ -77,9 +101,9 @@ const eventController = {
const events = await Event.findAll({
where: {
is_visible: true,
[Sequelize.Op.and]: [
{ start_datetime: { [Sequelize.Op.gte]: start } },
{ start_datetime: { [Sequelize.Op.lte]: end } }
[Op.and]: [
{ start_datetime: { [Op.gte]: start } },
{ start_datetime: { [Op.lte]: end } }
]
},
order: [['start_datetime', 'ASC']],

View File

@@ -1,5 +1,5 @@
const { Event, Comment, Tag, Place } = require('../model')
const Sequelize = require('sequelize')
const { Op } = require('sequelize')
const config = require('../config')
const moment = require('moment')
const ics = require('ics')
@@ -21,7 +21,7 @@ const exportController = {
}
const events = await Event.findAll({
order: [['start_datetime', 'ASC']],
where: { start_datetime: { [Sequelize.Op.gte]: yesterday } },
where: { start_datetime: { [Op.gte]: yesterday } },
include: [Comment, {
model: Tag,
where: whereTag

View File

@@ -3,14 +3,16 @@ const Mastodon = require('mastodon-api')
const User = require('../models/user')
const { Event, Tag, Place } = require('../models/event')
const eventController = require('./event')
const config = require('../config')
const mail = require('../mail')
const bot = require('./bot')
const { Op } = require('sequelize')
const userController = {
async login (req, res) {
// find the user
const user = await User.findOne({ where: { email: req.body.email } })
const user = await User.findOne({ where: { email: { [Op.eq]: req.body.email } } })
if (!user) {
res.status(404).json({ success: false, message: 'AUTH_FAIL' })
} else if (user) {
@@ -82,6 +84,7 @@ const userController = {
} catch (e) {
console.log(e)
}
let event = await Event.create(eventDetails)
await event.setPlace(place)
@@ -89,7 +92,7 @@ const userController = {
console.log(body.tags)
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
const tags = await Tag.findAll({ where: { tag: body.tags } })
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
await event.addTags(tags)
}
if (req.user) await req.user.addEvent(event)
@@ -102,7 +105,9 @@ const userController = {
event.save()
}
mail.send(config.admin, 'event', { event })
// insert reminder
const reminders = await eventController.getReminders(event)
await event.setReminders(reminders)
return res.json(event)
},
@@ -121,7 +126,7 @@ const userController = {
await event.update(body)
let place
try {
place = await Place.findOrCreate({ where: { name: body.place_name },
place = await Place.findOrCreate({ where: { name: { [Op.eq]: body.place_name } },
defaults: { address: body.place_address } })
.spread((place, created) => place)
} catch (e) {
@@ -132,7 +137,7 @@ const userController = {
console.log(body.tags)
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
const tags = await Tag.findAll({ where: { tag: body.tags } })
const tags = await Tag.findAll({ where: { tag: { [Op.eq]: body.tags } } })
await event.addTags(tags)
}
const newEvent = await Event.findByPk(event.id, { include: [User, Tag, Place] })

27
app/cron.js Normal file
View File

@@ -0,0 +1,27 @@
const mail = require('./mail')
const { Event, Reminder, EventReminder, User, Place, Tag } = require('./model')
async function loop () {
console.log('nel loop')
// get all event reminder in queue
const eventReminders = await EventReminder.findAll()
const promises = eventReminders.map(async e => {
const event = await Event.findByPk(e.eventId, { include: [User, Place, Tag] })
console.log('EVENT ')
console.log(event)
if (!event.place) return
const reminder = await Reminder.findByPk(e.reminderId)
try {
await mail.send(reminder.email, 'event', { event })
} catch (e) {
console.log('DENTRO CATCH!', e)
return false
}
return e.destroy()
})
return Promise.all(promises)
}
setInterval(loop, 20000)
loop()

10
app/emails/event/html.pug Normal file
View File

@@ -0,0 +1,10 @@
h3 #{event.title}
p Dove: #{event.place.name} - #{event.place.address}
p Quando: #{datetime(event.start_datetime)}
br
<img style="width: 100%" src="#{config.apiurl}/#{event.image_path}" />
p #{event.description}
<a href="#{config.baseurl}/event/#{event.id}">#{config.baseurl}/event/#{event.id}</a>
hr
<a href="#{config.baseurl}">#{config.title} - #{config.description}</a>

View File

@@ -0,0 +1 @@
= `[${config.title}] ${event.title} @${event.place.name} ${datetime(event.start_datetime)}`

8
app/emails/mail.css Normal file
View File

@@ -0,0 +1,8 @@
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid #555;
}

View File

@@ -0,0 +1,6 @@
h4 Gancio
p= t('registration_email')
small --
small https://cisti.org

View File

@@ -0,0 +1 @@
= `[Gancio] Richiesta registrazione`

3
app/locales/en.json Normal file
View File

@@ -0,0 +1,3 @@
{
"registration_email": "Ciao, la tua registrazione sarà confermata nei prossimi giorni. Riceverai una conferma non temere."
}

3
app/locales/es.json Normal file
View File

@@ -0,0 +1,3 @@
{
"registration_email": "registration_email"
}

3
app/locales/it.json Normal file
View File

@@ -0,0 +1,3 @@
{
"registration_email": "Ciao, la tua registrazione sarà confermata nei prossimi giorni. Riceverai una conferma non temere."
}

3
app/locales/zh.json Normal file
View File

@@ -0,0 +1,3 @@
{
"registration_email": "registration_email"
}

View File

@@ -1,16 +1,19 @@
const Email = require('email-templates')
const path = require('path')
const config = require('./config')
const moment = require('moment')
moment.locale('it')
const mail = {
send (addresses, template, locals) {
locals.locale = config.locale
const email = new Email({
views: { root: path.join(__dirname, 'emails') },
juice: true,
juiceResources: {
preserveImportant: true,
webResources: {
relativeTo: path.join(__dirname, '..', 'emails')
relativeTo: path.join(__dirname, 'emails')
}
},
message: {
@@ -26,7 +29,7 @@ const mail = {
to: addresses,
bcc: config.admin
},
locals
locals: { ...locals, config, datetime: datetime => moment(datetime).format('ddd, D MMMM HH:mm') }
})
}
}

View File

@@ -1,4 +1,4 @@
const User = require('./models/user')
const { Event, Comment, Tag, Place, MailReminder } = require('./models/event')
const { Event, Comment, Tag, Place, Reminder, EventReminder } = require('./models/event')
module.exports = { User, Event, Comment, Tag, Place, MailReminder }
module.exports = { User, Event, Comment, Tag, Place, Reminder, EventReminder }

View File

@@ -24,10 +24,10 @@ const Comment = db.define('comment', {
text: Sequelize.STRING
})
const MailReminder = db.define('reminder', {
const Reminder = db.define('reminder', {
filters: Sequelize.JSON,
mail: Sequelize.STRING,
send_on_add: Sequelize.BOOLEAN,
email: Sequelize.STRING,
notify_on_add: Sequelize.BOOLEAN,
send_reminder: Sequelize.BOOLEAN
})
@@ -42,10 +42,14 @@ Event.hasMany(Comment)
Event.belongsToMany(Tag, { through: 'tagEvent' })
Tag.belongsToMany(Event, { through: 'tagEvent' })
const EventReminder = db.define('EventReminder')
Event.belongsToMany(Reminder, { through: EventReminder })
Reminder.belongsToMany(Event, { through: EventReminder })
Event.belongsTo(User)
Event.belongsTo(Place)
User.hasMany(Event)
Place.hasMany(Event)
module.exports = { Event, Comment, Tag, Place, MailReminder }
module.exports = { Event, Comment, Tag, Place, Reminder, EventReminder }

22
app/server.js Normal file
View File

@@ -0,0 +1,22 @@
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const api = require('./api')
const cors = require('cors')
const path = require('path')
const port = process.env.PORT || 9000
app.set('views', path.join(__dirname, 'views'))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cors())
app.use('/static', express.static(path.join(__dirname, 'uploads')))
app.use('/uploads', express.static('uploads'))
app.use('/api', api)
app.use('/', express.static(path.join(__dirname, '..', 'client', 'dist')))
app.use('/css', express.static(path.join(__dirname, '..', 'client', 'dist', 'css')))
app.use('/js', express.static(path.join(__dirname, '..', 'client', 'dist', 'js')))
app.use('*', express.static(path.join(__dirname, '..', 'client', 'dist', 'index.html')))
app.listen(port)
console.log('Magic happens at http://localhost:' + port)

56
app/storage.js Normal file
View File

@@ -0,0 +1,56 @@
const fs = require('fs')
const os = require('os')
const path = require('path')
const crypto = require('crypto')
const mkdirp = require('mkdirp')
const sharp = require('sharp')
function getDestination (req, file, cb) {
cb(null, os.tmpdir())
}
function DiskStorage (opts) {
if (typeof opts.destination === 'string') {
mkdirp.sync(opts.destination)
this.getDestination = function ($0, $1, cb) { cb(null, opts.destination) }
} else {
this.getDestination = (opts.destination || getDestination)
}
}
DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
var that = this
that.getDestination(req, file, function (err, destination) {
if (err) return cb(err)
const filename = crypto.randomBytes(16).toString('hex') + '.webp'
const finalPath = path.join(destination, filename)
const outStream = fs.createWriteStream(finalPath)
const resizer = sharp().resize(800).webp()
file.stream.pipe(resizer).pipe(outStream)
outStream.on('error', cb)
outStream.on('finish', function () {
cb(null, {
destination,
filename,
path: finalPath,
size: outStream.bytesWritten
})
})
})
}
DiskStorage.prototype._removeFile = function _removeFile (req, file, cb) {
var path = file.path
delete file.destination
delete file.filename
delete file.path
fs.unlink(path, cb)
}
module.exports = function (opts) {
return new DiskStorage(opts)
}

24
app/views/feed/rss.pug Normal file
View File

@@ -0,0 +1,24 @@
doctype xml
rss(version='2.0', xmlns:atom='<a href="http://www.w3.org/2005/Atom" rel="nofollow">http://www.w3.org/2005/Atom</a>')
channel
title #{config.title}
link <a href="#{config.baseurl}" rel="nofollow">#{config.baseurl}</a>
<atom:link href="#{config.apiurl}/export/feed/rss" rel='self' type='application/rss+xml' />
description #{config.description}
language #{config.locale}
//- if events.length
lastBuildDate= new Date(posts[0].publishedAt).toUTCString()
each event in events
item
title= event.title
link <a href="#{config.baseurl}/event/#{event.id}" rel="nofollow">#{config.baseurl}/event/#{event.id}</a>
description
| <![CDATA[
| <h4>#{event.title}</h4>
| <strong>#{event.place.name} - #{event.place.address}</strong>
| #{moment(event.start_datetime).format("ddd, D MMMM HH:mm")}<br/>
| <img src="#{config.apiurl}/#{event.image_path}"/>
| <pre>!{event.description}</pre>
| ]]>
pubDate= new Date(event.createdAt).toUTCString()
guid(isPermaLink='false') <a href="#{config.baseurl}/event/#{event.id}" rel="nofollow">#{config.baseurl}/event/#{event.id}</a>