summaryrefslogtreecommitdiff
path: root/ui/app/models/account.js
blob: cb008cc0c5d10e9ba56786ef853973e9ed4db04f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// An account is an abstraction of a user and a provider.
// The user part is optional, so an Account might just represent a provider.
//

import bitmask from 'lib/bitmask'

export default class Account {

  constructor(address, props={}) {
    this.address = address
    this._authenticated = props.authenticated
  }

  //
  // currently, bitmask.js uses address for id, so we return address here too.
  // also, we don't know uuid until after authentication.
  //
  // TODO: change to uuid when possible.
  //
  get id() {
    return this._address
  }

  get domain() {
    return this._address.split('@')[1]
  }

  get address() {
    return this._address
  }

  set address(address) {
    if (!address.match('@')) {
      this._address = '@' + address
    } else {
      this._address = address
    }
  }

  get userpart() {
    return this._address.split('@')[0]
  }

  get authenticated() {
    return this._authenticated
  }

  get hasEmail() {
    return true
  }

  //
  // returns a promise, fulfill is passed account object
  //
  login(password) {
    return bitmask.bonafide.user.auth(this.address, password).then(
      response => {
        if (response.uuid) {
          this._uuid = response.uuid
          this._authenticated = true
        }
        return this
      }
    )
  }

  //
  // returns a promise, fulfill is passed account object
  //
  logout() {
    return bitmask.bonafide.user.logout(this.id).then(
      response => {
        this._authenticated = false
        this._address = '@' + this.domain
        return this
      }
    )
  }

  //
  // returns the matching account in the list of accounts
  //
  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
      }
    }
    return account
  }

  static findOrAdd(address) {
    let account = Account.find(address)
    if (!account) {
      account = new Account(address)
      Account.list.push(account)
    }
    return account
  }

  //
  // returns a promise, fullfill is passed account object
  //
  static active() {
    return bitmask.bonafide.user.active().then(
      response => {
        if (response.user == '<none>') {
          return null
        } else {
          return new Account(response.user, {authenticated: true})
        }
      }
    )
  }

  static add(account) {
    if (!Account.list.find(i => {return i.id == account.id})) {
      Account.list.push(account)
    }
  }

  //
  // For now, accounts are really just providers. Eventually,
  // there will be a separate account and provider list.
  //
  // Returns the account in the list that is closest to the one
  // We removed.
  //
  static remove(account) {
    return bitmask.bonafide.provider.delete(account.domain).then(
      response => {
        let index = Account.list.findIndex(i => {
          return i.id == account.id
        })
        Account.list = Account.list.filter(i => {
          return i.id != account.id
        })
        if (index >= Account.list.length) {
          index = index - 1
        } else if (index == -1) {
          index = 0
        }
        return Account.list[index]
      }
    )
  }

  static create(address, password, invite=null) {
    return bitmask.bonafide.user.create(address, password, invite).then(
      response => {
        return new Account(address)
      }
    )
  }

  static initializeList(domains) {
    for (let domain of domains) {
      Account.add(new Account(domain))
    }
  }

  //
  // inserts at the front of the account list
  // removing any other accounts with the same domain.
  //
  // this is a temporary hack to support the old behavior
  // util the backend has a proper concept of an account list.
  //
  static addPrimary(account) {
    Account.list = Account.list.filter(i => {
      return i.domain != account.domain
    })
    Account.list.unshift(account)
  }
}

Account.list = []