git ssb

2+

mixmix / ssb-client-intro



Tree: 51abc9ea9bd81acab00665fd68b0ea38202d9a7a

Files: 51abc9ea9bd81acab00665fd68b0ea38202d9a7a / 4_lastMsgs_live_with_author.js

1860 bytesRaw
1const Client = require('ssb-client')
2const pull = require('pull-stream')
3const paraMap = require('pull-paramap')
4const Abortable = require('pull-abortable')
5const get = require('lodash/get')
6
7var minutes = 60*1000
8
9Client((err, sbot) => {
10 if (err) throw err
11
12 var someTimeAgo = Number(new Date()) - 60*minutes
13
14 pull(
15 sbot.createFeedStream({ reverse: true, gt: someTimeAgo, live: true }), // source
16 addAuthorName(),
17 pull.drain(renderMsg) // sink
18 )
19
20 function renderMsg (msg) {
21 if (msg.sync) {
22 console.log('*** up to date, now listening for new messages ***')
23 return
24 }
25
26 console.log(`author: ${get(msg, 'value.authorName')} (${get(msg, 'value.author')})`)
27 console.log('time:', new Date(msg.value.timestamp))
28 console.log('type:', get(msg, 'value.content.type'))
29 console.log('text:', get(msg, 'value.content.text'))
30 console.log()
31 }
32
33 function addAuthorName () {
34 return paraMap((msg, doneCb) => {
35 if (msg.sync) {
36 doneCb(null, msg)
37 return
38 }
39
40 // take a msg in, pull out the author id : @yeasdasd..asd
41 const id = get(msg, 'value.author')
42 const abortable = Abortable()
43
44 // create a new user stream, and look for the most recent `about` message they have published
45 pull(
46 sbot.createUserStream({ id, reverse: true }),
47 abortable,
48 pull.filter(msg => {
49 return get(msg, 'value.content.type') === 'about' &&
50 get(msg, 'value.content.about') === id
51 }),
52 pull.drain(aboutMsg => {
53 // add the name from that about message to decorate the orignal msg
54 msg.value.authorName = get(aboutMsg, 'value.content.name')
55
56 doneCb(null, msg)
57
58 // abort the stream once you'e found the most recent about msg
59 abortable.abort()
60 })
61 )
62 })
63 }
64
65})
66
67
68

Built with git-ssb-web