diff --git a/app/api/v0/accounts/UserCreate.js b/app/api/v0/accounts/UserCreate.js deleted file mode 100644 index 590234da..00000000 --- a/app/api/v0/accounts/UserCreate.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const uuid = require( 'uuid/v4' ) -const jwt = require( 'jsonwebtoken' ) - -const User = require( '../../../../models/User' ) - -module.exports = function( req, res ) { - winston.debug( 'register new user route' ) - if ( !req.body.email ) { res.status( 400 ); return res.send( { success: false, message: 'Do not fuck with us. Give us your email.' } ) } - if ( !req.body.password ) { res.status( 400 ); return res.send( { success: false, message: 'Passwords are a necessary evil, fam.' } ) } - - let myUser = new User( { - email: req.body.email, - password: req.body.password, - company: req.body.company, - name: req.body.name ? req.body.name : 'Anonymous', - surname: req.body.surname ? req.body.surname : '', - apitoken: null - } ) - - let sessionSecret = process.env.SESSION_SECRET - - User.findOne( { 'email': req.body.email } ) - .then( user => { - if ( user ) throw new Error( 'Email taken. Please login. Thanks!') - myUser.apitoken = 'JWT ' + jwt.sign( { _id: myUser._id }, sessionSecret, { expiresIn: '2y' } ) - return myUser.save( ) - } ) - .then( savedUser => { - let token = 'JWT ' + jwt.sign( { _id: myUser._id, name: myUser.name }, sessionSecret, { expiresIn: '24h' } ) - return res.send( { success: true, message: 'User saved. Redirect to login.', apitoken: savedUser.apitoken, token: token } ) - } ) - .catch( err => { - res.status( 400 ) - return res.send( { success: false, message: err.message } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserGet.js b/app/api/v0/accounts/UserGet.js deleted file mode 100644 index a89a1322..00000000 --- a/app/api/v0/accounts/UserGet.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') - -const User = require('../../../../models/User') - -module.exports = function ( req, res ) { - User.findOne( { _id : req.user._id }, '-password' ) - .then( myUser => { - if( !myUser ) throw new Error( 'no user found.' ) - res.send( { success: true, user: myUser } ) - }) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserGetClients.js b/app/api/v0/accounts/UserGetClients.js deleted file mode 100644 index a85d3c10..00000000 --- a/app/api/v0/accounts/UserGetClients.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) - -const UserAppClient = require( '../../../../models/UserAppClient' ) - -module.exports = ( req, res ) => { - UserAppClient.find( { owner: req.user._id } ) - .then( clients => { - if ( !clients ) throw new Error( 'Failed to find clients.' ) - res.send( { success: true, message: 'Stream list for user ' + req.user._id, clients: clients } ) - } ) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserGetStreams.js b/app/api/v0/accounts/UserGetStreams.js deleted file mode 100644 index e43e6346..00000000 --- a/app/api/v0/accounts/UserGetStreams.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const mongoose = require( 'mongoose' ) - -const DataStream = require( '../../../../models/DataStream' ) - -module.exports = ( req, res ) => { - winston.debug( chalk.bgGreen( 'Getting *all* streams for user ', req.user._id ) ) - let userSelect = '_id name surname email company' - let userOwnedStreams = [ ] - DataStream.find( { owner: req.user._id }, '-layers -objects' ) - .populate( { path: 'canRead', select: userSelect } ) - .populate( { path: 'canWrite', select: userSelect } ) - .then( streams => { - userOwnedStreams = streams - return DataStream.find( { '$or': [ { 'canWrite': mongoose.Types.ObjectId( req.user._id ) }, { 'canRead': mongoose.Types.ObjectId( req.user._id ) } ] } , '-layers -objects' ) - .populate({path: 'owner', select: userSelect}) - } ) - .then( sharedWithStreams => { - res.send( { success: true, message: 'Stream list for user ' + req.user._id, streams: userOwnedStreams, sharedStreams: sharedWithStreams } ) - } ) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: 'Something failed.' } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserLogin.js b/app/api/v0/accounts/UserLogin.js deleted file mode 100644 index 31993333..00000000 --- a/app/api/v0/accounts/UserLogin.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') -const jwt = require('jsonwebtoken') - -const User = require('../../../../models/User') - -module.exports = function( req, res ) { - if( !req.body.email ) return res.send( { success: false, message:'Do not fuck with us'} ) - if( !req.body.password ) return res.send( { success: false, message:'Do not fuck with us'} ) - - let sessionSecret = process.env.SESSION_SECRET - - User.findOne( { 'email': req.body.email.toLowerCase() } ) - .then( myUser => { - if( !myUser ) throw 'Invalid credentials.' - myUser.validatePassword( req.body.password, myUser.password, match => { - if( match === false ) { - res.status( 401 ) - return res.send( { success: false, message: 'Invalid credentials.' } ) - } - myUser.logins.push( { date: Date.now() } ) - myUser.save() - let token = 'JWT ' + jwt.sign( { _id: myUser._id, name: myUser.name }, sessionSecret, { expiresIn: '24h' } ) - res.send( { success: true, token: token, apiToken: myUser.apitoken, message:'You have logged in.' } ) - }) - } ) - .catch( err => { - res.status( 401 ) - res.send( { success: false, message: err } ) - }) -} diff --git a/app/api/v0/accounts/UserProfile.js b/app/api/v0/accounts/UserProfile.js deleted file mode 100644 index d5c24c1b..00000000 --- a/app/api/v0/accounts/UserProfile.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') - -const User = require('../../../../models/User') - -module.exports = function ( req, res ) { - if( !req.body.userId ) { - res.status(400) - res.send( { success: false, message: "Malformed request." } ) - } - User.findOne( { _id : req.body.userId }, '_id name surname email company' ) - .then( user => { - if( !user ) throw new Error( 'no users found.' ) - res.send( { success: true, users: user } ) - }) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserPut.js b/app/api/v0/accounts/UserPut.js deleted file mode 100644 index 282abc87..00000000 --- a/app/api/v0/accounts/UserPut.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') - -const User = require('../../../../models/User') - -module.exports = function ( req, res ) { - User.findOne( { _id : req.user._id }, '-password' ) - .then( user => { - if( !user ) throw new Error( 'no user found.' ) - user.name = req.body.name ? req.body.name : user.name - user.surname = req.body.surname ? req.body.surname : user.surname - user.company = req.body.company ? req.body.company : user.company - user.markModified('name') - user.markModified('surname') - user.markModified('company') - return user.save() - }) - .then( result => { - res.send( { success: true, message: 'User profile updated.' } ) - }) - .catch( err => { - res.status(400) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserSearch.js b/app/api/v0/accounts/UserSearch.js deleted file mode 100644 index 0835171c..00000000 --- a/app/api/v0/accounts/UserSearch.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') - -const User = require('../../../../models/User') - -module.exports = function ( req, res ) { - if( !req.body.email ) { - res.status(400) - return res.send( { success: false, message: "Malformed request." } ) - } - if( req.body.email.length < 2 ) { - res.status(400) - return res.send( { success: false, message: "Please provide more than two letters." } ) - } - User.find( { email : { "$regex": req.body.email, "$options": "i" } }, '_id name surname email company' ).limit( 5 ) - .then( myUsers => { - if( !myUsers ) throw new Error( 'no users found.' ) - res.send( { success: true, users: myUsers } ) - }) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/accounts/UserUpdate.js b/app/api/v0/accounts/UserUpdate.js deleted file mode 100644 index 59600f41..00000000 --- a/app/api/v0/accounts/UserUpdate.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') - -const User = require('../../../../models/User') - -module.exports = function ( req, res ) { - winston.debug('update user route') - User.findOne( { _id : req.user._id } ) - .then( myUser => { - if( req.body.name ) - myUser.name = req.body.name - if( req.body.company ) - myUser.company = req.body.company - if( req.body.password ) - myUser.password = req.body.password - myUser.save() - .then( () => { - let profile = { - _id: myUser._id, - name: myUser.name, - company: myUser.company, - apitoken: myUser.apitoken, - lastLogin: myUser.logins.length >= 2 ? myUser.logins[ myUser.logins.length - 2 ] : Date.now(), - } - res.send( { success: true, user: profile } ) - }) - - - }) - .catch( err => { - res.send( { success: false, message: err } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/clients/ClientCreate.js b/app/api/v0/clients/ClientCreate.js deleted file mode 100644 index e2e35f6b..00000000 --- a/app/api/v0/clients/ClientCreate.js +++ /dev/null @@ -1,35 +0,0 @@ -const winston = require('winston') -const chalk = require('chalk') -const Client = require( '../../../../models/UserAppClient') - -module.exports = ( req, res ) => { - if( !req.body.client ) { - res.status( 400 ) - return res.send( { success: false, message: 'Malformed request. Client not created.'} ) - } - - if( !req.body.user ) - winston.debug( 'Creating anonymous client: will not save to database.' ) - - let myClient = new Client( { - role: req.body.client.role, - documentName: req.body.client.documentName, - documentType: req.body.client.documentType, - documentGuid: req.body.client.documentGuid, - streamId: req.body.client.streamId, - online: true, - owner: req.user ? req.user._id : '' - } ) - if( !req.user ) - res.send( { success: true, message: 'Anonymous client created.', resource: { clientId: 'temp-'+myClient._id } } ) - else - myClient.save() - .then( result => { - res.send( { success: true, message: 'Client created.', resource: result } ) - }) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString() } ) - }) - -} \ No newline at end of file diff --git a/app/api/v0/clients/ClientGet.js b/app/api/v0/clients/ClientGet.js deleted file mode 100644 index 44f9e147..00000000 --- a/app/api/v0/clients/ClientGet.js +++ /dev/null @@ -1,19 +0,0 @@ -const winston = require('winston') -const chalk = require('chalk') -const Client = require( '../../../../models/UserAppClient') - -module.exports = ( req, res ) => { - if( !req.params.clientId ) { - res.status( 400 ) - return res.send( { success: false, message: 'Malformed request.'} ) - } - Client.findOne( { _id: req.params.clientId } ) - .then( result => { - if( !result ) throw new Error( 'No client found.' ) - res.send( { success: true, message: 'Client found.', resource: result } ) - }) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/clients/ClientPut.js b/app/api/v0/clients/ClientPut.js deleted file mode 100644 index e342950d..00000000 --- a/app/api/v0/clients/ClientPut.js +++ /dev/null @@ -1,26 +0,0 @@ -const winston = require('winston') -const chalk = require('chalk') -const Client = require( '../../../../models/UserAppClient') - -module.exports = ( req, res ) => { - if( !req.params.clientId || !req.body.client ) { - res.status( 400 ) - return res.send( { success: false, message: 'Malformed request.'} ) - } - Client.findOne( { _id: req.params.clientId } ) - .then( result => { - if( !result ) throw new Error( 'No client found.' ) - - for( let key in req.body.client ) - result[ key ] = req.body.client[ key ] - - return result.save() - }) - .then( result => { - res.send( { success: true, message: 'Client updated.' } ) - }) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/helpers/MergeLayers.js b/app/api/v0/helpers/MergeLayers.js deleted file mode 100644 index 80ae4301..00000000 --- a/app/api/v0/helpers/MergeLayers.js +++ /dev/null @@ -1,29 +0,0 @@ -// Gently returns a merged array of stream layers. -// Behaviour: -// If a layer is not present in the new list, it will be removed from the old list -// If a layer is present in the new list and old list, values will be updated (besides the guid) -// If a layer is present only in the new list, it is added. - -module.exports = ( oldLayers, newLayers ) => { - let layersToRemove = [] - for( let layer of oldLayers ) { - let matchingLayer = newLayers.find( l => layer.guid === l.guid ) - if( matchingLayer ) - for( let key in matchingLayer ) - layer[ key ] = matchingLayer[ key ] - else - layersToRemove.push( layer ) - } - - for( let layer of newLayers ) { - let matchingOldLayer = oldLayers.find( l => l.guid === layer.guid ) - if( !matchingOldLayer ) oldLayers.push( layer ) - } - - let result = [] - for( let layer of oldLayers ) { - let matchingLayer = layersToRemove.find( l => l.guid === layer.guid ) - if( !matchingLayer ) result.push( layer ) - } - return result; -} \ No newline at end of file diff --git a/app/api/v0/index.js b/app/api/v0/index.js deleted file mode 100644 index 1c27f08b..00000000 --- a/app/api/v0/index.js +++ /dev/null @@ -1,127 +0,0 @@ -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const passport = require( 'passport' ) - -const tokenCheck = require( './middleware/TokenCheck' ) - -module.exports = function( app, express, urlRoot ) { - var r = new express.Router( ) - - // strict auth will return a 401 if no authorization header is present. pass means req.user exists - let mandatoryAuthorisation = passport.authenticate( 'jwt-strict', { session: false } ) - // relaxed auth allows for annonymous access to the endpoint, but permissions should be checked inside. pass doesn't guarantee req.user - let optionalAuthorisation = passport.authenticate( [ 'jwt-strict', 'anonymous' ], { session: false } ) - - // - // ACCOUNTS - // - // create a new account - r.post( '/accounts/register', require( './accounts/UserCreate' ) ) - // returns a jwt-strict token - r.post( '/accounts/login', require( './accounts/UserLogin' ) ) - - // get profile - r.get( '/accounts/profile', mandatoryAuthorisation, require( './accounts/UserGet' ) ) - // update profile - r.put( '/accounts/profile', mandatoryAuthorisation, require( './accounts/UserPut' ) ) - // todo: r.patch(profile) - // get all streams for a specific user token - r.get( '/accounts/streams', mandatoryAuthorisation, require( './accounts/UserGetStreams' ) ) - // get all clients for a specific user token - r.get( '/accounts/clients', mandatoryAuthorisation, require( './accounts/UserGetClients' ) ) - - // get display profile - r.get( '/accounts/:userId', mandatoryAuthorisation, require( './accounts/UserProfile' ) ) - // search profiles by email (restrict to 10) - r.post( '/accounts/search', mandatoryAuthorisation, require( './accounts/UserSearch' ) ) - - - // - // CLIENTS // - // - r.post( '/clients/', optionalAuthorisation, require( './clients/ClientCreate' ) ) - r.get( '/clients/:clientId', mandatoryAuthorisation, require( './clients/ClientGet' ) ) - r.put( '/clients/:clientId', mandatoryAuthorisation, require( './clients/ClientPut' ) ) - // todo: r.patch(client) - - // - // STREAMS v0 // - // - // create a new stream - r.post( '/streams', mandatoryAuthorisation, require( './streams/StreamPost' ) ) - // get stream / perm check 'read' - r.get( '/streams/:streamId', optionalAuthorisation, require( './streams/StreamGet' ) ) - // update a stream / perm check 'write' - r.put( '/streams/:streamId', mandatoryAuthorisation, require( './streams/StreamPut' ) ) - // patch a stream / perm check 'write' - r.patch( '/streams/:streamId', mandatoryAuthorisation, require( './streams/StreamPatch' ) ) - // delete a stream / perm check 'delete' - r.delete( '/streams/:streamId', mandatoryAuthorisation, require( './streams/StreamDelete' ) ) - // duplicate a stream / perm check 'read' - r.post( '/streams/:streamId/clone', mandatoryAuthorisation, require( './streams/StreamDuplicate' ) ) - // diff a stream against another / perm check 'read' / perm check 'read' - r.get( '/streams/:streamId/diff/:otherId', optionalAuthorisation, require( './streams/StreamDiff' ) ) - - // - // STREAM OBJECTS // - // - // 1. Collection ops - // Get stream objects - r.get( '/streams/:streamId/objects', optionalAuthorisation, require( './streams/ObjectsGet' ) ) - // Add stream objects - r.post( '/streams/:streamId/objects', mandatoryAuthorisation, require( './streams/ObjectsPost' ) ) - // Replace stream objects - r.put( '/streams/:streamId/objects', mandatoryAuthorisation, require( './streams/ObjectsPut' ) ) - // Delete stream object list - r.delete( '/streams/:streamId/objects', mandatoryAuthorisation, require( './streams/ObjectsDelete' ) ) - - // - // OBJECTS // - // These routes are for creating objects. - // - // Create an object - r.post( '/objects', mandatoryAuthorisation, require( './objects/ObjectPost' ) ) - // Create many objects - r.post( '/objects/bulk', mandatoryAuthorisation, require( './objects/ObjectPostBulk' ) ) - // Get an object - r.get( '/objects/:objectId', optionalAuthorisation, require( './objects/ObjectGet' ) ) - // Get more objects - r.post( '/objects/getbulk/', optionalAuthorisation, require( './objects/ObjectsGetBulk' ) ) - // update one - r.put( '/objects/:objectId', mandatoryAuthorisation, require( './objects/ObjectPut' ) ) - // delete one - r.delete( '/objects/:objectId', mandatoryAuthorisation, require( './objects/ObjectDelete' ) ) - - // - // DEPRECATED // - // - // get stream name, should be replaced by allowing queries in GET /stream/:streamId - r.get( '/streams/:streamId/name', optionalAuthorisation, require( './streams/NameGet' ) ) - // update stream name, replaced by patch - r.put( '/streams/:streamId/name', mandatoryAuthorisation, require( './streams/NamePut' ) ) - // Replace stream layers, method still used by the gh client - r.put( '/streams/:streamId/layers', mandatoryAuthorisation, require( './streams/LayersPut' ) ) - - - // generate routes doc - let routes = [ ] - let count = 1 - r.stack.forEach( ( middleware ) => { - if ( middleware.route ) - routes.push( Object.keys( middleware.route.methods ).map( m => m.toUpperCase( ) ) + ': /api' + middleware.route.path ) - } ) - - let serverDescription = { - serverName: process.env.SERVER_NAME, - maxRequestSize: process.env.REQ_SIZE - } - - r.get( '/', ( req, res ) => { - serverDescription.routes = routes - serverDescription.version = '0.x.x' - res.json( serverDescription ) - } ) - - // mount all these routes up - app.use( urlRoot, r ) -} \ No newline at end of file diff --git a/app/api/v0/middleware/PermissionCheck.js b/app/api/v0/middleware/PermissionCheck.js deleted file mode 100644 index 96bce428..00000000 --- a/app/api/v0/middleware/PermissionCheck.js +++ /dev/null @@ -1,64 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const url = require( 'url' ) -const User = require( '../../../../models/User' ) - -module.exports = ( user, operation, resource ) => { - return new Promise( ( resolve, reject ) => { - - if ( !resource ) return reject( new Error( 'Resource not found.' ) ) - if ( user == null ) user = { role: 'guest', _id: '' } - - winston.debug( chalk.bgRed( 'checking perms' ), resource.private, '|', user.role, '|', user._id.toString( ), '|', resource.owner.toString( ), 'id:', resource.streamId ? resource.streamId : resource._id.toString( ) ) - - // admin or owner: anyhting goes - if ( user.role === 'admin' || user._id.toString( ) === resource.owner.toString( ) ) { - winston.debug( chalk.bgGreen( 'checking perms' ), 'user is admin or owner' ) - return resolve( resource ) - } - - if ( operation == null ) { - winston.debug( chalk.bgRed( 'checking perms' ), 'no operation specified' ) - return reject( new Error( `You are not authorised to ${operation}.` ) ) - } - - // let's get basic - let canRead = resource.canRead.map( x => x.toString( ) ) - let canWrite = resource.canWrite.map( x => x.toString( ) ) - - switch ( operation ) { - case 'write': - if ( canWrite.indexOf( user._id.toString( ) ) >= 0 ) { - winston.debug( chalk.bgGreen( 'checking perms' ), `user has ${operation} access` ) - return resolve( resource ) - } - winston.debug( chalk.bgRed( 'checking perms' ), `user has NO ${operation} access` ) - return reject( new Error( `You are not authorised to ${operation}.` ) ) - - case 'read': - if ( resource.private === false ) { - winston.debug( chalk.bgGreen( 'checking perms' ), `${operation} ok, resource is public` ) - return resolve( resource ) - } - if ( canWrite.indexOf( user._id.toString( ) ) >= 0 ) { - winston.debug( chalk.bgGreen( 'checking perms' ), `user has write & ${operation} access` ) - return resolve( resource ) - } - if ( canRead.indexOf( user._id.toString( ) ) >= 0 ) { - winston.debug( chalk.bgGreen( 'checking perms' ), `user has ${operation} access` ) - return resolve( resource ) - } - winston.debug( chalk.bgRed( 'checking perms' ), `user has NO ${operation} access` ) - return reject( new Error( `You are not authorised to ${operation}.` ) ) - - case 'delete': - // ownership or admin is already checked and resolved above - return reject( new Error( 'You do not own the stream.' ) ) - - default: - winston.debug( chalk.bgRed( 'checking perms' ), `operation ${operation} not defined` ) - return reject( new Error( `You are not authorised to ${operation}: unknown.` ) ) - } - } ) -} \ No newline at end of file diff --git a/app/api/v0/middleware/TokenCheck.js b/app/api/v0/middleware/TokenCheck.js deleted file mode 100644 index 7c20afcf..00000000 --- a/app/api/v0/middleware/TokenCheck.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict' -const winston = require('winston') -const chalk = require('chalk') -const User = require('../../../../models/User') - -module.exports = ( req, res, next ) => { - let token = req.get('speckle-token') - if( !token ) { - winston.debug( chalk.bgRed( 'No token provided.' ) ) - res.status(400) - return res.send( { success: false, message:'No token provided.' } ) - } - winston.debug( chalk.blue.underline('token check: ' + token )) - - User.findOne( { apitoken: token } ) - .then( myUser => { - if( !myUser ) { - throw new Error('No user with this token found. Are ye fooling us?') - } - req.user = myUser - return next() - }) - .catch( err => { - winston.debug( chalk.bgRed( 'token check failed: ' + token ) ) - res.status(401) - return res.send( { success: false, message:'Token check failed.', error: err } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/objects/ObjectDelete.js b/app/api/v0/objects/ObjectDelete.js deleted file mode 100644 index 7be97b52..00000000 --- a/app/api/v0/objects/ObjectDelete.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) - -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if( !req.params.objectId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No objectId provided.' } ) - } - - SpeckleObject.findOne( { _id: req.params.objectId } ) - .then( obj => { - if( !obj ) throw new Error( 'No obj found.' ) - obj.deleted = true - return obj.save() - }) - .then( result => { - return res.send( { success: true, message: 'Object was flagged as deleted.' } ) - }) - .catch( err => { - res.status( err.message === 'Unauthorized. Please log in.' ? 401 : 404 ) - res.send( { success: false, message: err.toString() } ) - }) -} \ No newline at end of file diff --git a/app/api/v0/objects/ObjectGet.js b/app/api/v0/objects/ObjectGet.js deleted file mode 100644 index d1ce74d6..00000000 --- a/app/api/v0/objects/ObjectGet.js +++ /dev/null @@ -1,20 +0,0 @@ -const q2m = require( 'query-to-mongo' ) - -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if ( !req.params.objectId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No object id provided.' } ) - } - let query = q2m( req.query ) - SpeckleObject.findOne( { _id: req.params.objectId }, query.options.fields ) - .then( object => { - if ( !object ) throw new Error( 'Could not find object.' ) - res.send( { success: true, resource: object } ) - } ) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/objects/ObjectPost.js b/app/api/v0/objects/ObjectPost.js deleted file mode 100644 index 5e319404..00000000 --- a/app/api/v0/objects/ObjectPost.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) - -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - SpeckleObject.findOne( { hash: req.body.object.hash }, '_id' ) - .then( result => { - if ( result ) { - res.send( { success: true, objectId: result._id, message: 'Object was already there.' } ) - throw new Error( 'Existing object' ) - } - return SpeckleObject.create( req.body.object ) - } ) - .then( object => { - res.send( { success: true, objectId: object._id, message: 'Inserted fresh object ' } ) - } ) - .catch( err => { - if ( err.message === 'Existing object' ) return - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/objects/ObjectPostBulk.js b/app/api/v0/objects/ObjectPostBulk.js deleted file mode 100644 index 0c8ece21..00000000 --- a/app/api/v0/objects/ObjectPostBulk.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const _ = require( 'lodash' ) - -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -// (1) Update all the objects that have an _id (replaces them completely) -// (2) Check in the db for existing objects by hash & map their _id to the obj list -// (3) Insert all the new objects -module.exports = ( req, res ) => { - if ( !req.body.objects ) { - res.status( 400 ) - return res.send( { success: false, message: 'Malformed request.' } ) - } - Promise.all( req.body.objects.reduce( ( arr, o ) => ( o._id !== undefined && o.type !== 'Placeholder' ) ? [ SpeckleObject.update( { _id: o._id }, o ), ...arr ] : arr, [ ] ) ) - .then( ( ) => SpeckleObject.find( { hash: { $in: req.body.objects.reduce( ( arr, o ) => o._id === undefined ? [ o.hash, ...arr ] : arr, [ ] ) } }, '_id hash' ) ) - .then( results => { - results.forEach( o => req.body.objects.filter( oo => oo.hash == o.hash ).forEach( oo => oo._id = o._id.toString( ) ) ) - return SpeckleObject.insertMany( req.body.objects.filter( so => so._id === undefined ) ) - } ) - .then( results => { - results.forEach( o => req.body.objects.filter( oo => oo.hash == o.hash ).forEach( oo => oo._id = o._id.toString( ) ) ) - res.send( { success: true, message: 'Saved objects to database.', objectIds: req.body.objects.map( o => o._id ) } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/objects/ObjectPut.js b/app/api/v0/objects/ObjectPut.js deleted file mode 100644 index c7ac6039..00000000 --- a/app/api/v0/objects/ObjectPut.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const mongoose = require( 'mongoose' ) - -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if ( !req.params.objectId ) { - res.status( 400 ) - return res.send( { success: false, message: 'Malformed request.' } ) - } - SpeckleObject.findOneAndUpdate( { _id: req.params.objectId }, req.body.object ) - .then( result => { - res.send( { success: true, message: 'Object updated.' } ) - } ) - .catch( err => { - res.status( 400 ) - return res.send( { success: false, message: err.toString( ) } ) - } ) - -} \ No newline at end of file diff --git a/app/api/v0/objects/ObjectsGetBulk.js b/app/api/v0/objects/ObjectsGetBulk.js deleted file mode 100644 index 1c64ef83..00000000 --- a/app/api/v0/objects/ObjectsGetBulk.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) -const q2m = require( 'query-to-mongo' ) - -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if ( !req.body.objects ) { - res.status( 400 ) - return res.send( { success: false, message: 'No object ids provided.' } ) - } - - let query = q2m( req.query ) - - query.criteria[ '_id' ] = { $in: req.body.objects } - SpeckleObject.find( query.criteria, query.options.fields, { sort: query.options.sort } ) - .then( objects => { - let list = req.body.objects.reduce( ( arr, o ) => [ ...arr, objects.find( oo => oo._id.toString( ) === o.toString( ) ) ], [ ] ) - res.send( { success: true, resources: list } ) - } ) - .catch( err => { - res.status( err.message === 'Unauthorized. Please log in.' ? 401 : 404 ) - res.send( { success: false, message: err.message, streamId: req.streamId } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/LayersPut.js b/app/api/v0/streams/LayersPut.js deleted file mode 100644 index b9e29588..00000000 --- a/app/api/v0/streams/LayersPut.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) - -const DataStream = require( '../../../../models/DataStream' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - if ( !req.body.layers ) { - res.status( 400 ) - return res.send( { success: false, message: 'No layers provided.' } ) - } - - DataStream.findOne( { streamId: req.params.streamId } ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - return PermissionCheck( req.user, 'write', stream ) - }) - .then( stream => { - stream.layers = req.body.layers - stream.markModified( 'layers' ) - return stream.save( ) - } ) - .then( stream => { - res.status( 200 ) - return res.send( { success: true, message: 'Stream layers were replaced. WARNING: Deprecated.' } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/NameGet.js b/app/api/v0/streams/NameGet.js deleted file mode 100644 index fbb029d2..00000000 --- a/app/api/v0/streams/NameGet.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) - -const DataStream = require( '../../../../models/DataStream' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - DataStream.findOne( { streamId: req.params.streamId }, 'name private owner canRead canWrite' ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - return PermissionCheck( req.user, 'read', stream ) - } ) - .then( stream => { - res.send( { success: true, name: stream.name, message: 'This api route will be deprecated. Please use PATCH /api/stream/:streamId' } ) - } ) - .catch( err => { - res.status( err.message.indexOf( 'authorised' ) >= 0 ? 401 : 404 ) - res.send( { success: false, message: err.message, streamId: req.streamId } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/NamePut.js b/app/api/v0/streams/NamePut.js deleted file mode 100644 index 871f0a7e..00000000 --- a/app/api/v0/streams/NamePut.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) - -const DataStream = require( '../../../../models/DataStream' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - - winston.debug( chalk.bgGreen( 'Getting stream', req.params.streamId ) ) - - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - DataStream.findOne( { streamId: req.params.streamId } ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - return PermissionCheck( req.user, 'read', stream ) - } ) - .then( stream => { - if ( !req.body.name ) throw new Error( 'No name was provided.' ) - stream.name = req.body.name - return stream.save( ) - } ) - .then( stream => { - res.status( 200 ) - return res.send( { success: true, message: 'Stream name was updated.', streamId: stream.streamId, message: 'This api route will be deprecated. Please use PATCH /api/stream/:streamId' } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ), streamId: req.streamId } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/ObjectsDelete.js b/app/api/v0/streams/ObjectsDelete.js deleted file mode 100644 index b80a320b..00000000 --- a/app/api/v0/streams/ObjectsDelete.js +++ /dev/null @@ -1,43 +0,0 @@ -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) -const _ = require( 'lodash' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId ) { - res.status( 400 ) - res.send( { success: false, message: 'Malformed request.' } ) - } - - DataStream.findOne( { streamId: req.params.streamId } ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - if ( !req.user || !( req.user._id.equals( stream.owner ) || stream.sharedWith.find( id => { req.user._id.equals( id ) } ) ) ) - throw new Error( 'Unauthorized.' ) - - if ( !req.body.objects ) { - winston.debug( 'Deleting all objects from the stream list.' ) - SpeckleObject.updateMany( { '_id': { $in: stream.objects } }, { $pullAll: { partOf: [ req.params.streamId ] } } ).exec( ) - stream.objects = [ ] - stream.markModified( 'objects' ) - return stream.save( ) - } else { - winston.debug( 'Deleting some objects from the stream list.' ) - SpeckleObject.updateMany( { '_id': { $in: req.body.objects } }, { $pullAll: { partOf: [ req.params.streamId ] } } ).exec( ) - stream.objects = _.differenceWith( stream.objects, req.body.objects, ( a, b ) => a.toString( ) === b.toString( ) ) - stream.markModified( 'objects' ) - return stream.save( ) - } - } ) - .then( stream => { - res.send( { success: true, message: 'Deleted objects list.' } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/ObjectsGet.js b/app/api/v0/streams/ObjectsGet.js deleted file mode 100644 index 24bdc937..00000000 --- a/app/api/v0/streams/ObjectsGet.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) -const q2m = require( 'query-to-mongo' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - let streamObjects = [ ] - DataStream.findOne( { streamId: req.params.streamId } ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - if ( stream.private && !req.user ) throw new Error( 'Unauthorized. Please log in.' ) - if ( stream.private && ( !req.user || !( req.user._id.equals( stream.owner ) || stream.sharedWith.find( id => { return req.user._id.equals( id ) } ) ) ) ) - throw new Error( 'Unauthorized. Please log in.' ) - streamObjects = stream.objects.map( o => o.toString( ) ) - let query = q2m( req.query ) - query.criteria[ '_id' ] = { $in: stream.objects } - return SpeckleObject.find( query.criteria, query.options.fields, { sort: query.options.sort, offset: query.options.offset, limit: query.options.limit } ) - } ) - .then( objects => { - let list = streamObjects.reduce( ( arr, o ) => { - let match = objects.find( oo => oo._id == o ) - if( match ) arr.push( match ) - return arr - }, [ ] ) - res.send( { success: true, objects: list, message: 'Object list returned. If querying, duplication of objects in list will not be respected.' } ) - } ) - .catch( err => { - res.status( err.message === 'Unauthorized. Please log in.' ? 401 : 404 ) - res.send( { success: false, message: err.message, streamId: req.streamId } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/ObjectsPost.js b/app/api/v0/streams/ObjectsPost.js deleted file mode 100644 index 75ab7ce8..00000000 --- a/app/api/v0/streams/ObjectsPost.js +++ /dev/null @@ -1,33 +0,0 @@ -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId || !req.body.objects ) { - res.status( 400 ) - res.send( { success: false, message: 'Malformed request.' } ) - } - - DataStream.findOne( { streamId: req.params.streamId } ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - if ( !req.user || !( req.user._id.equals( stream.owner ) || stream.sharedWith.find( id => { req.user._id.equals( id ) } ) ) ) - throw new Error( 'Unauthorized.' ) - - SpeckleObject.updateMany( { '_id': { $in: req.body.objects } }, { $addToSet: { partOf: req.params.streamId } } ).exec( ) - stream.objects.push( ...req.body.objects ) - stream.markModified( 'objects' ) - return stream.save( ) - } ) - .then( stream => { - res.send( { success: true, message: 'Appended objects to stream.', objects: stream.objects } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/ObjectsPut.js b/app/api/v0/streams/ObjectsPut.js deleted file mode 100644 index 4fc775ec..00000000 --- a/app/api/v0/streams/ObjectsPut.js +++ /dev/null @@ -1,38 +0,0 @@ -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) -const _ = require( 'lodash' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId || !req.body.objects ) { - res.status( 400 ) - res.send( { success: false, message: 'Malformed request.' } ) - } - - DataStream.findOne( { streamId: req.params.streamId } ) - .then( stream => { - if ( !stream ) throw new Error( 'No stream found.' ) - if ( !req.user || !( req.user._id.equals( stream.owner ) || stream.sharedWith.find( id => { req.user._id.equals( id ) } ) ) ) - throw new Error( 'Unauthorized.' ) - - // these should be non-hanging - let orphans = _.difference( stream.objects, req.body.objects ) - SpeckleObject.updateMany( { '_id': { $in: orphans } }, { $pullAll: { partOf: [ req.params.streamId ] } } ).exec( ) - SpeckleObject.updateMany( { '_id': { $in: req.body.objects } }, { $addToSet: { partOf: req.params.streamId } } ).exec( ) - - stream.objects = req.body.objects - stream.markModified( 'objects' ) - return stream.save( ) - } ) - .then( stream => { - res.send( { success: true, message: 'Replaced objects list.' } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamDelete.js b/app/api/v0/streams/StreamDelete.js deleted file mode 100644 index 40d1ace9..00000000 --- a/app/api/v0/streams/StreamDelete.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - let stream = {} - DataStream.findOne( { streamId: req.params.streamId } ) - .then( result => { - if ( !result ) throw new Error( 'No stream found.' ) - stream = result - return PermissionCheck( req.user, 'delete', result ) - } ) - .then( () => { - return stream.remove( ) - } ) - .then( result => { - return res.send( { success: true, message: 'Stream was deleted!' } ) - } ) - .catch( err => { - res.status( err.message === 'Unauthorized. Please log in.' ? 401 : 404 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamDiff.js b/app/api/v0/streams/StreamDiff.js deleted file mode 100644 index b2a220b0..00000000 --- a/app/api/v0/streams/StreamDiff.js +++ /dev/null @@ -1,52 +0,0 @@ -const winston = require( 'winston' ) -const _ = require( 'lodash' ) -const DataStream = require( '../../../../models/DataStream' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId || !req.params.otherId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - if ( req.params.streamId == req.params.otherId ) { - res.status( 400 ) - return res.send( { success: false, message: 'Can not diff the same stream, yo!' } ) - } - - let first = {} - let second = {} - - DataStream.find( { streamId: { $in: [ req.params.streamId, req.params.otherId ] } } ).lean( ) - .then( streams => { - if ( streams.length != 2 ) throw new Error( 'Failed to find streams.' ) - - first = streams.find( s => s.streamId === req.params.streamId ) - second = streams.find( s => s.streamId === req.params.otherId ) - - // check if user can read first stream - return PermissionCheck( req.user, 'read', first ) - } ) - .then( ( ) => { - // check if user can read second stream - return PermissionCheck( req.user, 'read', second ) - } ) - .then( ( ) => { - let objects = { common: null, inA: null, inB: null } - objects.inA = _.differenceWith( first.objects, second.objects, ( arrVal, otherVal ) => arrVal.toString( ) === otherVal.toString( ) ) - objects.inB = _.differenceWith( second.objects, first.objects, ( arrVal, otherVal ) => arrVal.toString( ) === otherVal.toString( ) ) - objects.common = _.intersectionWith( first.objects, second.objects, ( arrVal, otherVal ) => arrVal.toString( ) === otherVal.toString( ) ) - - let layers = { common: null, inA: null, inB: null } - layers.common = _.intersectionWith( first.layers, second.layers, ( arrVal, otherVal ) => arrVal.guid === otherVal.guid ) - layers.inA = _.differenceWith( first.layers, second.layers, ( arrVal, otherVal ) => arrVal.guid === otherVal.guid ) - layers.inB = _.differenceWith( second.layers, first.layers, ( arrVal, otherVal ) => arrVal.guid === otherVal.guid ) - - res.send( { success: true, objects: objects, layers: layers } ) - } ) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) - -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamDuplicate.js b/app/api/v0/streams/StreamDuplicate.js deleted file mode 100644 index f74ef668..00000000 --- a/app/api/v0/streams/StreamDuplicate.js +++ /dev/null @@ -1,60 +0,0 @@ -const winston = require( 'winston' ) -const mongoose = require( 'mongoose' ) -const shortId = require( 'shortid' ) - -const DataStream = require( '../../../../models/DataStream' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - let clone = {} - let parent = {} - let stream = {} - - DataStream.findOne( { streamId: req.params.streamId } ) - .then( result => { - stream = result - return PermissionCheck( req.user, 'read', result ) - } ) - .then( ( ) => { - if ( !stream ) throw new Error( 'Database fail.' ) - clone = new DataStream( stream ) - clone._id = mongoose.Types.ObjectId( ) - clone.streamId = shortId.generate( ) - clone.parent = stream.streamId - clone.children = [ ] - clone.name += ' Clone' - clone.createdAt = new Date - clone.updatedAt = new Date - clone.private = stream.private - - if ( req.user._id.toString( ) != stream.owner.toString( ) ) { - // new ownership - clone.owner = req.user._id - // grant original owner read access - clone.canRead = [ stream.owner ] - // make it private - clone.canWrite = [ ] - } - - stream.children.push( clone.streamId ) - clone.isNew = true - return stream.save( ) - } ) - .then( result => { - parent = result - return clone.save( ) - } ) - .then( result => { - res.send( { success: true, clone: { _id: result._id, streamId: clone.streamId }, parent: { _id: parent._id, streamId: parent.streamId, children: parent.children } } ) - } ) - .catch( err => { - res.status( 400 ) - res.send( { success: false, message: err, streamId: req.streamId } ) - } ) - -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamGet.js b/app/api/v0/streams/StreamGet.js deleted file mode 100644 index a83faf22..00000000 --- a/app/api/v0/streams/StreamGet.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const passport = require( 'passport' ) -const chalk = require( 'chalk' ) - - -const DataStream = require( '../../../../models/DataStream' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - let stream = null - DataStream.findOne( { streamId: req.params.streamId } ) - .then( doc => { - stream = doc - return PermissionCheck( req.user, 'read', doc ) - } ) - .then( ( ) => { - if ( !stream ) throw new Error( 'No stream found.' ) - return res.send( { success: true, message: 'Delivered stream.', stream: stream } ) - } ) - .catch( err => { - res.status( err.message.indexOf( 'authorised' ) >= 0 ? 401 : 404 ) - res.send( { success: false, message: err.message, streamId: req.streamId } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamPatch.js b/app/api/v0/streams/StreamPatch.js deleted file mode 100644 index 848665b0..00000000 --- a/app/api/v0/streams/StreamPatch.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const mongoose = require( 'mongoose' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) -const MergeLayers = require( '../helpers/MergeLayers' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - winston.debug( chalk.bgGreen( 'Patching stream', req.params.streamId ) ) - - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - let stream = {} - DataStream.findOne( { streamId: req.params.streamId } ) - .then( result => { - stream = result - return PermissionCheck( req.user, 'write', result ) - }) - .then(() => { - for ( var key in req.body ) { - if ( stream.toObject( ).hasOwnProperty( key ) ) { - stream[ key ] = req.body[ key ] - stream.markModified( key ) - } - } - return stream.save( ) - } ) - .then( result => { - res.send( { success: true, message: 'Patched stream.' } ) - } ) - .catch( err => { - winston.error( err ) - res.status( 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamPost.js b/app/api/v0/streams/StreamPost.js deleted file mode 100644 index 48ec3031..00000000 --- a/app/api/v0/streams/StreamPost.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const shortId = require( 'shortid' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) - -module.exports = ( req, res ) => { - let stream = new DataStream( { - owner: req.user._id, - streamId: shortId.generate( ), - } ) - - if ( !req.body.objects ) req.body.objects = [ ] - - Promise.all( req.body.objects.reduce( ( arr, o ) => ( o._id !== undefined && o.type !== 'Placeholder' ) ? [ SpeckleObject.update( { _id: o._id }, o ), ...arr ] : arr, [ ] ) ) - .then( ( ) => SpeckleObject.find( { hash: { $in: req.body.objects.reduce( ( arr, o ) => o._id === undefined ? [ o.hash, ...arr ] : arr, [ ] ) } }, '_id hash' ) ) - .then( results => { - results.forEach( o => req.body.objects.filter( oo => oo.hash == o.hash ).forEach( oo => oo._id = o._id.toString( ) ) ) - return SpeckleObject.insertMany( req.body.objects.filter( so => so._id === undefined ) ) - } ) - .then( results => { - results.forEach( o => req.body.objects.filter( oo => oo.hash == o.hash ).forEach( oo => oo._id = o._id.toString( ) ) ) - stream.objects = req.body.objects.map( o => o._id ) - stream.layers = req.body.layers - stream.name = req.body.name ? req.body.name : 'Speckle Stream' - - stream.private = req.body.private ? req.body.private : false - stream.parent = req.body.parent - stream.baseProperties = req.body.baseProperties - stream.globalMeasures = req.body.globalMeasures - stream.isComputedResult = req.body.isComputedResult ? req.body.isComputedResult : false - - SpeckleObject.updateMany( { '_id': { $in: stream.objects } }, { $addToSet: { partOf: stream.streamId } } ).exec( ) - return stream.save( ) - } ) - .then( streamm => { - winston.debug( 'Created stream', stream.streamId ) - return res.send( { success: true, message: 'Created stream.', stream: stream } ) - } ) - .catch( err => { - res.status( 400 ) - return res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/app/api/v0/streams/StreamPut.js b/app/api/v0/streams/StreamPut.js deleted file mode 100644 index 78c81edb..00000000 --- a/app/api/v0/streams/StreamPut.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict' -const winston = require( 'winston' ) -const chalk = require( 'chalk' ) -const mongoose = require( 'mongoose' ) - -const DataStream = require( '../../../../models/DataStream' ) -const SpeckleObject = require( '../../../../models/SpeckleObject' ) -const MergeLayers = require( '../helpers/MergeLayers' ) -const PermissionCheck = require( '../middleware/PermissionCheck' ) - -module.exports = ( req, res ) => { - winston.debug( chalk.bgGreen( 'Getting stream', req.params.streamId ) ) - if ( !req.params.streamId ) { - res.status( 400 ) - return res.send( { success: false, message: 'No stream id provided.' } ) - } - - let stream = {} - DataStream.findOne( { streamId: req.params.streamId } ) - .then( result => { - stream = result - return PermissionCheck( req.user, 'write', result ) - } ) - .then( ( ) => { - if ( req.body.private ) stream.private = req.body.private - if ( req.body.parent ) stream.parent = req.body.parent - if ( req.body.globalMeasures ) stream.globalMeasures = req.body.globalMeasures - if ( req.body.baseProperties ) stream.baseProperties = req.body.baseProperties - - stream.name = req.body.name ? req.body.name : stream.name - stream.layers = req.body.layers ? MergeLayers( stream.layers, req.body.layers ) : stream.layers - return Promise.all( req.body.objects.reduce( ( arr, o ) => ( o._id !== undefined && o.type !== 'Placeholder' ) ? [ SpeckleObject.update( { _id: o._id }, o ), ...arr ] : arr, [ ] ) ) - } ) - .then( ( ) => SpeckleObject.find( { hash: { $in: req.body.objects.reduce( ( arr, o ) => o._id === undefined ? [ o.hash, ...arr ] : arr, [ ] ) } }, '_id hash' ) ) - .then( results => { - results.forEach( o => req.body.objects.filter( oo => oo.hash == o.hash ).forEach( oo => oo._id = o._id.toString( ) ) ) - return SpeckleObject.insertMany( req.body.objects.filter( so => so._id === undefined ) ) - } ) - .then( results => { - results.forEach( o => req.body.objects.filter( oo => oo.hash == o.hash ).forEach( oo => oo._id = o._id.toString( ) ) ) - stream.objects = req.body.objects.map( o => o._id ) - // stream.markModified( 'name' ) - stream.markModified( 'layers' ) - stream.markModified( 'objects' ) - SpeckleObject.updateMany( { '_id': { $in: stream.objects } }, { $addToSet: { partOf: stream.streamId } } ).exec( ) - return stream.save( ) - } ) - .then( result => { - res.send( { success: true, message: 'Updated stream.', objects: req.body.objects.map( o => o._id ) } ) - } ) - .catch( err => { - winston.error( err ) - res.status( err.message.indexOf( 'authorised' ) >= 0 ? 401 : 400 ) - res.send( { success: false, message: err.toString( ) } ) - } ) -} \ No newline at end of file diff --git a/server.js b/server.js index 3b88aaca..8d29f43a 100644 --- a/server.js +++ b/server.js @@ -110,7 +110,7 @@ if ( cluster.isMaster ) { //////////////////////////////////////////////////////////////////////// // handle api versions gracefully - require( './app/api/v0/index' )( app, express, '/api/v0' ) + app.use( '/api/v0', ( req, res ) => res.status( 410 ).json( { error: 'The v0 API has been removed' } ) ) require( './app/api/v1/index' )( app, express, '/api/v1' ) ////////////////////////////////////////////////////////////////////////