Files
gancio/app/controller/user.js

253 lines
7.8 KiB
JavaScript
Raw Normal View History

2019-02-26 00:02:42 +01:00
const jwt = require('jsonwebtoken')
const Mastodon = require('mastodon-api')
const User = require('../models/user')
const { Event, Tag, Place } = require('../models/event')
const settingsController = require('./settings')
2019-03-10 01:01:23 +01:00
const eventController = require('./event')
2019-02-26 00:02:42 +01:00
const config = require('../config')
const mail = require('../mail')
2019-03-10 01:01:23 +01:00
const { Op } = require('sequelize')
2019-03-20 00:50:43 +01:00
const fs = require('fs')
const path = require('path')
2019-02-26 00:02:42 +01:00
const userController = {
async login (req, res) {
// find the user
2019-03-10 01:01:23 +01:00
const user = await User.findOne({ where: { email: { [Op.eq]: req.body.email } } })
2019-02-26 00:02:42 +01:00
if (!user) {
res.status(404).json({ success: false, message: 'AUTH_FAIL' })
} else if (user) {
if (!user.is_active) {
2019-03-07 14:59:28 +01:00
res.status(403).json({ success: false, message: 'NOT_CONFIRMED' })
2019-02-26 00:02:42 +01:00
// check if password matches
2019-03-07 14:59:28 +01:00
} else if (!await user.comparePassword(req.body.password)) {
2019-02-26 00:02:42 +01:00
res.status(403).json({ success: false, message: 'AUTH_FAIL' })
} else {
// if user is found and password is right
// create a token
const payload = { email: user.email }
var token = jwt.sign(payload, config.secret)
res.json({
success: true,
message: 'Enjoy your token!',
token,
user
})
}
}
},
async setToken (req, res) {
req.user.mastodon_auth = req.body
await req.user.save()
res.json(req.user)
},
async delEvent (req, res) {
const event = await Event.findByPk(req.params.id)
2019-03-14 11:12:56 +01:00
// check if event is mine (or user is admin)
2019-02-26 01:17:52 +01:00
if (event && (req.user.is_admin || req.user.id === event.userId)) {
2019-03-20 00:50:43 +01:00
if (event.image_path) {
const old_path = path.resolve(__dirname, '..', '..', 'uploads', event.image_path)
const old_thumb_path = path.resolve(__dirname, '..', '..', 'uploads', 'thumb', event.image_path)
await fs.unlink(old_path)
await fs.unlink(old_thumb_path)
}
2019-02-26 00:02:42 +01:00
await event.destroy()
res.sendStatus(200)
} else {
2019-03-07 14:59:28 +01:00
res.sendStatus(403)
2019-02-26 00:02:42 +01:00
}
},
2019-03-07 14:59:28 +01:00
// ADD EVENT
2019-03-05 21:57:53 +01:00
async addEvent (req, res) {
2019-02-26 00:02:42 +01:00
const body = req.body
2019-03-07 14:59:28 +01:00
// remove description tag and create anchor tags
2019-03-05 21:57:53 +01:00
const description = body.description
.replace(/(<([^>]+)>)/ig, '')
.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1">$1</a>')
2019-03-07 14:59:28 +01:00
2019-02-26 00:02:42 +01:00
const eventDetails = {
title: body.title,
2019-03-05 21:57:53 +01:00
description,
2019-02-26 00:02:42 +01:00
multidate: body.multidate,
start_datetime: body.start_datetime,
2019-03-07 14:59:28 +01:00
end_datetime: body.end_datetime,
is_visible: !!req.user
2019-02-26 00:02:42 +01:00
}
if (req.file) {
2019-03-20 00:50:43 +01:00
eventDetails.image_path = req.file.filename
2019-02-26 00:02:42 +01:00
}
let event = await Event.create(eventDetails)
2019-02-26 01:17:52 +01:00
// create place
2019-02-26 00:02:42 +01:00
let place
try {
2019-02-26 01:17:52 +01:00
place = await Place.findOrCreate({ where: { name: body.place_name },
defaults: { address: body.place_address } })
.spread((place, created) => place)
await event.setPlace(place)
2019-02-26 01:17:52 +01:00
} catch (e) {
console.error(e)
2019-02-26 00:02:42 +01:00
}
2019-03-10 01:01:23 +01:00
2019-02-26 00:02:42 +01:00
// create/assign tags
if (body.tags) {
2019-02-26 01:17:52 +01:00
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
2019-03-10 01:01:23 +01:00
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
2019-02-26 00:02:42 +01:00
await event.addTags(tags)
}
2019-03-07 14:59:28 +01:00
if (req.user) await req.user.addEvent(event)
2019-02-26 01:17:52 +01:00
event = await Event.findByPk(event.id, { include: [User, Tag, Place] })
2019-03-07 14:59:28 +01:00
2019-03-20 01:52:48 +01:00
// insert notifications
const notifications = await eventController.getNotifications(event)
await event.setNotifications(notifications)
2019-02-26 00:02:42 +01:00
return res.json(event)
},
async updateEvent (req, res) {
const body = req.body
const event = await Event.findByPk(body.id)
if (!req.user.is_admin && event.userId !== req.user.id) {
return res.sendStatus(403)
}
2019-03-05 21:57:53 +01:00
2019-03-19 23:55:44 +01:00
if (req.file) {
if (event.image_path) {
2019-03-20 00:50:43 +01:00
const old_path = path.resolve(__dirname, '..', '..', 'uploads', event.image_path)
const old_thumb_path = path.resolve(__dirname, '..', '..', 'uploads', 'thumb', event.image_path)
2019-03-19 23:55:44 +01:00
await fs.unlink(old_path, e => console.error(e))
2019-03-20 00:50:43 +01:00
await fs.unlink(old_thumb_path, e => console.error(e))
2019-03-19 23:55:44 +01:00
}
2019-03-20 00:50:43 +01:00
body.image_path = req.file.filename
2019-03-19 23:55:44 +01:00
}
2019-03-05 21:57:53 +01:00
body.description = body.description
.replace(/(<([^>]+)>)/ig, '') // remove all tags from description
.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1">$1</a>') // add links
2019-02-26 00:02:42 +01:00
await event.update(body)
let place
try {
2019-03-19 23:41:49 +01:00
place = await Place.findOrCreate({ where: { name: body.place_name },
2019-02-26 01:17:52 +01:00
defaults: { address: body.place_address } })
.spread((place, created) => place)
} catch (e) {
console.log('error', e)
2019-02-26 00:02:42 +01:00
}
await event.setPlace(place)
await event.setTags([])
if (body.tags) {
2019-02-26 01:17:52 +01:00
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
2019-03-13 18:13:00 +01:00
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
2019-02-26 00:02:42 +01:00
await event.addTags(tags)
2019-02-26 01:17:52 +01:00
}
const newEvent = await Event.findByPk(event.id, { include: [User, Tag, Place] })
2019-02-26 00:02:42 +01:00
return res.json(newEvent)
},
async getAuthURL (req, res) {
const instance = req.body.instance
const is_admin = req.body.admin && req.user.is_admin
const callback = `${config.baseurl}/${is_admin ? 'admin/oauth' : 'settings'}`
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
config.title, 'read write', callback)
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
`https://${instance}`, 'read write', callback)
if (is_admin) {
await settingsController.setAdminSetting('mastodon_auth', { client_id, client_secret, instance })
} else {
req.user.mastodon_auth = { client_id, client_secret, instance }
await req.user.save()
}
2019-02-26 00:02:42 +01:00
res.json(url)
},
async code (req, res) {
const { code, is_admin } = req.body
let client_id, client_secret, instance
const callback = `${config.baseurl}/${is_admin ? 'admin/oauth' : 'settings'}`
if (is_admin) {
const settings = await settingsController.settings();
({ client_id, client_secret, instance } = settings.mastodon_auth)
} else {
({ client_id, client_secret, instance } = req.user.mastodon_auth)
}
2019-02-26 00:02:42 +01:00
try {
const token = await Mastodon.getAccessToken(client_id, client_secret, code,
`https://${instance}`, callback)
const mastodon_auth = { client_id, client_secret, access_token: token, instance }
if (is_admin) {
await settingsController.setAdminSetting('mastodon_auth', mastodon_auth)
res.json(instance)
} else {
req.user.mastodon_auth = mastodon_auth
await req.user.save()
// await bot.add(req.user, token)
res.json(req.user)
}
2019-02-26 00:02:42 +01:00
} catch (e) {
res.json(e)
}
},
async current (req, res) {
res.json(req.user)
},
async getAll (req, res) {
const users = await User.findAll({
order: [['createdAt', 'DESC']]
})
res.json(users)
},
async update (req, res) {
const user = await User.findByPk(req.body.id)
if (user) {
if (!user.is_active && req.body.is_active) {
await mail.send(user.email, 'confirm', { user, config })
}
2019-02-26 00:02:42 +01:00
await user.update(req.body)
res.json(user)
} else {
res.send(400)
}
},
async register (req, res) {
const n_users = await User.count()
2019-02-26 00:02:42 +01:00
try {
if (n_users === 0) {
// the first registered user will be an active admin
req.body.is_active = req.body.is_admin = true
} else {
req.body.is_active = false
}
2019-02-26 00:02:42 +01:00
const user = await User.create(req.body)
try {
mail.send(user.email, 'register', { user })
} catch (e) {
return res.status(400).json(e)
}
const payload = { email: user.email }
const token = jwt.sign(payload, config.secret)
res.json({ user, token })
} catch (e) {
res.status(404).json(e)
}
}
}
module.exports = userController