1
0

PostList.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. class PostList extends Class
  2. constructor: ->
  3. @item_list = new ItemList(Post, "key")
  4. @posts = @item_list.items
  5. @need_update = true
  6. @directories = []
  7. @loaded = false
  8. @filter_post_ids = null
  9. @limit = 10
  10. queryComments: (post_uris, cb) =>
  11. query = "
  12. SELECT
  13. post_uri, comment.body, comment.date_added, comment.comment_id, json.cert_auth_type, json.cert_user_id, json.user_name, json.hub, json.directory, json.site
  14. FROM
  15. comment
  16. LEFT JOIN json USING (json_id)
  17. WHERE
  18. ? AND date_added < #{Time.timestamp()+120}
  19. ORDER BY date_added DESC
  20. "
  21. return Page.cmd "dbQuery", [query, {post_uri: post_uris}], cb
  22. queryLikes: (post_uris, cb) =>
  23. query = "SELECT post_uri, COUNT(*) AS likes FROM post_like WHERE ? GROUP BY post_uri"
  24. return Page.cmd "dbQuery", [query, {post_uri: post_uris}], cb
  25. update: =>
  26. @need_update = false
  27. param = {}
  28. if @directories == "all"
  29. where = "WHERE post_id IS NOT NULL AND post.date_added < #{Time.timestamp()+120} "
  30. else
  31. where = "WHERE directory IN #{Text.sqlIn(@directories)} AND post_id IS NOT NULL AND post.date_added < #{Time.timestamp()+120} "
  32. if @filter_post_ids
  33. where += "AND post_id IN #{Text.sqlIn(@filter_post_ids)} "
  34. if Page.local_storage.settings.hide_hello_zerome
  35. where += "AND post_id > 1 "
  36. query = "
  37. SELECT
  38. *
  39. FROM
  40. post
  41. LEFT JOIN json ON (post.json_id = json.json_id)
  42. #{where}
  43. ORDER BY date_added DESC
  44. LIMIT #{@limit+1}
  45. "
  46. @logStart "Update"
  47. Page.cmd "dbQuery", [query, param], (rows) =>
  48. items = []
  49. post_uris = []
  50. for row in rows
  51. row["key"] = row["site"]+"-"+row["directory"].replace("data/users/", "")+"_"+row["post_id"]
  52. row["post_uri"] = row["directory"].replace("data/users/", "") + "_" + row["post_id"]
  53. post_uris.push(row["post_uri"])
  54. # Get comments for latest posts
  55. @queryComments post_uris, (comment_rows) =>
  56. comment_db = {} # {Post id: posts}
  57. for comment_row in comment_rows
  58. comment_db[comment_row.site+"/"+comment_row.post_uri] ?= []
  59. comment_db[comment_row.site+"/"+comment_row.post_uri].push(comment_row)
  60. for row in rows
  61. row["comments"] = comment_db[row.site+"/"+row.post_uri]
  62. if @filter_post_ids?.length == 1 and row.post_id == parseInt(@filter_post_ids[0])
  63. row.selected = true
  64. @item_list.sync(rows)
  65. @loaded = true
  66. @logEnd "Update"
  67. Page.projector.scheduleRender()
  68. if @posts.length > @limit
  69. @addScrollwatcher()
  70. @queryLikes post_uris, (like_rows) =>
  71. like_db = {}
  72. for like_row in like_rows
  73. like_db[like_row["post_uri"]] = like_row["likes"]
  74. for row in rows
  75. row["likes"] = like_db[row["post_uri"]]
  76. @item_list.sync(rows)
  77. Page.projector.scheduleRender()
  78. handleMoreClick: =>
  79. @limit += 10
  80. @update()
  81. return false
  82. addScrollwatcher: =>
  83. setTimeout ( =>
  84. # Remove previous scrollwatchers for same item
  85. for item, i in Page.scrollwatcher.items
  86. if item[1] == @tag_more
  87. Page.scrollwatcher.items.splice(i, 1)
  88. break
  89. Page.scrollwatcher.add @tag_more, (tag) =>
  90. if tag.getBoundingClientRect().top == 0
  91. return
  92. @limit += 10
  93. @need_update = true
  94. Page.projector.scheduleRender()
  95. ), 2000
  96. storeMoreTag: (elem) =>
  97. @tag_more = elem
  98. render: =>
  99. if @need_update then @update()
  100. if not @posts.length
  101. if not @loaded
  102. return null
  103. else
  104. return h("div.post-list", [
  105. h("div.post-list-empty", {enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp}, [
  106. h("h2", "No posts yet"),
  107. h("a", {href: "?Users", onclick: Page.handleLinkClick}, "Let's follow some users!")
  108. ])
  109. ])
  110. return [
  111. h("div.post-list", @posts[0..@limit].map (post) =>
  112. try
  113. post.render()
  114. catch err
  115. h("div.error", ["Post render error:", err.message])
  116. Debug.formatException(err)
  117. ),
  118. if @posts.length > @limit
  119. h("a.more.small", {href: "#More", onclick: @handleMoreClick, enterAnimation: Animation.slideDown, exitAnimation: Animation.slideUp, afterCreate: @storeMoreTag}, "Show more posts...")
  120. ]
  121. window.PostList = PostList