git ssb

0+

mixmix / ssb-client-basic-tutorial



Tree: 324fed137cb04497adc9c508cdb230546136ec9f

Files: 324fed137cb04497adc9c508cdb230546136ec9f / async / get-avatar.js

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

Built with git-ssb-web