git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: abb85fa0947760cd74602bc9f246265ae015e3e1

Files: abb85fa0947760cd74602bc9f246265ae015e3e1 / sbot / channels.js

1308 bytesRaw
1var FlumeReduce = require('flumeview-reduce')
2var normalizeChannel = require('../lib/normalize-channel')
3
4module.exports = function (ssb, config) {
5 return ssb._flumeUse('patchwork-channels', FlumeReduce(1, reduce, map))
6}
7
8function isActivityMessage(msg) {
9 var isVote = msg.value.content.type === "vote";
10
11 return !isVote && msg.value.content.subscribed !== false
12 && msg.value.content.subscribed !== true;
13}
14
15function reduce (result, item) {
16 if (!result) result = {}
17 if (item) {
18 for (var channel in item) {
19 var value = result[channel]
20 if (!value) {
21 value = result[channel] = {count: 0, timestamp: 0}
22 }
23 value.count += 1
24
25 // We don't update the timestamp if the messsage was just somebody subscribing
26 // or unsubscribing from the channel, or it is a vote as we don't want it to register as
27 // 'recent activity'.
28 if (item[channel].isActivityMessage && item[channel].timestamp > value.timestamp) {
29 value.timestamp = item[channel].timestamp
30 }
31 }
32 }
33 return result
34}
35
36function map (msg) {
37 if (msg.value.content) {
38 var channel = normalizeChannel(msg.value.content.channel)
39 if (channel) {
40 return {
41 [channel]: {timestamp: msg.timestamp, isActivityMessage: isActivityMessage(msg) }
42 }
43 }
44 }
45}
46

Built with git-ssb-web