var nest = require('depnest')
var pull = require('pull-stream')

exports.needs = nest({
  'sbot.pull.query': 'first'
})

exports.gives = nest('contact.async.followerOf')

exports.create = function (api) {
  return nest('contact.async.followerOf', function (source, dest, cb) {
    pull(
      api.sbot.pull.query({query: [
        makeQuery(source, dest),
        {$map: ['value', 'content', 'following']}
      ]}),
      pull.collect(function (err, ary) {
        if (err) return cb(err)
        else cb(null, ary.pop()) // will be true, or undefined/false
      })
    )
  })
}

function makeQuery (a, b) {
  return {"$filter": {
    value: {
      author: a,
      content: {
        type: 'contact',
        contact: b,
        following: true
      }
    },
  }}
}

@@ -101,16 +101,49 @@
 .query':
+ 'first',%0A  'sbot.async.publish':
  'first'
@@ -164,24 +164,28 @@
 ives = nest(
+%7B%0A  
 'contact.asy
@@ -178,33 +178,59 @@
   'contact.async
-.
+': %5B'follow', 'unfollow', '
 followerOf')%0A%0Aex
@@ -224,16 +224,19 @@
 lowerOf'
+%5D%0A%7D
 )%0A%0Aexpor
@@ -278,16 +278,22 @@
 rn nest(
+%7B%0A    
 'contact
@@ -302,30 +302,69 @@
 sync
-.followerOf', function
+': %7Bfollow, unfollow, followerOf%7D%0A  %7D)%0A%0A  function followerOf
  (so
@@ -681,17 +681,295 @@
    )%0A  %7D
-)
+%0A%0A  function follow (id, cb) %7B%0A    api.sbot.async.publish(%7B%0A      type: 'contact',%0A      contact: id,%0A      following: true%0A    %7D, cb)%0A  %7D%0A%0A  function unfollow (id, cb) %7B%0A    api.sbot.async.publish(%7B%0A      type: 'contact',%0A      contact: id,%0A      following: false%0A    %7D, cb)%0A  %7D
 %0A%7D%0A%0Afunc

add contact.async.follow/unfollow