Files: 1c2359030ac279f1fdfef805d7938f3003372c24 / app / page / statsShow.js
2381 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Array: MutantArray, Dict, onceTrue, map } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | |
5 | exports.gives = nest('app.page.statsShow') |
6 | |
7 | exports.needs = nest({ |
8 | 'sbot.obs.connection': 'first' |
9 | }) |
10 | |
11 | exports.create = (api) => { |
12 | return nest('app.page.statsShow', statsShow) |
13 | |
14 | function statsShow (location) { |
15 | var blogs = MutantArray([]) |
16 | var comments = Dict() |
17 | var likes = Dict() |
18 | // comments(console.log) |
19 | |
20 | onceTrue(api.sbot.obs.connection, server => { |
21 | // console.log(Object.keys(server.blogStats)) |
22 | |
23 | fetchBlogs() |
24 | |
25 | function fetchBlogs () { |
26 | pull( |
27 | server.blogStats.readBlogs({ reverse: false }), |
28 | pull.drain(blog => { |
29 | blogs.push(blog) |
30 | fetchComments(blog) |
31 | fetchLikes(blog) |
32 | }) |
33 | ) |
34 | } |
35 | |
36 | function fetchComments (blog) { |
37 | if (!comments.has(blog.key)) comments.put(blog.key, MutantArray()) |
38 | |
39 | pull( |
40 | server.blogStats.readComments({ blog }), |
41 | pull.drain(comment => { |
42 | comments.get(blog.key).push(comment) |
43 | }) |
44 | ) |
45 | } |
46 | |
47 | function fetchLikes (blog) { |
48 | if (!likes.has(blog.key)) likes.put(blog.key, MutantArray()) |
49 | |
50 | pull( |
51 | server.blogStats.readLikes({ blog }), |
52 | pull.drain(comment => { |
53 | likes.get(blog.key).push(comment) |
54 | }) |
55 | ) |
56 | } |
57 | |
58 | // ///// test code ///// |
59 | var blogKey = '%3JeEg7voZF4aplk9xCEAfhFOx+zocbKhgstzvfD3G8w=.sha256' |
60 | // console.log('fetching comments', blogKey) // has 2 comments, 1 like |
61 | |
62 | pull( |
63 | server.blogStats.read({ |
64 | gt: ['L', blogKey, null], |
65 | lt: ['L', blogKey+'~', undefined], |
66 | // gt: ['L', blogKey, null], |
67 | // lte: ['L', blogKey+'~', undefined], |
68 | // limit: 100, |
69 | keys: true, |
70 | values: true, |
71 | seqs: false, |
72 | reverse: true |
73 | }), |
74 | // pull.filter(o => o.key[1] === blogKey), |
75 | pull.log(() => console.log('DONE')) |
76 | ) |
77 | /// ///// test code ///// |
78 | }) |
79 | |
80 | return h('Page -statsShow', [ |
81 | h('pre', map(blogs, blog => { |
82 | return h('div', [ |
83 | h('b', blog.value.content.title), |
84 | h('div', map(comments.get(blog.key), msg => 'C')), |
85 | h('div', map(likes.get(blog.key), msg => 'L')) |
86 | ]) |
87 | })) |
88 | ]) |
89 | } |
90 | } |
91 |
Built with git-ssb-web