modules/public.jsView |
---|
1 | 1 | var MutantMap = require('@mmckegg/mutant/map') |
2 | 2 | var computed = require('@mmckegg/mutant/computed') |
3 | 3 | var when = require('@mmckegg/mutant/when') |
4 | 4 | var send = require('@mmckegg/mutant/send') |
| 5 | +var pull = require('pull-stream') |
| 6 | +var extend = require('xtend') |
5 | 7 | |
6 | 8 | var plugs = require('patchbay/plugs') |
7 | 9 | var h = require('../lib/h') |
8 | 10 | var message_compose = plugs.first(exports.message_compose = []) |
9 | 11 | 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 | + |
10 | 15 | var feed_summary = plugs.first(exports.feed_summary = []) |
11 | 16 | var obs_channels = plugs.first(exports.obs_channels = []) |
12 | 17 | var obs_subscribed_channels = plugs.first(exports.obs_subscribed_channels = []) |
13 | 18 | var get_id = plugs.first(exports.get_id = []) |
26 | 31 | var loading = computed(subscribedChannels.sync, x => !x) |
27 | 32 | var localPeers = obs_local() |
28 | 33 | var following = obs_following(id) |
29 | 34 | |
| 35 | + var oldest = Date.now() - (2 * 24 * 60 * 60e3) |
| 36 | + getFirstMessage(id, (err, msg) => { |
| 37 | + if (msg) { |
| 38 | + |
| 39 | + if (msg.value.timestamp > oldest) { |
| 40 | + oldest = Date.now() |
| 41 | + } |
| 42 | + } |
| 43 | + }) |
| 44 | + |
30 | 45 | var whoToFollow = computed([obs_following(id), obs_recently_updated_feeds(200)], (following, recent) => { |
31 | 46 | return Array.from(recent).filter(x => x !== id && !following.has(x)).slice(0, 20) |
32 | 47 | }) |
33 | 48 | |
87 | 102 | }) |
88 | 103 | ]) |
89 | 104 | ]), |
90 | 105 | h('div.main', [ |
91 | | - feed_summary(sbot_log, [ |
| 106 | + feed_summary(getFeed, [ |
92 | 107 | message_compose({type: 'post'}, {placeholder: 'Write a public message'}) |
93 | 108 | ], { |
94 | 109 | waitUntil: computed([ |
95 | 110 | following.sync, |
96 | 111 | subscribedChannels.sync |
97 | 112 | ], x => x.every(Boolean)), |
98 | | - windowSize: 200, |
| 113 | + windowSize: 500, |
99 | 114 | filter: (item) => { |
100 | 115 | return ( |
101 | 116 | id === item.author || |
102 | 117 | following().has(item.author) || |
121 | 136 | }) |
122 | 137 | ]) |
123 | 138 | ]) |
124 | 139 | } |
| 140 | + |
| 141 | + |
| 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 | + } |
125 | 161 | } |
126 | 162 | |
127 | 163 | function fromDay (msg, fromTime) { |
128 | 164 | return (fromTime - msg.timestamp) < (24 * 60 * 60e3) |
156 | 192 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) { |
157 | 193 | return a.every((value, i) => value === b[i]) |
158 | 194 | } |
159 | 195 | } |
| 196 | + |
| 197 | +function getFirstMessage (feedId, cb) { |
| 198 | + sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb) |
| 199 | +} |