git ssb

0+

mixmix / ssb-client-basic-tutorial



Tree: 20f85b8d98278bf1dd978dcd5e2d2ce0ceed922b

Files: 20f85b8d98278bf1dd978dcd5e2d2ce0ceed922b / async / get-name.js

1422 bytesRaw
1const pull = require('pull-stream')
2
3module.exports = function (server) {
4 if (!server) throw new Error('day-posts helper requires a server!')
5 if (!server.query) throw new Error('day-posts helper requires a server with the ssb-query installed!')
6
7 return function getAuthorName (feedId, cb) {
8 // NOTE the data is coming in from the dayPosts source and has been mapped into the form { author, timestamp, text, root }
9
10 // 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)
11
12 const opts = {
13 limit: 1,
14 reverse: true,
15 query: [
16 {
17 $filter: {
18 value: {
19 author: feedId,
20 content: {
21 type: 'about',
22 about: feedId,
23 name: { $is: 'string' } // there's a name string present
24 }
25 },
26 timestamp: { $gt: 0 } // a hack that forces ordering by timestamp
27 }
28 },
29 {
30 $map: {
31 name: ['value', 'content', 'name']
32 }
33 }
34 ]
35 }
36
37 pull(
38 server.query.read(opts),
39 pull.collect((err, results) => {
40 if (err) {
41 cb(err)
42 return
43 }
44
45 if (!results || !results.length) cb(null, feedId)
46 else cb(null, results[0].name)
47 })
48 )
49 }
50}
51

Built with git-ssb-web