query.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var SEA = require('./sea');
  2. var Gun = SEA.Gun;
  3. // This is internal func queries public key(s) for alias.
  4. const queryGunAliases = (alias, gunRoot) => new Promise((resolve, reject) => {
  5. // load all public keys associated with the username alias we want to log in with.
  6. gunRoot.get('~@'+alias).once((data, key) => {
  7. //rev.off();
  8. if (!data) {
  9. // if no user, don't do anything.
  10. const err = 'No user!'
  11. Gun.log(err)
  12. return reject({ err })
  13. }
  14. // then figuring out all possible candidates having matching username
  15. const aliases = []
  16. let c = 0
  17. // TODO: how about having real chainable map without callback ?
  18. Gun.obj.map(data, (at, pub) => {
  19. if (!pub.slice || '~' !== pub.slice(0, 1)) {
  20. // TODO: ... this would then be .filter((at, pub))
  21. return
  22. }
  23. ++c
  24. // grab the account associated with this public key.
  25. gunRoot.get(pub).once(data => {
  26. pub = pub.slice(1)
  27. --c
  28. if (data){
  29. aliases.push({ pub, put: data })
  30. }
  31. if (!c && (c = -1)) {
  32. resolve(aliases)
  33. }
  34. })
  35. })
  36. if (!c) {
  37. reject({ err: 'Public key does not exist!' })
  38. }
  39. })
  40. })
  41. module.exports = queryGunAliases