diff --git a/lib/db/api.js b/lib/db/api.js index 65c5978f..85389bf7 100644 --- a/lib/db/api.js +++ b/lib/db/api.js @@ -121,7 +121,7 @@ dbapi.lookupUserByVncAuthResponse = function(response, serial) { }) } -dbapi.loadGroup = function(email) { +dbapi.loadUserDevices = function(email) { return db.run(r.table('devices').getAll(email, { index: 'owner' })) diff --git a/lib/units/api/controllers/device.js b/lib/units/api/controllers/device.js index 79473c0f..a7190c39 100644 --- a/lib/units/api/controllers/device.js +++ b/lib/units/api/controllers/device.js @@ -1,18 +1,15 @@ var Promise = require('bluebird') +var _ = require('lodash') var dbapi = require('../../../db/api') var logger = require('../../../util/logger') var datautil = require('../../../util/datautil') var log = logger.createLogger('api:controllers:device') -var wire = require('../../../wire') -var wireutil = require('../../../wire/util') module.exports = { getDevices: getDevices , getDeviceBySerial: getDeviceBySerial -, reserveDeviceBySerial: reserveDeviceBySerial -, releaseDeviceBySerial: releaseDeviceBySerial } var log = logger.createLogger('api:contoller:device') @@ -42,11 +39,17 @@ function getDevices(req, res) { function getDeviceBySerial(req, res) { var serial = req.swagger.params.serial.value + var fields = req.swagger.params.fields.value dbapi.loadDevice(serial) .then(function(device) { if (device) { datautil.normalize(device, req.user) + + if(fields) { + device = _.pick(device, fields.split(',')) + } + res.json({ success: true , device: device @@ -65,99 +68,3 @@ function getDeviceBySerial(req, res) { }) }) } - -function reserveDeviceBySerial(req, res) { - var serial = req.swagger.params.serial.value - - dbapi.loadDevice(serial) - .then(function(device) { - if (device) { - datautil.normalize(device, req.user) - if(!device.using) { - - var requirements = { - 'serial': { - 'value': serial, - 'match': 'exact' - } - } - - req.options.push.send([ - device.channel - , wireutil.envelope( - new wire.GroupMessage( - new wire.OwnerMessage( - req.user.email - , req.user.name - , req.user.group - ) - , null - , wireutil.toDeviceRequirements(requirements) - ) - ) - ]) - - res.json({ - success: true - , device: device - }) - - } else { - res.json(500, { - success: false - , description: 'Device is being used' - }) - } - } else { - res.json(500, { - success: false - , description: 'Bad device serial' - }) - } - }) -} - -function releaseDeviceBySerial(req, res) { - var serial = req.swagger.params.serial.value - - dbapi.loadDevice(serial) - .then(function(device) { - if (device) { - datautil.normalize(device, req.user) - if(device.using && device.owner.email == req.user.email) { - - var requirements = { - 'serial': { - 'value': serial, - 'match': 'exact' - } - } - - req.options.push.send([ - device.channel - , wireutil.envelope( - new wire.UngroupMessage( - wireutil.toDeviceRequirements(requirements) - ) - ) - ]) - - res.json({ - success: true - , device: device - }) - - } else { - res.json(500, { - success: false - , description: 'You cannot kick this device' - }) - } - } else { - res.json(500, { - success: false - , description: 'Bad device serial' - }) - } - }) -} diff --git a/lib/units/api/controllers/user.js b/lib/units/api/controllers/user.js index 01a3ee52..26d5310c 100644 --- a/lib/units/api/controllers/user.js +++ b/lib/units/api/controllers/user.js @@ -3,12 +3,16 @@ var Promise = require('bluebird') var dbapi = require('../../../db/api') var logger = require('../../../util/logger') var datautil = require('../../../util/datautil') +var wire = require('../../../wire') +var wireutil = require('../../../wire/util') var log = logger.createLogger('api:controllers:user') module.exports = { getCurrentUser: getCurrentUser -, getCurrentUserGroup: getCurrentUserGroup +, getCurrentUserDevices: getCurrentUserDevices +, addDeviceToUser: addDeviceToUser +, deleteDeviceFromUser: deleteDeviceFromUser } function getCurrentUser(req, res) { @@ -18,8 +22,8 @@ function getCurrentUser(req, res) { }) } -function getCurrentUserGroup(req, res) { - dbapi.loadGroup(req.user.email) +function getCurrentUserDevices(req, res) { + dbapi.loadUserDevices(req.user.email) .then(function(cursor) { return Promise.promisify(cursor.toArray, cursor)() .then(function(list) { @@ -39,3 +43,100 @@ function getCurrentUserGroup(req, res) { }) }) } + +function addDeviceToUser(req, res) { + var serial = req.body.serial + var timeout = req.body.timeout || null + + dbapi.loadDevice(serial) + .then(function(device) { + if (device) { + datautil.normalize(device, req.user) + if(device.ready && !device.using) { + + var requirements = { + 'serial': { + 'value': serial, + 'match': 'exact' + } + } + + req.options.push.send([ + device.channel + , wireutil.envelope( + new wire.GroupMessage( + new wire.OwnerMessage( + req.user.email + , req.user.name + , req.user.group + ) + , timeout + , wireutil.toDeviceRequirements(requirements) + ) + ) + ]) + + res.json(202, { + success: true + , description: 'Device Add request is accepted' + }) + + } else { + res.json(500, { + success: false + , description: 'Device is being used or not available' + }) + } + } else { + res.json(500, { + success: false + , description: 'Bad device serial' + }) + } + }) +} + +function deleteDeviceFromUser(req, res) { + var serial = req.body.serial + + dbapi.loadDevice(serial) + .then(function(device) { + if (device) { + datautil.normalize(device, req.user) + if(device.using && device.owner.email == req.user.email) { + + var requirements = { + 'serial': { + 'value': serial, + 'match': 'exact' + } + } + + req.options.push.send([ + device.channel + , wireutil.envelope( + new wire.UngroupMessage( + wireutil.toDeviceRequirements(requirements) + ) + ) + ]) + + res.json(202, { + success: true + , description: 'Device Release request is accepted' + }) + + } else { + res.json(500, { + success: false + , description: 'You cannot kick this device' + }) + } + } else { + res.json(500, { + success: false + , description: 'Bad device serial' + }) + } + }) +} diff --git a/lib/units/api/swagger/api_v1.yaml b/lib/units/api/swagger/api_v1.yaml index 0af4f604..dc6138af 100644 --- a/lib/units/api/swagger/api_v1.yaml +++ b/lib/units/api/swagger/api_v1.yaml @@ -24,7 +24,7 @@ tags: - name: device description: Device Operations paths: - /me: + /user: x-swagger-router-controller: user get: summary: User Profile @@ -43,27 +43,70 @@ paths: $ref: "#/definitions/ErrorResponse" security: - accessTokenAuth: [] -# TODO: Change group endpoint with something more easy to understandable endpoint - /group: + /user/devices: x-swagger-router-controller: user get: - summary: User Group - description: The User Group endpoint returns information about user group of current authorized user. - operationId: getCurrentUserGroup + summary: List devices owned by current user + description: The User Devices endpoint returns information about user group of current authorized user. + operationId: getCurrentUserDevices tags: - user responses: "200": - description: Current User's Group information + description: Current User Devices information schema: - $ref: "#/definitions/GroupResponse" + $ref: "#/definitions/DeviceListResponse" default: description: Unexpected Error schema: $ref: "#/definitions/ErrorResponse" security: - accessTokenAuth: [] - /accessTokens: + post: + summary: Add device to a user + description: The User Devices endpoint will request stf server for a new device. It will return request accepted if device is usable. + operationId: addDeviceToUser + tags: + - user + parameters: + - name: device + in: body + description: Device to add + required: true + schema: + $ref: "#/definitions/DeviceAddPayload" + responses: + "202": + description: Device Add Request Status + default: + description: Unexpected Error + schema: + $ref: "#/definitions/ErrorResponse" + security: + - accessTokenAuth: [] + delete: + summary: Release device from user + description: The User Devices endpoint will request for device release from stf server. + operationId: deleteDeviceFromUser + tags: + - user + parameters: + - name: device + in: body + description: Device to add + required: true + schema: + $ref: "#/definitions/DeviceDeletePayload" + responses: + "202": + description: Device Release Request Status + default: + description: Unexpected Error + schema: + $ref: "#/definitions/ErrorResponse" + security: + - accessTokenAuth: [] + /user/accessTokens: x-swagger-router-controller: token get: summary: Access Tokens @@ -115,57 +158,10 @@ paths: description: Device Serial required: true type: string - responses: - "200": - description: Device Information - schema: - $ref: "#/definitions/DeviceResponse" - default: - description: Unexpected Error - schema: - $ref: "#/definitions/ErrorResponse" - security: - - accessTokenAuth: [] - /swagger.json: - x-swagger-pipe: swagger_raw - /devices/{serial}/reserve: - x-swagger-router-controller: device - put: - summary: Reseve Device - description: The device reserve enpoint will reserve a device if device it usable - operationId: reserveDeviceBySerial - tags: - - device - parameters: - - name: serial - in: path - description: Device Serial - required: true - type: string - responses: - "200": - description: Device Information - schema: - $ref: "#/definitions/DeviceResponse" - default: - description: Unexpected Error - schema: - $ref: "#/definitions/ErrorResponse" - security: - - accessTokenAuth: [] - /devices/{serial}/release: - x-swagger-router-controller: device - put: - summary: Device Information - description: The device reserve enpoint will release a device - operationId: releaseDeviceBySerial - tags: - - device - parameters: - - name: serial - in: path - description: Device Serial - required: true + - name: fields + in: query + description: Fields query parameter takes a comma seperated list of fields. Only listed field will be return in response + required: false type: string responses: "200": @@ -187,14 +183,6 @@ definitions: properties: user: type: object - GroupResponse: - required: - - devices - properties: - devices: - type: array - items: - type: object AccessTokensResponse: required: - tokens @@ -223,6 +211,25 @@ definitions: properties: message: type: string + DeviceAddPayload: + description: payload object for adding device to user + required: + - serial + properties: + serial: + description: Device Serial + type: string + timeout: + description: Device timeout in ms. If device is kept idle for this period, it will be automatically disconnected. Default is provider group timeout. + type: integer + DeviceDeletePayload: + description: payload object for deleting device from user + required: + - serial + properties: + serial: + description: Device Serial + type: string securityDefinitions: accessTokenAuth: diff --git a/res/app/components/stf/device/device-service.js b/res/app/components/stf/device/device-service.js index a01df982..833962ed 100644 --- a/res/app/components/stf/device/device-service.js +++ b/res/app/components/stf/device/device-service.js @@ -181,7 +181,7 @@ module.exports = function DeviceServiceFactory($http, socket, EnhanceDeviceServi , digest: true }) - oboe('/api/v1/group') + oboe('/api/v1/user/devices') .node('devices[*]', function (device) { tracker.add(device) }) diff --git a/res/app/components/stf/tokens/access-token-service.js b/res/app/components/stf/tokens/access-token-service.js index a815b828..214aa8cf 100644 --- a/res/app/components/stf/tokens/access-token-service.js +++ b/res/app/components/stf/tokens/access-token-service.js @@ -6,7 +6,7 @@ module.exports = function AccessTokenServiceFactory( var AccessTokenService = {} AccessTokenService.getAccessTokens = function() { - return $http.get('/api/v1/accessTokens') + return $http.get('/api/v1/user/accessTokens') } AccessTokenService.generateAccessToken = function(title) {