diff --git a/lib/db/api.js b/lib/db/api.js index d1606897..9e77b74e 100644 --- a/lib/db/api.js +++ b/lib/db/api.js @@ -58,6 +58,7 @@ dbapi.resetUserSettings = function(email) { } dbapi.insertUserAdbKey = function(email, key) { + console.log('insertUserAdbKey', email, key) return db.run(r.table('users').get(email).update({ adbKeys: r.row('adbKeys').default([]).append({ title: key.title diff --git a/lib/units/app/index.js b/lib/units/app/index.js index 67f58368..50d13a91 100644 --- a/lib/units/app/index.js +++ b/lib/units/app/index.js @@ -10,13 +10,11 @@ var csrf = require('csurf') var Promise = require('bluebird') var httpProxy = require('http-proxy') var compression = require('compression') -var adbkit = require('adbkit') var logger = require('../../util/logger') var pathutil = require('../../util/pathutil') var dbapi = require('../../db/api') var datautil = require('../../util/datautil') -var requtil = require('../../util/requtil') var auth = require('./middleware/auth') var deviceIconMiddleware = require('./middleware/device-icons') @@ -149,78 +147,6 @@ module.exports = function(options) { }) }) - app.post('/api/v1/app/user/keys/adb', function(req, res) { - requtil.validate(req, function() { - req.checkBody('title').notEmpty().len(1, 100) - req.checkBody('key').notEmpty() - }) - .then(function() { - return adbkit.util.parsePublicKey(req.body.key) - }) - .then(function(key) { - return dbapi.lookupUsersByAdbKey(key.fingerprint) - .then(function(users) { - if (users.length) { - throw new dbapi.DuplicateSecondaryIndexError() - } - else { - // Well, this doesn't guarantee that no one else inserts the - // same key before we do, but it's hardly a big enough problem - // to consider right now. - return dbapi.insertUserAdbKey(req.user.email, { - title: req.body.title - , fingerprint: key.fingerprint - }) - .then(function() { - res.send({ - success: true - , key: { - title: req.body.title - , fingerprint: key.fingerprint - } - }) - }) - } - }) - }) - .catch(requtil.ValidationError, function(err) { - res.status(400).send({ - success: false - , error: 'ValidationError' - , validationErrors: err.errors - }) - }) - .catch(dbapi.DuplicateSecondaryIndexError, function() { - res.status(400).send({ - success: false - , error: 'DuplicateKeyError' - }) - }) - .catch(function(err) { - log.error('Failed to insert ADB key: ', err.stack) - res.status(500).send({ - success: false - , error: 'ServerError' - }) - }) - }) - - app.delete('/api/v1/app/user/keys/adb/:id', function(req, res) { - dbapi.deleteUserAdbKey(req.user.email, req.params.id) - .then(function() { - res.send({ - success: true - }) - }) - .catch(function(err) { - log.error('Failed to delete ADB key: ', err.stack) - res.status(500).send({ - success: false - , error: 'ServerError' - }) - }) - }) - app.get('/api/v1/app/group', function(req, res) { dbapi.loadGroup(req.user.email) .then(function(cursor) { diff --git a/lib/units/device/plugins/connect.js b/lib/units/device/plugins/connect.js index 0984bbb6..a79d0eba 100644 --- a/lib/units/device/plugins/connect.js +++ b/lib/units/device/plugins/connect.js @@ -4,6 +4,7 @@ var syrup = require('syrup') var Promise = require('bluebird') var logger = require('../../../util/logger') +var grouputil = require('../../../util/grouputil') var wire = require('../../../wire') var wireutil = require('../../../wire/util') var lifecycle = require('../../../util/lifecycle') @@ -33,14 +34,28 @@ module.exports = syrup.serial() var resolver = Promise.defer() function notify() { - push.send([ - solo.channel - , wireutil.envelope(new wire.JoinGroupByAdbFingerprintMessage( - options.serial - , key.fingerprint - , key.comment - )) - ]) + group.get() + .then(function(currentGroup) { + push.send([ + solo.channel + , wireutil.envelope(new wire.JoinGroupByAdbFingerprintMessage( + options.serial + , key.fingerprint + , key.comment + , currentGroup.group + )) + ]) + }) + .catch(grouputil.NoGroupError, function() { + push.send([ + solo.channel + , wireutil.envelope(new wire.JoinGroupByAdbFingerprintMessage( + options.serial + , key.fingerprint + , key.comment + )) + ]) + }) } function joinListener(group, identifier) { diff --git a/lib/units/processor/index.js b/lib/units/processor/index.js index 0ace153a..a83b770e 100644 --- a/lib/units/processor/index.js +++ b/lib/units/processor/index.js @@ -86,9 +86,15 @@ module.exports = function(options) { )) ]) } - else { - /* ask user */ - log.debug('ask user') + else if (message.currentGroup) { + appDealer.send([ + message.currentGroup + , wireutil.envelope(new wire.JoinGroupByAdbFingerprintMessage( + message.serial + , message.fingerprint + , message.comment + )) + ]) } }) .catch(function(err) { diff --git a/lib/units/websocket/index.js b/lib/units/websocket/index.js index 5bd19546..21992b10 100644 --- a/lib/units/websocket/index.js +++ b/lib/units/websocket/index.js @@ -7,6 +7,7 @@ var zmq = require('zmq') var Promise = require('bluebird') var _ = require('lodash') var request = Promise.promisifyAll(require('request')) +var adbkit = require('adbkit') var logger = require('../../util/logger') var wire = require('../../wire') @@ -133,6 +134,12 @@ module.exports = function(options) { ) }) }) + .on(wire.JoinGroupByAdbFingerprintMessage, function(channel, message) { + socket.emit('user.keys.adb.confirm', { + title: message.comment + , fingerprint: message.fingerprint + }) + }) .on(wire.LeaveGroupMessage, function(channel, message) { socket.emit('device.change', { important: true @@ -254,6 +261,79 @@ module.exports = function(options) { .on('user.settings.reset', function() { dbapi.resetUserSettings(user.email) }) + .on('user.keys.adb.add', function(data) { + return adbkit.util.parsePublicKey(data.key) + .then(function(key) { + return dbapi.lookupUsersByAdbKey(key.fingerprint) + .then(function(cursor) { + return cursor.toArray() + }) + .then(function(users) { + if (users.length) { + throw new dbapi.DuplicateSecondaryIndexError() + } + else { + return dbapi.insertUserAdbKey(user.email, { + title: data.title + , fingerprint: key.fingerprint + }) + } + }) + .then(function() { + socket.emit('user.keys.adb.added', { + title: data.title + , fingerprint: key.fingerprint + }) + }) + }) + .then(function() { + push.send([ + wireutil.global + , wireutil.envelope(new wire.AdbKeysUpdatedMessage()) + ]) + }) + .catch(dbapi.DuplicateSecondaryIndexError, function() { + // No-op + }) + }) + .on('user.keys.adb.accept', function(data) { + return dbapi.lookupUsersByAdbKey(data.fingerprint) + .then(function(cursor) { + return cursor.toArray() + }) + .then(function(users) { + if (users.length) { + throw new dbapi.DuplicateSecondaryIndexError() + } + else { + return dbapi.insertUserAdbKey(user.email, { + title: data.title + , fingerprint: data.fingerprint + }) + } + }) + .then(function() { + socket.emit('user.keys.adb.added', { + title: data.title + , fingerprint: data.fingerprint + }) + }) + .then(function() { + push.send([ + user.group + , wireutil.envelope(new wire.AdbKeysUpdatedMessage()) + ]) + }) + .catch(dbapi.DuplicateSecondaryIndexError, function() { + // No-op + }) + }) + .on('user.keys.adb.remove', function(data) { + return dbapi.deleteUserAdbKey(user.email, data.fingerprint) + .then(function() { + socket.emit('user.keys.adb.removed', data) + }) + }) // Touch events .on('input.touchDown', function(channel, data) { push.send([ diff --git a/lib/wire/wire.proto b/lib/wire/wire.proto index f7424a40..941ad235 100644 --- a/lib/wire/wire.proto +++ b/lib/wire/wire.proto @@ -251,6 +251,7 @@ message JoinGroupByAdbFingerprintMessage { required string serial = 1; required string fingerprint = 2; optional string comment = 3; + optional string currentGroup = 4; } message AdbKeysUpdatedMessage { diff --git a/res/app/components/stf/common-ui/modals/add-adb-key-modal/add-adb-key-modal-service.js b/res/app/components/stf/common-ui/modals/add-adb-key-modal/add-adb-key-modal-service.js index 37efbeaa..92ba2fc0 100644 --- a/res/app/components/stf/common-ui/modals/add-adb-key-modal/add-adb-key-modal-service.js +++ b/res/app/components/stf/common-ui/modals/add-adb-key-modal/add-adb-key-modal-service.js @@ -9,11 +9,9 @@ module.exports = $scope.modal.title = data.title $scope.ok = function () { - console.log('add key') $modalInstance.close(true) } - $scope.$watch('modal.showAdd', function (newValue) { if (newValue === false) { $scope.ok() @@ -36,9 +34,7 @@ module.exports = } }) - modalInstance.result.then(function () { - }, function () { - }) + return modalInstance.result } return service diff --git a/res/app/components/stf/user/user-service.js b/res/app/components/stf/user/user-service.js index 4357c13a..e1de7c07 100644 --- a/res/app/components/stf/user/user-service.js +++ b/res/app/components/stf/user/user-service.js @@ -1,4 +1,9 @@ -module.exports = function UserServiceFactory($http, AppState) { +module.exports = function UserServiceFactory( + $rootScope +, socket +, AppState +, AddAdbKeyModalService +) { var UserService = {} var user = UserService.currentUser = AppState.user @@ -8,20 +13,38 @@ module.exports = function UserServiceFactory($http, AppState) { } UserService.addAdbKey = function(key) { - return $http.post('/api/v1/app/user/keys/adb', key) - .success(function(data) { - UserService.getAdbKeys().push(data.key) - }) + socket.emit('user.keys.adb.add', key) + } + + UserService.acceptAdbKey = function(key) { + socket.emit('user.keys.adb.accept', key) } UserService.removeAdbKey = function(key) { - return $http.delete('/api/v1/app/user/keys/adb/' + key.fingerprint) - .success(function() { - user.adbKeys = UserService.getAdbKeys().filter(function(someKey) { - return someKey.fingerprint !== key.fingerprint - }) - }) + socket.emit('user.keys.adb.remove', key) } + socket.on('user.keys.adb.added', function(key) { + UserService.getAdbKeys().push(key) + $rootScope.$broadcast('user.keys.adb.updated', user.adbKeys) + $rootScope.$apply() + }) + + socket.on('user.keys.adb.removed', function(key) { + user.adbKeys = UserService.getAdbKeys().filter(function(someKey) { + return someKey.fingerprint !== key.fingerprint + }) + $rootScope.$broadcast('user.keys.adb.updated', user.adbKeys) + $rootScope.$apply() + }) + + socket.on('user.keys.adb.confirm', function(data) { + AddAdbKeyModalService.open(data).then(function(result) { + if (result) { + UserService.acceptAdbKey(data) + } + }) + }) + return UserService } diff --git a/res/app/settings/keys/adb-keys/adb-keys-controller.js b/res/app/settings/keys/adb-keys/adb-keys-controller.js index 9e830b0b..61fa7e17 100644 --- a/res/app/settings/keys/adb-keys/adb-keys-controller.js +++ b/res/app/settings/keys/adb-keys/adb-keys-controller.js @@ -1,10 +1,4 @@ -module.exports = function AdbKeysCtrl($scope, $http, UserService, AddAdbKeyModalService) { - - //AddAdbKeyModalService.open({ - // title: 'PC1264', - // fingerprint: 'bb:86:60:39:d7:a2:e3:09:93:09:cc:f6:e8:37:99:3f' - //}) - +module.exports = function AdbKeysCtrl($scope, $http, UserService) { $scope.adbKeys = [] function updateKeys() { @@ -12,8 +6,9 @@ module.exports = function AdbKeysCtrl($scope, $http, UserService, AddAdbKeyModal } $scope.removeKey = function (key) { - UserService.removeAdbKey(key).then(updateKeys) + UserService.removeAdbKey(key) } + $scope.$on('user.keys.adb.updated', updateKeys) updateKeys() }