-
Notifications
You must be signed in to change notification settings - Fork 5
/
blobs.js
executable file
·62 lines (52 loc) · 1.76 KB
/
blobs.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
#! /usr/bin/env node
var pull = require('pull-stream')
module.exports = function (userId, sbot, screen) {
var listWidget = require('./lib/widgets/list')(screen, ' Blobs: ' + userId + ' ')
// load data
var blobs, blobMessageMap = {}
pull(
// fetch messages by `userId` which link to a blob
sbot.links({ source: userId, dest: '&', values: true }),
// group together messages that publish a blob
pull.filter(function (index) {
var blobId = index.dest
if (!blobMessageMap[blobId]) {
blobMessageMap[blobId] = [index]
return true
}
blobMessageMap[blobId].push(index)
return false
}),
// collect into an array
pull.collect(function (err, _blobs) {
if (err) throw err
blobs = _blobs
// sort by the number of references to the blob
blobs.sort(function (a, b) {
return blobMessageMap[b.dest].length - blobMessageMap[a.dest].length
})
// render in the list widget
var listItems = blobs.map(function (index) {
return index.dest + ' ('+blobMessageMap[index.dest].length+' references)'
})
listWidget.setItems(listItems)
listWidget.select(0)
screen.render()
})
)
listWidget.on('select', function (el, selected) {
// load up a message view with the selected message
var popup = require('./lib/widgets/popup')(screen, JSON.stringify(blobMessageMap[blobs[selected].dest], 0, 2))
screen.stack.replace(listWidget, popup)
})
return listWidget
}
if (!module.parent) {
var argv = require('minimist')(process.argv.slice(2))
var userId = argv._[0]
if (!require('ssb-ref').isFeed(userId)) {
console.error('Usage: blobs.js {feedid}')
process.exit(1)
}
require('./lib/app')(module.exports.bind(null, userId))
}