aboutsummaryrefslogtreecommitdiff
path: root/lib/follows.js
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-12-12 17:03:17 -0800
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-12-12 17:03:17 -0800
commitc329eae0e6ff3d36d7f21349980d51d9dd587faa (patch)
treeca9d4bea107b457cf5264ae9080cd8b533dec235 /lib/follows.js
parenta34dfa54a947eb69c31b3196a8be8a8705a9f4b3 (diff)
downloadpatchfoo-c329eae0e6ff3d36d7f21349980d51d9dd587faa.tar.gz
patchfoo-c329eae0e6ff3d36d7f21349980d51d9dd587faa.zip
Calculate follows using ssb-contact
Avoid use of `live: true` which triggers a bug in the query engine. Use a single live stream to keep called-back objects up-to-date
Diffstat (limited to 'lib/follows.js')
-rw-r--r--lib/follows.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/follows.js b/lib/follows.js
new file mode 100644
index 0000000..b262520
--- /dev/null
+++ b/lib/follows.js
@@ -0,0 +1,43 @@
+var pull = require('pull-stream')
+var memo = require('asyncmemo')
+var lru = require('hashlru')
+
+module.exports = Follows
+
+function Follows(sbot, contacts) {
+ if (!(this instanceof Follows)) return new Follows(sbot)
+
+ this.sbot = sbot
+ this.contacts = contacts
+ var followsCache = lru(100)
+ this.getFollows = memo({cache: followsCache}, this.getFollows)
+
+ pull(
+ sbot.messagesByType({type: 'contact', old: false}),
+ pull.drain(function (msg) {
+ var author = msg && msg.value && msg.value.author
+ var c = msg && msg.value && msg.value.content
+ var follows = author && followsCache.get(author)
+ if (follows && c && c.contact) follows[c.contact] = c.following
+ }, function (err) {
+ if (err) console.trace(err)
+ })
+ )
+}
+
+Follows.prototype.getFollows = function (id, cb) {
+ var follows = {}
+ pull(
+ this.contacts.createFollowsStream(id),
+ pull.drain(function (feed) {
+ follows[feed] = true
+ }, function (err) {
+ if (err) return cb(err)
+ cb(null, follows)
+ })
+ )
+}
+
+Follows.prototype.close = function (cb) {
+ this.sbot.close(cb)
+}