aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-11-25 20:07:18 -0700
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-11-25 20:13:41 -0700
commitf51983b5e78e1dd23ae6482b97679a5618560429 (patch)
treeb36fdcfb0bb4045b57427f186bab4a2a937d03df /lib
parent59bfa01faf1939dc6b327b05439cb0b23cfddcfa (diff)
downloadpatchfoo-f51983b5e78e1dd23ae6482b97679a5618560429.tar.gz
patchfoo-f51983b5e78e1dd23ae6482b97679a5618560429.zip
Use ssb-contact again, with block(er)s
It streams the friends list rather than buffering it
Diffstat (limited to 'lib')
-rw-r--r--lib/app.js7
-rw-r--r--lib/contact.js91
-rw-r--r--lib/serve.js59
3 files changed, 28 insertions, 129 deletions
diff --git a/lib/app.js b/lib/app.js
index 5557b58..da41459 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -6,7 +6,7 @@ var u = require('./util')
var pull = require('pull-stream')
var multicb = require('multicb')
var paramap = require('pull-paramap')
-var getContacts = require('./contact')
+var Contacts = require('ssb-contact')
var About = require('./about')
var Serve = require('./serve')
var Render = require('./render')
@@ -51,7 +51,6 @@ function App(sbot, config) {
sbot.blobs.size.bind(sbot.blobs))
this.getFollows = memo(this._getFollows.bind(this))
this.getVotes = memo({cache: lru(100)}, this._getVotes.bind(this))
- this.getContacts = getContacts.bind(null, this.sbot)
this.unboxMsg = this.unboxMsg.bind(this)
@@ -465,6 +464,10 @@ App.prototype.streamMyChannels = function (id, opts) {
)
}
+App.prototype.createContactStreams = function (id) {
+ return new Contacts(this.sbot).createContactStreams(id)
+}
+
function compareVoted(a, b) {
return b.value - a.value
}
diff --git a/lib/contact.js b/lib/contact.js
deleted file mode 100644
index c419921..0000000
--- a/lib/contact.js
+++ /dev/null
@@ -1,91 +0,0 @@
-var pull = require('pull-stream')
-var multicb = require('multicb')
-
-function accumulateNonNull(a, b) {
- return b == null ? a : b
-}
-
-module.exports = function (sbot, id, cb) {
- var followed = {}, followedBy = {}, blocked = {}, blockedBy = {}
- var done = multicb({pluck: 1})
- pull(
- sbot.links2.read({
- reverse: true, // oldest first. ssb-links has this switched
- query: [
- {$filter: {
- source: id,
- rel: [{$prefix: 'contact'}]
- }},
- {$reduce: {
- id: 'dest',
- following: {$collect: ['rel', 1]},
- blocking: {$collect: ['rel', 2]}
- }}
- ]
- }),
- pull.drain(function (op) {
- var following = op.following.reduce(accumulateNonNull, null)
- var blocking = op.blocking.reduce(accumulateNonNull, null)
- if (following != null) followed[op.id] = following
- if (blocking != null) blocked[op.id] = blocking
- }, done())
- )
- pull(
- sbot.links2.read({
- reverse: true, // oldest first. ssb-links has this switched
- query: [
- {$filter: {
- dest: id,
- rel: [{$prefix: 'contact'}]
- }},
- {$reduce: {
- id: 'source',
- following: {$collect: ['rel', 1]},
- blocking: {$collect: ['rel', 2]}
- }}
- ]
- }),
- pull.drain(function (op) {
- var following = op.following.reduce(accumulateNonNull, null)
- var blocking = op.blocking.reduce(accumulateNonNull, null)
- if (following != null) followedBy[op.id] = following
- if (blocking != null) blockedBy[op.id] = blocking
- }, done())
- )
-
- done(function (err) {
- if (err) return cb(new Error(err.stack || err))
- var id
- var friendsList = []
- var followingList = []
- var blockingList = []
- var followedByList = []
- var blockedByList = []
-
- for (id in followed) {
- if (followed[id]) {
- if (followedBy[id]) friendsList.push(id)
- else followingList.push(id)
- }
- }
- for (id in followedBy) {
- if (followedBy[id] && !followed[id]) {
- followedByList.push(id)
- }
- }
- for (id in blocked) {
- if (blocked[id]) blockingList.push(id)
- }
- for (id in blockedBy) {
- if (blockedBy[id]) blockedByList.push(id)
- }
-
- cb(null, {
- follows: followingList,
- followers: followedByList,
- friends: friendsList,
- blocks: blockingList,
- blockers: blockedByList
- })
- })
-}
diff --git a/lib/serve.js b/lib/serve.js
index e05acd5..47bc7c2 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -797,10 +797,10 @@ Serve.prototype.channels = function (ext) {
Serve.prototype.contacts = function (path) {
var self = this
var id = String(path).substr(1)
+ var contacts = self.app.createContactStreams(id)
- function renderFriendsList(ids) {
+ function renderFriendsList() {
return pull(
- pull.values(ids),
paramap(function (id, cb) {
self.app.getAbout(id, function (err, about) {
var name = about && about.name || id.substr(0, 8) + '…'
@@ -815,40 +815,27 @@ Serve.prototype.contacts = function (path) {
)
}
- self.app.getContacts(id, function (err, contacts) {
- if (err) return self.respond(500, err.stack || err)
- pull(
- cat([
- ph('section', {}, [
- ph('h3', {}, ['Contacts: ', self.phIdLink(id)]),
- contacts.friends.length ? [
- ph('h4', {}, 'Friends'),
- renderFriendsList(contacts.friends),
- ] : [],
- contacts.follows.length ? [
- ph('h4', {}, 'Follows'),
- renderFriendsList(contacts.follows),
- ] : [],
- contacts.followers.length ? [
- ph('h4', {}, 'Followers'),
- renderFriendsList(contacts.followers),
- ] : [],
- contacts.blocks.length ? [
- ph('h4', {}, 'Blocks'),
- renderFriendsList(contacts.blocks),
- ] : [],
- contacts.blockers.length ? [
- ph('h4', {}, 'Blocked by'),
- renderFriendsList(contacts.blockers),
- ] : [],
- ])
- ]),
- self.wrapPage('contacts: ' + id),
- self.respondSink(200, {
- 'Content-Type': ctype('html')
- })
- )
- })
+ pull(
+ cat([
+ ph('section', {}, [
+ ph('h3', {}, ['Contacts: ', self.phIdLink(id)]),
+ ph('h4', {}, 'Friends'),
+ renderFriendsList()(contacts.friends),
+ ph('h4', {}, 'Follows'),
+ renderFriendsList()(contacts.follows),
+ ph('h4', {}, 'Followers'),
+ renderFriendsList()(contacts.followers),
+ ph('h4', {}, 'Blocks'),
+ renderFriendsList()(contacts.blocks),
+ ph('h4', {}, 'Blocked by'),
+ renderFriendsList()(contacts.blockers)
+ ])
+ ]),
+ this.wrapPage('contacts: ' + id),
+ this.respondSink(200, {
+ 'Content-Type': ctype('html')
+ })
+ )
}
Serve.prototype.about = function (path) {