fix notifications

This commit is contained in:
les 2020-07-08 00:57:28 +02:00
parent 0a51c42fe0
commit 5a5c62efb4
5 changed files with 24 additions and 26 deletions

View file

@ -305,10 +305,11 @@ const eventController = {
// without waiting for the task manager // without waiting for the task manager
if (event.recurrent) { if (event.recurrent) {
eventController._createRecurrent() eventController._createRecurrent()
} } else {
// send notifications (mastodon / email) // send notifications (mastodon / email)
const notifier = require('../../notifier') const notifier = require('../../notifier')
notifier.notifyEvent('Create', event.id) notifier.notifyEvent('Create', event.id)
}
} catch (e) { } catch (e) {
debug(e) debug(e)
res.sendStatus(400) res.sendStatus(400)
@ -373,10 +374,10 @@ const eventController = {
// without waiting for the task manager // without waiting for the task manager
if (event.recurrent) { if (event.recurrent) {
eventController._createRecurrent() eventController._createRecurrent()
} } else {
const notifier = require('../../notifier') const notifier = require('../../notifier')
notifier.notifyEvent('Update', event.id) notifier.notifyEvent('Update', event.id)
}
}, },
async remove (req, res) { async remove (req, res) {

View file

@ -7,6 +7,7 @@ const sequelize = require('./index')
const Resource = require('./resource') const Resource = require('./resource')
const Notification = require('./notification') const Notification = require('./notification')
const EventNotification = require('./eventnotification')
const Place = require('./place') const Place = require('./place')
const User = require('./user') const User = require('./user')
const Tag = require('./tag') const Tag = require('./tag')
@ -43,13 +44,16 @@ Event.belongsTo(Place)
Place.hasMany(Event) Place.hasMany(Event)
Event.belongsTo(User) Event.belongsTo(User)
User.hasMany(Event)
Event.belongsToMany(Tag, { through: 'event_tags' }) Event.belongsToMany(Tag, { through: 'event_tags' })
Event.belongsToMany(Notification, { through: 'event_notification' }) Event.belongsToMany(Notification, { through: EventNotification })
Notification.belongsToMany(Event, { through: 'event_notification' }) Notification.belongsToMany(Event, { through: EventNotification })
Event.hasMany(Resource) Event.hasMany(Resource)
Resource.belongsTo(Event) Resource.belongsTo(Event)
Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' }) Event.hasMany(Event, { as: 'child', foreignKey: 'parentId' })
Event.belongsTo(Event, { as: 'parent' }) Event.belongsTo(Event, { as: 'parent' })
@ -62,7 +66,7 @@ Event.prototype.toAP = function (username, locale, to = []) {
${plainDescription} ${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 name: this.place && this.place.name
}, },
attachment, attachment,
tag: tags.map(tag => ({ tag: tags && tags.map(tag => ({
type: 'Hashtag', type: 'Hashtag',
name: '#' + tag, name: '#' + tag,
href: '/tags/' + tag href: '/tags/' + tag

View file

@ -1,7 +1,6 @@
const sequelize = require('./index') const sequelize = require('./index')
const { Model, DataTypes } = require('sequelize') const { Model, DataTypes } = require('sequelize')
// const Event = require('./event')
class Notification extends Model {} class Notification extends Model {}
@ -27,6 +26,4 @@ Notification.init({
}] }]
}) })
// Notification.belongsToMany(Event, { through: 'event_notification' })
module.exports = Notification module.exports = Notification

View file

@ -3,8 +3,6 @@ const bcrypt = require('bcryptjs')
const { Model, DataTypes } = require('sequelize') const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index') const sequelize = require('./index')
// const Event = require('./event')
class User extends Model {} class User extends Model {}
User.init({ User.init({
@ -36,8 +34,6 @@ User.init({
} }
}) })
// User.hasMany(Event)
User.prototype.comparePassword = async function (pwd) { User.prototype.comparePassword = async function (pwd) {
if (!this.password) { return false } if (!this.password) { return false }
const ret = await bcrypt.compare(pwd, this.password) const ret = await bcrypt.compare(pwd, this.password)

View file

@ -4,11 +4,11 @@ const debug = require('debug')('notifier')
const fediverseHelpers = require('./federation/helpers') const fediverseHelpers = require('./federation/helpers')
const Event = require('./api/models/event') const Event = require('./api/models/event')
const Notification = require('./api/models/event') const Notification = require('./api/models/notification')
const EventNotification = require('./api/models/event') const EventNotification = require('./api/models/eventnotification')
const User = require('./api/models/event') const User = require('./api/models/user')
const Place = require('./api/models/event') const Place = require('./api/models/place')
const Tag = require('./api/models/event') const Tag = require('./api/models/tag')
const eventController = require('./api/controller/event') const eventController = require('./api/controller/event')
@ -35,7 +35,7 @@ const notifier = {
async notifyEvent (action, eventId) { async notifyEvent (action, eventId) {
const event = await Event.findByPk(eventId, { const event = await Event.findByPk(eventId, {
include: [Tag, Place, Notification, { model: User }] include: [Tag, Place, Notification, User]
}) })
debug('%s -> %s', action, event.title) debug('%s -> %s', action, event.title)
@ -47,7 +47,7 @@ const notifier = {
const promises = event_notifications.map(async notification => { const promises = event_notifications.map(async notification => {
try { try {
// await notification.event_notification.update({ status: 'sending' }) await notification.event_notification.update({ status: 'sending' })
await notifier.sendNotification(notification, event) await notifier.sendNotification(notification, event)
notification.event_notification.status = 'sent' notification.event_notification.status = 'sent'
} catch (err) { } catch (err) {
@ -71,9 +71,9 @@ const notifier = {
e.status = 'sent' e.status = 'sent'
return e.save() return e.save()
} catch (err) { } catch (err) {
console.error(err) debug(err)
e.status = 'error' e.status = 'error'
// e.error = err e.error = err
return e.save() return e.save()
} }
}) })