From 74ca52116504c6844d590ac07ef17ee788501924 Mon Sep 17 00:00:00 2001 From: cel Date: Wed, 12 Apr 2017 17:35:59 -0700 Subject: Show follows, followers, and friends (WIP) --- lib/app.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'lib/app.js') diff --git a/lib/app.js b/lib/app.js index 8ac01d6..6cc71a0 100644 --- a/lib/app.js +++ b/lib/app.js @@ -200,3 +200,62 @@ App.prototype.streamPeers = function (opts) { }) }) } + +App.prototype.getFollows = function (id, cb) { + var self = this + pull( + self.sbot.links({source: id, rel: 'contact', values: true}), + pull.filter(function (msg) { + var c = msg && msg.value && msg.value.content + return c && c.type === 'contact' && msg.value.author === id + }), + pull.reduce(function (acc, msg) { + var c = msg.value.content + if (c.following) acc[c.contact] = true + else delete acc[c.contact] + return acc + }, {}, cb) + ) +} + +App.prototype.getFollowers = function (id, cb) { + var self = this + pull( + self.sbot.links({dest: id, rel: 'contact', values: true}), + pull.filter(function (msg) { + var c = msg && msg.value && msg.value.content + return c && c.type === 'contact' && c.contact === id + }), + pull.reduce(function (acc, msg) { + var c = msg.value.content + if (c.following) acc[msg.value.author] = true + else delete acc[msg.value.author] + return acc + }, {}, cb) + ) +} + +App.prototype.getFriendInfo = function (id, cb) { + var self = this + var done = multicb({pluck: 1, spread: true}) + self.getFollows(id, done()) + self.getFollowers(id, done()) + done(function (err, followsObj, followersObj) { + if (err) return cb(err) + var friends = [] + var follows = [] + var followers = [] + for (var k in followsObj) { + if (followersObj[k]) friends.push(k) + else follows.push(k) + } + for (var k in followersObj) { + if (!followsObj[k]) followers.push(k) + } + cb(null, { + friends: friends, + follows: follows, + followers: followers, + }) + }) +} -- cgit v1.2.3