front end, config
This commit is contained in:
@@ -10,12 +10,12 @@ const bot = require('./bot')
|
||||
const userController = {
|
||||
async login (req, res) {
|
||||
// find the user
|
||||
const user = await User.findOne({where: { email: req.body.email }})
|
||||
const user = await User.findOne({ where: { email: req.body.email } })
|
||||
if (!user) {
|
||||
res.status(404).json({ success: false, message: 'AUTH_FAIL' })
|
||||
} else if (user) {
|
||||
if (!user.is_active) {
|
||||
res.status(403).json({success: false, message: 'NOT)CONFIRMED'})
|
||||
res.status(403).json({ success: false, message: 'NOT)CONFIRMED' })
|
||||
}
|
||||
// check if password matches
|
||||
else if (!await user.comparePassword(req.body.password)) {
|
||||
@@ -42,10 +42,9 @@ const userController = {
|
||||
},
|
||||
|
||||
async delEvent (req, res) {
|
||||
//check if event is mine
|
||||
// check if event is mine
|
||||
const event = await Event.findByPk(req.params.id)
|
||||
if (event && (req.user.is_admin || req.user.id === event.userId))
|
||||
{
|
||||
if (event && (req.user.is_admin || req.user.id === event.userId)) {
|
||||
await event.destroy()
|
||||
res.sendStatus(200)
|
||||
} else {
|
||||
@@ -67,30 +66,32 @@ const userController = {
|
||||
eventDetails.image_path = req.file.path
|
||||
}
|
||||
|
||||
//create place
|
||||
// create place
|
||||
let place
|
||||
try {
|
||||
place = await Place.findOrCreate({where: {name: body.place_name},
|
||||
defaults: {address: body.place_address }})
|
||||
.spread((place, created) => place)
|
||||
} catch(e) {
|
||||
place = await Place.findOrCreate({ where: { name: body.place_name },
|
||||
defaults: { address: body.place_address } })
|
||||
.spread((place, created) => place)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
let event = await Event.create(eventDetails)
|
||||
await event.setPlace(place)
|
||||
|
||||
|
||||
// create/assign tags
|
||||
console.log(body.tags)
|
||||
if (body.tags) {
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t})), {ignoreDuplicates: true})
|
||||
const tags = await Tag.findAll({where: { tag: body.tags }})
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
||||
const tags = await Tag.findAll({ where: { tag: body.tags } })
|
||||
await event.addTags(tags)
|
||||
}
|
||||
await req.user.addEvent(event)
|
||||
event = await Event.findByPk(event.id, {include: [User, Tag, Place]})
|
||||
event = await Event.findByPk(event.id, { include: [User, Tag, Place] })
|
||||
// check if bot exists
|
||||
if (req.user.mastodon_auth) {
|
||||
const post = await bot.post(req.user, event)
|
||||
event.activitypub_id = post.id
|
||||
event.save()
|
||||
}
|
||||
return res.json(event)
|
||||
},
|
||||
@@ -101,25 +102,27 @@ const userController = {
|
||||
await event.update(body)
|
||||
let place
|
||||
try {
|
||||
place = await Place.findOrCreate({where: {name: body.place_name},
|
||||
defaults: {address: body.place_address }})
|
||||
.spread((place, created) => place)
|
||||
} catch(e) {
|
||||
console.log('catch', e)
|
||||
place = await Place.findOrCreate({ where: { name: body.place_name },
|
||||
defaults: { address: body.place_address } })
|
||||
.spread((place, created) => place)
|
||||
} catch (e) {
|
||||
console.log('error', e)
|
||||
}
|
||||
await event.setPlace(place)
|
||||
await event.setTags([])
|
||||
console.log(body.tags)
|
||||
if (body.tags) {
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t})), {ignoreDuplicates: true})
|
||||
const tags = await Tag.findAll({where: { tag: body.tags }})
|
||||
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
||||
const tags = await Tag.findAll({ where: { tag: body.tags } })
|
||||
await event.addTags(tags)
|
||||
}
|
||||
const newEvent = await Event.findByPk(event.id, {include: [User, Tag, Place]})
|
||||
}
|
||||
const newEvent = await Event.findByPk(event.id, { include: [User, Tag, Place] })
|
||||
// check if bot exists
|
||||
if (req.user.mastodon_auth) {
|
||||
const post = await bot.post(req.user, newEvent)
|
||||
}
|
||||
event.activitypub_id = post.id
|
||||
await event.save()
|
||||
}
|
||||
return res.json(newEvent)
|
||||
},
|
||||
|
||||
@@ -133,7 +136,7 @@ const userController = {
|
||||
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`, 'eventi', 'read write', `${config.baseurl}/settings`)
|
||||
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret, `https://${instance}`, 'read write', `${config.baseurl}/settings`)
|
||||
console.log(req.user)
|
||||
req.user.instance = instance
|
||||
req.user.mastodon_instance = instance
|
||||
req.user.mastodon_auth = { client_id, client_secret }
|
||||
await req.user.save()
|
||||
res.json(url)
|
||||
@@ -142,13 +145,13 @@ const userController = {
|
||||
async code (req, res) {
|
||||
const code = req.body.code
|
||||
const { client_id, client_secret } = req.user.mastodon_auth
|
||||
const instance = req.user.instance
|
||||
const instance = req.user.mastodon_instance
|
||||
try {
|
||||
const token = await Mastodon.getAccessToken(client_id, client_secret, code, `https://${instance}`, '${config.baseurl}/settings')
|
||||
const mastodon_auth = { client_id, client_secret, access_token: token}
|
||||
const token = await Mastodon.getAccessToken(client_id, client_secret, code, `https://${instance}`, `${config.baseurl}/settings`)
|
||||
const mastodon_auth = { client_id, client_secret, access_token: token }
|
||||
req.user.mastodon_auth = mastodon_auth
|
||||
await req.user.save()
|
||||
await botController.add(token)
|
||||
await bot.add(token)
|
||||
res.json(req.user)
|
||||
} catch (e) {
|
||||
res.json(e)
|
||||
|
||||
Reference in New Issue
Block a user