diff options
Diffstat (limited to 'www/app/models')
-rw-r--r-- | www/app/models/account.js | 67 | ||||
-rw-r--r-- | www/app/models/dummy_account.js | 33 |
2 files changed, 94 insertions, 6 deletions
diff --git a/www/app/models/account.js b/www/app/models/account.js index 367961bf..fa3b9813 100644 --- a/www/app/models/account.js +++ b/www/app/models/account.js @@ -8,11 +8,7 @@ import bitmask from 'lib/bitmask' export default class Account { constructor(address, props={}) { - if (!address.match('@')) { - this._address = '@' + address - } else { - this._address = address - } + this.address = address this._authenticated = props.authenticated } @@ -34,6 +30,14 @@ export default class Account { return this._address } + set address(address) { + if (!address.match('@')) { + this._address = '@' + address + } else { + this._address = address + } + } + get userpart() { return this._address.split('@')[0] } @@ -42,6 +46,10 @@ export default class Account { return this._authenticated } + get hasEmail() { + return true + } + // // returns a promise, fulfill is passed account object // @@ -71,6 +79,33 @@ export default class Account { } // + // returns the matching account in the list of accounts, or adds it + // if it is not already present. + // + static find(address) { + // search by full address + let account = Account.list.find(i => { + return i.address == address + }) + // failing that, search by domain + if (!account) { + let domain = '@' + address.split('@')[1] + account = Account.list.find(i => { + return i.address == domain + }) + if (account) { + account.address = address + } + } + // failing that, create new account + if (!account) { + account = new Account(address) + Account.list.push(account) + } + return account + } + + // // returns a promise, fullfill is passed account object // static active() { @@ -85,4 +120,24 @@ export default class Account { ) } -}
\ No newline at end of file + static add(account) { + if (!Account.list.find(i => {return i.id == account.id})) { + Account.list.push(account) + } + } + + static remove(account) { + Account.list = Account.list.filter(i => { + return i.id != account.id + }) + } + // return Account.list + // return new Promise(function(resolve, reject) { + // window.setTimeout(function() { + // resolve(['@blah', '@lala']) + // }, 1000) + // }) + // } +} + +Account.list = []
\ No newline at end of file diff --git a/www/app/models/dummy_account.js b/www/app/models/dummy_account.js new file mode 100644 index 00000000..bf0391be --- /dev/null +++ b/www/app/models/dummy_account.js @@ -0,0 +1,33 @@ +// +// A proxy of an account, but with a different ID. For testing. +// + +import bitmask from 'lib/bitmask' + +export default class DummyAccount { + + constructor(account) { + this.account = account + } + + get id() { + return 'dummy--' + this.account.address + } + + get domain() {return this.account.domain} + get address() {return this.account.address} + get userpart() {return this.account.userpart} + get authenticated() {return this.account.authenticated} + get hasEmail() {return this.account.hasEmail} + login(password) {return this.account.login(password)} + + logout() { + return bitmask.user.logout(this.address).then( + response => { + this._authenticated = false + this._address = '@' + this.domain + return this + } + ) + } +} |