Files
gancio/app/controller/event.js

163 lines
4.5 KiB
JavaScript
Raw Normal View History

2019-03-11 00:20:37 +01:00
const { User, Event, Comment, Tag, Place, Notification } = require('../model')
2019-02-26 00:02:42 +01:00
const moment = require('moment')
2019-03-10 01:01:23 +01:00
const { Op } = require('sequelize')
const lodash = require('lodash')
2019-03-11 00:20:37 +01:00
const crypto = require('crypto')
2019-02-26 00:02:42 +01:00
const eventController = {
async addComment (req, res) {
// comment could be added to an event or to another comment
2019-03-10 01:01:23 +01:00
let event = await Event.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } } })
2019-02-26 00:02:42 +01:00
if (!event) {
2019-03-10 01:01:23 +01:00
const comment = await Comment.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
2019-02-26 00:02:42 +01:00
event = comment.event
}
const comment = new Comment(req.body)
event.addComment(comment)
res.json(comment)
},
2019-02-26 01:17:52 +01:00
async getMeta (req, res) {
2019-02-26 00:02:42 +01:00
const places = await Place.findAll()
const tags = await Tag.findAll()
2019-02-26 01:17:52 +01:00
res.json({ tags, places })
2019-02-26 00:02:42 +01:00
},
2019-03-11 00:20:37 +01:00
async getNotifications (event) {
2019-03-10 01:01:23 +01:00
function match (event, filters) {
// matches if no filter specified
if (!filters) return true
2019-03-20 01:52:48 +01:00
// check for visibility
if (typeof filters.is_visible !== 'undefined' && filters.is_visible !== event.is_visible) return false
2019-03-20 01:52:48 +01:00
if (!filters.tags && !filters.places) return true
2019-03-10 01:01:23 +01:00
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
}
}
}
2019-03-11 00:20:37 +01:00
const notifications = await Notification.findAll()
2019-03-10 01:01:23 +01:00
2019-03-11 00:20:37 +01:00
// get notification that matches with selected event
return notifications.filter(notification => match(event, notification.filters))
2019-03-10 01:01:23 +01:00
},
2019-02-26 00:02:42 +01:00
async updateTag (req, res) {
const tag = await Tag.findByPk(req.body.tag)
if (tag) {
res.json(await tag.update(req.body))
} else {
2019-03-22 00:49:47 +01:00
res.sendStatus(404)
2019-02-26 00:02:42 +01:00
}
},
2019-03-07 14:59:28 +01:00
2019-03-03 01:04:24 +01:00
async updatePlace (req, res) {
const place = await Place.findByPk(req.body.id)
await place.update(req.body)
res.json(place)
},
2019-03-07 14:59:28 +01:00
2019-02-26 01:17:52 +01:00
async get (req, res) {
2019-02-26 00:02:42 +01:00
const id = req.params.event_id
2019-02-26 01:17:52 +01:00
const event = await Event.findByPk(id, { include: [User, Tag, Comment, Place] })
2019-02-26 00:02:42 +01:00
res.json(event)
},
2019-03-07 14:59:28 +01:00
async confirm (req, res) {
const id = req.params.event_id
const event = await Event.findByPk(id)
2019-03-11 00:20:37 +01:00
2019-03-07 14:59:28 +01:00
try {
await event.update({ is_visible: true })
2019-03-20 01:52:48 +01:00
// insert notification
const notifications = await eventController.getNotifications(event)
await event.setNotifications(notifications)
2019-03-22 00:20:47 +01:00
res.sendStatus(200)
2019-03-07 14:59:28 +01:00
} catch (e) {
2019-03-22 00:20:47 +01:00
res.sendStatus(404)
2019-03-07 14:59:28 +01:00
}
},
async unconfirm (req, res) {
const id = req.params.event_id
const event = await Event.findByPk(id)
try {
await event.update({ is_visible: false })
2019-03-22 00:49:47 +01:00
res.sendStatus(200)
} catch (e) {
2019-03-22 00:49:47 +01:00
res.sendStatus(404)
}
},
2019-03-07 14:59:28 +01:00
async getUnconfirmed (req, res) {
const events = await Event.findAll({
where: {
is_visible: false
},
order: [['start_datetime', 'ASC']],
include: [Tag, Place]
})
res.json(events)
},
2019-03-11 00:20:37 +01:00
async addNotification (req, res) {
2019-03-10 01:01:23 +01:00
try {
const notification = {
2019-03-22 00:49:47 +01:00
filters: { is_visible: true },
email: req.body.email,
type: 'mail',
remove_code: crypto.randomBytes(16).toString('hex')
}
await Notification.create(notification)
2019-03-10 01:01:23 +01:00
res.sendStatus(200)
} catch (e) {
res.sendStatus(404)
}
2019-03-07 14:59:28 +01:00
},
2019-03-11 00:20:37 +01:00
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) {
2019-03-22 00:49:47 +01:00
return res.sendStatus(404)
2019-03-11 00:20:37 +01:00
}
2019-03-22 00:49:47 +01:00
res.sendStatus(200)
2019-03-11 00:20:37 +01:00
},
2019-02-26 00:02:42 +01:00
async getAll (req, res) {
// this is due how v-calendar shows dates
let start = moment().year(req.params.year).month(req.params.month)
.startOf('month').startOf('isoWeek')
let end = moment().year(req.params.year).month(req.params.month).endOf('month')
const shownDays = end.diff(start, 'days')
if (shownDays <= 34) end = end.add(1, 'week')
end = end.endOf('isoWeek')
2019-02-26 00:02:42 +01:00
const events = await Event.findAll({
where: {
2019-03-07 14:59:28 +01:00
is_visible: true,
2019-03-10 01:01:23 +01:00
[Op.and]: [
{ start_datetime: { [Op.gte]: start } },
{ start_datetime: { [Op.lte]: end } }
2019-02-26 00:02:42 +01:00
]
},
2019-02-26 12:37:51 +01:00
order: [['start_datetime', 'ASC']],
2019-02-26 00:02:42 +01:00
include: [User, Comment, Tag, Place]
})
res.json(events)
2019-02-26 01:17:52 +01:00
}
2019-02-26 00:02:42 +01:00
}
module.exports = eventController