From 5a5c62efb45f56693373c58b0252fc9542a193de Mon Sep 17 00:00:00 2001 From: les Date: Wed, 8 Jul 2020 00:57:28 +0200 Subject: [PATCH] fix notifications --- server/api/controller/event.js | 13 +++++++------ server/api/models/event.js | 12 ++++++++---- server/api/models/notification.js | 3 --- server/api/models/user.js | 4 ---- server/notifier.js | 18 +++++++++--------- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/server/api/controller/event.js b/server/api/controller/event.js index bead4162..ea96b2f8 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -305,10 +305,11 @@ const eventController = { // without waiting for the task manager if (event.recurrent) { eventController._createRecurrent() + } else { + // send notifications (mastodon / email) + const notifier = require('../../notifier') + notifier.notifyEvent('Create', event.id) } - // send notifications (mastodon / email) - const notifier = require('../../notifier') - notifier.notifyEvent('Create', event.id) } catch (e) { debug(e) res.sendStatus(400) @@ -373,10 +374,10 @@ const eventController = { // without waiting for the task manager if (event.recurrent) { eventController._createRecurrent() + } else { + const notifier = require('../../notifier') + notifier.notifyEvent('Update', event.id) } - - const notifier = require('../../notifier') - notifier.notifyEvent('Update', event.id) }, async remove (req, res) { diff --git a/server/api/models/event.js b/server/api/models/event.js index bc4d40af..9f676c7e 100644 --- a/server/api/models/event.js +++ b/server/api/models/event.js @@ -7,6 +7,7 @@ const sequelize = require('./index') const Resource = require('./resource') const Notification = require('./notification') +const EventNotification = require('./eventnotification') const Place = require('./place') const User = require('./user') const Tag = require('./tag') @@ -43,13 +44,16 @@ Event.belongsTo(Place) Place.hasMany(Event) Event.belongsTo(User) +User.hasMany(Event) + Event.belongsToMany(Tag, { through: 'event_tags' }) -Event.belongsToMany(Notification, { through: 'event_notification' }) -Notification.belongsToMany(Event, { through: 'event_notification' }) +Event.belongsToMany(Notification, { through: EventNotification }) +Notification.belongsToMany(Event, { through: EventNotification }) Event.hasMany(Resource) Resource.belongsTo(Event) + Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' }) Event.belongsTo(Event, { as: 'parent' }) @@ -62,7 +66,7 @@ Event.prototype.toAP = function (username, locale, to = []) { ${plainDescription} - ${tags.map(t => `#${t}`)} + ${tags && tags.map(t => `#${t}`)} ` @@ -90,7 +94,7 @@ Event.prototype.toAP = function (username, locale, to = []) { name: this.place && this.place.name }, attachment, - tag: tags.map(tag => ({ + tag: tags && tags.map(tag => ({ type: 'Hashtag', name: '#' + tag, href: '/tags/' + tag diff --git a/server/api/models/notification.js b/server/api/models/notification.js index d9acb8c7..3f212f5a 100644 --- a/server/api/models/notification.js +++ b/server/api/models/notification.js @@ -1,7 +1,6 @@ const sequelize = require('./index') const { Model, DataTypes } = require('sequelize') -// const Event = require('./event') class Notification extends Model {} @@ -27,6 +26,4 @@ Notification.init({ }] }) -// Notification.belongsToMany(Event, { through: 'event_notification' }) - module.exports = Notification diff --git a/server/api/models/user.js b/server/api/models/user.js index e86b4131..95c06b7e 100644 --- a/server/api/models/user.js +++ b/server/api/models/user.js @@ -3,8 +3,6 @@ const bcrypt = require('bcryptjs') const { Model, DataTypes } = require('sequelize') const sequelize = require('./index') -// const Event = require('./event') - class User extends Model {} User.init({ @@ -36,8 +34,6 @@ User.init({ } }) -// User.hasMany(Event) - User.prototype.comparePassword = async function (pwd) { if (!this.password) { return false } const ret = await bcrypt.compare(pwd, this.password) diff --git a/server/notifier.js b/server/notifier.js index c108b955..0dcef354 100644 --- a/server/notifier.js +++ b/server/notifier.js @@ -4,11 +4,11 @@ const debug = require('debug')('notifier') const fediverseHelpers = require('./federation/helpers') const Event = require('./api/models/event') -const Notification = require('./api/models/event') -const EventNotification = require('./api/models/event') -const User = require('./api/models/event') -const Place = require('./api/models/event') -const Tag = require('./api/models/event') +const Notification = require('./api/models/notification') +const EventNotification = require('./api/models/eventnotification') +const User = require('./api/models/user') +const Place = require('./api/models/place') +const Tag = require('./api/models/tag') const eventController = require('./api/controller/event') @@ -35,7 +35,7 @@ const notifier = { async notifyEvent (action, eventId) { const event = await Event.findByPk(eventId, { - include: [Tag, Place, Notification, { model: User }] + include: [Tag, Place, Notification, User] }) debug('%s -> %s', action, event.title) @@ -47,7 +47,7 @@ const notifier = { const promises = event_notifications.map(async notification => { try { - // await notification.event_notification.update({ status: 'sending' }) + await notification.event_notification.update({ status: 'sending' }) await notifier.sendNotification(notification, event) notification.event_notification.status = 'sent' } catch (err) { @@ -71,9 +71,9 @@ const notifier = { e.status = 'sent' return e.save() } catch (err) { - console.error(err) + debug(err) e.status = 'error' - // e.error = err + e.error = err return e.save() } })