aboutsummaryrefslogtreecommitdiff
path: root/lib/contacts.js
blob: 846cdf98f2249996e5f1121e2a0a42101a3b4813 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var pull = require('pull-stream')
var defer = require('pull-defer')
var many = require('pull-many')

module.exports = Contacts

function Contacts(sbot) {
  if (!(this instanceof Contacts)) return new Contacts(sbot)
  this.sbot = sbot
}

Contacts.prototype._createContactStream = function (source, dest) {
  return pull(
    this.sbot.links({
      source: source,
      dest: dest,
      rel: 'contact',
      values: true,
      reverse: true
    }),
    pull.filter(function (msg) {
      var c = msg && msg.value && msg.value.content
      return c && c.type === 'contact' && (!dest || c.contact === dest)
    }),
    pull.map(function (msg) {
      var c = msg && msg.value && msg.value.content
      return {
        source: msg.value.author,
        dest: c.contact,
        msg: msg,
        value: c.following ? true : c.flagged || c.blocking ? false : null
      }
    }),
    pull.unique(function (edge) {
      return edge.source + '-' + edge.dest
    })
  )
}

Contacts.prototype.createFollowsStream = function (id) {
  return pull(
    this._createContactStream(id, null),
    pull.filter('value'),
    pull.map('dest')
  )
}

Contacts.prototype.createFollowersStream = function (id) {
  return pull(
    this._createContactStream(null, id),
    pull.filter('value'),
    pull.map('source')
  )
}

Contacts.prototype.createFollowedFollowersStream = function (source, dest) {
  var follows = {}, followers = {}
  return pull(
    many([
      this._createContactStream(source, null),
      this._createContactStream(null, dest)
    ]),
    pull.filter('value'),
    pull.map(function (edge) {
      if (edge.source === source) {
        if (followers[edge.dest]) {
          delete followers[edge.dest]
          return edge.dest
        } else {
          follows[edge.dest] = true
        }
      } else if (edge.dest === dest) {
        if (follows[edge.source]) {
          delete follows[edge.source]
          return edge.source
        } else {
          followers[edge.source] = true
        }
      }
    }),
    pull.filter()
  )
}

Contacts.prototype.createFriendsStream = function (opts, endCb) {
  if (typeof opts === 'string') opts = {id: opts}
  var id = opts.id
  var msgIds = opts.msgIds
  var follows = {}, followers = {}
  var blocks = {}, blockers = {}
  return pull(
    many([
      this._createContactStream(id, null),
      this._createContactStream(null, id)
    ]),
    pull.map(function (edge) {
      if (edge.value) {
        if (edge.source === id) {
          if (followers[edge.dest]) {
            var item2 = followers[edge.dest]
            delete followers[edge.dest]
            return msgIds ? {feed: edge.dest, msg: edge.msg, msg2: item2.msg} : edge.dest
          } else {
            follows[edge.dest] = msgIds ? {feed: edge.dest, msg: edge.msg} : edge.dest
          }
        } else if (edge.dest === id) {
          if (follows[edge.source]) {
            var item2 = follows[edge.source]
            delete follows[edge.source]
            return msgIds ? {feed: edge.source, msg: edge.msg, msg2: item2.msg} : edge.source
          } else {
            followers[edge.source] = msgIds ? {feed: edge.source, msg: edge.msg} : edge.source
          }
        }
      } else if (edge.value === false) {
        if (edge.source === id) {
          blocks[edge.dest] = msgIds ? {feed: edge.dest, msg: edge.msg} : edge.dest
        } else if (edge.dest === id) {
          blockers[edge.source] = msgIds ? {feed: edge.source, msg: edge.msg} : edge.source
        }
      }
    }),
    pull.filter(),
    endCb && function (read) {
      return function (abort, cb) {
        read(abort, function (end, data) {
          cb(end, data)
          if (end) endCb(end === true ? null : end, {
            followers: Object.values(followers),
            follows: Object.values(follows),
            blocks: Object.values(blocks),
            blockers: Object.values(blockers),
          })
        })
      }
    }
  )
}

Contacts.prototype.createContactStreams = function (opts) {
  var msgIds = opts.msgIds
  var follows = defer.source()
  var followers = defer.source()
  var blocks = defer.source()
  var blockers = defer.source()
  var friends = this.createFriendsStream(opts, function (err, more) {
    follows.resolve(err ? pull.error(err) : pull.values(more.follows))
    followers.resolve(err ? pull.error(err) : pull.values(more.followers))
    blocks.resolve(err ? pull.error(err) : pull.values(more.blocks))
    blockers.resolve(err ? pull.error(err) : pull.values(more.blockers))
  })
  return {
    friends: friends,
    follows: follows,
    followers: followers,
    blocks: blocks,
    blockers: blockers,
  }
}