Browse Source

begin separating find and create

Kieran Gibb 2 years ago
parent
commit
8380e7129b
No known key found for this signature in database
1 changed files with 62 additions and 46 deletions
  1. 62
    46
      index.js

+ 62
- 46
index.js View File

@@ -54,56 +54,63 @@ class GroupStore {
54 54
     })
55 55
   }
56 56
 
57
-  get (id) {
58
-    var self = this
59
-    return new Promise((resolve, reject) => {
60
-      self.ready((err) => {
61
-        if (err) return reject(err)
62
-        var key, name, keys
63
-
64
-        if (crypto.isKey(id)) key = id
65
-        if (!crypto.isKey(id) && isString(id)) name = id
66
-
67
-        // Ensure we either have no key, or the key is a valid key
68
-        if (!(!key || crypto.isKey(key))) return reject(new Error('invalid: key format'))
69
-
70
-        if (name) {
71
-          var group = this.groups[name]
72
-          if (group) return resolve(group)
73
-        }
74
-
75
-        if (key) {
76
-          keys = crypto.unpack(key)
77
-          var group = this.groups[keys.address.toString('hex')]
78
-          if (group) return resolve(group)
79
-        } else {
80
-          key = crypto.accessKey()
81
-          keys = crypto.unpack(key)
82
-        }
83
-
84
-        var { address, encryptionKey } = keys
57
+  async get (id) {
58
+    try {
59
+      await this.ready()
60
+      var { address, name } = this._validate(id)
61
+
62
+      if (name) {
63
+        var group = this.groups[name]
64
+      } else if (address) {
65
+        var group = this.groups[address.toString('hex')]
85 66
         var groupOpts = this.config.groups[address]
86
-        if (groupOpts && name && groupOpts.name !== name) return reject(new Error('invalid: group with this name already exists'))
87
-
88
-        var group = encryptionKey
89
-          ? this.createGroup(this.storage, address, Object.assign(this.opts, { encryptionKey }))
90
-          : this.createGroup(this.storage, address, this.opts)
67
+        if (groupOpts && name && groupOpts.name !== name) throw new Error('invalid: group with this name already exists')
68
+      }
91 69
 
92
-        this.groups[address.toString('hex')] = group
93
-        this.groups[address] = group
94
-        if (name) this.groups[name] = group
70
+      if (group) {
71
+        await group.ready()
72
+        return group
73
+      }
95 74
 
96
-        if (!groupOpts) {
97
-          if (name) keys.name = name
98
-          this.config.groups.set(address, keys)
99
-          this.config.save()
100
-        }
75
+      return null
76
+    } catch (error) {
77
+      debug(error)
78
+      return error
79
+    }
80
+  }
101 81
 
102
-        group.ready(() => {
103
-          return resolve(group)
104
-        })
105
-      })
106
-    })
82
+  async create (id) {
83
+    try {
84
+      await this.ready()
85
+      var group = await this.get(id)
86
+      if (group) throw new Error('group already exists')
87
+
88
+      var name = id
89
+      var address = crypto.address()
90
+      var encryptionKey = crypto.encryptionKey()
91
+
92
+      var group = this.createGroup(
93
+        this.storage,
94
+        address,
95
+        Object.assign(this.opts, { encryptionKey })
96
+      )
97
+
98
+      this.groups[address.toString('hex')] = group
99
+      this.groups[address] = group
100
+      if (name) this.groups[name] = group
101
+
102
+      var groupOpts = group.keys
103
+      if (name) groupOpts.name = name
104
+      this.config.groups.set(group.publicKey, groupOpts)
105
+      this.config.save()
106
+
107
+      await group.ready()
108
+
109
+      return group
110
+    } catch (error) {
111
+      debug(error)
112
+      return error
113
+    }
107 114
   }
108 115
 
109 116
   // ------------------------------------------------------------------
@@ -133,4 +140,13 @@ class GroupStore {
133 140
 
134 141
     next(0)
135 142
   }
143
+
144
+  _validate (id) {
145
+    if (crypto.isKey(id)) key = id
146
+    if (!crypto.isKey(id) && isString(id)) name = id
147
+    // Ensure we either have no key, or the key is a valid key
148
+    if (!(!key || crypto.isKey(key))) throw new Error('invalid: key format')
149
+    return { key, name }
150
+  }
151
+
136 152
 }