Users.coffee 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. class Users extends Class
  2. constructor: ->
  3. @user_address = {}
  4. @archived = null
  5. getArchived: (cb) =>
  6. if @archived
  7. return cb(@archived)
  8. Page.cmd "fileGet", "data/archived.json", (res) =>
  9. @archived = JSON.parse(res)
  10. cb(@archived)
  11. getUsernames: (addresses, cb) =>
  12. query = """
  13. SELECT directory, value AS cert_user_id
  14. FROM json
  15. LEFT JOIN keyvalue USING (json_id)
  16. WHERE ? AND file_name = 'content.json' AND key = 'cert_user_id'
  17. """
  18. Page.cmd "dbQuery", [query, {directory: addresses}], (rows) =>
  19. usernames = {}
  20. for row in rows
  21. usernames[row.directory] = row.cert_user_id
  22. @user_address[row.cert_user_id] = row.directory
  23. if rows.length == addresses.length
  24. cb(usernames)
  25. return
  26. @log "Not found all username in sql, try to find in archived file"
  27. @getArchived (archived) =>
  28. for auth_address, row of archived
  29. @user_address[row.cert_user_id] = auth_address
  30. if auth_address in addresses
  31. usernames[auth_address] ?= row.cert_user_id
  32. cb(usernames)
  33. getAddress: (usernames, cb) ->
  34. unknown_address = (username for username in usernames when not @user_address[username]?)
  35. if unknown_address.length == 0
  36. cb(@user_address)
  37. return
  38. query = """
  39. SELECT value, directory
  40. FROM keyvalue
  41. LEFT JOIN json USING (json_id)
  42. WHERE ?
  43. """
  44. Page.cmd "dbQuery", [query, {"key": "cert_user_id", "value": unknown_address}], (rows) =>
  45. for row in rows
  46. @user_address[row.value] = row.directory
  47. cb(@user_address)
  48. getAll: (cb) ->
  49. Page.cmd "dbQuery", ["SELECT value, directory FROM keyvalue LEFT JOIN json USING (json_id) WHERE key = 'cert_user_id'"], (rows) =>
  50. if rows.error then return false
  51. @user_address = {}
  52. for row in rows
  53. @user_address[row.value] = row.directory
  54. @getArchived (archived) =>
  55. for auth_address, row of archived
  56. @user_address[row.cert_user_id] = auth_address
  57. cb @user_address
  58. window.Users = Users