forked from arj03/ssb-browser-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-onboarding-json.js
144 lines (124 loc) · 3.46 KB
/
generate-onboarding-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var pull = require('pull-stream')
var feedId = '@6CAxOI3f+LUOVrbAl0IemqiS7ATpQvr9Mdw9LC4+Uv0=.ed25519'
// ripped out of ssb-social-index because we need the keys, not the values
function read (sbot, dest) {
const content = { type: 'about' }
content['about'] = dest
return pull(
sbot.backlinks.read({
reverse: true,
query: [{ $filter: {
dest,
value: { content: content }
} }]
})
)
}
function latestValue (sbot, key, dest, cb) {
var value = null
var msgKey = null
pull(
read(sbot, dest),
pull.filter(msg => {
return msg.value.content && msg.value.content[key] &&
!(msg.value.content[key] && msg.value.content[key].remove) &&
msg.value.author == dest
}),
pull.take(1),
pull.drain(msg => {
value = msg.value.content[key]
msgKey = msg.key
}, (err) => {
if (err) return cb(err)
cb(null, { msgKey, value } )
})
)
}
require('ssb-client')(function (err, sbot) {
if(err) throw err
// key => { lastest name about, latest image about, last message }
var data = {}
var asyncs = 0
function check_async() {
asyncs -= 1
if (asyncs == 0) {
var total = 0
var max = 0
var count = 0
var oldest = new Date()
oldest.setMonth(oldest.getMonth() - 3)
oldest = +oldest
for (var f in data) {
if (data[f].latestMsg && data[f].latestMsg.timestamp > oldest) {
total += data[f].latestMsg.seq
if (data[f].latestMsg.seq > max)
max = data[f].latestMsg.seq
count++
}
}
/*
console.log(total)
console.log(count)
console.log(max)
*/
console.log(JSON.stringify(data))
sbot.close()
}
}
pull(
sbot.friends.hopStream({live: false, old: true}),
pull.drain((hops) => {
var friends = Object.keys(hops).filter(feedId => hops[feedId] <= 1)
friends.forEach((friend) => {
asyncs += 1
sbot.friends.isFollowing({
source: feedId,
dest: friend
}, (err, isOk) => {
if (isOk) {
data[friend] = {}
asyncs += 1
latestValue(sbot, 'name', friend, (err, res) => {
data[friend].nameAbout = res.msgKey
data[friend].name = res.value
check_async()
})
asyncs += 1
latestValue(sbot, 'image', friend, (err, res) => {
data[friend].imageAbout = res.msgKey
if (typeof(res.value) == 'string')
data[friend].image = res.value
else if (res.value && res.value.link)
data[friend].image = res.value.link
check_async()
})
asyncs += 1
latestValue(sbot, 'description', friend, (err, res) => {
data[friend].descriptionAbout = res.msgKey
data[friend].description = res.value
check_async()
})
asyncs += 1
pull(
sbot.createUserStream({
id: friend,
reverse: true,
limit: 1
}),
pull.drain((msg) => {
data[friend].latestMsg = {
key: msg.key,
seq: msg.value.sequence,
timestamp: msg.value.timestamp
}
}, check_async)
)
check_async()
} else {
check_async()
}
})
})
})
)
})