You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devbug/app/controllers/api/v1.controller.js

138 lines
4.3 KiB

5 years ago
/*
* v1 Controller
* -------------------------------------------------------------
* Put some description here!
*/
const Out = _flitter.model('v1:Out')
const Project = _flitter.model('v1:Project')
5 years ago
const Invite = _flitter.model('v1:Invite')
5 years ago
const Snippet = _flitter.model('v1:Snippet')
const share_api = {
project: Project,
snippet: Snippet,
}
const share_views = {
project: async function(item){
return '/dash/v1/project/view/'+item.id
},
snippet: async function(item){
const project = await Project.findById(item.project_id);
return '/dash/v1/project/snippet/'+project.id+'/view/'+item.uuid
},
}
5 years ago
class v1 {
/*
* Serve the main page.
*/
main(req, res){
/*
* Return the main view.
* It must be passed the response.
* View parameters can be passed as an optional third
* argument to the view() method.
*/
return view(res, 'view_name')
}
async new_out( req, res, next ){
console.log(req.body)
const project = await Project.findOne({ uuid: req.params.key })
if ( !project ){
return res.status(404).send({
success: false,
error: 'Project not found with specified key.'
})
}
if ( !req.body.data ){
return res.status(400).send({
success: false,
error: 'Missing data.'
})
}
let data
try {
data = JSON.parse(req.body.data)
}
catch (e){
return res.status(400).send({
success: false,
error: 'Invalid JSON.'
})
}
if ( !data.brief ){
data.brief = 'Breakpoint. (No Message.)'
}
5 years ago
const out = new Out({
brief: data.brief,
data: JSON.stringify(data.data),
project_id: project.id
})
await out.save()
return res.send({
success: true
})
}
5 years ago
async invite_show(req, res, next){
const invite = await Invite.findById(req.params.id)
if ( !invite ) _flitter.error(res, 404, {reason: 'Invitation not found with the specified URL.'})
if ( invite.used ) _flitter.error(res, 401, {reason: 'This invitation link has been used or has expired.'})
5 years ago
const share_model = share_api[invite.api_type];
const project = await share_model.findById(invite.project_id)
if ( !project ) _flitter.error(res, 404, {reason: 'This '+invite.api_type+' no longer exists.'})
5 years ago
const user = await _flitter.model('User').findOne({uuid: invite.by_user_id})
if ( !user ) _flitter.error(res, 500, {reason: 'This user no longer exists. Sorry.'})
return _flitter.view(res, 'dash_v1:accept', {invite, project, created_by_user: user})
5 years ago
}
async invite_accept(req, res, next){
const invite = await Invite.findById(req.params.id)
if ( !invite ) _flitter.error(res, 404, {reason: 'Invitation not found with the specified URL.'})
if ( invite.used ) _flitter.error(res, 401, {reason: 'This invitation link has been used or has expired.'})
5 years ago
const share_model = share_api[invite.api_type];
const project = await share_model.findById(invite.project_id)
if ( !project ) _flitter.error(res, 404, {reason: 'This '+invite.api_type+' no longer exists.'})
5 years ago
const user = await _flitter.model('User').findOne({uuid: invite.by_user_id})
if ( !user ) _flitter.error(res, 500, {reason: 'This user no longer exists. Sorry.'})
// if we're signed in
if ( req.session.auth && req.session.auth.user ){
if ( project.user_id !== req.session.auth.uuid && !project.shared_user_ids.includes(req.session.auth.uuid) ){
project.shared_user_ids.push(req.session.auth.uuid)
await project.save()
}
invite.used = true
await invite.save()
5 years ago
return res.redirect(await share_views[invite.api_type](project))
5 years ago
}
else {
req.session.invite = true
req.session.invite_data = {
invite: invite.id,
project: project.id,
user: user.id,
}
return res.redirect('/dash/v1')
}
}
5 years ago
}
module.exports = exports = v1