mail notification
This commit is contained in:
@@ -47,9 +47,9 @@ api.get('/event/meta', eventController.getMeta)
|
||||
// get unconfirmed events
|
||||
api.get('/event/unconfirmed', isAuth, isAdmin, eventController.getUnconfirmed)
|
||||
|
||||
// add event reminder
|
||||
api.post('/event/reminder', eventController.addReminder)
|
||||
// api.del('/event/reminder/:id', eventController.delReminder)
|
||||
// add event notification
|
||||
api.post('/event/notification', eventController.addNotification)
|
||||
api.delete('/event/del_notification/:code', eventController.delNotification)
|
||||
|
||||
// get event
|
||||
api.get('/event/:event_id', eventController.get)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
const { User, Event, Comment, Tag, Place, Reminder } = require('../model')
|
||||
const { User, Event, Comment, Tag, Place, Notification } = require('../model')
|
||||
const moment = require('moment')
|
||||
const { Op } = require('sequelize')
|
||||
const lodash = require('lodash')
|
||||
const crypto = require('crypto')
|
||||
|
||||
const eventController = {
|
||||
|
||||
async addComment (req, res) {
|
||||
@@ -23,7 +25,7 @@ const eventController = {
|
||||
res.json({ tags, places })
|
||||
},
|
||||
|
||||
async getReminders (event) {
|
||||
async getNotifications (event) {
|
||||
function match (event, filters) {
|
||||
// matches if no filter specified
|
||||
if (!filters.tags.length && !filters.places.length) return true
|
||||
@@ -37,10 +39,10 @@ const eventController = {
|
||||
}
|
||||
}
|
||||
}
|
||||
const reminders = await Reminder.findAll()
|
||||
const notifications = await Notification.findAll()
|
||||
|
||||
// get reminder that matches with selected event
|
||||
return reminders.filter(reminder => match(event, reminder.filters))
|
||||
// get notification that matches with selected event
|
||||
return notifications.filter(notification => match(event, notification.filters))
|
||||
},
|
||||
|
||||
async updateTag (req, res) {
|
||||
@@ -68,6 +70,11 @@ const eventController = {
|
||||
async confirm (req, res) {
|
||||
const id = req.params.event_id
|
||||
const event = await Event.findByPk(id)
|
||||
|
||||
// insert notification
|
||||
const notifications = await eventController.getNotifications(event)
|
||||
await event.setNotifications(notifications)
|
||||
|
||||
try {
|
||||
await event.update({ is_visible: true })
|
||||
res.send(200)
|
||||
@@ -87,14 +94,28 @@ const eventController = {
|
||||
res.json(events)
|
||||
},
|
||||
|
||||
async addReminder (req, res) {
|
||||
async addNotification (req, res) {
|
||||
try {
|
||||
await Reminder.create(req.body)
|
||||
const notification = req.body
|
||||
notification.remove_code = crypto.randomBytes(16).toString('hex')
|
||||
await Notification.create(req.body)
|
||||
res.sendStatus(200)
|
||||
} catch (e) {
|
||||
res.sendStatus(404)
|
||||
}
|
||||
},
|
||||
|
||||
async delNotification (req, res) {
|
||||
const remove_code = req.params.code
|
||||
try {
|
||||
const notification = await Notification.findOne({ where: { remove_code: { [Op.eq]: remove_code } } })
|
||||
await notification.destroy()
|
||||
} catch (e) {
|
||||
return res.send('Error')
|
||||
}
|
||||
res.send('Ok, notification removed')
|
||||
},
|
||||
|
||||
async getAll (req, res) {
|
||||
const start = moment().year(req.params.year).month(req.params.month).startOf('month').subtract(1, 'week')
|
||||
const end = moment().year(req.params.year).month(req.params.month).endOf('month').add(1, 'week')
|
||||
|
||||
@@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken')
|
||||
const Mastodon = require('mastodon-api')
|
||||
|
||||
const User = require('../models/user')
|
||||
const { Event, Tag, Place } = require('../models/event')
|
||||
const { Event, Tag, Place, Notification } = require('../models/event')
|
||||
const eventController = require('./event')
|
||||
const config = require('../config')
|
||||
const mail = require('../mail')
|
||||
@@ -105,9 +105,14 @@ const userController = {
|
||||
event.save()
|
||||
}
|
||||
|
||||
// insert reminder
|
||||
const reminders = await eventController.getReminders(event)
|
||||
await event.setReminders(reminders)
|
||||
if (req.user) {
|
||||
// insert notifications
|
||||
const notifications = await eventController.getNotifications(event)
|
||||
await event.setNotifications(notifications)
|
||||
} else {
|
||||
const notification = await Notification.create({ type: 'admin_email' })
|
||||
await event.setNotification(notification)
|
||||
}
|
||||
|
||||
return res.json(event)
|
||||
},
|
||||
|
||||
23
app/cron.js
23
app/cron.js
@@ -1,20 +1,23 @@
|
||||
const mail = require('./mail')
|
||||
const { Event, Reminder, EventReminder, User, Place, Tag } = require('./model')
|
||||
const { Event, Notification, EventNotification, 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 => {
|
||||
// get all event notification in queue
|
||||
const eventNotifications = await EventNotification.findAll()
|
||||
const promises = eventNotifications.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)
|
||||
const notification = await Notification.findByPk(e.notificationId)
|
||||
try {
|
||||
await mail.send(reminder.email, 'event', { event })
|
||||
if (notification.type === 'mail') {
|
||||
await mail.send(notification.email, 'event', { event })
|
||||
} else if (notification.type === 'mail_admin') {
|
||||
const admins = await User.findAll({ where: { is_admin: true } })
|
||||
await Promise.all(admins.map(admin =>
|
||||
mail.send(admin.email, 'event', { event, to_confirm: true, notification })))
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('DENTRO CATCH!', e)
|
||||
console.log('CATCH!', e)
|
||||
return false
|
||||
}
|
||||
return e.destroy()
|
||||
|
||||
@@ -6,5 +6,10 @@ br
|
||||
p #{event.description}
|
||||
|
||||
<a href="#{config.baseurl}/event/#{event.id}">#{config.baseurl}/event/#{event.id}</a>
|
||||
p #{event.tags.join(', ')}
|
||||
hr
|
||||
<a href="#{config.baseurl}">#{config.title} - #{config.description}</a>
|
||||
if to_confirm
|
||||
p Puoi confermare questo evento <a href="#{config.apiurl}/api/event/confirm/#{event.id}">#{config.apiurl}qui</a>
|
||||
else
|
||||
p Puoi eliminare queste notifiche <a href="#{confir.apiurl}/api/del_notification/#{notification.remove_code}">qui</a>
|
||||
<a href="#{config.baseurl}">#{config.title} - #{config.description}</a>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const User = require('./models/user')
|
||||
const { Event, Comment, Tag, Place, Reminder, EventReminder } = require('./models/event')
|
||||
const { Event, Comment, Tag, Place, Notification, EventNotification } = require('./models/event')
|
||||
|
||||
module.exports = { User, Event, Comment, Tag, Place, Reminder, EventReminder }
|
||||
module.exports = { User, Event, Comment, Tag, Place, Notification, EventNotification }
|
||||
|
||||
@@ -24,11 +24,14 @@ const Comment = db.define('comment', {
|
||||
text: Sequelize.STRING
|
||||
})
|
||||
|
||||
const Reminder = db.define('reminder', {
|
||||
const Notification = db.define('notification', {
|
||||
filters: Sequelize.JSON,
|
||||
email: Sequelize.STRING,
|
||||
notify_on_add: Sequelize.BOOLEAN,
|
||||
send_reminder: Sequelize.BOOLEAN
|
||||
remove_code: Sequelize.STRING,
|
||||
type: {
|
||||
type: Sequelize.ENUM,
|
||||
values: ['mail', 'admin_mail', 'activity_pub']
|
||||
}
|
||||
})
|
||||
|
||||
const Place = db.define('place', {
|
||||
@@ -42,9 +45,9 @@ 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 })
|
||||
const EventNotification = db.define('EventNotification')
|
||||
Event.belongsToMany(Notification, { through: EventNotification })
|
||||
Notification.belongsToMany(Event, { through: EventNotification })
|
||||
|
||||
Event.belongsTo(User)
|
||||
Event.belongsTo(Place)
|
||||
@@ -52,4 +55,4 @@ Event.belongsTo(Place)
|
||||
User.hasMany(Event)
|
||||
Place.hasMany(Event)
|
||||
|
||||
module.exports = { Event, Comment, Tag, Place, Reminder, EventReminder }
|
||||
module.exports = { Event, Comment, Tag, Place, Notification, EventNotification }
|
||||
|
||||
@@ -4,6 +4,7 @@ const bodyParser = require('body-parser')
|
||||
const api = require('./api')
|
||||
const cors = require('cors')
|
||||
const path = require('path')
|
||||
const config = require('./config')
|
||||
const port = process.env.PORT || 9000
|
||||
|
||||
app.set('views', path.join(__dirname, 'views'))
|
||||
@@ -19,4 +20,4 @@ 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)
|
||||
console.log(`[${config.title}] Started ${process.env.NODE_ENV} mode at ${config.baseurl} (api @ ${config.apiurl})`, config)
|
||||
|
||||
Reference in New Issue
Block a user