Commit a683541f27c2757c560090b0fa816b37d947d42f
get a count of particular channel and omit duplicates
Kieran committed on 5/23/2018, 6:10:33 AMParent: 27ed847e4353a6feefac698e4cd0697e7bf5d90a
Files changed
intermediate/6_channelMentions.test.js | ||
---|---|---|
@@ -1,46 +1,0 @@ | ||
1 | -const test = require('tape') | |
2 | -const Server = require('scuttle-testbot') | |
3 | -const pull = require('pull-stream') | |
4 | - | |
5 | -test('get a count of the number of posts to a channel', t => { | |
6 | - Server.use(require('./channel')) | |
7 | - const server = Server() | |
8 | - | |
9 | - t.plan(1) | |
10 | - | |
11 | - pull( | |
12 | - pull.values(['myco', 'ssb', 'economics', 'monkeys', 'pineapples', null, 'pineapples', undefined]), | |
13 | - pull.asyncMap(function (channel, cb) { | |
14 | - // write 5 messages to the database | |
15 | - server.publish({ type: 'post', channel, text: 'hello world' }, cb) | |
16 | - }), | |
17 | - pull.collect((err, msgs) => { | |
18 | - // use channel plugin to find out how many times each channel has been mentioned | |
19 | - server.channel.all((err, data) => { | |
20 | - t.deepEqual({ myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }, data) | |
21 | - server.close() | |
22 | - }) | |
23 | - }) | |
24 | - ) | |
25 | -}) | |
26 | - | |
27 | -test('get a count of how many times all channels have been mentioned', t => { | |
28 | - Server.use(require('./channel')) | |
29 | - const server = Server() | |
30 | - | |
31 | - t.plan(1) | |
32 | - | |
33 | - pull( | |
34 | - pull.values(['#myco', '#ssb', '#economics', '#monkeys', '#pineapples', null, '#pineapples', undefined]), | |
35 | - pull.asyncMap(function (channel, cb) { | |
36 | - server.publish({ type: 'post', text: `hello world ${ channel }`, mentions: [{ link: channel }] }, cb) | |
37 | - }), | |
38 | - pull.collect((err, msgs) => { | |
39 | - // use channel plugin to find out how many times each channel has been mentioned | |
40 | - server.channel.all((err, data) => { | |
41 | - t.deepEqual({ myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }, data) | |
42 | - server.close() | |
43 | - }) | |
44 | - }) | |
45 | - ) | |
46 | -}) |
intermediate/channelV1.js | ||
---|---|---|
@@ -1,0 +1,37 @@ | ||
1 … | +const flumeView = require('flumeview-reduce') | |
2 … | + | |
3 … | +const NAME = 'channel' | |
4 … | +const VERSION = 1 | |
5 … | + | |
6 … | +module.exports = { | |
7 … | + name: NAME, | |
8 … | + version: VERSION, | |
9 … | + manifest: { | |
10 … | + all: 'async' | |
11 … | + }, | |
12 … | + init: function (server, config) { | |
13 … | + const view = server._flumeUse( | |
14 … | + NAME, | |
15 … | + flumeView(VERSION, reduce, map, null, initialState()) | |
16 … | + ) | |
17 … | + | |
18 … | + return { | |
19 … | + all: view.get | |
20 … | + } | |
21 … | + } | |
22 … | +} | |
23 … | + | |
24 … | +function map (msg) { | |
25 … | + var content = msg.value.content | |
26 … | + return content.channel | |
27 … | +} | |
28 … | + | |
29 … | +// if map returns null or undefined, reduce is skipped | |
30 … | +function reduce (accumulator, channel) { | |
31 … | + accumulator[channel] = (accumulator[channel] || 0) + 1 | |
32 … | + return accumulator | |
33 … | +} | |
34 … | + | |
35 … | +function initialState () { | |
36 … | + return {} | |
37 … | +} |
intermediate/channel.js | ||
---|---|---|
@@ -1,46 +1,0 @@ | ||
1 | -const flumeView = require('flumeview-reduce') | |
2 | - | |
3 | -const NAME = 'channel' | |
4 | -const VERSION = 1 | |
5 | - | |
6 | -module.exports = { | |
7 | - name: NAME, | |
8 | - version: VERSION, | |
9 | - manifest: { | |
10 | - all: 'async' | |
11 | - }, | |
12 | - init: function (server, config) { | |
13 | - const view = server._flumeUse( | |
14 | - NAME, | |
15 | - flumeView(VERSION, reduce, map, null, initialState()) | |
16 | - ) | |
17 | - | |
18 | - return { | |
19 | - all: view.get | |
20 | - } | |
21 | - } | |
22 | -} | |
23 | - | |
24 | -function map (msg) { | |
25 | - var content = msg.value.content | |
26 | - var channelAttribute = content.channel | |
27 | - | |
28 | - var mentions = content.mentions || [] | |
29 | - var mentionedChannel = mentions | |
30 | - .filter(mention => Boolean(mention.link)) | |
31 | - .filter(mention => mention.link.match(/^#/)) | |
32 | - .map(mention => mention.link.replace('#','')) | |
33 | - .shift() | |
34 | - | |
35 | - return channelAttribute || mentionedChannel | |
36 | -} | |
37 | - | |
38 | -// if map returns null or undefined, reduce is skipped | |
39 | -function reduce (accumulator, channel) { | |
40 | - accumulator[channel] = (accumulator[channel] || 0) + 1 | |
41 | - return accumulator | |
42 | -} | |
43 | - | |
44 | -function initialState () { | |
45 | - return {} | |
46 | -} |
intermediate/channelV1.test.js | ||
---|---|---|
@@ -1,0 +1,25 @@ | ||
1 … | +const test = require('tape') | |
2 … | +const Server = require('scuttle-testbot') | |
3 … | +const pull = require('pull-stream') | |
4 … | + | |
5 … | +test('get a count of the number of posts to a channel', t => { | |
6 … | + Server.use(require('./channelV1')) | |
7 … | + const server = Server() | |
8 … | + | |
9 … | + t.plan(1) | |
10 … | + | |
11 … | + pull( | |
12 … | + pull.values(['myco', 'ssb', 'economics', 'monkeys', 'pineapples', null, 'pineapples', undefined]), | |
13 … | + pull.asyncMap(function (channel, cb) { | |
14 … | + // write 5 messages to the database | |
15 … | + server.publish({ type: 'post', channel, text: 'hello world' }, cb) | |
16 … | + }), | |
17 … | + pull.collect((err, msgs) => { | |
18 … | + // use channel plugin to find out how many times each channel has been mentioned | |
19 … | + server.channel.all((err, data) => { | |
20 … | + t.deepEqual({ myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }, data) | |
21 … | + server.close() | |
22 … | + }) | |
23 … | + }) | |
24 … | + ) | |
25 … | +}) |
intermediate/channelV2.js | ||
---|---|---|
@@ -1,0 +1,46 @@ | ||
1 … | +const flumeView = require('flumeview-reduce') | |
2 … | + | |
3 … | +const NAME = 'channel' | |
4 … | +const VERSION = 2 | |
5 … | + | |
6 … | +module.exports = { | |
7 … | + name: NAME, | |
8 … | + version: VERSION, | |
9 … | + manifest: { | |
10 … | + all: 'async' | |
11 … | + }, | |
12 … | + init: function (server, config) { | |
13 … | + const view = server._flumeUse( | |
14 … | + NAME, | |
15 … | + flumeView(VERSION, reduce, map, null, initialState()) | |
16 … | + ) | |
17 … | + | |
18 … | + return { | |
19 … | + all: view.get | |
20 … | + } | |
21 … | + } | |
22 … | +} | |
23 … | + | |
24 … | +function map (msg) { | |
25 … | + var content = msg.value.content | |
26 … | + var channelAttribute = content.channel | |
27 … | + | |
28 … | + var mentions = content.mentions || [] | |
29 … | + var mentionedChannel = mentions | |
30 … | + .filter(mention => Boolean(mention.link)) | |
31 … | + .filter(mention => mention.link.match(/^#/)) | |
32 … | + .map(mention => mention.link.replace('#','')) | |
33 … | + .shift() | |
34 … | + | |
35 … | + return channelAttribute || mentionedChannel | |
36 … | +} | |
37 … | + | |
38 … | +// if map returns null or undefined, reduce is skipped | |
39 … | +function reduce (accumulator, channel) { | |
40 … | + accumulator[channel] = (accumulator[channel] || 0) + 1 | |
41 … | + return accumulator | |
42 … | +} | |
43 … | + | |
44 … | +function initialState () { | |
45 … | + return {} | |
46 … | +} |
intermediate/channelV2.test.js | ||
---|---|---|
@@ -1,0 +1,47 @@ | ||
1 … | +const test = require('tape') | |
2 … | +const Server = require('scuttle-testbot') | |
3 … | +const pull = require('pull-stream') | |
4 … | + | |
5 … | +// This test is the same except we're using the V2 view | |
6 … | +test('get a count of the number of posts to a channel', t => { | |
7 … | + Server.use(require('./channelV2')) | |
8 … | + const server = Server() | |
9 … | + | |
10 … | + t.plan(1) | |
11 … | + | |
12 … | + pull( | |
13 … | + pull.values(['myco', 'ssb', 'economics', 'monkeys', 'pineapples', null, 'pineapples', undefined]), | |
14 … | + pull.asyncMap(function (channel, cb) { | |
15 … | + // write 5 messages to the database | |
16 … | + server.publish({ type: 'post', channel, text: 'hello world' }, cb) | |
17 … | + }), | |
18 … | + pull.collect((err, msgs) => { | |
19 … | + // use channel plugin to find out how many times each channel has been mentioned | |
20 … | + server.channel.all((err, data) => { | |
21 … | + t.deepEqual({ myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }, data) | |
22 … | + server.close() | |
23 … | + }) | |
24 … | + }) | |
25 … | + ) | |
26 … | +}) | |
27 … | + | |
28 … | +test('get a count of how many times all channels have been mentioned', t => { | |
29 … | + Server.use(require('./channelV2')) | |
30 … | + const server = Server() | |
31 … | + | |
32 … | + t.plan(1) | |
33 … | + | |
34 … | + pull( | |
35 … | + pull.values(['#myco', '#ssb', '#economics', '#monkeys', '#pineapples', null, '#pineapples', undefined]), | |
36 … | + pull.asyncMap(function (channel, cb) { | |
37 … | + server.publish({ type: 'post', text: `hello world ${ channel }`, mentions: [{ link: channel }] }, cb) | |
38 … | + }), | |
39 … | + pull.collect((err, msgs) => { | |
40 … | + // use channel plugin to find out how many times each channel has been mentioned | |
41 … | + server.channel.all((err, data) => { | |
42 … | + t.deepEqual({ myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }, data) | |
43 … | + server.close() | |
44 … | + }) | |
45 … | + }) | |
46 … | + ) | |
47 … | +}) |
intermediate/channelV3.js | ||
---|---|---|
@@ -1,0 +1,61 @@ | ||
1 … | +const flumeView = require('flumeview-reduce') | |
2 … | + | |
3 … | +const NAME = 'channel' | |
4 … | +const VERSION = 3 | |
5 … | + | |
6 … | +module.exports = { | |
7 … | + name: NAME, | |
8 … | + version: VERSION, | |
9 … | + manifest: { | |
10 … | + all: 'async', | |
11 … | + count: 'async' | |
12 … | + }, | |
13 … | + init: function (server, config) { | |
14 … | + // Our original view | |
15 … | + const view = server._flumeUse( | |
16 … | + NAME, | |
17 … | + flumeView( | |
18 … | + VERSION, | |
19 … | + (soFar, channels) => { | |
20 … | + // reduce | |
21 … | + channels.forEach(channel => { | |
22 … | + soFar[channel] = (soFar[channel] || 0) + 1 | |
23 … | + }) | |
24 … | + return soFar | |
25 … | + }, | |
26 … | + (msg) => { | |
27 … | + // map | |
28 … | + var content = msg.value.content | |
29 … | + var mentions = content.mentions || [] | |
30 … | + // Put content.channel in list, even if null / undefined | |
31 … | + mentions.push({ link: content.channel }) | |
32 … | + channels = mentions | |
33 … | + // map to string | |
34 … | + .map(men => men.link) | |
35 … | + // remove falsey | |
36 … | + .filter(Boolean) | |
37 … | + // remove # | |
38 … | + .map(parseChannel) | |
39 … | + | |
40 … | + // remove duplicates and return array | |
41 … | + return Array.from(new Set(channels)) | |
42 … | + | |
43 … | + function parseChannel (channel) { | |
44 … | + return channel.replace('#', '') | |
45 … | + } | |
46 … | + }, | |
47 … | + null, | |
48 … | + {} | |
49 … | + ) | |
50 … | + ) | |
51 … | + | |
52 … | + return { | |
53 … | + all: view.get, | |
54 … | + count: (name, cb) => { | |
55 … | + view.get((err, data) => { | |
56 … | + cb(null, data[name]) | |
57 … | + }) | |
58 … | + } | |
59 … | + } | |
60 … | + } | |
61 … | +} |
intermediate/channelV3.test.js | |||
---|---|---|---|
@@ -1,0 +1,71 @@ | |||
1 … | +const test = require('tape') | ||
2 … | +const Server = require('scuttle-testbot') | ||
3 … | +const pull = require('pull-stream') | ||
4 … | + | ||
5 … | +// this test is the same except we're using the V3 view | ||
6 … | +test('get a count of the number of posts to a channel', t => { | ||
7 … | + Server.use(require('./channelV3')) | ||
8 … | + const server = Server() | ||
9 … | + | ||
10 … | + t.plan(1) | ||
11 … | + | ||
12 … | + pull( | ||
13 … | + pull.values(['myco', 'ssb', 'economics', 'monkeys', 'pineapples', null, 'pineapples', undefined]), | ||
14 … | + pull.asyncMap(function (channel, cb) { | ||
15 … | + // write 5 messages to the database | ||
16 … | + server.publish({ type: 'post', channel, text: 'hello world' }, cb) | ||
17 … | + }), | ||
18 … | + pull.collect((err, msgs) => { | ||
19 … | + // use channel plugin to find out how many times each channel has been mentioned | ||
20 … | + server.channel.all((err, data) => { | ||
21 … | + t.deepEqual(data, { myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }) | ||
22 … | + server.close() | ||
23 … | + }) | ||
24 … | + }) | ||
25 … | + ) | ||
26 … | +}) | ||
27 … | + | ||
28 … | +// this test is the same except we're using the V3 view | ||
29 … | +test('get a count of how many times all channels have been mentioned', t => { | ||
30 … | + Server.use(require('./channelV3')) | ||
31 … | + const server = Server() | ||
32 … | + | ||
33 … | + t.plan(1) | ||
34 … | + | ||
35 … | + pull( | ||
36 … | + pull.values(['#myco', '#ssb', '#economics', '#monkeys', '#pineapples', null, '#pineapples', undefined]), | ||
37 … | + pull.asyncMap((channel, cb) => { | ||
38 … | + server.publish({ type: 'post', text: `hello world ${ channel }`, mentions: [{ link: channel }] }, cb) | ||
39 … | + }), | ||
40 … | + pull.collect((err, msgs) => { | ||
41 … | + server.channel.all((err, data) => { | ||
42 … | + t.deepEqual(data, { myco: 1, ssb: 1, economics: 1, monkeys: 1, pineapples: 2 }) | ||
43 … | + server.close() | ||
44 … | + }) | ||
45 … | + }) | ||
46 … | + ) | ||
47 … | +}) | ||
48 … | + | ||
49 … | +test('get a count of a particular channel mentions and omit duplicate mentions', t => { | ||
50 … | + Server.use(require('./channelV3')) | ||
51 … | + const server = Server() | ||
52 … | + | ||
53 … | + t.plan(1) | ||
54 … | + | ||
55 … | + pull( | ||
56 … | + pull.values([ | ||
57 … | + { type: 'post', channel: '#myco', text: 'clathrus archerii', mentions: [{ link: '#myco' }, { link: '#mushrooms'}, { link: '#newzealand' }, { link: '#newzealand' }] }, | ||
58 … | + { type: 'post', channel: '#newzealand', text: 'tongariro', mentions: [{ link: '#newzealand' }, { link: '#hiking' }] }, | ||
59 … | + { type: 'post', channel: 'monkeys', text: 'always more monkeys OoooAHAHAHA', mentions: [{ link: '#apes' }] } | ||
60 … | + ]), | ||
61 … | + pull.asyncMap((post, cb) => { | ||
62 … | + server.publish(post, cb) | ||
63 … | + }), | ||
64 … | + pull.collect((err, msgs) => { | ||
65 … | + server.channel.count('newzealand', (err, count) => { | ||
66 … | + t.equal(count, 2) | ||
67 … | + server.close() | ||
68 … | + }) | ||
69 … | + }) | ||
70 … | + ) | ||
71 … | +}) |
Built with git-ssb-web