Head.coffee 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. class Head extends Class
  2. constructor: ->
  3. @menu = new Menu()
  4. @follows = []
  5. handleSelectUserClick: ->
  6. if "Merger:ZeroMe" not in Page.site_info.settings.permissions
  7. Page.cmd "wrapperPermissionAdd", "Merger:ZeroMe", =>
  8. Page.updateSiteInfo =>
  9. Page.content.update()
  10. else
  11. Page.cmd "certSelect", {"accepted_domains": ["zeroid.bit"], "accept_any": true}
  12. return false
  13. handleFollowMenuItemClick: (type, item) =>
  14. selected = not @follows[type]
  15. @follows[type] = selected
  16. item[2] = selected
  17. @saveFollows()
  18. Page.projector.scheduleRender()
  19. return true
  20. handleMenuClick: =>
  21. if not Page.site_info?.cert_user_id
  22. return @handleSelectUserClick()
  23. Page.cmd "feedListFollow", [], (@follows) =>
  24. @menu.items = []
  25. @menu.items.push ["Follow username mentions", ( (item) =>
  26. return @handleFollowMenuItemClick("Mentions", item)
  27. ), @follows["Mentions"]]
  28. @menu.items.push ["Follow comments on your posts", ( (item) =>
  29. return @handleFollowMenuItemClick("Comments on your posts", item)
  30. ), @follows["Comments on your posts"]]
  31. @menu.items.push ["Follow new followers", ( (item) =>
  32. return @handleFollowMenuItemClick("New followers", item)
  33. ), @follows["New followers"]]
  34. @menu.items.push ["Hide \"Hello ZeroMe!\" messages", ( (item) =>
  35. Page.local_storage.settings.hide_hello_zerome = not Page.local_storage.settings.hide_hello_zerome
  36. item[2] = Page.local_storage.settings.hide_hello_zerome
  37. Page.projector.scheduleRender()
  38. Page.saveLocalStorage()
  39. Page.content.need_update = true
  40. return false
  41. ), Page.local_storage.settings.hide_hello_zerome]
  42. if (key for key of Page.user_hubs).length > 1
  43. @menu.items.push ["---"]
  44. for key, val of Page.user_hubs
  45. ((key) =>
  46. @menu.items.push ["Use hub #{key}", ( (item) =>
  47. Page.local_storage.settings.hub = key
  48. Page.saveLocalStorage()
  49. Page.checkUser()
  50. ), Page.user.row.site == key]
  51. )(key)
  52. @menu.toggle()
  53. Page.projector.scheduleRender()
  54. return false
  55. saveFollows: =>
  56. out = {}
  57. if @follows["Mentions"]
  58. out["Mentions"] = ["
  59. SELECT
  60. 'mention' AS type,
  61. comment.date_added AS date_added,
  62. 'a comment' AS title,
  63. '@' || user_name || ': ' || comment.body AS body,
  64. '?Post/' || json.site || '/' || REPLACE(post_uri, '_', '/') AS url
  65. FROM comment
  66. LEFT JOIN json USING (json_id)
  67. WHERE
  68. comment.body LIKE '%@#{Page.user.row.user_name}%'
  69. UNION
  70. SELECT
  71. 'mention' AS type,
  72. post.date_added AS date_added,
  73. 'In ' || json.user_name || \"'s post\" AS title,
  74. '@' || json.user_name || ': ' || post.body AS body,
  75. '?Post/' || json.site || '/' || REPLACE(json.directory, 'data/users/', '') || '/' || post_id AS url
  76. FROM post
  77. LEFT JOIN json USING (json_id)
  78. WHERE
  79. post.body LIKE '%@#{Page.user.row.user_name}%'
  80. ", [""]]
  81. if @follows["Comments on your posts"]
  82. out["Comments on your posts"] = ["
  83. SELECT
  84. 'comment' AS type,
  85. comment.date_added AS date_added,
  86. 'Your post' AS title,
  87. '@' || json.user_name || ': ' || comment.body AS body,
  88. '?Post/' || site || '/' || REPLACE(post_uri, '_', '/') AS url
  89. FROM comment
  90. LEFT JOIN json USING (json_id)
  91. WHERE
  92. post_uri LIKE '#{Page.user.auth_address}%'
  93. ", [""]]
  94. if @follows["New followers"]
  95. out["New followers"] = ["
  96. SELECT
  97. 'follow' AS type,
  98. follow.date_added AS date_added,
  99. json.user_name || ' started following you' AS title,
  100. '' AS body,
  101. '?Profile/' || json.hub || REPLACE(json.directory, 'data/users', '') AS url
  102. FROM follow
  103. LEFT JOIN json USING(json_id)
  104. WHERE
  105. auth_address = '#{Page.user.auth_address}'
  106. GROUP BY json.directory
  107. ", [""]]
  108. Page.cmd "feedFollow", [out]
  109. render: =>
  110. h("div.head.center", [
  111. h("a.logo", {href: "?Home", onclick: Page.handleLinkClick}, h("img", {src: "img/logo.svg", height: 40, onerror: "this.src='img/logo.png'; this.onerror=null;"})),
  112. if Page.user?.hub
  113. # Registered user
  114. h("div.right.authenticated", [
  115. h("div.user",
  116. h("a.name.link", {href: Page.user.getLink(), onclick: Page.handleLinkClick}, Page.user.row.user_name),
  117. h("a.address", {href: "#Select+user", onclick: @handleSelectUserClick}, Page.site_info.cert_user_id)
  118. ),
  119. h("a.settings", {href: "#Settings", onclick: Page.returnFalse, onmousedown: @handleMenuClick}, "\u22EE")
  120. @menu.render()
  121. ])
  122. else if not Page.user?.hub and Page.site_info?.cert_user_id
  123. # Cert selected, but not registered
  124. h("div.right.selected", [
  125. h("div.user",
  126. h("a.name.link", {href: "?Create+profile", onclick: Page.handleLinkClick}, "Create profile"),
  127. h("a.address", {href: "#Select+user", onclick: @handleSelectUserClick}, Page.site_info.cert_user_id)
  128. ),
  129. @menu.render()
  130. h("a.settings", {href: "#Settings", onclick: Page.returnFalse, onmousedown: @handleMenuClick}, "\u22EE")
  131. ])
  132. else if not Page.user?.hub and Page.site_info
  133. # No cert selected
  134. h("div.right.unknown", [
  135. h("div.user",
  136. h("a.name.link", {href: "#Select+user", onclick: @handleSelectUserClick}, "Visitor"),
  137. h("a.address", {href: "#Select+user", onclick: @handleSelectUserClick}, "Select your account")
  138. ),
  139. @menu.render()
  140. h("a.settings", {href: "#Settings", onclick: Page.returnFalse, onmousedown: @handleMenuClick}, "\u22EE")
  141. ])
  142. else
  143. h("div.right.unknown")
  144. ])
  145. window.Head = Head