1
0

UserList.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. class UserList extends Class
  2. constructor: (@type="new") ->
  3. @item_list = new ItemList(User, "key")
  4. @users = @item_list.items
  5. @need_update = true
  6. @limit = 5
  7. @followed_by = null
  8. @search = null
  9. update: ->
  10. @loading = true
  11. params = {}
  12. if @search
  13. search_where = "AND (json.user_name LIKE :search_like OR user.user_name LIKE :search_like OR json.cert_user_id LIKE :search_like)"
  14. params["search_like"] = "%#{@search}%"
  15. else
  16. search_where = ""
  17. if @followed_by
  18. query = """
  19. SELECT user.user_name, follow.*, user.*
  20. FROM follow
  21. LEFT JOIN user USING (auth_address, hub)
  22. WHERE
  23. follow.json_id = #{@followed_by.row.json_id} AND user.json_id IS NOT NULL
  24. UNION
  25. SELECT user.user_name, follow.*, user.*
  26. FROM follow
  27. LEFT JOIN json ON (json.directory = 'data/userdb/' || follow.auth_address)
  28. LEFT JOIN user ON (user.json_id = json.json_id)
  29. WHERE
  30. follow.json_id = #{@followed_by.row.json_id} AND user.json_id IS NOT NULL AND
  31. follow.date_added < #{Time.timestamp()+120}
  32. ORDER BY date_added DESC
  33. LIMIT #{@limit}
  34. """
  35. else if @type == "suggested"
  36. followed_user_addresses = (key.replace(/.*\//, "") for key, val of Page.user.followed_users)
  37. followed_user_directories = ("data/users/"+key for key in followed_user_addresses)
  38. if not followed_user_addresses.length
  39. return
  40. query = """
  41. SELECT
  42. COUNT(DISTINCT(json.directory)) AS num,
  43. GROUP_CONCAT(DISTINCT(json.user_name)) AS followed_by,
  44. follow.*,
  45. json_suggested.avatar
  46. FROM follow
  47. LEFT JOIN json USING (json_id)
  48. LEFT JOIN json AS json_suggested ON (json_suggested.directory = 'data/users/' || follow.auth_address AND json_suggested.avatar IS NOT NULL)
  49. WHERE
  50. json.directory IN #{Text.sqlIn(followed_user_directories)} AND
  51. auth_address NOT IN #{Text.sqlIn(followed_user_addresses)} AND
  52. auth_address != '#{Page.user.auth_address}' AND
  53. date_added < #{Time.timestamp()+120}
  54. GROUP BY follow.auth_address
  55. ORDER BY num DESC
  56. LIMIT #{@limit}
  57. """
  58. else if @type == "active"
  59. query = """
  60. SELECT
  61. json.*,
  62. json.site AS json_site,
  63. json.directory AS json_directory,
  64. json.file_name AS json_file_name,
  65. json.cert_user_id AS json_cert_user_id,
  66. json.hub AS json_hub,
  67. json.user_name AS json_user_name,
  68. json.avatar AS json_avatar,
  69. COUNT(*) AS posts
  70. FROM
  71. post LEFT JOIN json USING (json_id)
  72. WHERE
  73. post.date_added > #{Time.timestamp() - 60*60*24*7}
  74. GROUP BY json_id
  75. ORDER BY posts DESC
  76. LIMIT #{@limit}
  77. """
  78. else
  79. query = """
  80. SELECT
  81. user.*,
  82. json.site AS json_site,
  83. json.directory AS json_directory,
  84. json.file_name AS json_file_name,
  85. json.cert_user_id AS json_cert_user_id,
  86. json.hub AS json_hub,
  87. json.user_name AS json_user_name,
  88. json.avatar AS json_avatar
  89. FROM
  90. user LEFT JOIN json USING (json_id)
  91. WHERE
  92. date_added < #{Time.timestamp()+120}
  93. #{search_where}
  94. ORDER BY date_added DESC
  95. LIMIT #{@limit}
  96. """
  97. Page.cmd "dbQuery", [query, params], (rows) =>
  98. rows_by_user = {} # Deduplicating
  99. followed_by_displayed = {}
  100. for row in rows
  101. if row.json_cert_user_id # File in user directory
  102. row.cert_user_id = row.json_cert_user_id
  103. row.auth_address = row.json_directory.replace("data/userdb/", "").replace("data/users/", "")
  104. if not row.auth_address # Just created user, no content.json yet
  105. continue
  106. if row.followed_by
  107. row.followed_by = (username for username in row.followed_by.split(",") when not followed_by_displayed[username])[0]
  108. followed_by_displayed[row.followed_by] = true # Only display every user once
  109. row.key = row.hub+"/"+row.auth_address
  110. if not rows_by_user[row.hub+row.auth_address]
  111. rows_by_user[row.hub+row.auth_address] = row
  112. user_rows = (val for key, val of rows_by_user)
  113. @item_list.sync(user_rows)
  114. @loading = false
  115. Page.projector.scheduleRender()
  116. render: (type="normal") =>
  117. if @need_update
  118. @need_update = false
  119. setTimeout ( => @update() ), 100 # Low prioriy
  120. if not @users.length
  121. return null
  122. h("div.UserList.users"+type+"."+@type, {afterCreate: Animation.show}, @users.map (user) =>
  123. user.renderList(type)
  124. )
  125. window.UserList = UserList