Files: 20f85b8d98278bf1dd978dcd5e2d2ce0ceed922b / v03.js
2523 bytesRaw
1 | const Connection = require('ssb-client') |
2 | const pull = require('pull-stream') |
3 | pull.paraMap = require('pull-paramap') |
4 | const daysPosts = require('./source/days-posts') |
5 | |
6 | console.log('Connecting') |
7 | |
8 | Connection((err, server) => { |
9 | if (err) throw err |
10 | console.log('Connection established') |
11 | |
12 | const today = new Date(2018, 9, 17) |
13 | |
14 | console.time('get posts') |
15 | pull( |
16 | daysPosts(server)(today), |
17 | pull.paraMap(getAuthorName, 50), // run up to 50 asyncrhonous maps in parallel |
18 | pull.collect(onDone) |
19 | ) |
20 | |
21 | // Note you could use pull.asyncMap, but it only does 1 async map at a time... it's 240x slower on my machine! |
22 | |
23 | function getAuthorName (data, cb) { |
24 | // NOTE the data is coming in from the dayPosts source and has been mapped into the form { author, timestamp, text, root } |
25 | |
26 | // cb is a function provided to us by pull-paramap which we use to pass results out once we're done and to pass things on to the next part of the stream (the collect here) |
27 | |
28 | const feedId = data.author |
29 | |
30 | const opts = { |
31 | limit: 1, |
32 | reverse: true, |
33 | query: [ |
34 | { |
35 | $filter: { |
36 | value: { |
37 | author: feedId, |
38 | content: { |
39 | type: 'about', |
40 | about: feedId, |
41 | name: { $is: 'string' } // there's a name string present |
42 | } |
43 | }, |
44 | timestamp: { $gt: 0 } // a hack that forces ordering by timestamp |
45 | } |
46 | }, |
47 | { |
48 | $map: { |
49 | name: ['value', 'content', 'name'] |
50 | } |
51 | } |
52 | ] |
53 | } |
54 | |
55 | pull( |
56 | server.query.read(opts), |
57 | pull.collect((err, results) => { |
58 | if (err) { |
59 | cb(err) |
60 | return |
61 | } |
62 | |
63 | var name |
64 | if (!results || !results.length) name = feedId |
65 | else name = results[0].name |
66 | // console.log(name) // debug / see the names fly by as we get them! |
67 | |
68 | data.authorName = name |
69 | // stample the name we found to the data object |
70 | |
71 | cb(null, data) |
72 | }) |
73 | ) |
74 | } |
75 | |
76 | function onDone (err, data) { |
77 | if (err) { |
78 | console.error('oh noes', err) |
79 | server.close() |
80 | return |
81 | } |
82 | |
83 | data.forEach(msg => { |
84 | prettyPrint(msg) |
85 | console.log('------') |
86 | }) |
87 | |
88 | console.log(`${data.length} messages`) |
89 | console.timeEnd('get posts') |
90 | server.close() |
91 | } |
92 | }) |
93 | |
94 | // helpers |
95 | |
96 | function prettyPrint (obj) { |
97 | console.log(JSON.stringify(obj, null, 2)) |
98 | // this just print the full object out as a string that's been nicely indented |
99 | // with each level of nesting |
100 | } |
101 |
Built with git-ssb-web