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' )
2020-01-30 12:37:19 +01:00
// const { Task, TaskManager } = require('../../taskManager')
2019-04-03 00:25:12 +02:00
const eventController = {
2020-01-27 00:47:03 +01:00
async _getMeta ( ) {
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-27 00:47:03 +01:00
return { places , tags }
} ,
async getMeta ( req , res ) {
res . json ( await eventController . _getMeta ( ) )
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 )
2020-01-30 12:37:19 +01:00
let event
try {
event = await Event . findByPk ( id , {
attributes : {
exclude : [ 'createdAt' , 'updatedAt' ]
} ,
include : [
{ model : Tag , attributes : [ 'tag' , 'weigth' ] , through : { attributes : [ ] } } ,
{ model : Place , attributes : [ 'name' , 'address' ] } ,
{ model : Resource , where : ! is _admin && { hidden : false } , required : false } ,
{ model : Event , required : false , as : 'parent' }
] ,
order : [ [ Resource , 'id' , 'DESC' ] ]
} )
} catch ( e ) {
return res . sendStatus ( 400 )
}
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 {
2020-01-30 12:37:19 +01:00
await event . update ( { is _visible : false } )
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-30 12:37:19 +01:00
async _select ( start = moment . unix ( ) , limit = 100 ) {
2020-01-21 01:24:32 +01:00
const where = {
// confirmed event only
2020-01-30 12:37:19 +01:00
recurrent : null ,
2020-01-21 01:24:32 +01:00
is _visible : true ,
2020-01-27 00:47:03 +01:00
start _datetime : { [ Op . gt ] : start }
2020-01-15 23:51:57 +01:00
}
2020-01-27 00:47:03 +01:00
const events = await Event . findAll ( {
2020-01-15 23:51:57 +01:00
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-27 00:47:03 +01:00
{ model : Tag , attributes : [ 'tag' ] , required : false , 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-27 00:47:03 +01:00
return _ ( events ) . map ( e => {
e = e . get ( )
e . tags = e . tags ? e . tags . map ( t => t && t . tag ) : [ ]
2020-01-21 01:24:32 +01:00
return e
2019-07-13 01:02:11 +02:00
} )
2020-01-27 00:47:03 +01:00
} ,
2020-01-21 01:24:32 +01:00
2020-01-27 00:47:03 +01:00
/ * *
* Select events based on params
* /
async select ( req , res ) {
const start = req . query . start || moment ( ) . unix ( )
const limit = req . query . limit || 100
2020-01-30 12:37:19 +01:00
res . json ( await eventController . _select ( start , limit ) )
} ,
2020-01-21 01:24:32 +01:00
2020-01-30 12:37:19 +01:00
/ * *
* Ensure we have at least 3 instances of recurrent events
* /
_createRecurrentOccurrence ( e ) {
const event = {
parentId : e . id ,
title : e . title ,
description : e . description ,
image _path : e . image _path ,
is _visible : e . is _visible ,
userId : e . userId ,
placeId : e . placeId
}
const recurrent = e . recurrent
let left = 3 - e . child . length
const start = e . child . length ? moment . unix ( e . child [ e . child . length - 1 ] . start _datetime ) : moment ( )
let cursor = 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 ( ) )
// 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 ) {
if ( ! left ) { break }
left -= 1
const first _event _of _week = cursor . clone ( )
days . forEach ( d => {
debug ( cursor )
if ( type === 'ordinal' ) {
cursor . date ( d )
} else {
cursor . day ( d - 1 )
}
event . start _datetime = cursor . unix ( )
event . end _datetime = event . start _datetime + duration
Event . create ( event )
cursor . set ( 'hour' , start _date . hour ( ) ) . set ( 'minute' , start _date . minutes ( ) )
} )
cursor = first _event _of _week . add ( toAdd . n , toAdd . unit )
}
} ,
/ * *
* Create instances of recurrent events
* Remove old
* @ param { * } start _datetime
* /
async _createRecurrent ( start _datetime = moment ( ) . unix ( ) ) {
// select recurrent events
const events = await Event . findAll ( {
where : { is _visible : true , recurrent : { [ Op . ne ] : null } } ,
include : [ { model : Event , as : 'child' , required : false , where : { start _datetime : { [ Op . gt ] : start _datetime } } } ] ,
order : [ 'start_datetime' ]
} )
const creations = [ ]
events
. filter ( e => e . child && e . child . length < 3 )
. forEach ( e => {
eventController . _createRecurrentOccurrence ( e )
} )
return Promise . all ( creations )
}
2019-04-03 00:25:12 +02:00
}
module . exports = eventController