aboutsummaryrefslogtreecommitdiff
path: root/lib/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/app.js')
-rw-r--r--lib/app.js115
1 files changed, 115 insertions, 0 deletions
diff --git a/lib/app.js b/lib/app.js
index d22e2b7..5630f41 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -8,6 +8,8 @@ var ssbAvatar = require('ssb-avatar')
var hasher = require('pull-hash/ext/ssb')
var multicb = require('multicb')
var paramap = require('pull-paramap')
+var many = require('pull-many')
+var defer = require('pull-defer')
var Serve = require('./serve')
var Render = require('./render')
@@ -239,3 +241,116 @@ App.prototype.streamChannels = function (opts) {
pull.unique()
)
}
+
+App.prototype.streamFollows = function (id) {
+ return pull(
+ this.sbot.links({
+ source: id,
+ rel: 'contact',
+ values: true,
+ reverse: true,
+ }),
+ pull.map(function (msg) {
+ return msg.value.content
+ }),
+ pull.filter(),
+ pull.unique(function (c) {
+ return c.contact
+ }),
+ pull.filter(function (c) {
+ return c.following
+ }),
+ pull.map(function (c) {
+ return c.contact
+ })
+ )
+}
+
+App.prototype.streamFollowers = function (id) {
+ return pull(
+ this.sbot.links({
+ dest: id,
+ rel: 'contact',
+ values: true,
+ reverse: true,
+ }),
+ pull.unique(function (msg) {
+ return msg.value.author
+ }),
+ pull.filter(function (msg) {
+ var c = msg.value.content
+ return c && c.following
+ }),
+ pull.map(function (msg) {
+ return msg.value.author
+ })
+ )
+}
+
+App.prototype.streamFriends = function (id, endCb) {
+ var follows = {}, followers = {}
+ return pull(
+ many([
+ pull(
+ this.streamFollows(id),
+ pull.map(function (id) {
+ return {id: id, follow: true}
+ })
+ ),
+ pull(
+ this.streamFollowers(id),
+ pull.map(function (id) {
+ return {id: id, follower: true}
+ })
+ )
+ ]),
+ pull.filter(function (op) {
+ if (op.follow) {
+ if (followers[op.id]) {
+ delete followers[op.id]
+ return true
+ } else {
+ follows[op.id] = true
+ return false
+ }
+ }
+ if (op.follower) {
+ if (follows[op.id]) {
+ delete follows[op.id]
+ return true
+ } else {
+ followers[op.id] = true
+ return false
+ }
+ }
+ }),
+ pull.map(function (op) {
+ return op.id
+ }),
+ endCb && function (read) {
+ return function (abort, cb) {
+ read(abort, function (end, data) {
+ cb(end, data)
+ if (end) endCb(end === true ? null : end, {
+ followers: Object.keys(followers),
+ follows: Object.keys(follows),
+ })
+ })
+ }
+ }
+ )
+}
+
+App.prototype.createContactStreams = function (id) {
+ var follows = defer.source()
+ var followers = defer.source()
+ var friends = this.streamFriends(id, function (err, more) {
+ follows.resolve(err ? pull.error(err) : pull.values(more.follows))
+ followers.resolve(err ? pull.error(err) : pull.values(more.followers))
+ })
+ return {
+ friends: friends,
+ follows: follows,
+ followers: followers,
+ }
+}