From 2c07ad6dd7afd4b02c9a6793d55da4600c66a448 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Wed, 29 Oct 2014 16:21:44 -0300 Subject: [PATCH] Display a message for users whose have wallet:: but not profile:: --- js/controllers/createProfile.js | 4 +++- js/controllers/home.js | 4 +++- js/models/Identity.js | 19 ++++++++++++++++++- js/models/Wallet.js | 20 +++++++++++++++++++- js/plugins/InsightStorage.js | 10 +++++++--- js/plugins/LocalStorage.js | 25 +++++++++++++++++++++++++ js/services/identityService.js | 20 +++++++++++++++++++- 7 files changed, 94 insertions(+), 8 deletions(-) diff --git a/js/controllers/createProfile.js b/js/controllers/createProfile.js index 34997f5a7..5dae983b9 100644 --- a/js/controllers/createProfile.js +++ b/js/controllers/createProfile.js @@ -2,7 +2,9 @@ angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) { controllerUtils.redirIfLogged(); - $scope.retreiving = false; + $scope.retreiving = true; + + identityService.check($scope); $scope.createProfile = function(form) { if (form && form.$invalid) { diff --git a/js/controllers/home.js b/js/controllers/home.js index 2f0a87b12..ab9cefac6 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -2,7 +2,9 @@ angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) { controllerUtils.redirIfLogged(); - $scope.retreiving = false; + $scope.retreiving = true; + + identityService.check($scope); $scope.openProfile = function(form) { if (form && form.$invalid) { diff --git a/js/models/Identity.js b/js/models/Identity.js index 0ea694285..12f76a4fb 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -59,8 +59,12 @@ function Identity(opts) { this.wallets = opts.wallets || {}; }; +Identity.getStoragePrefix = function() { + return 'profile::'; +}; + Identity.getKeyForEmail = function(email) { - return 'profile::' + bitcore.util.sha256ripe160(email).toString('hex'); + return Identity.getStoragePrefix() + bitcore.util.sha256ripe160(email).toString('hex'); }; Identity.prototype.getId = function() { @@ -482,6 +486,19 @@ Identity.prototype.addWallet = function(wallet, cb) { this.storage.setItem(wallet.getStorageKey(), wallet.toObj(), cb); }; +/** + * check if any profile exists on storage + * @param opts.storageOpts + * @param cb + */ +Identity.checkIfExistsAny = function(opts, cb) { + var storage = opts.storage || opts.pluginManager.get('DB'); + storage.getFirst(Identity.getStoragePrefix(), { + onlyKey: true + }, function(err, v, k) { + return cb(k ? true : false); + }); +}; /** * @desc Checks if a version is compatible with the current version diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 8b71acbef..e0d972df3 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -165,14 +165,32 @@ Wallet.COPAYER_PAIR_LIMITS = { 12: 1, }; +Wallet.getStoragePrefix = function() { + return 'wallet::'; +}; + Wallet.getStorageKey = function(str) { - return 'wallet::' + str; + return Wallet.getStoragePrefix() + str; }; Wallet.prototype.getStorageKey = function() { return Wallet.getStorageKey(this.getId()); }; +/** + * check if any wallet exists on storage + * @param opts.storageOpts + * @param cb + */ +Wallet.checkIfExistsAny = function(opts, cb) { + var storage = opts.storage || opts.pluginManager.get('DB'); + storage.getFirst(Wallet.getStoragePrefix(), { + onlyKey: true + }, function(err, v, k) { + return cb(k ? true : false); + }); +}; + /* for stubbing */ Wallet._newInsight = function(opts) { return new Insight(opts); diff --git a/js/plugins/InsightStorage.js b/js/plugins/InsightStorage.js index 9ecbddd15..f153ee309 100644 --- a/js/plugins/InsightStorage.js +++ b/js/plugins/InsightStorage.js @@ -78,9 +78,13 @@ InsightStorage.prototype.clear = function(callback) { }; InsightStorage.prototype.allKeys = function(callback) { - // NOOP - // TODO: Add functionality? - callback(); + // TODO: compatibility with localStorage + return callback(null); +}; + +InsightStorage.prototype.getFirst = function(prefix, opts, callback) { + // TODO: compatibility with localStorage + return callback(null, true, true); }; module.exports = InsightStorage; diff --git a/js/plugins/LocalStorage.js b/js/plugins/LocalStorage.js index 6715d8cf1..ccdfaa1a8 100644 --- a/js/plugins/LocalStorage.js +++ b/js/plugins/LocalStorage.js @@ -1,4 +1,5 @@ 'use strict'; +var _ = require('lodash'); function LocalStorage() { this.type = 'DB'; @@ -51,4 +52,28 @@ LocalStorage.prototype.allKeys = function(cb) { return cb(null, ret); }; +LocalStorage.prototype.getFirst = function(prefix, opts, cb) { + opts = opts || {}; + var that = this; + + this.allKeys(function(err, allKeys) { + var keys = _.filter(allKeys, function(k) { + if ((k === prefix) || k.indexOf(prefix) === 0) return true; + }); + + if (keys.length === 0) + return cb(new Error('not found')); + + if (opts.onlyKey) + return cb(null, null, keys[0]); + + that.getItem(keys[0], function(err, data) { + if (err) { + return cb(err); + } + return cb(null, data, keys[0]); + }); + }); +}; + module.exports = LocalStorage; diff --git a/js/services/identityService.js b/js/services/identityService.js index fcf876a1a..4b184a3ed 100644 --- a/js/services/identityService.js +++ b/js/services/identityService.js @@ -4,6 +4,24 @@ angular.module('copayApp.services') .factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) { var root = {}; + root.check = function (scope) { + copay.Identity.checkIfExistsAny({ + pluginManager: pluginManager, + }, function(anyProfile) { + copay.Wallet.checkIfExistsAny({ + pluginManager: pluginManager, + }, function(anyWallet) { + scope.retreiving = false; + scope.anyProfile = anyProfile ? true : false; + scope.anyWallet = anyWallet ? true : false; + + if (!scope.anyProfile) { + $location.path('/createProfile'); + } + }); + }); + }; + root.create = function(scope, form) { var iden = copay.Identity.create({ email: form.email.$modelValue, @@ -60,7 +78,7 @@ angular.module('copayApp.services') } scope.loading = false; }); - } + }; return root; });