2019-04-03 00:25:12 +02:00
const crypto = require ( 'crypto' )
2019-10-20 14:22:55 +02:00
const moment = require ( 'moment-timezone' )
2019-04-03 00:25:12 +02:00
const { Op } = require ( 'sequelize' )
2020-01-21 01:24:32 +01:00
const _ = require ( 'lodash' )
2019-12-04 01:18:05 +01:00
const { event : Event , resource : Resource , tag : Tag , place : Place , notification : Notification } = require ( '../models' )
2019-05-30 12:04:14 +02:00
const Sequelize = require ( 'sequelize' )
2019-10-20 14:22:55 +02:00
const exportController = require ( './export' )
2019-09-11 23:17:12 +02:00
const debug = require ( 'debug' ) ( 'controller:event' )
2019-04-03 00:25:12 +02:00
const eventController = {
2019-12-04 01:18:05 +01:00
/ * * a d d a r e s o u r c e t o e v e n t
2019-11-06 11:31:56 +01:00
* @ todo not used anywhere , should we use with webmention ?
* @ todo should we use this for roply coming from fediverse ?
* /
2019-12-04 01:18:05 +01:00
// async addComment (req, res) {
// // 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 Resource.findOne({ where: { activitypub_id: { [Op.eq]: req.body.id } }, include: Event })
// event = comment.event
// }
// const comment = new Comment(req.body)
// event.addComment(comment)
// res.json(comment)
// },
2019-04-03 00:25:12 +02:00
2019-09-11 19:12:24 +02:00
async getMeta ( req , res ) {
2019-05-30 12:04:14 +02:00
const places = await Place . findAll ( {
2019-05-30 12:12:51 +02:00
order : [ [ Sequelize . literal ( 'weigth' ) , 'DESC' ] ] ,
2019-05-30 12:04:14 +02:00
attributes : {
2019-09-11 19:12:24 +02:00
include : [ [ Sequelize . fn ( 'count' , Sequelize . col ( 'events.placeId' ) ) , 'weigth' ] ] ,
2020-01-21 01:24:32 +01:00
exclude : [ 'createdAt' , 'updatedAt' ]
2019-05-30 12:04:14 +02:00
} ,
2019-05-30 12:12:51 +02:00
include : [ { model : Event , attributes : [ ] } ] ,
group : [ 'place.id' ]
2019-05-30 12:04:14 +02:00
} )
const tags = await Tag . findAll ( {
2020-01-15 23:51:57 +01:00
raw : true ,
2019-06-07 17:02:33 +02:00
order : [ [ 'weigth' , 'DESC' ] ] ,
2019-05-30 12:04:14 +02:00
attributes : {
2020-01-21 01:24:32 +01:00
exclude : [ 'createdAt' , 'updatedAt' ]
2019-09-11 19:12:24 +02:00
}
2019-05-30 12:12:51 +02:00
} )
2019-05-30 12:04:14 +02:00
2020-01-21 01:24:32 +01:00
res . json ( { tags , places } )
2019-04-03 00:25:12 +02:00
} ,
2019-10-02 21:04:03 +02:00
async getNotifications ( event , action ) {
debug ( 'getNotifications "%s" (%s)' , event . title , action )
2019-09-11 19:12:24 +02:00
function match ( event , filters ) {
2019-04-03 00:25:12 +02:00
// matches if no filter specified
2019-09-11 19:12:24 +02:00
if ( ! filters ) { return true }
2019-04-03 00:25:12 +02:00
// check for visibility
2019-09-11 19:12:24 +02:00
if ( typeof filters . is _visible !== 'undefined' && filters . is _visible !== event . is _visible ) { return false }
2019-04-03 00:25:12 +02:00
2019-09-11 19:12:24 +02:00
if ( ! filters . tags && ! filters . places ) { return true }
if ( ! filters . tags . length && ! filters . places . length ) { return true }
2019-04-03 00:25:12 +02:00
if ( filters . tags . length ) {
2020-01-21 01:24:32 +01:00
const m = _ . intersection ( event . tags . map ( t => t . tag ) , filters . tags )
2019-09-11 19:12:24 +02:00
if ( m . length > 0 ) { return true }
2019-04-03 00:25:12 +02:00
}
if ( filters . places . length ) {
if ( filters . places . find ( p => p === event . place . name ) ) {
return true
}
}
}
2019-10-02 21:04:03 +02:00
2020-01-21 01:24:32 +01:00
const notifications = await Notification . findAll ( { where : { action } , include : [ Event ] } )
2019-04-03 00:25:12 +02:00
// get notification that matches with selected event
2019-10-02 21:04:03 +02:00
const ret = notifications . filter ( notification => match ( event , notification . filters ) )
return ret
2019-04-03 00:25:12 +02:00
} ,
2019-09-11 19:12:24 +02:00
async updatePlace ( req , res ) {
2019-04-03 00:25:12 +02:00
const place = await Place . findByPk ( req . body . id )
await place . update ( req . body )
res . json ( place )
} ,
2019-06-14 23:26:13 +02:00
// TODO retrieve next/prev event also
// select id, start_datetime, title from events where start_datetime > (select start_datetime from events where id=89) order by start_datetime limit 20;
2019-09-11 19:12:24 +02:00
async get ( req , res ) {
2019-10-20 14:22:55 +02:00
const format = req . params . format || 'json'
2019-07-04 01:09:35 +02:00
const is _admin = req . user && req . user . is _admin
2020-01-15 23:51:57 +01:00
const id = Number ( req . params . event _id )
2019-10-26 00:27:12 +02:00
let event = await Event . findByPk ( id , {
2019-09-11 19:12:24 +02:00
attributes : {
2019-07-23 16:26:54 +02:00
exclude : [ 'createdAt' , 'updatedAt' ]
2019-07-23 01:31:43 +02:00
} ,
2019-06-14 23:26:13 +02:00
include : [
2019-06-26 14:44:21 +02:00
{ model : Tag , attributes : [ 'tag' , 'weigth' ] , through : { attributes : [ ] } } ,
{ model : Place , attributes : [ 'name' , 'address' ] } ,
2019-12-04 01:18:05 +01:00
{ model : Resource , where : ! is _admin && { hidden : false } , required : false }
2019-06-07 17:02:33 +02:00
] ,
2020-01-15 23:51:57 +01:00
order : [ [ Resource , 'id' , 'DESC' ] ]
2019-05-30 12:04:14 +02:00
} )
2019-06-25 01:05:38 +02:00
2019-07-04 01:02:45 +02:00
if ( event && ( event . is _visible || is _admin ) ) {
2019-10-26 00:27:12 +02:00
event = event . toJSON ( )
event . tags = event . tags . map ( t => t . tag )
2019-10-20 14:22:55 +02:00
if ( format === 'json' ) {
res . json ( event )
} else if ( format === 'ics' ) {
2019-11-06 11:21:00 +01:00
// last arg is alarms/reminder, ref: https://github.com/adamgibbons/ics#attributes (alarms)
exportController . ics ( req , res , [ event ] , [ {
action : 'display' ,
trigger : { hours : 1 , before : true }
} ] )
2019-10-20 14:22:55 +02:00
}
2019-06-12 22:26:28 +02:00
} else {
res . sendStatus ( 404 )
}
2019-04-03 00:25:12 +02:00
} ,
2019-11-06 11:31:56 +01:00
/ * * c o n f i r m a n a n o n y m o u s e v e n t
* and send its relative notifications
* /
2019-09-11 19:12:24 +02:00
async confirm ( req , res ) {
2019-07-04 00:29:50 +02:00
const id = Number ( req . params . event _id )
2019-04-03 00:25:12 +02:00
const event = await Event . findByPk ( id )
2019-09-11 19:12:24 +02:00
if ( ! event ) { return res . sendStatus ( 404 ) }
2019-10-28 17:42:21 +01:00
if ( ! req . user . is _admin && req . user . id !== event . userId ) {
return res . sendStatus ( 403 )
}
2019-04-03 00:25:12 +02:00
try {
2019-07-04 01:02:45 +02:00
event . is _visible = true
await event . save ( )
2019-07-08 01:53:37 +02:00
2019-04-03 00:25:12 +02:00
res . sendStatus ( 200 )
2019-09-11 19:12:24 +02:00
2019-07-08 01:53:37 +02:00
// send notification
2019-10-02 21:04:03 +02:00
const notifier = require ( '../../notifier' )
notifier . notifyEvent ( 'Create' , event . id )
2019-04-03 00:25:12 +02:00
} catch ( e ) {
res . sendStatus ( 404 )
}
} ,
2019-09-11 19:12:24 +02:00
async unconfirm ( req , res ) {
2019-07-04 00:29:50 +02:00
const id = Number ( req . params . event _id )
2019-04-03 00:25:12 +02:00
const event = await Event . findByPk ( id )
2019-10-28 17:33:20 +01:00
if ( ! event ) { return req . sendStatus ( 404 ) }
2019-10-28 17:42:21 +01:00
if ( ! req . user . is _admin && req . user . id !== event . userId ) {
return res . sendStatus ( 403 )
}
2019-04-03 00:25:12 +02:00
try {
2019-07-04 01:02:45 +02:00
event . is _visible = false
await event . save ( )
2019-04-03 00:25:12 +02:00
res . sendStatus ( 200 )
} catch ( e ) {
res . sendStatus ( 404 )
}
} ,
2019-11-06 11:31:56 +01:00
/** get all unconfirmed events */
2019-09-11 19:12:24 +02:00
async getUnconfirmed ( req , res ) {
2019-04-03 00:25:12 +02:00
const events = await Event . findAll ( {
where : {
is _visible : false
} ,
order : [ [ 'start_datetime' , 'ASC' ] ] ,
include : [ Tag , Place ]
} )
res . json ( events )
} ,
2019-09-11 19:12:24 +02:00
async addNotification ( req , res ) {
2019-04-03 00:25:12 +02:00
try {
const notification = {
filters : { is _visible : true } ,
email : req . body . email ,
type : 'mail' ,
remove _code : crypto . randomBytes ( 16 ) . toString ( 'hex' )
}
await Notification . create ( notification )
res . sendStatus ( 200 )
} catch ( e ) {
res . sendStatus ( 404 )
}
} ,
2019-09-11 19:12:24 +02:00
async delNotification ( req , res ) {
2019-04-03 00:25:12 +02:00
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 . sendStatus ( 404 )
}
res . sendStatus ( 200 )
} ,
2020-01-21 01:24:32 +01:00
async addRecurrent ( start , places , where _tags , limit ) {
const where = {
is _visible : true ,
recurrent : { [ Op . ne ] : null }
// placeId: places
}
const events = await Event . findAll ( {
where ,
limit ,
attributes : {
exclude : [ 'slug' , 'likes' , 'boost' , 'userId' , 'is_visible' , 'description' , 'createdAt' , 'updatedAt' , 'placeId' ]
} ,
order : [ 'start_datetime' , [ Tag , 'weigth' , 'DESC' ] ] ,
include : [
{ model : Resource , required : false , attributes : [ 'id' ] } ,
{ model : Tag , ... where _tags , attributes : [ 'tag' ] , through : { attributes : [ ] } } ,
{ model : Place , required : false , attributes : [ 'id' , 'name' , 'address' ] }
]
} )
debug ( ` Found ${ events . length } recurrent events ` )
let allEvents = [ ]
_ . forEach ( events , e => {
allEvents = allEvents . concat ( eventController . createEventsFromRecurrent ( e . get ( ) , start ) )
} )
debug ( ` Created ${ allEvents . length } events ` )
return allEvents
} ,
// build singular events from a recurrent pattern
createEventsFromRecurrent ( e , start , dueTo = null ) {
const events = [ ]
const recurrent = JSON . parse ( e . recurrent )
if ( ! recurrent . frequency ) { return false }
if ( ! dueTo ) {
dueTo = moment . unix ( start ) . add ( 2 , 'month' )
}
let cursor = moment . unix ( start ) . startOf ( 'week' )
const start _date = moment . unix ( e . start _datetime )
const duration = moment . unix ( e . end _datetime ) . diff ( start _date , 's' )
const frequency = recurrent . frequency
const days = recurrent . days
const type = recurrent . type
// default frequency is '1d' => each day
const toAdd = { n : 1 , unit : 'day' }
// each week or 2 (search for the first specified day)
if ( frequency === '1w' || frequency === '2w' ) {
cursor . add ( days [ 0 ] - 1 , 'day' )
if ( frequency === '2w' ) {
const nWeeks = cursor . diff ( e . start _datetime , 'w' ) % 2
if ( ! nWeeks ) { cursor . add ( 1 , 'week' ) }
}
toAdd . n = Number ( frequency [ 0 ] )
toAdd . unit = 'week'
// cursor.set('hour', start_date.hour()).set('minute', start_date.minutes())
}
cursor . set ( 'hour' , start _date . hour ( ) ) . set ( 'minute' , start _date . minutes ( ) )
// each month or 2
if ( frequency === '1m' || frequency === '2m' ) {
// find first match
toAdd . n = 1
toAdd . unit = 'month'
if ( type === 'weekday' ) {
} else if ( type === 'ordinal' ) {
}
}
// add event at specified frequency
while ( true ) {
const first _event _of _week = cursor . clone ( )
days . forEach ( d => {
if ( type === 'ordinal' ) {
cursor . date ( d )
} else {
cursor . day ( d - 1 )
}
if ( cursor . isAfter ( dueTo ) || cursor . isBefore ( start ) ) { return }
e . start _datetime = cursor . unix ( )
e . end _datetime = e . start _datetime + duration
events . push ( Object . assign ( { } , e ) )
} )
if ( cursor . isAfter ( dueTo ) ) { break }
cursor = first _event _of _week . add ( toAdd . n , toAdd . unit )
cursor . set ( 'hour' , start _date . hour ( ) ) . set ( 'minute' , start _date . minutes ( ) )
}
return events
} ,
/ * *
* Select events based on params
* /
2020-01-15 23:51:57 +01:00
async select ( req , res ) {
2020-01-21 01:24:32 +01:00
const start = req . query . start || moment ( ) . unix ( )
const limit = req . query . limit || 100
const show _recurrent = req . query . show _recurrent || true
2020-01-15 23:51:57 +01:00
const filter _tags = req . query . tags || ''
const filter _places = req . query . places || ''
2020-01-21 01:24:32 +01:00
debug ( ` select limit: ${ limit } rec: ${ show _recurrent } tags: ${ filter _tags } places: ${ filter _places } ` )
2020-01-15 23:51:57 +01:00
let where _tags = { }
2020-01-21 01:24:32 +01:00
const where = {
// confirmed event only
is _visible : true ,
start _datetime : { [ Op . gt ] : start } ,
recurrent : null
2020-01-15 23:51:57 +01:00
}
if ( filter _tags ) {
where _tags = { where : { tag : filter _tags . split ( ',' ) } }
}
if ( filter _places ) {
where . placeId = filter _places . split ( ',' )
}
let events = await Event . findAll ( {
where ,
limit ,
attributes : {
2020-01-21 01:24:32 +01:00
exclude : [ 'slug' , 'likes' , 'boost' , 'userId' , 'is_visible' , 'description' , 'createdAt' , 'updatedAt' , 'placeId' ]
2020-01-15 23:51:57 +01:00
// include: [[Sequelize.fn('COUNT', Sequelize.col('activitypub_id')), 'ressources']]
} ,
order : [ 'start_datetime' , [ Tag , 'weigth' , 'DESC' ] ] ,
include : [
{ model : Resource , required : false , attributes : [ 'id' ] } ,
2020-01-21 01:24:32 +01:00
{ model : Tag , ... where _tags , attributes : [ 'tag' ] , through : { attributes : [ ] } } ,
2019-05-30 12:12:51 +02:00
{ model : Place , required : false , attributes : [ 'id' , 'name' , 'address' ] }
2019-04-29 00:27:29 +02:00
]
2019-04-03 00:25:12 +02:00
} )
2019-07-11 23:31:37 +02:00
2020-01-21 01:24:32 +01:00
let recurrentEvents = [ ]
events = _ . map ( events , e => e . get ( ) )
if ( show _recurrent ) {
recurrentEvents = await eventController . addRecurrent ( start , where . placeId , where _tags , limit )
events = _ . concat ( events , recurrentEvents )
2019-07-11 23:31:37 +02:00
}
2020-01-21 01:24:32 +01:00
// flat tags
events = _ ( events ) . map ( e => {
e . tags = e . tags . map ( t => t . tag )
return e
2019-07-13 01:02:11 +02:00
} )
// allEvents.sort((a,b) => a.start_datetime-b.start_datetime)
2020-01-21 01:24:32 +01:00
res . json ( events . sort ( ( a , b ) => a . start _datetime - b . start _datetime ) )
// res.json(recurrentEvents)
2019-04-03 00:25:12 +02:00
}
2020-01-21 01:24:32 +01:00
// async getAll (req, res) {
// // this is due how v-calendar shows dates
// const start = moment()
// .year(req.params.year)
// .month(req.params.month)
// .startOf('month')
// .startOf('week')
// let end = moment()
// .year(req.params.year)
// .month(req.params.month)
// .endOf('month')
// const shownDays = end.diff(start, 'days')
// if (shownDays <= 35) { end = end.add(1, 'week') }
// end = end.endOf('week')
// let events = await Event.findAll({
// where: {
// // return only confirmed events
// is_visible: true,
// [Op.or]: [
// // return all recurrent events regardless start_datetime
// { recurrent: { [Op.ne]: null } },
// // and events in specified range
// { start_datetime: { [Op.between]: [start.unix(), end.unix()] } }
// ]
// },
// attributes: { exclude: ['createdAt', 'updatedAt', 'placeId'] },
// order: [[Tag, 'weigth', 'DESC']],
// include: [
// { model: Resource, required: false, attributes: ['id'] },
// { model: Tag, required: false },
// { model: Place, required: false, attributes: ['id', 'name', 'address'] }
// ]
// })
// events = events.map(e => e.get()).map(e => {
// e.tags = e.tags.map(t => t.tag)
// return e
// })
// let allEvents = events.filter(e => !e.recurrent || e.recurrent.length === 0)
// events.filter(e => e.recurrent && e.recurrent.length).forEach(e => {
// const events = createEventsFromRecurrent(e, end)
// if (events) { allEvents = allEvents.concat(events) }
// })
// // allEvents.sort((a,b) => a.start_datetime-b.start_datetime)
// res.json(allEvents.sort((a, b) => a.start_datetime - b.start_datetime))
// }
2019-04-03 00:25:12 +02:00
}
module . exports = eventController