Files: d274b4c0b5d5a0b27a731e62ba54882cf4354efd / modules / feed / html / rollup.js
9811 bytesRaw
1 | var nest = require('depnest') |
2 | var {Value, Proxy, Array: MutantArray, h, computed, when, onceTrue, throttle, resolve} = require('mutant') |
3 | var pull = require('pull-stream') |
4 | var Abortable = require('pull-abortable') |
5 | var Scroller = require('../../../lib/scroller') |
6 | var nextStepper = require('../../../lib/next-stepper') |
7 | var extend = require('xtend') |
8 | var paramap = require('pull-paramap') |
9 | |
10 | var bumpMessages = { |
11 | 'vote': 'liked this message', |
12 | 'post': 'replied to this message', |
13 | 'about': 'added changes', |
14 | 'mention': 'mentioned you', |
15 | 'channel-mention': 'mentioned this channel' |
16 | } |
17 | |
18 | // bump even for first message |
19 | var rootBumpTypes = ['mention', 'channel-mention'] |
20 | |
21 | exports.needs = nest({ |
22 | 'about.obs.name': 'first', |
23 | 'app.sync.externalHandler': 'first', |
24 | 'message.html.canRender': 'first', |
25 | 'message.html.render': 'first', |
26 | 'message.sync.isBlocked': 'first', |
27 | 'profile.html.person': 'first', |
28 | 'message.html.link': 'first', |
29 | 'message.sync.root': 'first', |
30 | 'feed.pull.rollup': 'first', |
31 | 'feed.pull.withReplies': 'first', |
32 | 'sbot.async.get': 'first', |
33 | 'keys.sync.id': 'first', |
34 | 'intl.sync.i18n': 'first', |
35 | 'message.html.missing': 'first' |
36 | }) |
37 | |
38 | exports.gives = nest({ |
39 | 'feed.html.rollup': true |
40 | }) |
41 | |
42 | exports.create = function (api) { |
43 | const i18n = api.intl.sync.i18n |
44 | return nest('feed.html.rollup', function (getStream, { |
45 | prepend, |
46 | rootFilter = returnTrue, |
47 | bumpFilter = returnTrue, |
48 | compactFilter = returnFalse, |
49 | prefiltered = false, |
50 | displayFilter = returnTrue, |
51 | updateStream, // override the stream used for realtime updates |
52 | waitFor = true |
53 | }) { |
54 | var updates = Value(0) |
55 | var yourId = api.keys.sync.id() |
56 | var throttledUpdates = throttle(updates, 200) |
57 | var updateLoader = h('a Notifier -loader', { href: '#', 'ev-click': refresh }, [ |
58 | 'Show ', h('strong', [throttledUpdates]), ' ', plural(throttledUpdates, i18n('update'), i18n('updates')) |
59 | ]) |
60 | |
61 | var abortLastFeed = null |
62 | var content = Value() |
63 | var loading = Proxy(true) |
64 | var newSinceRefresh = new Set() |
65 | var highlightItems = new Set() |
66 | |
67 | var container = h('Scroller', { |
68 | style: { overflow: 'auto' }, |
69 | hooks: [(element) => { |
70 | // don't activate until added to DOM |
71 | refresh() |
72 | |
73 | // deactivate when removed from DOM |
74 | return () => { |
75 | if (abortLastFeed) { |
76 | abortLastFeed() |
77 | abortLastFeed = null |
78 | } |
79 | } |
80 | }] |
81 | }, [ |
82 | h('div.wrapper', [ |
83 | h('section.prepend', prepend), |
84 | content, |
85 | when(loading, h('Loading -large')) |
86 | ]) |
87 | ]) |
88 | |
89 | onceTrue(waitFor, () => { |
90 | // display pending updates |
91 | pull( |
92 | updateStream || pull( |
93 | getStream({old: false}), |
94 | LookupRoot() |
95 | ), |
96 | pull.filter((msg) => { |
97 | // only render posts that have a root message |
98 | var root = msg.root || msg |
99 | return root && root.value && root.value.content && rootFilter(root) && bumpFilter(msg) && displayFilter(msg) |
100 | }), |
101 | pull.drain((msg) => { |
102 | if (msg.value.content.type === 'vote') return |
103 | if (api.app.sync.externalHandler(msg)) return |
104 | |
105 | // Only increment the 'new since' for items that we render on |
106 | // the feed as otherwise the 'show <n> updates message' will be |
107 | // shown on new messages that patchwork cannot render |
108 | if (canRenderMessage(msg) && (!msg.root || canRenderMessage(msg.root))) { |
109 | newSinceRefresh.add(msg.key) |
110 | } |
111 | |
112 | if (updates() === 0 && msg.value.author === yourId && container.scrollTop < 20) { |
113 | refresh() |
114 | } else { |
115 | updates.set(newSinceRefresh.size) |
116 | } |
117 | }) |
118 | ) |
119 | }) |
120 | |
121 | var result = MutantArray([ |
122 | when(updates, updateLoader), |
123 | container |
124 | ]) |
125 | |
126 | result.pendingUpdates = throttledUpdates |
127 | result.reload = refresh |
128 | |
129 | return result |
130 | |
131 | function canRenderMessage (msg) { |
132 | return api.message.html.canRender(msg) |
133 | } |
134 | |
135 | function refresh () { |
136 | onceTrue(waitFor, () => { |
137 | if (abortLastFeed) abortLastFeed() |
138 | updates.set(0) |
139 | content.set(h('section.content')) |
140 | |
141 | var abortable = Abortable() |
142 | abortLastFeed = abortable.abort |
143 | |
144 | highlightItems = newSinceRefresh |
145 | newSinceRefresh = new Set() |
146 | |
147 | var done = Value(false) |
148 | var stream = nextStepper(getStream, {reverse: true, limit: 50}) |
149 | var scroller = Scroller(container, content(), renderItem, () => done.set(true)) |
150 | |
151 | // track loading state |
152 | loading.set(computed([done, scroller.queue], (done, queue) => { |
153 | return !done && queue < 5 |
154 | })) |
155 | |
156 | pull( |
157 | stream, |
158 | abortable, |
159 | pull.filter(msg => msg && msg.value && msg.value.content), |
160 | prefiltered ? pull( |
161 | pull.filter(msg => !api.message.sync.isBlocked(msg)), |
162 | api.feed.pull.withReplies() |
163 | ) : pull( |
164 | pull.filter(bumpFilter), |
165 | api.feed.pull.rollup(rootFilter) |
166 | ), |
167 | scroller |
168 | ) |
169 | }) |
170 | } |
171 | |
172 | function renderItem (item, opts) { |
173 | var partial = opts && opts.partial |
174 | var meta = null |
175 | var previousId = item.key |
176 | |
177 | var groupedBumps = {} |
178 | var lastBumpType = null |
179 | |
180 | var rootBumpType = bumpFilter(item) |
181 | if (rootBumpTypes.includes(rootBumpType)) { |
182 | lastBumpType = rootBumpType |
183 | groupedBumps[lastBumpType] = [item] |
184 | } |
185 | |
186 | item.replies.forEach(msg => { |
187 | var value = bumpFilter(msg) |
188 | if (value) { |
189 | var type = typeof value === 'string' ? value : getType(msg) |
190 | ;(groupedBumps[type] = groupedBumps[type] || []).unshift(msg) |
191 | lastBumpType = type |
192 | } |
193 | }) |
194 | |
195 | var replies = item.replies.filter(isReply).sort(byAssertedTime) |
196 | var replyElements = replies.filter(displayFilter).slice(-3).map((msg) => { |
197 | var result = api.message.html.render(msg, { |
198 | previousId, |
199 | compact: compactFilter(msg), |
200 | priority: highlightItems.has(msg.key) ? 2 : 0 |
201 | }) |
202 | previousId = msg.key |
203 | return [ |
204 | // insert missing message marker (if can't be found) |
205 | api.message.html.missing(last(msg.value.content.branch), msg), |
206 | result |
207 | ] |
208 | }) |
209 | |
210 | var renderedMessage = api.message.html.render(item, { |
211 | compact: compactFilter(item), |
212 | includeForks: false, // this is a root message, so forks are already displayed as replies |
213 | priority: highlightItems.has(item.key) ? 2 : 0 |
214 | }) |
215 | |
216 | if (!renderedMessage) return h('div') |
217 | if (lastBumpType) { |
218 | var bumps = lastBumpType === 'vote' |
219 | ? getLikeAuthors(groupedBumps[lastBumpType]) |
220 | : getAuthors(groupedBumps[lastBumpType]) |
221 | |
222 | var description = i18n(bumpMessages[lastBumpType] || 'added changes') |
223 | meta = h('div.meta', [ |
224 | many(bumps, api.profile.html.person, i18n), ' ', description |
225 | ]) |
226 | } |
227 | |
228 | return h('FeedEvent -post', { |
229 | attributes: { |
230 | 'data-root-id': item.key |
231 | } |
232 | }, [ |
233 | meta, |
234 | renderedMessage, |
235 | when(replies.length > replyElements.length || partial, |
236 | h('a.full', {href: item.key, anchor: replies[0] && replies[0].key}, [i18n('View full thread') + ' (', replies.length, ')']) |
237 | ), |
238 | h('div.replies', replyElements) |
239 | ]) |
240 | } |
241 | }) |
242 | |
243 | function LookupRoot () { |
244 | return paramap((msg, cb) => { |
245 | var rootId = api.message.sync.root(msg) |
246 | if (rootId) { |
247 | api.sbot.async.get(rootId, (_, value) => { |
248 | cb(null, extend(msg, { |
249 | root: {key: rootId, value} |
250 | })) |
251 | }) |
252 | } else { |
253 | cb(null, msg) |
254 | } |
255 | }) |
256 | } |
257 | } |
258 | |
259 | function plural (value, single, many) { |
260 | return computed(value, (value) => { |
261 | if (value === 1) { |
262 | return single |
263 | } else { |
264 | return many |
265 | } |
266 | }) |
267 | } |
268 | |
269 | function many (ids, fn, intl) { |
270 | ids = Array.from(ids) |
271 | var featuredIds = ids.slice(0, 4) |
272 | |
273 | if (ids.length) { |
274 | if (ids.length > 4) { |
275 | return [ |
276 | fn(featuredIds[0]), ', ', |
277 | fn(featuredIds[1]), ', ', |
278 | fn(featuredIds[2]), intl(' and '), |
279 | ids.length - 3, intl(' others') |
280 | ] |
281 | } else if (ids.length === 4) { |
282 | return [ |
283 | fn(featuredIds[0]), ', ', |
284 | fn(featuredIds[1]), ', ', |
285 | fn(featuredIds[2]), intl(' and '), |
286 | fn(featuredIds[3]) |
287 | ] |
288 | } else if (ids.length === 3) { |
289 | return [ |
290 | fn(featuredIds[0]), ', ', |
291 | fn(featuredIds[1]), intl(' and '), |
292 | fn(featuredIds[2]) |
293 | ] |
294 | } else if (ids.length === 2) { |
295 | return [ |
296 | fn(featuredIds[0]), intl(' and '), |
297 | fn(featuredIds[1]) |
298 | ] |
299 | } else { |
300 | return fn(featuredIds[0]) |
301 | } |
302 | } |
303 | } |
304 | |
305 | function getAuthors (items) { |
306 | return items.reduce((result, msg) => { |
307 | result.add(msg.value.author) |
308 | return result |
309 | }, new Set()) |
310 | } |
311 | |
312 | function getLikeAuthors (items) { |
313 | return items.reduce((result, msg) => { |
314 | if (msg.value.content.type === 'vote') { |
315 | if (msg.value.content && msg.value.content.vote && msg.value.content.vote.value === 1) { |
316 | result.add(msg.value.author) |
317 | } else { |
318 | result.delete(msg.value.author) |
319 | } |
320 | } |
321 | return result |
322 | }, new Set()) |
323 | } |
324 | |
325 | function isReply (msg) { |
326 | if (msg.value && msg.value.content) { |
327 | var type = msg.value.content.type |
328 | return type === 'post' || (type === 'about' && msg.value.content.attendee) |
329 | } |
330 | } |
331 | |
332 | function getType (msg) { |
333 | return msg && msg.value && msg.value.content && msg.value.content.type |
334 | } |
335 | |
336 | function returnTrue () { |
337 | return true |
338 | } |
339 | |
340 | function returnFalse () { |
341 | return false |
342 | } |
343 | |
344 | function byAssertedTime (a, b) { |
345 | return a.value.timestamp - b.value.timestamp |
346 | } |
347 | |
348 | function last (array) { |
349 | if (Array.isArray(array)) { |
350 | return array[array.length - 1] |
351 | } else { |
352 | return array |
353 | } |
354 | } |
355 |
Built with git-ssb-web