aboutsummaryrefslogtreecommitdiff
path: root/lib/contact.js
blob: 676c84a6f064651cbe6e79565d6ced540a4c7f52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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({
      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({
      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
    })
  })
}