var FlumeReduce = require('flumeview-reduce') var normalizeChannel = require('../lib/normalize-channel') module.exports = function (ssb, config) { return ssb._flumeUse('patchwork-channels', FlumeReduce(1, reduce, map)) } function reduce (result, item) { if (!result) result = {} if (item) { for (var channel in item) { var value = result[channel] if (!value) { value = result[channel] = {count: 0, timestamp: 0} } value.count += 1 if (item[channel].timestamp > value.timestamp) { value.timestamp = item[channel].timestamp } } } return result } function map (msg) { if (msg.value.content) { var isLike = msg.value.content.type === 'vote' var isSubscription = msg.value.content.type === 'channel' var channel = normalizeChannel(msg.value.content.channel) // filter out likes and subscriptions if (channel && !isLike && !isSubscription) { return { [channel]: {timestamp: msg.timestamp} } } } }