-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcore-helpers.js
96 lines (83 loc) · 2.71 KB
/
core-helpers.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
const validate = require('ssb-validate')
const keys = require('ssb-keys')
const pull = require('pull-stream')
const raf = require('polyraf')
const path = require('path')
exports.connectAndRemember = function (addr, data) {
SSB.conn.connect(addr, data, (err, rpc) => {
SSB.conn.remember(addr, Object.assign(data, { autoconnect: true }))
})
}
exports.getPeer = function() {
let connPeers = Array.from(SSB.conn.hub().entries())
connPeers = connPeers.filter(([, x]) => !!x.key).map(([address, data]) => ({ address, data }))
var goodPeer = connPeers.find(cp => cp.data.type != 'room')
let peers = Object.values(SSB.peers).flat()
if (goodPeer) return peers.find(p => p.id == goodPeer.data.key)
else if (peers.length > 0) return peers[0]
else return null
}
function deleteDatabaseFile(filename) {
const file = raf(path.join(SSB.config.path, filename))
file.open((err, done) => {
if (err) return console.error(err)
file.destroy()
})
}
exports.removeDB = function() {
deleteDatabaseFile('log.bipf')
// remove all indexes including jitdb and partial
const IdbKvStore = require('idb-kv-store')
const store = new IdbKvStore(path.join(SSB.dir, "indexes"))
store.clear()
}
exports.removeBlobs = function() {
function listDir(fs, path)
{
fs.root.getDirectory(path, {}, function(dirEntry) {
var dirReader = dirEntry.createReader()
dirReader.readEntries(function(entries) {
for(var i = 0; i < entries.length; i++) {
var entry = entries[i]
if (entry.isDirectory) {
//console.log('Directory: ' + entry.fullPath);
listDir(fs, entry.fullPath)
}
else if (entry.isFile) {
console.log('deleting file: ' + entry.fullPath)
const file = raf(entry.fullPath)
file.open((err, done) => {
if (err) return console.error(err)
file.destroy()
})
}
}
})
})
}
window.webkitRequestFileSystem(window.PERSISTENT, 0, function (fs) {
listDir(fs, '/.ssb-lite/blobs')
})
}
exports.convertHopsIntoGraph = function(hops) {
const following = []
const blocking = []
const extended = []
const feeds = Object.keys(hops)
for (var i = 0; i < feeds.length; ++i) {
const feed = feeds[i]
if (hops[feed] == 1)
following.push(feed)
else if (hops[feed] > 0 && hops[feed] <= SSB.config.friends.hops)
extended.push(feed)
else if (hops[feed] == -1)
blocking.push(feed)
}
return { following, extended, blocking }
}
exports.getGraphForFeed = function(feedId, cb) {
SSB.friends.hops({ start: feedId }, (err, hops) => {
if (err) return cb(err)
else cb(null, exports.convertHopsIntoGraph(hops, feedId == SSB.id))
})
}