Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 2a4cf6f

Browse files
committed
handle empty database with new contacts index
1 parent 179a7f9 commit 2a4cf6f

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

sbot/contacts.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,41 @@ exports.manifest = {
1313
}
1414

1515
exports.init = function (ssb, config) {
16-
var view = ssb._flumeUse('patchwork-contacts', FlumeReduce(0, reduce, map))
16+
var values = {}
17+
18+
var view = ssb._flumeUse('patchwork-contacts', FlumeReduce(0, function reduce (result, item) {
19+
// used by the reducer view
20+
if (!result) result = {}
21+
if (item) {
22+
for (let author in item) {
23+
for (let contact in item[author]) {
24+
if (!result[author]) result[author] = {}
25+
result[author][contact] = item[author][contact]
26+
}
27+
}
28+
}
29+
30+
// always make sure values is the latest result
31+
// hack around result being null on index initialize
32+
values = result
33+
return result
34+
}, function map (msg) {
35+
// used by the reducer view
36+
if (msg.value && msg.value.content && msg.value.content.type === 'contact' && ref.isFeed(msg.value.content.contact)) {
37+
return {
38+
[msg.value.author]: {
39+
[msg.value.content.contact]: getContactState(msg.value.content)
40+
}
41+
}
42+
}
43+
}))
44+
45+
view.get((err, result) => {
46+
if (!err && result) {
47+
// initialize values
48+
values = result
49+
}
50+
})
1751

1852
return {
1953
// expose raw view to other plugins (not over rpc)
@@ -22,15 +56,15 @@ exports.init = function (ssb, config) {
2256
isFollowing: function ({ source, dest }, cb) {
2357
view.get((err, graph) => {
2458
if (err) return cb(err)
25-
var following = graph[source] && graph[source][dest] === true
59+
var following = graph && graph[source] && graph[source][dest] === true
2660
cb(null, following)
2761
})
2862
},
2963

3064
isBlocking: function ({ source, dest }, cb) {
3165
view.get((err, graph) => {
3266
if (err) return cb(err)
33-
var blocking = graph[source] && graph[source][dest] === false
67+
var blocking = graph && graph[source] && graph[source][dest] === false
3468
cb(null, blocking)
3569
})
3670
},
@@ -170,7 +204,6 @@ exports.init = function (ssb, config) {
170204
var stream = PullPushAbort()
171205

172206
var lastResolvedValues = {}
173-
var values = {}
174207

175208
var timer = null
176209
var queued = false
@@ -200,7 +233,6 @@ exports.init = function (ssb, config) {
200233
if (!sync) {
201234
// we'll store the incoming values (they will be updated as the view updates so
202235
// do not need to be manually patched)
203-
values = msg
204236
sync = true
205237
update()
206238

@@ -264,28 +296,3 @@ function resolveValues (values, yourId) {
264296
}
265297
return result
266298
}
267-
268-
function reduce (result, item) {
269-
// used by the reducer view
270-
if (!result) result = {}
271-
if (item) {
272-
for (let author in item) {
273-
for (let contact in item[author]) {
274-
if (!result[author]) result[author] = {}
275-
result[author][contact] = item[author][contact]
276-
}
277-
}
278-
}
279-
return result
280-
}
281-
282-
function map (msg) {
283-
// used by the reducer view
284-
if (msg.value && msg.value.content && msg.value.content.type === 'contact' && ref.isFeed(msg.value.content.contact)) {
285-
return {
286-
[msg.value.author]: {
287-
[msg.value.content.contact]: getContactState(msg.value.content)
288-
}
289-
}
290-
}
291-
}

sbot/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ exports.init = function (ssb, config) {
9191
patchwork.contacts.raw.get((err, graph) => {
9292
if (!err) {
9393
ssb.gossip.peers().slice().forEach((peer) => {
94-
if (graph[ssb.id]) {
94+
if (graph && graph[ssb.id]) {
9595
var value = graph[ssb.id][peer.key]
9696
if (value === true) { // following pub
9797
ssb.gossip.add(peer, 'friends')
@@ -138,7 +138,7 @@ exports.init = function (ssb, config) {
138138
if (!value.content) return
139139
var address = value.content.address
140140
if (replicating.has(value.author) && address && ref.isFeed(address.key)) {
141-
var blocking = graph[ssb.id] && graph[ssb.id][address.key] === false
141+
var blocking = graph && graph[ssb.id] && graph[ssb.id][address.key] === false
142142
if (!blocking) {
143143
discovered.add(address.key)
144144
ssb.gossip.add(address, 'pub')

0 commit comments

Comments
 (0)