git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Commit 58d09b546019da27320afef34de21bd46d0d03cf

fix including subscribed channels in patchwork main feed

fixes #579
Matt McKegg committed on 7/1/2017, 9:57:57 AM
Parent: ed3ae5da08e3aa78197343e41af3a66efbe9ad11

Files changed

lib/normalize-channel.jsadded
modules/page/html/render/public.jschanged
sbot/channels.jschanged
sbot/roots.jschanged
sbot/subscriptions.jschanged
lib/normalize-channel.jsView
@@ -1,0 +1,8 @@
1+module.exports = function (channel) {
2+ if (typeof channel === 'string') {
3+ channel = channel.replace(/\s/g, '')
4+ if (channel.length > 0 && channel.length < 30) {
5+ return channel
6+ }
7+ }
8+}
modules/page/html/render/public.jsView
@@ -68,9 +68,13 @@
6868 updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})),
6969 bumpFilter: function (msg) {
7070 if (msg.value && msg.value.content && typeof msg.value.content === 'object') {
7171 var author = msg.value.author
72- return id === author || following().has(author)
72+
73+ // TODO: should normalize this channel
74+ var channel = msg.value.content.channel
75+ var isSubscribed = channel ? subscribedChannels().has(channel) : false
76+ return isSubscribed || id === author || following().has(author)
7377 }
7478 },
7579 waitFor: computed([
7680 following.sync,
sbot/channels.jsView
@@ -1,5 +1,6 @@
11 var FlumeReduce = require('flumeview-reduce')
2+var normalizeChannel = require('../lib/normalize-channel')
23
34 module.exports = function (ssb, config) {
45 return ssb._flumeUse('patchwork-channels', FlumeReduce(1, reduce, map))
56 }
@@ -21,13 +22,13 @@
2122 return result
2223 }
2324
2425 function map (msg) {
25- if (msg.value.content && typeof msg.value.content.channel === 'string') {
26- var channel = msg.value.content.channel
27- if (channel.length > 0 && channel.length < 30) {
26+ if (msg.value.content) {
27+ var channel = normalizeChannel(msg.value.content.channel)
28+ if (channel) {
2829 return {
29- [channel.replace(/\s/g, '')]: {timestamp: msg.timestamp}
30+ [channel]: {timestamp: msg.timestamp}
3031 }
3132 }
3233 }
3334 }
sbot/roots.jsView
@@ -3,8 +3,9 @@
33 var FlumeViewLevel = require('flumeview-level')
44 var pullCat = require('pull-cat')
55 var HLRU = require('hashlru')
66 var extend = require('xtend')
7+var normalizeChannel = require('../lib/normalize-channel')
78
89 // HACK: pull it out of patchcore
910 var getRoot = require('patchcore/message/sync/root').create().message.sync.root
1011
@@ -194,10 +195,10 @@
194195 if (err) return cb(err)
195196 cb(null, function (ids, msg) {
196197 return (
197198 ids.includes(msg.value.author) ||
198- checkFollowing(contacts, ids, msg.value.author) ||
199- checkChannel(subscriptions, ids, msg.value.content.channel)
199+ checkChannel(subscriptions, ids, msg.value.content.channel) ||
200+ checkFollowing(contacts, ids, msg.value.author)
200201 )
201202 })
202203 })
203204 })
@@ -210,11 +211,11 @@
210211 return value && value[0]
211212 }
212213
213214 function checkChannel (lookup, ids, channel) {
214- channel = typeof channel === 'string' ? channel.replace(/\s/g, '') : null
215+ channel = normalizeChannel(channel)
215216 if (channel) {
216- var value = mostRecentValue(ids.map(id => lookup[`${id}:channel`]))
217+ var value = mostRecentValue(ids.map(id => lookup[`${id}:${channel}`]))
217218 return value && value[1]
218219 }
219220 }
220221
sbot/subscriptions.jsView
@@ -1,29 +1,32 @@
11 var FlumeReduce = require('flumeview-reduce')
2+var normalizeChannel = require('../lib/normalize-channel')
23
34 module.exports = function (ssb, config) {
4- return ssb._flumeUse('patchwork-subscriptions', FlumeReduce(1, reduce, map))
5+ return ssb._flumeUse('patchwork-subscriptions', FlumeReduce(3, reduce, map))
56 }
67
78 function reduce (result, item) {
8- if (!result) result = []
9- if (Array.isArray(item)) {
9+ if (!result) result = {}
10+ if (item) {
1011 for (var key in item) {
1112 if (!result[key] || result[key][0] < item[key][0]) {
12- result[key] = item
13+ result[key] = item[key]
1314 }
1415 }
1516 }
1617 return result
1718 }
1819
1920 function map (msg) {
2021 if (msg.value.content && msg.value.content.type === 'channel') {
21- if (typeof msg.value.content.channel === 'string' && typeof msg.value.content.subscribed === 'boolean') {
22- var channel = msg.value.content.channel.replace(/\s/g, '')
23- var key = `${msg.value.author}:${channel}`
24- return [{
25- [key]: [msg.timestamp, msg.value.content.subscribed]
26- }]
22+ if (typeof msg.value.content.subscribed === 'boolean') {
23+ var channel = normalizeChannel(msg.value.content.channel)
24+ if (channel) {
25+ var key = `${msg.value.author}:${channel}`
26+ return {
27+ [key]: [msg.timestamp, msg.value.content.subscribed]
28+ }
29+ }
2730 }
2831 }
2932 }

Built with git-ssb-web