git ssb

0+

mixmix / ssb-client-basic-tutorial



Tree: 324fed137cb04497adc9c508cdb230546136ec9f

Files: 324fed137cb04497adc9c508cdb230546136ec9f / v05.js

2094 bytesRaw
1const Connection = require('ssb-client')
2const pull = require('pull-stream')
3pull.paraMap = require('pull-paramap')
4const html = require('yo-yo')
5const daysPosts = require('./source/days-posts')
6const getName = require('./async/get-name')
7const getAvatar = require('./async/get-avatar')
8
9const App = html`
10 <div style="margin: 2rem;">Loading...</div>
11`
12document.body.appendChild(App)
13
14Connection((err, server) => {
15 if (err) throw err
16
17 const today = new Date(2018, 9, 17)
18
19 pull(
20 daysPosts(server)(today),
21 pull.paraMap(addName, 50), // run up to 50 asyncrhonous maps in parallel
22 pull.paraMap(addAvatar, 50), // run up to 50 asyncrhonous maps in parallel
23 pull.collect(onDone)
24 )
25
26 function addName (data, cb) {
27 getName(server)(data.author, (err, name) => {
28 if (err) cb(err)
29 else {
30 data.authorName = name
31 cb(null, data)
32 }
33 })
34 }
35 function addAvatar (data, cb) {
36 getAvatar(server)(data.author, (err, avatar) => {
37 if (err) cb(err)
38 else {
39 data.avatar = avatar
40 cb(null, data)
41 }
42 })
43 }
44
45 function onDone (err, data) {
46 if (err) {
47 console.error('oh noes', err)
48 server.close()
49 return
50 }
51
52 const newView = Messages(data)
53 html.update(App, newView)
54
55 console.log(`${data.length} messages`)
56 console.timeEnd('get posts')
57 server.close()
58 }
59})
60
61function Messages (data) {
62 return html`
63 <div style="font-family: arial;">
64 ${data.map(Message)}
65 </div>
66 `
67}
68
69function Message (msgData) {
70 const { avatar, authorName, timestamp, text, root } = msgData
71
72 return html`
73 <div style="margin: 2rem;">
74 ${Avatar(avatar)}
75 <strong>${authorName}</strong> - ${new Date(timestamp).toLocaleString()}
76 <p style="font-size: .8rem; margin: 0"> ${root ? 'thread:' : ''} ${root}</p>
77 <p>${text}</p>
78 </div>
79 `
80}
81
82function Avatar (blobId) {
83 if (!blobId) return
84
85 const url = `http://localhost:8989/blobs/get/${blobId}`
86 // this may be patchbay specific
87
88 return html`
89 <img src=${url} style="width: 2rem; height: 2rem;"/>
90 `
91}
92

Built with git-ssb-web