2019-06-21 22:01:34 +00:00
/ *
* v1 Controller
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
* Put some description here !
* /
const Project = _flitter . model ( 'v1:Project' )
const Out = _flitter . model ( 'v1:Out' )
2019-06-24 16:45:22 +00:00
const Invite = _flitter . model ( 'v1:Invite' )
2019-06-21 22:01:34 +00:00
class v1 {
/ *
* Serve the main page .
* /
async main ( req , res ) {
const projects = await Project . find ( { archived : false , user _id : req . session . auth . uuid } )
2019-06-23 17:17:35 +00:00
let find = {
shared _user _ids : {
$elemMatch : {
$eq : req . session . auth . uuid
}
}
}
const shared _projects = await Project . find ( find )
2019-06-21 22:01:34 +00:00
/ *
* Return the main view .
* It must be passed the response .
* View parameters can be passed as an optional third
* argument to the view ( ) method .
* /
2019-06-23 17:17:35 +00:00
return _flitter . view ( res , 'dash_v1:main' , { projects , shared _projects , user : req . session . auth . user } )
2019-06-21 22:01:34 +00:00
}
new _project _show ( req , res , next ) {
2019-06-22 17:51:29 +00:00
return _flitter . view ( res , 'dash_v1:project' , { show _back : true , title : 'Create New Project' } )
}
async project _edit _show ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) {
2019-06-23 17:17:35 +00:00
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
}
// check access perms
if ( ! ( project . user _id === req . session . auth . uuid ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permissions to edit this project.' } )
2019-06-22 17:51:29 +00:00
}
2019-06-23 17:17:35 +00:00
return _flitter . view ( res , 'dash_v1:project' , { show _back : true , title : 'Update Project' , project _name : project . name , user : req . session . auth . user } )
2019-06-22 17:51:29 +00:00
}
async project _edit _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) {
2019-06-23 17:17:35 +00:00
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
2019-06-22 17:51:29 +00:00
}
if ( ! req . body || ! req . body . name ) {
2019-06-23 17:17:35 +00:00
return _flitter . view ( res , 'dash_v1:project' , { user : req . session . auth . user , show _back : true , title : 'Update Project' , project _name : project . name , errors : [ 'Project name is required.' ] } )
}
// check access perms
if ( ! ( project . user _id === req . session . auth . uuid ) ) {
return _flitter . error ( res , 401 , { reason : 'Project not found with the specified ID.' } )
2019-06-22 17:51:29 +00:00
}
project . name = req . body . name
await project . save ( )
return res . redirect ( '/dash/v1' )
2019-06-21 22:01:34 +00:00
}
async new _project _do ( req , res , next ) {
if ( ! req . body . name ) {
2019-06-23 17:17:35 +00:00
return _flitter . view ( res , 'dash_v1:project' , { user : req . session . auth . user , show _back : true , title : 'Create Project' , errors : [ 'Project name is required.' ] } )
2019-06-21 22:01:34 +00:00
}
const project = new Project ( {
name : req . body . name ,
user _id : req . session . auth . uuid ,
data : JSON . stringify ( {
created : Date . now ( ) ,
modified : Date . now ( )
} ) ,
shared _user _ids : [ ] ,
} )
await project . save ( )
return res . redirect ( '/dash/v1' )
}
async project _view ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) {
2019-06-23 17:17:35 +00:00
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
2019-06-21 22:01:34 +00:00
}
2019-06-22 17:51:29 +00:00
const outs = await Out . find ( { project _id : project . id } ) . sort ( '-created' )
2019-06-23 17:17:35 +00:00
if ( ! ( project . user _id === req . session . auth . uuid ) && ! ( project . shared _user _ids . includes ( req . session . auth . uuid ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this project.' } )
}
return _flitter . view ( res , 'dash_v1:view' , { user : req . session . auth . user , project , outs , show _back : true , title : 'View: ' + project . name } )
2019-06-21 22:01:34 +00:00
}
async out _view ( req , res , next ) {
const out = await Out . findById ( req . params . id )
2019-06-23 17:17:35 +00:00
if ( ! out ) {
return _flitter . error ( res , 404 , { reason : 'Output not found with the specified ID.' } )
}
let pretty
try {
pretty = JSON . stringify ( JSON . parse ( out . data ) , null , 4 )
}
catch ( e ) {
return _flitter . error ( res , 500 , { reason : 'Unable to parse output data. Data contains invalid JSON.' } )
}
const project = await Project . findById ( out . project _id )
2019-06-21 22:01:34 +00:00
2019-06-23 17:17:35 +00:00
if ( ! project || ( ! ( project . user _id === req . session . auth . uuid ) && ! ( project . shared _user _ids . includes ( req . session . auth . uuid ) ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this project.' } )
}
2019-06-21 22:01:34 +00:00
// TODO permission access check
2019-06-24 16:45:22 +00:00
return _flitter . view ( res , 'dash_v1:out' , { project , user : req . session . auth . user , out , prettyd : pretty , show _back : true , title : out . brief , title _small : true } ) ;
2019-06-22 17:51:29 +00:00
}
2019-06-23 17:17:35 +00:00
async out _delete ( req , res , next ) {
const out = await Out . findById ( req . params . id )
2019-06-22 17:51:29 +00:00
2019-06-23 17:17:35 +00:00
const project = await Project . findById ( req . params . project )
if ( ! project || ( ! ( project . user _id === req . session . auth . uuid ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
}
if ( out ) {
await out . delete ( )
}
return res . redirect ( '/dash/v1/project/view/' + req . params . project )
}
async project _delete _show ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) {
return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
}
if ( ! ( project . user _id === req . session . auth . uuid ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
}
return _flitter . view ( res , 'dash_v1:confirm' , { user : req . session . auth . user , project , show _back : true , title : 'Are you sure?' , text : 'Deleting this project will remove all stored breakpoint data. This action cannot be undone.' , destination : '/dash/v1/project/delete/' + req . params . id } )
2019-06-22 17:51:29 +00:00
}
async project _delete _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
2019-06-23 17:17:35 +00:00
if ( project && ( ! ( project . user _id === req . session . auth . uuid ) ) ) {
return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
}
2019-06-22 17:51:29 +00:00
if ( project ) {
const outs = await Out . find ( { project _id : project . id } )
for ( const key in outs ) {
await outs [ key ] . delete ( )
}
await project . delete ( )
}
return res . redirect ( '/dash/v1' )
2019-06-21 22:01:34 +00:00
}
2019-06-23 17:17:35 +00:00
view _code ( req , res , next ) {
2019-06-24 14:35:49 +00:00
return _flitter . view ( res , 'dash_v1:code' , { user : req . session . auth . user , title : 'Using DevBug Inline' } )
2019-06-23 17:17:35 +00:00
}
async project _share _show ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
if ( ! ( project . user _id === req . session . auth . uuid ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
let find = {
uuid : { $nin : [ ] }
}
find . uuid . $nin . push ( req . session . auth . uuid )
find . uuid . $nin = find . uuid . $nin . concat ( project . shared _user _ids )
const to _share = await _flitter . model ( 'User' ) . find ( find )
find = {
uuid : { $in : find . uuid . $nin }
}
const shared = await _flitter . model ( 'User' ) . find ( find )
return _flitter . view ( res , 'dash_v1:share' , { user : req . session . auth . user , sharing : { to _share , shared } , project , title : 'Share Project: ' + project . name , show _back : true } )
}
async project _share _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
const target _user = await _flitter . model ( 'User' ) . findOne ( { uuid : req . params . user } )
if ( ! target _user ) return _flitter . error ( res , 404 , { reason : 'User not found with the specified ID.' } )
if ( ! ( project . user _id === req . session . auth . uuid ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this project." } )
if ( ! ( project . user _id === target _user . uuid ) && ! ( project . shared _user _ids . includes ( target _user . uuid ) ) ) {
project . shared _user _ids . push ( target _user . uuid )
await project . save ( )
}
return res . redirect ( '/dash/v1/project/share/' + project . id )
}
async project _share _revoke ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
const target _user = await _flitter . model ( 'User' ) . findOne ( { uuid : req . params . user } )
if ( ! target _user ) return _flitter . error ( res , 404 , { reason : 'User not found with the specified ID.' } )
if ( ! ( project . user _id === req . session . auth . uuid || project . shared _user _ids . includes ( req . session . auth . uuid ) ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this project." } )
const to _dash = project . shared _user _ids . includes ( req . session . auth . uuid )
if ( ! ( target _user . uuid === project . user _id ) && ( project . shared _user _ids . includes ( target _user . uuid ) ) ) {
project . shared _user _ids . splice ( project . shared _user _ids . indexOf ( target _user . uuid ) , 1 )
await project . save ( )
}
if ( to _dash ) return res . redirect ( '/dash/v1' )
return res . redirect ( '/dash/v1/project/share/' + project . id )
}
async project _share _transfer ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
const target _user = await _flitter . model ( 'User' ) . findOne ( { uuid : req . params . user } )
if ( ! target _user ) return _flitter . error ( res , 404 , { reason : 'User not found with the specified ID.' } )
if ( ! project . user _id === req . session . auth . uuid ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
project . user _id = target _user . uuid
project . shared _user _ids . push ( req . session . auth . uuid )
await project . save ( )
return res . redirect ( '/dash/v1' )
}
2019-06-24 16:45:22 +00:00
async project _share _invite ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'Project not found with the specified ID.' } )
if ( ! project . user _id === req . session . auth . uuid ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this project.' } )
let share _data = {
project _id : project . id ,
by _user _id : req . session . auth . uuid ,
created _on : Date . now ( )
}
const share = new Invite ( share _data )
await share . save ( )
return _flitter . view ( res , 'dash_v1:invite' , { share , project , title : 'Generate Invite Link' , show _back : true } )
}
async accept _invite ( req , res , next ) {
if ( ! req . session . invite ) return res . redirect ( '/dash/v1' )
const invite = await Invite . findById ( req . session . invite _data . invite )
if ( ! invite ) return _flitter . error ( res , 404 , { reason : 'This invitation is no longer valid. Sorry.' } )
const project = await Project . findById ( req . session . invite _data . project )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'This project no longer exists.' } )
const user = await _flitter . model ( 'User' ) . findById ( req . session . invite _data . user )
if ( ! user ) return _flitter . error ( res , 404 , { reason : 'This user no longer exists. Sorry.' } )
if ( ! project . shared _user _ids . includes ( req . session . auth . uuid ) && ! ( project . user _id === req . session . auth . uuid ) ) {
project . shared _user _ids . push ( req . session . auth . uuid )
await project . save ( )
}
invite . used = true
await invite . save ( )
req . session . invite = false
req . session . invite _data = false
return res . redirect ( '/dash/v1/project/view/' + project . id )
}
2019-06-21 22:01:34 +00:00
}
2019-06-23 17:17:35 +00:00
module . exports = exports = v1