git ssb

2+

mixmix / ticktack



Tree: e82f383b2cc56a63e4afa6eb79a83f8b874713f6

Files: e82f383b2cc56a63e4afa6eb79a83f8b874713f6 / backup / async / exportIdentity.js

2842 bytesRaw
1const nest = require('depnest')
2const { onceTrue } = require('mutant')
3const path = require('path')
4const fs = require('fs')
5const parallel = require('run-parallel')
6const mapLimit = require('map-limit')
7
8const config = require('../../config').create().config.sync.load()
9const gossipFile = path.join(config.path, 'gossip.json')
10const secretFile = path.join(config.path, 'secret')
11
12exports.gives = nest('backup.async.exportIdentity')
13
14exports.needs = nest({
15 'sbot.obs.connection': 'first'
16})
17
18exports.create = function (api) {
19 return nest('backup.async.exportIdentity', (filename, cb) => {
20 if (typeof filename === 'undefined') return cb(new Error('backup requires a filename'))
21
22 console.log(`should export identity to file ${filename}`)
23
24 var backup = {
25 exportDate: new Date().toISOString(),
26 secret: fs.readFileSync(secretFile, 'utf8'),
27 gossip: require(gossipFile)
28 }
29
30 onceTrue(api.sbot.obs.connection, sbot => {
31 parallel([
32 getLatestSequence,
33 getGossipFollowers,
34 getPeersLatestSequence
35 ], save)
36
37 function getLatestSequence (done) {
38 sbot.latestSequence(sbot.id, (err, seq) => {
39 if (err) return done(err)
40
41 backup.mySequence = { latest: seq }
42 done(null)
43 })
44 }
45
46 function getGossipFollowers (done) {
47 // the peers in gossip list who follow me
48 sbot.friends.get({ dest: sbot.id }, (err, followers) => {
49 if (err) return done(err)
50
51 backup.gossip.forEach(record => {
52 if (followers[record.key]) record.followsMe = true
53 })
54
55 done(null)
56 })
57 }
58
59 function getPeersLatestSequence (done) {
60 sbot.friends.get({ source: sbot.id }, (err, d) => {
61 if (err) return done(err)
62
63 var follows = Object.keys(d).filter(id => d[id] === true)
64
65 mapLimit(follows, 5,
66 (id, cb) => sbot.latestSequence(id, (err, seq) => {
67 if (err && err.message && err.message.indexOf('not found') > 0) {
68 console.error(err)
69 return cb(null, null) // don't include this user
70 }
71 if (err) return cb(err)
72
73 cb(null, [ id, seq ])
74 }),
75 (err, peers) => {
76 if (err) return done(err)
77
78 // TODO change
79 backup.peersLatestSequence = peers
80 .filter(Boolean)
81 .reduce((soFar, [ id, seq ]) => {
82 if (seq) soFar[id] = seq
83 return soFar
84 }, {})
85 done(null)
86 }
87 )
88 })
89 }
90 })
91
92 function save (err, success) {
93 if (err) return cb(err)
94
95 fs.writeFileSync(filename, JSON.stringify(backup, null, 2), 'utf8')
96 cb(null, true)
97 }
98
99 return true
100 })
101}
102

Built with git-ssb-web