aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-04-12 17:35:59 -0700
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-04-12 18:51:16 -0700
commit74ca52116504c6844d590ac07ef17ee788501924 (patch)
treef72cb93c04cd7fd41a7216857339ba54f68aec68 /lib
parent4bc88fee677e467a6444fd16ec6a90e7c9548aaa (diff)
downloadpatchfoo-74ca52116504c6844d590ac07ef17ee788501924.tar.gz
patchfoo-74ca52116504c6844d590ac07ef17ee788501924.zip
Show follows, followers, and friends (WIP)
Diffstat (limited to 'lib')
-rw-r--r--lib/app.js59
-rw-r--r--lib/serve.js48
2 files changed, 104 insertions, 3 deletions
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,
+ })
+ })
+}
diff --git a/lib/serve.js b/lib/serve.js
index 2f96d57..b394f39 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -799,10 +799,40 @@ Serve.prototype.wrapPage = function (title, searchQ) {
)
}
+Serve.prototype.renderFriend = function (id, cb) {
+ var el = this.app.render.idLink(id, function (err) {
+ cb(err, el)
+ })
+}
+
+Serve.prototype.friendList = function (friends, cb) {
+ var self = this
+ var list = h('div')
+ var first = true
+ pull(
+ pull.values(friends),
+ paramap(function (id, cb) {
+ self.renderFriend(id, function (err, el) {
+ if (err) el = u.renderError(err, ext)
+ cb(null, el)
+ })
+ }, 8),
+ pull.drain(function (el) {
+ if (first) first = false
+ else list.appendChild(h('span', ', '))
+ list.appendChild(el)
+ }, cb)
+ )
+ return list
+}
+
Serve.prototype.wrapUserFeed = function (id) {
var self = this
return u.hyperwrap(function (thread, cb) {
- self.app.getAbout(id, function (err, about) {
+ var done = multicb({pluck: 1, spread: true})
+ self.app.getAbout(id, done())
+ self.app.getFriendInfo(id, done())
+ done(function (err, about, info) {
if (err) return cb(err)
var done = multicb({pluck: 1, spread: true})
done()(null, [
@@ -815,8 +845,20 @@ Serve.prototype.wrapUserFeed = function (id) {
h('code', h('small', id)),
about.description ? h('div',
{innerHTML: self.app.render.markdown(about.description)}) : ''
- )))
- ),
+ )),
+ h('tr',
+ h('td', 'Friends:'),
+ h('td', self.friendList(info.friends, done()))
+ ),
+ h('tr',
+ h('td', 'Follows:'),
+ h('td', self.friendList(info.follows, done()))
+ ),
+ h('tr',
+ h('td', 'Followers:'),
+ h('td', self.friendList(info.followers, done()))
+ )
+ )),
thread
])
done(cb)