Files: ef5b680f12314fbee18f0b73237211b34f26360c / lib / dump.js
1716 bytesRaw
1 | var Pull = require("pull-stream"); |
2 | var Paramap = require("pull-paramap"); |
3 | var AsyncMemo = require("asyncmemo"); |
4 | var getAvatar = require("ssb-avatar"); |
5 | var Query = require("./query"); |
6 | var Format = require("./format"); |
7 | var Dump = module.exports = {}; |
8 | |
9 | Dump.records = function (sbot, opts, each, done) { |
10 | Pull(Query.inDomain(sbot, opts.name), |
11 | Query.drainSet(each, done)); |
12 | }; |
13 | |
14 | function sortRecords(records) { |
15 | records.forEach(function (r) { |
16 | r.labels = typeof r.name === 'string' |
17 | ? r.name.split(/\./).reverse() : []; |
18 | }); |
19 | records.sort(function (a, b) { |
20 | var len = Math.max(a.labels.length, b.labels.length); |
21 | for (var i = 0; i < len; i++) { |
22 | var labA = a.labels[i] || ''; |
23 | var labB = b.labels[i] || ''; |
24 | if (labA > labB) return 1; |
25 | if (labA < labB) return -1; |
26 | } |
27 | return 0; |
28 | }) |
29 | } |
30 | |
31 | Dump.formattedRecords = function (sbot, opts, each, done) { |
32 | sbot.whoami(function (err, feed) { |
33 | if (err) throw err; |
34 | var getAbout = AsyncMemo(getAvatar, sbot, feed.id); |
35 | |
36 | Pull(Query.inDomain(sbot, opts.name), |
37 | Query.collectSet(function (err, records) { |
38 | if (err) throw err; |
39 | records.forEach(Format.formatNames(opts.name)); |
40 | sortRecords(records); |
41 | Pull(Pull.values(records), |
42 | Paramap(function (record, cb) { |
43 | getAbout(record.author, function (err, about) { |
44 | record.authorName = '@' + about.name; |
45 | cb(err, record); |
46 | }); |
47 | }, 8), |
48 | Pull.map(Format.recordsToLines(Format.getMaxLengths(records))), |
49 | Pull.drain(each, done)) |
50 | })); |
51 | }); |
52 | }; |
53 |
Built with git-ssb-web