git ssb

0+

dinoworm ๐Ÿ› / ssb-reveal



Tree: 6a7f12faedfeb0912d99c14588d1deb6db8cecce

Files: 6a7f12faedfeb0912d99c14588d1deb6db8cecce / bin.js

2433 bytesRaw
1#!/usr/bin/env node
2
3var pull = require('pull-stream')
4var Client = require('ssb-client')
5var sort = require('ssb-sort')
6var parallel = require('run-parallel')
7
8var rootMessageId = process.argv[2]
9
10if (rootMessageId == null) {
11 console.log('usage: ssb-reveal <rootMessageId>')
12} else {
13 run()
14}
15
16function run () {
17 Client(function (err, client) {
18 if (err) throw err
19
20 parallel(
21 [
22 cb => getMessage({ client, messageId: rootMessageId }, cb),
23 cb => getThreadRepliesByRootId({ client, rootMessageId }, cb),
24 cb => client.about.get(cb)
25 ],
26 function (err, [rootMessage, threadReplies, about]) {
27 if (err) throw err
28
29 var thread = [rootMessage, ...threadReplies]
30 thread.forEach(function (message, index) {
31 if (index === 0) {
32 console.log(`- ${formatMessageLink({ message, about })}`)
33 } else {
34 console.log(` - ${formatMessageLink({ message, about })}`)
35 }
36 })
37
38 client.close()
39 }
40 )
41 })
42}
43
44function formatMessageLink ({ message, about }) {
45 var authorName = about[message.value.author]['name'][message.value.author][0]
46 var time = formatTimestamp(message.value.timestamp)
47 return `[${time} : ${authorName}](${message.key}?unbox=${message.value.unbox})`
48}
49
50function formatTimestamp (timestamp) {
51 var date = new Date(timestamp)
52 var year = date.getUTCFullYear()
53 var month = date.getUTCMonth()
54 var day = date.getUTCDate()
55 var hours = date.getUTCHours()
56 var minutes = date.getUTCMinutes()
57 return `${pad(year)}-${pad(month)}-${pad(day)} ${pad(hours)}:${pad(minutes)}`
58}
59
60function pad (number) {
61 return number < 10 ? `0${number}` : number
62}
63
64// wrap client.get to be same format as returned by indexes
65function getMessage ({ client, messageId }, cb) {
66 client.get(
67 {
68 private: true,
69 id: rootMessageId
70 },
71 function (err, message) {
72 if (err) cb(err)
73 else cb(null, { key: messageId, value: message })
74 }
75 )
76}
77
78function getThreadRepliesByRootId ({ client, rootMessageId }, cb) {
79 var query = [
80 {
81 $filter: {
82 value: {
83 content: {
84 root: rootMessageId
85 },
86 timestamp: {
87 $gt: 1
88 }
89 }
90 }
91 }
92 ]
93
94 pull(
95 client.query.read({ query }),
96 pull.collect(function (err, messages) {
97 if (err) return cb(err)
98
99 cb(null, sort(messages))
100 })
101 )
102}
103
104

Built with git-ssb-web