git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Commit 79f97e8cf3ed986dcec454f112c3bc114d180bcf

fallback to user timestamp ordered feed after 48 hrs of scrollback, and for new users (while feeds stabilise)

Matt McKegg committed on 11/2/2016, 12:33:23 AM
Parent: 754c2c529f956adf3d576fbd5ed319dbc2291d59

Files changed

api/index.jschanged
modules/public.jschanged
api/index.jsView
@@ -137,7 +137,16 @@
137137 sbot.whoami(cb)
138138 },
139139 sbot_progress: function () {
140140 return sbot.replicate.changes()
141+ },
142+ sbot_feed: function (opts) {
143+ return pull(
144+ sbot.createFeedStream(opts),
145+ pull.through(function (e) {
146+ CACHE[e.key] = CACHE[e.key] || e.value
147+ infoCache.updateFrom(e)
148+ })
149+ )
141150 }
142151 }
143152 }
modules/public.jsView
@@ -1,13 +1,18 @@
11 var MutantMap = require('@mmckegg/mutant/map')
22 var computed = require('@mmckegg/mutant/computed')
33 var when = require('@mmckegg/mutant/when')
44 var send = require('@mmckegg/mutant/send')
5+var pull = require('pull-stream')
6+var extend = require('xtend')
57
68 var plugs = require('patchbay/plugs')
79 var h = require('../lib/h')
810 var message_compose = plugs.first(exports.message_compose = [])
911 var sbot_log = plugs.first(exports.sbot_log = [])
12+var sbot_feed = plugs.first(exports.sbot_feed = [])
13+var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
14+
1015 var feed_summary = plugs.first(exports.feed_summary = [])
1116 var obs_channels = plugs.first(exports.obs_channels = [])
1217 var obs_subscribed_channels = plugs.first(exports.obs_subscribed_channels = [])
1318 var get_id = plugs.first(exports.get_id = [])
@@ -26,8 +31,18 @@
2631 var loading = computed(subscribedChannels.sync, x => !x)
2732 var localPeers = obs_local()
2833 var following = obs_following(id)
2934
35+ var oldest = Date.now() - (2 * 24 * 60 * 60e3)
36+ getFirstMessage(id, (err, msg) => {
37+ if (msg) {
38+ // fall back to timestamp stream before this, give 48 hrs for feeds to stabilize
39+ if (msg.value.timestamp > oldest) {
40+ oldest = Date.now()
41+ }
42+ }
43+ })
44+
3045 var whoToFollow = computed([obs_following(id), obs_recently_updated_feeds(200)], (following, recent) => {
3146 return Array.from(recent).filter(x => x !== id && !following.has(x)).slice(0, 20)
3247 })
3348
@@ -87,16 +102,16 @@
87102 })
88103 ])
89104 ]),
90105 h('div.main', [
91- feed_summary(sbot_log, [
106+ feed_summary(getFeed, [
92107 message_compose({type: 'post'}, {placeholder: 'Write a public message'})
93108 ], {
94109 waitUntil: computed([
95110 following.sync,
96111 subscribedChannels.sync
97112 ], x => x.every(Boolean)),
98- windowSize: 200,
113+ windowSize: 500,
99114 filter: (item) => {
100115 return (
101116 id === item.author ||
102117 following().has(item.author) ||
@@ -121,8 +136,29 @@
121136 })
122137 ])
123138 ])
124139 }
140+
141+ // scoped
142+
143+ function getFeed (opts) {
144+ if (opts.lt && opts.lt < oldest) {
145+ opts = extend(opts, {lt: parseInt(opts.lt, 10)})
146+ console.log('using old feed', opts)
147+ return pull(
148+ sbot_feed(opts),
149+ pull.map((msg) => {
150+ if (msg.sync) {
151+ return msg
152+ } else {
153+ return {key: msg.key, value: msg.value, timestamp: msg.value.timestamp}
154+ }
155+ })
156+ )
157+ } else {
158+ return sbot_log(opts)
159+ }
160+ }
125161 }
126162
127163 function fromDay (msg, fromTime) {
128164 return (fromTime - msg.timestamp) < (24 * 60 * 60e3)
@@ -156,4 +192,8 @@
156192 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
157193 return a.every((value, i) => value === b[i])
158194 }
159195 }
196+
197+function getFirstMessage (feedId, cb) {
198+ sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb)
199+}

Built with git-ssb-web