2019-04-03 00:25:12 +02:00
|
|
|
const express = require('express')
|
|
|
|
const multer = require('multer')
|
2019-12-10 22:29:36 +01:00
|
|
|
const cors = require('cors')()
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2021-05-19 16:38:22 +02:00
|
|
|
const { isAuth, isAdmin } = require('./auth')
|
2019-04-03 00:25:12 +02:00
|
|
|
const eventController = require('./controller/event')
|
|
|
|
const exportController = require('./controller/export')
|
|
|
|
const userController = require('./controller/user')
|
|
|
|
const settingsController = require('./controller/settings')
|
2019-12-04 01:18:05 +01:00
|
|
|
const instanceController = require('./controller/instance')
|
|
|
|
const apUserController = require('./controller/ap_user')
|
|
|
|
const resourceController = require('./controller/resource')
|
2019-12-26 11:46:21 +01:00
|
|
|
const oauthController = require('./controller/oauth')
|
2020-02-16 21:03:50 +01:00
|
|
|
const announceController = require('./controller/announce')
|
2020-10-10 00:40:47 +02:00
|
|
|
const helpers = require('../helpers')
|
2019-06-11 17:44:11 +02:00
|
|
|
const storage = require('./storage')
|
2019-04-03 00:25:12 +02:00
|
|
|
const upload = multer({ storage })
|
2019-07-23 01:31:43 +02:00
|
|
|
|
2020-03-14 21:11:46 +01:00
|
|
|
const config = require('config')
|
2021-03-05 14:17:10 +01:00
|
|
|
const log = require('../log')
|
2019-09-11 19:12:24 +02:00
|
|
|
|
2019-08-25 14:34:26 +02:00
|
|
|
const api = express.Router()
|
2019-12-26 11:46:21 +01:00
|
|
|
api.use(express.urlencoded({ extended: false }))
|
|
|
|
api.use(express.json())
|
2019-06-06 23:54:32 +02:00
|
|
|
|
2020-01-31 14:56:31 +01:00
|
|
|
/**
|
|
|
|
* Get current authenticated user
|
|
|
|
* @category User
|
2020-02-02 15:02:37 +01:00
|
|
|
* @name /api/user
|
|
|
|
* @type GET
|
2020-02-02 21:08:16 +01:00
|
|
|
* @example **Response**
|
|
|
|
* ```json
|
|
|
|
{
|
|
|
|
"description" : null,
|
|
|
|
"recover_code" : "",
|
|
|
|
"id" : 1,
|
|
|
|
"createdAt" : "2020-01-29T18:10:16.630Z",
|
|
|
|
"updatedAt" : "2020-01-30T22:42:14.789Z",
|
|
|
|
"is_active" : true,
|
|
|
|
"settings" : "{}",
|
|
|
|
"email" : "eventi@cisti.org",
|
|
|
|
"is_admin" : true
|
|
|
|
}
|
|
|
|
```
|
2020-01-31 14:56:31 +01:00
|
|
|
*/
|
|
|
|
api.get('/user', isAuth, (req, res) => res.json(req.user))
|
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
api.post('/user/recover', userController.forgotPassword)
|
|
|
|
api.post('/user/check_recover_code', userController.checkRecoverCode)
|
|
|
|
api.post('/user/recover_password', userController.updatePasswordWithRecoverCode)
|
|
|
|
|
2019-06-18 14:45:04 +02:00
|
|
|
// register and add users
|
|
|
|
api.post('/user/register', userController.register)
|
2019-10-30 14:58:40 +01:00
|
|
|
api.post('/user', isAdmin, userController.create)
|
2019-06-18 14:45:04 +02:00
|
|
|
|
2019-09-11 11:58:42 +02:00
|
|
|
// update user
|
2021-05-19 16:38:22 +02:00
|
|
|
api.put('/user', isAuth, userController.update)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
// delete user
|
2019-10-30 14:58:40 +01:00
|
|
|
api.delete('/user/:id', isAdmin, userController.remove)
|
2021-05-19 16:38:22 +02:00
|
|
|
api.delete('/user', isAdmin, userController.remove)
|
2019-06-18 15:13:13 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
// get all users
|
2019-10-30 14:58:40 +01:00
|
|
|
api.get('/users', isAdmin, userController.getAll)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
// update a place (modify address..)
|
2019-10-30 14:58:40 +01:00
|
|
|
api.put('/place', isAdmin, eventController.updatePlace)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-31 14:56:31 +01:00
|
|
|
/**
|
|
|
|
* Add a new event
|
|
|
|
* @category Event
|
2020-02-02 15:02:37 +01:00
|
|
|
* @name /event
|
|
|
|
* @type POST
|
2021-06-04 20:37:19 +02:00
|
|
|
* @info `Content-Type` has to be `multipart/form-data` to support image upload
|
2020-01-31 14:56:31 +01:00
|
|
|
* @param {string} title - event's title
|
|
|
|
* @param {string} description - event's description (html accepted and sanitized)
|
|
|
|
* @param {string} place_name - the name of the place
|
|
|
|
* @param {string} [place_address] - the address of the place
|
|
|
|
* @param {integer} start_datetime - start timestamp
|
|
|
|
* @param {integer} multidate - is a multidate event?
|
|
|
|
* @param {array} tags - List of tags
|
|
|
|
* @param {object} [recurrent] - Recurrent event details
|
|
|
|
* @param {string} [recurrent.frequency] - could be `1w` or `2w`
|
|
|
|
* @param {string} [recurrent.type] - not used
|
|
|
|
* @param {array} [recurrent.days] - array of days
|
|
|
|
* @param {image} [image] - Image
|
|
|
|
*/
|
2021-05-19 16:38:22 +02:00
|
|
|
|
|
|
|
// allow anyone to add an event (anon event has to be confirmed, TODO: flood protection)
|
|
|
|
api.post('/event', upload.single('image'), eventController.add)
|
|
|
|
|
|
|
|
api.put('/event', isAuth, upload.single('image'), eventController.update)
|
|
|
|
api.get('/event/import', isAuth, helpers.importURL)
|
2020-10-10 00:40:47 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
// remove event
|
2021-05-19 16:38:22 +02:00
|
|
|
api.delete('/event/:id', isAuth, eventController.remove)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
// get tags/places
|
|
|
|
api.get('/event/meta', eventController.getMeta)
|
|
|
|
|
|
|
|
// get unconfirmed events
|
2019-10-30 14:58:40 +01:00
|
|
|
api.get('/event/unconfirmed', isAdmin, eventController.getUnconfirmed)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-27 00:47:03 +01:00
|
|
|
// add event notification TODO
|
2019-04-03 00:25:12 +02:00
|
|
|
api.post('/event/notification', eventController.addNotification)
|
|
|
|
api.delete('/event/notification/:code', eventController.delNotification)
|
|
|
|
|
2019-06-21 23:52:18 +02:00
|
|
|
api.get('/settings', settingsController.getAllRequest)
|
2019-10-30 14:58:40 +01:00
|
|
|
api.post('/settings', isAdmin, settingsController.setRequest)
|
2020-07-05 23:53:37 +02:00
|
|
|
api.post('/settings/logo', isAdmin, multer({ dest: config.upload_path }).single('logo'), settingsController.setLogo)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-27 00:47:03 +01:00
|
|
|
// confirm event
|
2021-05-19 16:38:22 +02:00
|
|
|
api.put('/event/confirm/:event_id', isAuth, eventController.confirm)
|
|
|
|
api.put('/event/unconfirm/:event_id', isAuth, eventController.unconfirm)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2019-07-04 01:20:32 +02:00
|
|
|
// get event
|
2019-12-10 22:29:36 +01:00
|
|
|
api.get('/event/:event_id.:format?', cors, eventController.get)
|
2019-07-04 01:20:32 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
// export events (rss/ics)
|
2019-12-10 22:29:36 +01:00
|
|
|
api.get('/export/:type', cors, exportController.export)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
// get events in this range
|
2020-10-17 00:41:21 +02:00
|
|
|
api.get('/events', cors, eventController.select)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2019-12-04 01:18:05 +01:00
|
|
|
api.get('/instances', isAdmin, instanceController.getAll)
|
|
|
|
api.get('/instances/:instance_domain', isAdmin, instanceController.get)
|
|
|
|
api.post('/instances/toggle_block', isAdmin, instanceController.toggleBlock)
|
|
|
|
api.post('/instances/toggle_user_block', isAdmin, apUserController.toggleBlock)
|
|
|
|
api.put('/resources/:resource_id', isAdmin, resourceController.hide)
|
|
|
|
api.delete('/resources/:resource_id', isAdmin, resourceController.remove)
|
|
|
|
api.get('/resources', isAdmin, resourceController.getAll)
|
2019-10-30 15:01:15 +01:00
|
|
|
|
2020-02-16 21:03:50 +01:00
|
|
|
// - ADMIN ANNOUNCEMENTS
|
|
|
|
api.get('/announcements', isAdmin, announceController.getAll)
|
|
|
|
api.post('/announcements', isAdmin, announceController.add)
|
|
|
|
api.put('/announcements/:announce_id', isAdmin, announceController.update)
|
|
|
|
api.delete('/announcements/:announce_id', isAdmin, announceController.remove)
|
|
|
|
|
2021-03-10 15:26:09 +01:00
|
|
|
// OAUTH
|
2021-05-19 16:38:22 +02:00
|
|
|
api.get('/clients', isAuth, oauthController.getClients)
|
|
|
|
api.get('/client/:client_id', isAuth, oauthController.getClient)
|
2019-12-26 11:46:21 +01:00
|
|
|
api.post('/client', oauthController.createClient)
|
|
|
|
|
2020-01-27 00:47:03 +01:00
|
|
|
api.use((req, res) => res.sendStatus(404))
|
2019-09-11 19:12:24 +02:00
|
|
|
|
|
|
|
// Handle 500
|
|
|
|
api.use((error, req, res, next) => {
|
2021-03-05 14:17:10 +01:00
|
|
|
log.error(error)
|
2019-09-11 19:12:24 +02:00
|
|
|
res.status(500).send('500: Internal Server Error')
|
|
|
|
})
|
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
module.exports = api
|