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-07-23 21:44:40 +00:00
const Snippet = _flitter . model ( 'v1:Snippet' )
2019-07-24 14:52:58 +00:00
const share _api = {
project : Project ,
snippet : Snippet ,
}
2019-07-24 17:05:27 +00:00
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
} ,
}
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
2019-07-10 19:10:36 +00:00
let view _find = {
2019-06-23 17:17:35 +00:00
shared _user _ids : {
$elemMatch : {
$eq : req . session . auth . uuid
}
}
}
2019-07-10 19:10:36 +00:00
let edit _find = {
edit _user _ids : {
$elemMatch : {
$eq : req . session . auth . uuid
}
}
}
2019-06-23 17:17:35 +00:00
2019-07-10 19:10:36 +00:00
const shared _projects = {
view : await Project . find ( view _find ) ,
edit : await Project . find ( edit _find ) ,
}
2019-06-21 22:01:34 +00:00
2019-07-24 17:05:27 +00:00
const shared _snippets = {
view : await Snippet . find ( view _find ) ,
edit : await Snippet . find ( edit _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-07-24 17:05:27 +00:00
return _flitter . view ( res , 'dash_v1:main' , { projects , shared _projects , shared _snippets , 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
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission . project . edit ( project , req . session . auth . user ) ) {
2019-06-23 17:17:35 +00:00
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
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission . project . edit ( project , req . session . auth . user ) ) {
2019-07-10 19:10:36 +00:00
return _flitter . error ( res , 401 , { reason : 'You do not have permissions to edit this project.' } )
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-07-24 17:05:27 +00:00
const snippets = await Snippet . find ( { project _id : project . id } )
2019-06-23 17:17:35 +00:00
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission . project . view ( project , req . session . auth . user ) ) {
2019-06-23 17:17:35 +00:00
return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this project.' } )
}
2019-07-24 17:05:27 +00:00
return _flitter . view ( res , 'dash_v1:view' , { user : req . session . auth . user , snippets , 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 )
2019-07-10 19:10:36 +00:00
console . log ( 'Pretty out: ' , pretty )
2019-06-23 17:17:35 +00:00
}
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-07-24 17:05:27 +00:00
if ( ! project || ( ! await devbug . permission . project . view ( project , req . session . auth . user ) ) ) {
2019-06-23 17:17:35 +00:00
return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this project.' } )
}
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 )
2019-07-24 17:05:27 +00:00
if ( ! project || ( ! await devbug . permission . project . edit ( project , req . session . auth . user ) ) ) {
2019-06-23 17:17:35 +00:00
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.' } )
}
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission . project . owns ( project , req . session . auth . user ) ) {
2019-06-23 17:17:35 +00:00
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
2019-07-24 17:05:27 +00:00
if ( project && ( ! await devbug . permission . project . owns ( project , req . session . auth . user ) ) ) {
2019-06-23 17:17:35 +00:00
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 ) {
2019-07-24 14:52:58 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
2019-06-23 17:17:35 +00:00
2019-07-24 14:52:58 +00:00
if ( ! project ) return _flitter . error ( res , 404 , { reason : req . params . api + ' not found with the specified ID.' } )
2019-06-23 17:17:35 +00:00
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . owns ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this ' + req . params . api + '.' } )
2019-06-23 17:17:35 +00:00
2019-07-10 19:10:36 +00:00
// Find read-only users
const read _find = {
uuid : { $in : [ ] }
2019-06-23 17:17:35 +00:00
}
2019-07-10 19:10:36 +00:00
read _find . uuid . $in = read _find . uuid . $in . concat ( project . shared _user _ids )
const read = await _flitter . model ( 'User' ) . find ( read _find )
// Find edit users
const edit _find = {
uuid : { $in : [ ] }
}
edit _find . uuid . $in = edit _find . uuid . $in . concat ( project . edit _user _ids )
const edit = await _flitter . model ( 'User' ) . find ( edit _find )
// Find other users
const other _find = {
uuid : { $nin : [ project . user _id ] }
}
other _find . uuid . $nin = other _find . uuid . $nin . concat ( project . edit _user _ids ) . concat ( project . shared _user _ids )
const other = await _flitter . model ( 'User' ) . find ( other _find )
// Get the owner user
const owner = await _flitter . model ( 'User' ) . findOne ( { uuid : project . user _id } )
const sharing = {
read ,
edit ,
other ,
owner ,
current _owns : ( project . user _id === req . session . auth . uuid )
2019-06-23 17:17:35 +00:00
}
2019-07-24 14:52:58 +00:00
return _flitter . view ( res , 'dash_v1:share' , { user : req . session . auth . user , sharing , item : project , api : req . params . api , title : 'Share ' + req . params . api + ': ' + project . name , show _back : true } )
2019-06-23 17:17:35 +00:00
}
async project _share _do ( req , res , next ) {
2019-07-24 14:52:58 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : req . params . api + ' not found with the specified ID.' } )
2019-06-23 17:17:35 +00:00
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.' } )
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . owns ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this " + req . params . api + "." } )
2019-06-23 17:17:35 +00:00
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 ( )
}
2019-07-24 14:52:58 +00:00
return res . redirect ( '/dash/v1/' + req . params . api + '/share/' + project . id )
2019-06-23 17:17:35 +00:00
}
2019-07-10 19:10:36 +00:00
async project _share _edit _do ( req , res , next ) {
2019-07-24 14:52:58 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
2019-07-10 19:10:36 +00:00
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.' } )
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . owns ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this " + req . params . api + "." } )
2019-07-10 19:10:36 +00:00
if ( ! ( project . user _id === target _user . uuid ) && ! ( project . edit _user _ids . includes ( target _user . uuid ) ) ) {
// check if read access. If so, revoke.
if ( project . shared _user _ids . includes ( target _user . uuid ) ) {
project . shared _user _ids . splice ( project . shared _user _ids . indexOf ( target _user . uuid ) , 1 )
}
project . edit _user _ids . push ( target _user . uuid )
await project . save ( )
}
2019-07-24 14:52:58 +00:00
return res . redirect ( '/dash/v1/' + req . params . api + '/share/' + project . id )
2019-07-10 19:10:36 +00:00
}
2019-06-23 17:17:35 +00:00
async project _share _revoke ( req , res , next ) {
2019-07-24 14:52:58 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : req . params . api + ' not found with the specified ID.' } )
2019-06-23 17:17:35 +00:00
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.' } )
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . view ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this " + req . params . api + "." } )
2019-06-23 17:17:35 +00:00
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' )
2019-07-24 14:52:58 +00:00
return res . redirect ( '/dash/v1/' + req . params . api + '/share/' + project . id )
2019-06-23 17:17:35 +00:00
}
2019-07-10 19:10:36 +00:00
async project _share _revoke _edit ( req , res , next ) {
2019-07-24 14:52:58 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : req . params . api + ' not found with the specified ID.' } )
2019-07-10 19:10:36 +00:00
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.' } )
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . view ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : "You do not have permission to edit this " + req . params . api + "." } )
2019-07-10 19:10:36 +00:00
const to _dash = project . edit _user _ids . includes ( req . session . auth . uuid )
if ( ! ( target _user . uuid === project . user _id ) && ( project . edit _user _ids . includes ( target _user . uuid ) ) ) {
project . edit _user _ids . splice ( project . edit _user _ids . indexOf ( target _user . uuid ) , 1 )
await project . save ( )
}
if ( to _dash ) return res . redirect ( '/dash/v1' )
2019-07-24 14:52:58 +00:00
return res . redirect ( '/dash/v1/' + req . params . api + '/share/' + project . id )
2019-07-10 19:10:36 +00:00
}
2019-06-23 17:17:35 +00:00
async project _share _transfer ( req , res , next ) {
2019-07-24 14:52:58 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : req . params . api + ' not found with the specified ID.' } )
2019-06-23 17:17:35 +00:00
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.' } )
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . owns ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this ' + req . params . api + '.' } )
2019-06-23 17:17:35 +00:00
project . user _id = target _user . uuid
project . shared _user _ids . push ( req . session . auth . uuid )
2019-06-24 19:15:09 +00:00
2019-07-10 19:10:36 +00:00
if ( project . shared _user _ids . includes ( target _user . uuid ) ) {
2019-06-24 19:15:09 +00:00
project . shared _user _ids . splice ( project . shared _user _ids . indexOf ( target _user . uuid ) , 1 )
}
2019-07-10 19:10:36 +00:00
if ( project . edit _user _ids . includes ( target _user . uuid ) ) {
project . edit _user _ids . splice ( project . edit _user _ids . indexOf ( target _user . uuid ) , 1 )
}
2019-06-23 17:17:35 +00:00
await project . save ( )
return res . redirect ( '/dash/v1' )
}
2019-06-24 16:45:22 +00:00
async project _share _invite ( req , res , next ) {
2019-07-24 17:05:27 +00:00
const share _model = share _api [ req . params . api ]
if ( ! share _model ) return _flitter . error ( res , 400 , { reason : 'Invalid Share API endpoint.' } )
const project = await share _model . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : req . params . api + ' not found with the specified ID.' } )
2019-06-24 16:45:22 +00:00
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission [ req . params . api ] . owns ( project , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this ' + req . params . api + '.' } )
2019-06-24 16:45:22 +00:00
let share _data = {
project _id : project . id ,
2019-07-24 17:05:27 +00:00
api _type : req . params . api ,
2019-06-24 16:45:22 +00:00
by _user _id : req . session . auth . uuid ,
created _on : Date . now ( )
}
const share = new Invite ( share _data )
await share . save ( )
2019-07-24 17:05:27 +00:00
return _flitter . view ( res , 'dash_v1:invite' , { share , project , title : 'Sharing link for ' + req . params . api , show _back : true } )
2019-06-24 16:45:22 +00:00
}
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.' } )
2019-07-24 17:05:27 +00:00
const share _model = share _api [ invite . api _type ] ;
const project = await share _model . findById ( req . session . invite _data . project )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'This ' + invite . api _type + ' no longer exists.' } )
2019-06-24 16:45:22 +00:00
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
2019-07-24 17:05:27 +00:00
return res . redirect ( await share _views [ invite . api _type ] ( project ) )
2019-06-24 16:45:22 +00:00
}
2019-07-23 21:44:40 +00:00
async project _snippet _new ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'The specified project does not exist.' } )
return _flitter . view ( res , 'dash_v1:snippet' , { project , user : req . session . auth . user , title : 'Create Snippet' , show _back : true } ) ;
}
async project _snippet _new _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'The specified project does not exist.' } )
if ( ! req . body . title || ! req . body . data ) {
2019-07-24 14:52:58 +00:00
return _flitter . view ( res , 'dash_v1:snippet' , { project , user : req . session . auth . user , title : 'Create Snippet' , show _back : true } )
}
// check required fields: title, data, mode
let fail = false
if ( ! req . body . title ) fail = 'Snippet title is required.'
else if ( ! req . body . data ) fail = 'Snippet data is required.'
else if ( ! req . body . mode ) fail = 'Snippet mode is required.'
if ( fail ) {
return _flitter . view ( res , 'dash_v1:snippet' , { project , user : req . session . auth . user , title : 'Create Snippet' , show _back : true , errors : [ fail ] } )
2019-07-23 21:44:40 +00:00
}
const snippet _data = {
2019-07-24 14:52:58 +00:00
name : req . body . title ,
2019-07-23 21:44:40 +00:00
data : req . body . data ,
2019-07-24 14:52:58 +00:00
mode : req . body . mode ,
2019-07-23 21:44:40 +00:00
user _id : req . session . auth . uuid ,
2019-07-24 14:52:58 +00:00
project _id : project . id ,
}
console . log ( { snippet _data } )
2019-07-23 21:44:40 +00:00
2019-07-24 14:52:58 +00:00
const snippet = new Snippet ( snippet _data )
await snippet . save ( )
2019-07-23 21:44:40 +00:00
return res . redirect ( '/dash/v1/project/snippet/' + req . params . id + '/view/' + snippet . uuid )
}
2019-07-24 14:52:58 +00:00
// TODO access checks
2019-07-23 21:44:40 +00:00
async project _snippet _view ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'The specified project does not exist.' } )
const snippet = await Snippet . findOne ( { uuid : req . params . snippet } )
if ( ! snippet ) return _flitter . error ( res , 404 , { reason : 'The specified snippet does not exist.' } )
2019-07-24 17:05:27 +00:00
if ( ! await devbug . permission . snippet . view ( snippet , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to view this snippet.' } )
const is _owner = await devbug . permission . snippet . owns ( snippet , req . session . auth . user )
return _flitter . view ( res , 'dash_v1:snippet' , { snippet , project , is _owner , user : req . session . auth . user , title : 'Snippet: ' + snippet . name , show _back : true , readonly : true } )
}
async project _snippet _delete ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'The specified project does not exist.' } )
const snippet = await Snippet . findOne ( { uuid : req . params . snippet } )
if ( ! snippet ) return _flitter . error ( res , 404 , { reason : 'The specified snippet does not exist.' } )
if ( ! await devbug . permission . snippet . owns ( snippet , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this snippet.' } )
2019-07-24 14:52:58 +00:00
2019-07-24 17:05:27 +00:00
await snippet . delete ( )
return res . redirect ( '/dash/v1/project/view/' + project . id )
}
async project _snippet _edit ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'The specified project does not exist.' } )
const snippet = await Snippet . findOne ( { uuid : req . params . snippet } )
if ( ! snippet ) return _flitter . error ( res , 404 , { reason : 'The specified snippet does not exist.' } )
if ( ! await devbug . permission . snippet . edit ( snippet , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this snippet.' } )
return _flitter . view ( res , 'dash_v1:snippet' , { project , snippet , user : req . session . auth . user , title : 'Snippet: ' + snippet . name , readonly : false , show _back : true } )
}
async project _snippet _edit _do ( req , res , next ) {
const project = await Project . findById ( req . params . id )
if ( ! project ) return _flitter . error ( res , 404 , { reason : 'The specified project does not exist.' } )
const snippet = await Snippet . findOne ( { uuid : req . params . snippet } )
if ( ! snippet ) return _flitter . error ( res , 404 , { reason : 'The specified snippet does not exist.' } )
if ( ! await devbug . permission . snippet . edit ( snippet , req . session . auth . user ) ) return _flitter . error ( res , 401 , { reason : 'You do not have permission to edit this snippet.' } )
// check required fields: title, data, mode
let fail = false
if ( ! req . body . title ) fail = 'Snippet title is required.'
else if ( ! req . body . data ) fail = 'Snippet data is required.'
else if ( ! req . body . mode ) fail = 'Snippet mode is required.'
if ( fail ) {
return _flitter . view ( res , 'dash_v1:snippet' , { snippet , project , user : req . session . auth . user , title : 'Update Snippet' , show _back : true , errors : [ fail ] } )
}
snippet . name = req . body . title
snippet . data = req . body . data
snippet . mode = req . body . mode
await snippet . save ( )
2019-07-23 21:44:40 +00:00
2019-07-24 17:05:27 +00:00
return res . redirect ( '/dash/v1/project/snippet/' + project . id + '/view/' + snippet . uuid )
2019-07-23 21:44:40 +00:00
}
2019-07-24 20:30:51 +00:00
async show _usage _page ( req , res , next ) {
const valid _pages = [ 'main' , 'node' , 'php' , 'ecma' , 'api' ] ;
if ( ! valid _pages . includes ( req . params . page ) ) return _flitter . error ( res , 404 , { reason : 'The page could not be found.' } )
const title = req . params . page . charAt ( 0 ) . toUpperCase ( ) + req . params . page . slice ( 1 ) ;
return _flitter . view ( res , 'dash_v1:use:' + req . params . page . replace ( /\W/g , '' ) , {
show _back : true ,
title : 'Using DevBug - ' + title ,
user : req . session . auth . user ,
} )
}
2019-06-21 22:01:34 +00:00
}
2019-06-23 17:17:35 +00:00
module . exports = exports = v1