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