git ssb

16+

cel / patchfoo



Commit c329eae0e6ff3d36d7f21349980d51d9dd587faa

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
cel committed on 12/13/2017, 1:03:17 AM
Parent: a34dfa54a947eb69c31b3196a8be8a8705a9f4b3

Files changed

lib/app.jschanged
lib/follows.jsadded
lib/app.jsView
@@ -7,8 +7,9 @@
77 var multicb = require('multicb')
88 var paramap = require('pull-paramap')
99 var Contacts = require('ssb-contact')
1010 var About = require('./about')
11 +var Follows = require('./follows')
1112 var Serve = require('./serve')
1213 var Render = require('./render')
1314 var Git = require('./git')
1415 var cat = require('pull-cat')
@@ -48,16 +49,16 @@
4849 this.reverseNameCache = lru(500)
4950 this.reverseEmojiNameCache = lru(500)
5051 this.getBlobSize = memo({cache: this.blobSizeCache = lru(100)},
5152 sbot.blobs.size.bind(sbot.blobs))
52- this.getFollows = memo(this._getFollows.bind(this))
5353 this.getVotes = memo({cache: lru(100)}, this._getVotes.bind(this))
5454
5555 this.unboxMsg = this.unboxMsg.bind(this)
5656
5757 this.render = new Render(this, this.opts)
5858 this.git = new Git(this)
5959 this.contacts = new Contacts(this.sbot)
60 + this.follows = new Follows(this.sbot, this.contacts)
6061
6162 this.monitorBlobWants()
6263 }
6364
@@ -689,9 +690,9 @@
689690 if (filter === 'all'
690691 || author === myId
691692 || author === opts.feed
692693 || msg.key === opts.msgId) return cb(null, show)
693- self.getFollows(myId, function (err, follows) {
694 + self.follows.getFollows(myId, function (err, follows) {
694695 if (err) return cb(err)
695696 if (follows[author]) return cb(null, show)
696697 self.getVotes(msg.key, function (err, votes) {
697698 if (err) return cb(err)
@@ -706,43 +707,14 @@
706707 }
707708
708709 App.prototype.isFollowing = function (src, dest, cb) {
709710 var self = this
710- self.getFollows(src, function (err, follows) {
711 + self.follows.getFollows(src, function (err, follows) {
711712 if (err) return cb(err)
712713 return cb(null, follows[dest])
713714 })
714715 }
715716
716-App.prototype._getFollows = function (id, cb) {
717- var follows = {}
718- function ready(err) {
719- if (!cb) return
720- var _cb = cb
721- cb = null
722- _cb(err, follows)
723- }
724- pull(
725- this.sbot.links2.read({
726- live: true,
727- query: [
728- {$filter: {
729- source: id,
730- rel: [{$prefix: 'contact'}]
731- }},
732- {$map: {
733- following: ['rel', 1],
734- feed: 'dest'
735- }}
736- ]
737- }),
738- pull.drain(function (link) {
739- if (link.sync) return ready()
740- follows[link.feed] = link.following
741- }, ready)
742- )
743-}
744-
745717 App.prototype._getVotes = function (id, cb) {
746718 var votes = {}
747719 pull(
748720 this.sbot.links2.read({
lib/follows.jsView
@@ -1,0 +1,43 @@
1 +var pull = require('pull-stream')
2 +var memo = require('asyncmemo')
3 +var lru = require('hashlru')
4 +
5 +module.exports = Follows
6 +
7 +function Follows(sbot, contacts) {
8 + if (!(this instanceof Follows)) return new Follows(sbot)
9 +
10 + this.sbot = sbot
11 + this.contacts = contacts
12 + var followsCache = lru(100)
13 + this.getFollows = memo({cache: followsCache}, this.getFollows)
14 +
15 + pull(
16 + sbot.messagesByType({type: 'contact', old: false}),
17 + pull.drain(function (msg) {
18 + var author = msg && msg.value && msg.value.author
19 + var c = msg && msg.value && msg.value.content
20 + var follows = author && followsCache.get(author)
21 + if (follows && c && c.contact) follows[c.contact] = c.following
22 + }, function (err) {
23 + if (err) console.trace(err)
24 + })
25 + )
26 +}
27 +
28 +Follows.prototype.getFollows = function (id, cb) {
29 + var follows = {}
30 + pull(
31 + this.contacts.createFollowsStream(id),
32 + pull.drain(function (feed) {
33 + follows[feed] = true
34 + }, function (err) {
35 + if (err) return cb(err)
36 + cb(null, follows)
37 + })
38 + )
39 +}
40 +
41 +Follows.prototype.close = function (cb) {
42 + this.sbot.close(cb)
43 +}

Built with git-ssb-web