ZeroMail.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. window.h = maquette.h
  2. class ZeroMail extends ZeroFrame
  3. init: ->
  4. @params = {}
  5. @site_info = null
  6. @on_site_info = new Promise()
  7. @on_local_storage = new Promise()
  8. @server_info = null
  9. @user = new User()
  10. @users = new Users()
  11. @local_storage = null
  12. createProjector: ->
  13. @leftbar = new Leftbar()
  14. @message_lists = new MessageLists()
  15. @message_show = new MessageShow()
  16. @message_create = new MessageCreate()
  17. @projector = maquette.createProjector()
  18. if base.href.indexOf("?") == -1
  19. @route("")
  20. else
  21. @route(base.href.replace(/.*?\?/, ""))
  22. @projector.replace($("#MessageLists"), @message_lists.render)
  23. @projector.replace($("#MessageShow"), @message_show.render)
  24. @projector.replace($("#Leftbar"), @leftbar.render)
  25. @projector.merge($("#MessageCreate"), @message_create.render)
  26. # Update every minute to keep time since fields up-to date
  27. setInterval ( ->
  28. Page.projector.scheduleRender()
  29. ), 60*1000
  30. # Route site urls
  31. route: (query) ->
  32. @params = Text.parseQuery(query)
  33. @log "Route", @params
  34. if @params.to
  35. @on_site_info.then =>
  36. @message_create.show(@params.to)
  37. @cmd "wrapperReplaceState", [{}, "", @createUrl("to", "")] # Remove to parameter from url
  38. if @params.url == "Sent"
  39. @message_lists.setActive("sent")
  40. # Add/remove/change parameter to current site url
  41. createUrl: (key, val) ->
  42. params = JSON.parse(JSON.stringify(@params)) # Clone
  43. if typeof key == "Object"
  44. vals = key
  45. for key, val of keys
  46. params[key] = val
  47. else
  48. params[key] = val
  49. return "?"+Text.encodeQuery(params)
  50. getLocalStorage: ->
  51. @on_site_info.then =>
  52. @cmd "wrapperGetLocalStorage", [], (@local_storage) =>
  53. @local_storage ?= {}
  54. @local_storage.read ?= {}
  55. @local_storage.deleted ?= []
  56. @local_storage.parsed ?= {}
  57. # Re-index db
  58. if @local_storage.parsed.version? < 1
  59. @local_storage.parsed = {"version": 1}
  60. console.log("Reindexing...")
  61. @local_storage.parsed.last_secret ?= {} # Last parsed secrets: {user_address: last_parsed_secret_id, ...}
  62. @local_storage.parsed.last_message ?= {} # Last parsed messages: {user_address: last_parsed_message_id, ...}
  63. @local_storage.parsed.my_secret ?= {} # Secrets sent to me: {user_address: secret_id}
  64. @local_storage.parsed.my_message ?= {} # Decrypted messages: {user_address: [message_id, ...], ...}
  65. @on_local_storage.resolve(@local_storage)
  66. saveLocalStorage: (cb) ->
  67. if @local_storage
  68. @cmd "wrapperSetLocalStorage", @local_storage, (res) =>
  69. if cb then cb(res)
  70. onOpenWebsocket: (e) =>
  71. @cmd "siteInfo", {}, (site_info) =>
  72. @setSiteInfo(site_info)
  73. @cmd "serverInfo", {}, (server_info) =>
  74. @setServerInfo(server_info)
  75. # Parse incoming requests from UiWebsocket server
  76. onRequest: (cmd, params) ->
  77. if cmd == "setSiteInfo" # Site updated
  78. @setSiteInfo(params)
  79. else
  80. @log "Unknown command", params
  81. setSiteInfo: (site_info) ->
  82. @site_info = site_info
  83. if site_info.event?[0] == "cert_changed"
  84. @getLocalStorage()
  85. if site_info.tasks > 20
  86. limit_interval = 60000
  87. else
  88. limit_interval = 6000
  89. RateLimit limit_interval, =>
  90. @log "onSiteInfo RateLimit"
  91. @leftbar.onSiteInfo(site_info)
  92. @user.onSiteInfo(site_info)
  93. @message_create.onSiteInfo(site_info)
  94. @message_lists.onSiteInfo(site_info)
  95. @projector.scheduleRender()
  96. @getLocalStorage()
  97. @on_site_info.resolve()
  98. setServerInfo: (server_info) ->
  99. @server_info = server_info
  100. @projector.scheduleRender()
  101. window.Page = new ZeroMail()
  102. window.Page.createProjector()