var pull = require('pull-stream') // Reduce a feed's own contact and channel-subscription messages into the sets // they describe. createUserStream runs oldest-first, so a later unfollow // correctly undoes an earlier follow. // // Both /user-feed/ and /items are built on this: "who does this feed listen // to" is the whole selection rule, which is what lets a new kiosk show up by // being followed rather than by being added to a list somewhere. module.exports = function getFollows (sbot, feedId, cb) { var following = Object.create(null) var channelSubscriptions = Object.create(null) pull( sbot.createUserStream({ id: feedId }), pull.filter(function (msg) { var c = msg && msg.value && msg.value.content if (!c || typeof c !== 'object') return false // also skips private (string) content return c.type === 'contact' || (c.type === 'channel' && typeof c.subscribed !== 'undefined') }), pull.drain(function (msg) { var c = msg.value.content if (c.type === 'contact') { if (!c.contact) return if (c.following) following[c.contact] = true else delete following[c.contact] } else { if (!c.channel) return if (c.subscribed) channelSubscriptions[c.channel] = true else delete channelSubscriptions[c.channel] } }, function (err) { if (err) return cb(err) cb(null, { following: following, channelSubscriptions: channelSubscriptions }) }) ) }