git ssb

0+

mixmix / ssb-server-channel



Commit ef0e10c810ad27d20f17c2d63252757f3d0f4cb9

update index to include timestamps

mix irving committed on 2/15/2018, 2:52:29 AM
Parent: 8c7fdd33cde838895b5c0ab9073e759d1a631da8

Files changed

index.jschanged
package-lock.jsonchanged
index.jsView
@@ -1,74 +1,78 @@
11 const FlumeView = require('flumeview-reduce')
22 const get = require('lodash/get')
33 const set = require('lodash/set')
44
5-const FLUME_VIEW_VERSION = 1.0
5 +const FLUME_VIEW_VERSION = 1.1
66
77 module.exports = {
88 name: 'channel',
99 version: require('./package.json').version,
1010 manifest: {
1111 get: 'async',
1212 stream: 'source',
13- subscriptions: 'async'
13 + subscriptions: 'async',
14 + reduce: 'sync'
1415 },
1516 init: (server, config) => {
1617 // console.log('///// CHANNELS plugin loaded /////')
1718
1819 const view = server._flumeUse(
19- 'channels',
20 + 'channel',
2021 FlumeView(FLUME_VIEW_VERSION, reduce, map, null, initialState())
2122 )
2223
2324 return {
2425 get: view.get,
2526 subscriptions: view.get,
2627 stream: view.stream,
28 + reduce
2729 }
2830 }
2931 }
3032
3133 function initialState() {
3234 return {}
3335 }
3436
35-
3637 function map(msg) {
3738 if (get(msg, 'value.content.type') !== 'channel') return null
3839
3940 const author = msg.value.author
40- const channel = get(msg, 'value.content.channel')
41 + const timestamp = msg.value.timestamp
42 + const channel = get(msg, 'value.content.channel', '').replace(/^#/, '')
4143 const subscribed = get(msg, 'value.content.subscribed')
4244
4345 if (typeof channel === undefined || typeof subscribed === undefined) {
4446 console.log('Malformed channel subscription', msg)
4547 return null
4648 }
4749
4850 return {
51 + author,
52 + timestamp,
4953 channel,
50- author,
5154 subscribed
5255 }
5356 }
5457
55-function reduce(soFar, newSub) {
58 +function reduce (soFar, newSub) {
5659 process.stdout.write('c')
57- const { channel, author, subscribed } = newSub
60 + const { author, timestamp, channel, subscribed } = newSub
5861
59- let channelSubs = get(soFar, [channel], [])
62 + var channelSubs = get(soFar, [channel], [])
63 + var current = channelSubs.find(entry => entry[0] === author)
6064
61- channelSubs = new Set(channelSubs)
65 + // if current recorded statement was more recent than this 'newSub', ignore newSub
66 + if (current && current[1] > timestamp) return soFar
6267
63- if (subscribed) {
64- channelSubs.add(author)
65- } else {
66- channelSubs.delete(author)
67- }
68 + // remove all subs entries for this author
69 + channelSubs = channelSubs.filter(entry => entry[0] !== author)
6870
69- soFar[channel] = [...channelSubs]
71 + if (subscribed) channelSubs.push([author, Number(new Date())])
7072
73 + soFar[channel] = channelSubs
74 +
7175 return soFar
7276 }
7377
7478 // state:
package-lock.jsonView
@@ -1,6 +1,6 @@
11 {
2- "name": "ssb-server-channels",
2 + "name": "ssb-server-channel",
33 "version": "1.0.2",
44 "lockfileVersion": 1,
55 "requires": true,
66 "dependencies": {

Built with git-ssb-web