Commit 593a79b4db22884bf30be44d48a33fce2ea3074b
always take a single options arg, for consistency
Dominic Tarr committed on 5/20/2017, 2:01:53 AMParent: 3b2dc16dbbbc0325cda645d454ecda892d5c87a5
Files changed
plugins/invite.js | changed |
plugins/invite.js | |||
---|---|---|---|
@@ -1,4 +1,5 @@ | |||
1 … | +'use strict' | ||
1 | 2 … | var crypto = require('crypto') | |
2 | 3 … | var ssbKeys = require('ssb-keys') | |
3 | 4 … | var toAddress = require('../lib/util').toAddress | |
4 | 5 … | var cont = require('cont') | |
@@ -26,8 +27,12 @@ | |||
26 | 27 … | function isObject(o) { | |
27 | 28 … | return o && 'object' === typeof o | |
28 | 29 … | } | |
29 | 30 … | ||
31 … | +function isNumber(n) { | ||
32 … | + return 'number' === typeof n && !isNaN(n) | ||
33 … | +} | ||
34 … | + | ||
30 | 35 … | module.exports = { | |
31 | 36 … | name: 'invite', | |
32 | 37 … | version: '1.0.0', | |
33 | 38 … | manifest: mdm.manifest(apidoc), | |
@@ -59,18 +64,19 @@ | |||
59 | 64 … | }) | |
60 | 65 … | }) | |
61 | 66 … | ||
62 | 67 … | return { | |
63 | - create: valid.async(function (n, note, cb) { | ||
64 | - var modern = false | ||
65 | - if(isObject(n) && n.modern) { | ||
66 | - n = 1 | ||
67 | - modern = true | ||
68 … | + create: valid.async(function (opts, cb) { | ||
69 … | + opts = opts || {} | ||
70 … | + if(isNumber(opts)) | ||
71 … | + opts = {uses: opts} | ||
72 … | + else if(isObject(opts)) { | ||
73 … | + if(opts.modern) | ||
74 … | + opts.uses = 1 | ||
68 | 75 … | } | |
69 | - if(isFunction(note)) { | ||
70 | - cb = note | ||
71 | - note = null | ||
72 | - } | ||
76 … | + else if(isFunction(opts)) | ||
77 … | + cb = opts, opts = {} | ||
78 … | + | ||
73 | 79 … | var addr = server.getAddress() | |
74 | 80 … | var host = ref.parseAddress(addr).host | |
75 | 81 … | if(!config.allowPrivate && (ip.isPrivate(host) || 'localhost' === host)) | |
76 | 82 … | return cb(new Error('Server has no public ip address, ' | |
@@ -89,16 +95,16 @@ | |||
89 | 95 … | // store metadata under the generated pubkey | |
90 | 96 … | var owner = server.id | |
91 | 97 … | codesDB.put(keyCap.id, { | |
92 | 98 … | id: keyCap.id, | |
93 | - total: +n, | ||
94 | - note: note, | ||
99 … | + total: +opts.uses || 1, | ||
100 … | + note: opts.note, | ||
95 | 101 … | used: 0, | |
96 | 102 … | permissions: {allow: ['invite.use', 'getAddress'], deny: null} | |
97 | 103 … | }, function (err) { | |
98 | 104 … | // emit the invite code: our server address, plus the key-seed | |
99 | 105 … | if(err) cb(err) | |
100 | - else if(modern && server.ws && server.ws.getAddress) { | ||
106 … | + else if(opts.modern && server.ws && server.ws.getAddress) { | ||
101 | 107 … | cb(null, server.ws.getAddress()+':'+seed.toString('base64')) | |
102 | 108 … | } | |
103 | 109 … | else { | |
104 | 110 … | addr = ref.parseAddress(addr) | |
@@ -219,5 +225,4 @@ | |||
219 | 225 … | } | |
220 | 226 … | } | |
221 | 227 … | } | |
222 | 228 … | ||
223 | - |
Built with git-ssb-web