[feat] add actions to notifications (add/del/update)

This commit is contained in:
les
2019-10-02 21:04:03 +02:00
parent d3febcdc05
commit 4fe78aa345
9 changed files with 131 additions and 74 deletions

View File

@@ -2,17 +2,16 @@ const crypto = require('crypto')
const moment = require('moment')
const { Op } = require('sequelize')
const lodash = require('lodash')
const { event: Event, comment: Comment, tag: Tag, place: Place, user: User, notification: Notification } = require('../models')
const { event: Event, comment: Comment, tag: Tag, place: Place,
user: User, notification: Notification, event_notification: EventNotification } = require('../models')
const Sequelize = require('sequelize')
const notifier = require('../../notifier')
const federation = require('../../federation/helpers')
const debug = require('debug')('controller:event')
const eventController = {
// NOT USED ANYWHERE, comments are added from fediverse
// NOT USED ANYWHERE, comments are added from fediverse, should we remove this?
async addComment (req, res) {
// comment could be added to an event or to another comment
// comments could be added to an event or to another comment
let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
if (!event) {
const comment = await Comment.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
@@ -44,7 +43,8 @@ const eventController = {
res.json({ tags, places })
},
async getNotifications (event) {
async getNotifications (event, action) {
debug('getNotifications "%s" (%s)', event.title, action)
function match (event, filters) {
// matches if no filter specified
if (!filters) { return true }
@@ -64,10 +64,12 @@ const eventController = {
}
}
}
const notifications = await Notification.findAll()
const notifications = await Notification.findAll({ where: { action }, include: [ Event ] })
// get notification that matches with selected event
return notifications.filter(notification => match(event, notification.filters))
const ret = notifications.filter(notification => match(event, notification.filters))
return ret
},
async updateTag (req, res) {
@@ -123,8 +125,8 @@ const eventController = {
res.sendStatus(200)
// send notification
// notifier.notifyEvent(event.id)
// federation.sendEvent(event, req.user)
const notifier = require('../../notifier')
notifier.notifyEvent('Create', event.id)
} catch (e) {
res.sendStatus(404)
}

View File

@@ -32,7 +32,7 @@ const exportController = {
},
include: [ { model: Tag, ...where_tags }, { model: Place, attributes: ['name', 'id', 'address'] }]
})
switch (type) {
case 'rss':
case 'feed':

View File

@@ -8,9 +8,6 @@ const config = require('config')
const mail = require('../mail')
const { user: User, event: Event, tag: Tag, place: Place, fed_users: FedUsers } = require('../models')
const settingsController = require('./settings')
const federation = require('../../federation/helpers')
const util = require('util')
const generateKeyPair = util.promisify(crypto.generateKeyPair)
const debug = require('debug')('user:controller')
const userController = {
@@ -56,12 +53,14 @@ const userController = {
try {
console.error('media files not removed')
// TOFIX
// await fs.unlink(old_path)
// await fs.unlink(old_thumb_path)
await fs.unlink(old_thumb_path)
await fs.unlink(old_path)
} catch (e) {
console.error(e)
debug(e)
}
}
const notifier = require('../../notifier')
await notifier.notifyEvent('Delete', event.id)
await event.destroy()
res.sendStatus(200)
} else {
@@ -75,12 +74,11 @@ const userController = {
const eventDetails = {
title: body.title,
// remove html tag
// remove html tags
description: body.description ? body.description.replace(/(<([^>]+)>)/ig, '') : '',
multidate: body.multidate,
start_datetime: body.start_datetime,
end_datetime: body.end_datetime,
recurrent: body.recurrent,
// publish this event only if authenticated
is_visible: !!req.user
@@ -101,8 +99,9 @@ const userController = {
await event.setPlace(place)
event.place = place
} catch (e) {
console.error(e)
debug(e)
}
// create/assign tags
if (body.tags) {
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
@@ -112,21 +111,18 @@ const userController = {
event.tags = tags
}
// associate user to event and reverse
if (req.user) {
await req.user.addEvent(event)
await event.setUser(req.user)
}
// send response to client
// return created event to the client
res.json(event)
const user = await User.findByPk(req.user.id, { include: { model: FedUsers, as: 'followers' }})
if (user) { federation.sendEvent(event, user) }
// res.sendStatus(200)
// send notification (mastodon/email/confirmation)
// notifier.notifyEvent(event.id)
// send notification (mastodon/email)
const notifier = require('../../notifier')
notifier.notifyEvent('Create', event.id)
},
async updateEvent (req, res) {
@@ -167,6 +163,8 @@ const userController = {
}
const newEvent = await Event.findByPk(event.id, { include: [Tag, Place] })
res.json(newEvent)
const notifier = require('../../notifier')
notifier.notifyEvent('Update', event.id)
},
async forgotPassword (req, res) {

View File

@@ -22,7 +22,7 @@ const mail = {
}
},
message: {
from: `${config.title} <${config.admin_email}>`
from: `📅 ${config.title} <${config.admin_email}>`
},
send: true,
i18n: {
@@ -32,7 +32,7 @@ const mail = {
updateFiles: false,
defaultLocale: settings.locale,
locale: settings.locale,
locales: ['it', 'es']
locales: ['it', 'es'] // TOFIX
},
transport: config.smtp
})
@@ -44,9 +44,9 @@ const mail = {
},
locals: {
...locals,
locale: 'it',
locale: 'it', // TOFIX
config: { title: config.title, baseurl: config.baseurl, description: config.description },
datetime: datetime => moment(datetime).format('ddd, D MMMM HH:mm')
datetime: datetime => moment.unix(datetime).utc(false).format('ddd, D MMMM HH:mm')
}
}
return email.send(msg)

View File

@@ -10,5 +10,5 @@ module.exports = (sequelize, DataTypes) => {
}
}, {})
return event_notification
return event_notification
}

View File

@@ -4,13 +4,23 @@ module.exports = (sequelize, DataTypes) => {
filters: DataTypes.JSON,
email: DataTypes.STRING,
remove_code: DataTypes.STRING,
action: {
type: DataTypes.ENUM,
values: ['Create', 'Update', 'Delete']
},
type: {
type: DataTypes.ENUM,
values: ['mail', 'admin_email', 'mastodon']
values: ['mail', 'admin_email', 'ap']
}
}, {})
}, {
indexes: [{
unique: true,
fields: ['action', 'type']
}]
})
notification.associate = function (models) {
notification.belongsToMany(models.event, { through: models.event_notification })
notification.belongsToMany(models.event, { through: 'event_notification' })
// associations can be defined here
}
return notification