Files: 7ad6c4c93a2a569d77fe037b75fe50be89343492 / modules / feed / html / rollup.js
10674 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 unreadIds = new Set() |
65 | var newSinceRefresh = new Set() |
66 | var highlightItems = new Set() |
67 | |
68 | var container = h('Scroller', { |
69 | style: { overflow: 'auto' }, |
70 | hooks: [(element) => { |
71 | // don't activate until added to DOM |
72 | refresh() |
73 | |
74 | // deactivate when removed from DOM |
75 | return () => { |
76 | if (abortLastFeed) { |
77 | abortLastFeed() |
78 | abortLastFeed = null |
79 | } |
80 | } |
81 | }] |
82 | }, [ |
83 | h('div.wrapper', [ |
84 | h('section.prepend', prepend), |
85 | content, |
86 | when(loading, h('Loading -large')) |
87 | ]) |
88 | ]) |
89 | |
90 | onceTrue(waitFor, () => { |
91 | // display pending updates |
92 | pull( |
93 | updateStream || pull( |
94 | getStream({old: false}), |
95 | LookupRoot() |
96 | ), |
97 | pull.filter((msg) => { |
98 | // only render posts that have a root message |
99 | var root = msg.root || msg |
100 | return root && root.value && root.value.content && rootFilter(root) && bumpFilter(msg) && displayFilter(msg) |
101 | }), |
102 | pull.drain((msg) => { |
103 | if (msg.value.content.type === 'vote') return |
104 | if (api.app.sync.externalHandler(msg)) return |
105 | |
106 | // Only increment the 'new since' for items that we render on |
107 | // the feed as otherwise the 'show <n> updates message' will be |
108 | // shown on new messages that patchwork cannot render |
109 | if (canRenderMessage(msg) && (!msg.root || canRenderMessage(msg.root))) { |
110 | newSinceRefresh.add(msg.key) |
111 | unreadIds.add(msg.key) |
112 | } |
113 | |
114 | if (updates() === 0 && msg.value.author === yourId && container.scrollTop < 20) { |
115 | refresh() |
116 | } else { |
117 | updates.set(newSinceRefresh.size) |
118 | } |
119 | }) |
120 | ) |
121 | }) |
122 | |
123 | var result = MutantArray([ |
124 | when(updates, updateLoader), |
125 | container |
126 | ]) |
127 | |
128 | result.pendingUpdates = throttledUpdates |
129 | result.reload = refresh |
130 | |
131 | return result |
132 | |
133 | function canRenderMessage (msg) { |
134 | return api.message.html.canRender(msg) |
135 | } |
136 | |
137 | function refresh () { |
138 | onceTrue(waitFor, () => { |
139 | if (abortLastFeed) abortLastFeed() |
140 | updates.set(0) |
141 | content.set(h('section.content')) |
142 | |
143 | var abortable = Abortable() |
144 | abortLastFeed = abortable.abort |
145 | |
146 | highlightItems = newSinceRefresh |
147 | newSinceRefresh = new Set() |
148 | |
149 | var done = Value(false) |
150 | var stream = nextStepper(getStream, {reverse: true, limit: 50}) |
151 | var scroller = Scroller(container, content(), renderItem, { |
152 | onDone: () => done.set(true), |
153 | onItemVisible: (item) => { |
154 | if (Array.isArray(item.msgIds)) { |
155 | item.msgIds.forEach(id => { |
156 | unreadIds.delete(id) |
157 | }) |
158 | } |
159 | } |
160 | }) |
161 | |
162 | // track loading state |
163 | loading.set(computed([done, scroller.queue], (done, queue) => { |
164 | return !done && queue < 5 |
165 | })) |
166 | |
167 | pull( |
168 | stream, |
169 | abortable, |
170 | pull.filter(msg => msg && msg.value && msg.value.content), |
171 | prefiltered ? pull( |
172 | pull.filter(msg => !api.message.sync.isBlocked(msg)), |
173 | api.feed.pull.withReplies() |
174 | ) : pull( |
175 | pull.filter(bumpFilter), |
176 | api.feed.pull.rollup(rootFilter) |
177 | ), |
178 | scroller |
179 | ) |
180 | }) |
181 | } |
182 | |
183 | function renderItem (item, opts) { |
184 | var partial = opts && opts.partial |
185 | var meta = null |
186 | var previousId = item.key |
187 | |
188 | var groupedBumps = {} |
189 | var lastBumpType = null |
190 | |
191 | var rootBumpType = bumpFilter(item) |
192 | if (rootBumpTypes.includes(rootBumpType)) { |
193 | lastBumpType = rootBumpType |
194 | groupedBumps[lastBumpType] = [item] |
195 | } |
196 | |
197 | item.replies.forEach(msg => { |
198 | var value = bumpFilter(msg) |
199 | if (value) { |
200 | var type = typeof value === 'string' ? value : getType(msg) |
201 | ;(groupedBumps[type] = groupedBumps[type] || []).unshift(msg) |
202 | lastBumpType = type |
203 | } |
204 | }) |
205 | |
206 | var replies = item.replies.filter(isReply).sort(byAssertedTime) |
207 | var highlightedReplies = replies.filter(getPriority) |
208 | var replyElements = replies.filter(displayFilter).slice(-3).map((msg) => { |
209 | var result = api.message.html.render(msg, { |
210 | previousId, |
211 | compact: compactFilter(msg, item), |
212 | priority: getPriority(msg) |
213 | }) |
214 | previousId = msg.key |
215 | |
216 | return [ |
217 | // insert missing message marker (if can't be found) |
218 | api.message.html.missing(last(msg.value.content.branch), msg), |
219 | result |
220 | ] |
221 | }) |
222 | |
223 | var renderedMessage = api.message.html.render(item, { |
224 | compact: compactFilter(item), |
225 | includeForks: false, // this is a root message, so forks are already displayed as replies |
226 | priority: getPriority(item) |
227 | }) |
228 | |
229 | unreadIds.delete(item.key) |
230 | |
231 | if (!renderedMessage) return h('div') |
232 | if (lastBumpType) { |
233 | var bumps = lastBumpType === 'vote' |
234 | ? getLikeAuthors(groupedBumps[lastBumpType]) |
235 | : getAuthors(groupedBumps[lastBumpType]) |
236 | |
237 | var description = i18n(bumpMessages[lastBumpType] || 'added changes') |
238 | meta = h('div.meta', [ |
239 | many(bumps, api.profile.html.person, i18n), ' ', description |
240 | ]) |
241 | } |
242 | |
243 | // if there are new messages, view full thread goes to the top of those, otherwise to very first reply |
244 | var anchorReply = highlightedReplies.length >= 3 ? highlightedReplies[0] : replies[0] |
245 | |
246 | var result = h('FeedEvent -post', { |
247 | attributes: { |
248 | 'data-root-id': item.key |
249 | } |
250 | }, [ |
251 | meta, |
252 | renderedMessage, |
253 | when(replies.length > replyElements.length || partial, |
254 | h('a.full', {href: item.key, anchor: anchorReply && anchorReply.key}, [i18n('View full thread') + ' (', replies.length, ')']) |
255 | ), |
256 | h('div.replies', replyElements) |
257 | ]) |
258 | |
259 | result.msgIds = [item.key].concat(item.replies.map(x => x.key)) |
260 | |
261 | return result |
262 | } |
263 | |
264 | function getPriority (msg) { |
265 | if (highlightItems.has(msg.key)) { |
266 | return 2 |
267 | } else if (unreadIds.has(msg.key)) { |
268 | return 1 |
269 | } else { |
270 | return 0 |
271 | } |
272 | } |
273 | }) |
274 | |
275 | function LookupRoot () { |
276 | return paramap((msg, cb) => { |
277 | var rootId = api.message.sync.root(msg) |
278 | if (rootId) { |
279 | api.sbot.async.get(rootId, (_, value) => { |
280 | cb(null, extend(msg, { |
281 | root: {key: rootId, value} |
282 | })) |
283 | }) |
284 | } else { |
285 | cb(null, msg) |
286 | } |
287 | }) |
288 | } |
289 | } |
290 | |
291 | function plural (value, single, many) { |
292 | return computed(value, (value) => { |
293 | if (value === 1) { |
294 | return single |
295 | } else { |
296 | return many |
297 | } |
298 | }) |
299 | } |
300 | |
301 | function many (ids, fn, intl) { |
302 | ids = Array.from(ids) |
303 | var featuredIds = ids.slice(0, 4) |
304 | |
305 | if (ids.length) { |
306 | if (ids.length > 4) { |
307 | return [ |
308 | fn(featuredIds[0]), ', ', |
309 | fn(featuredIds[1]), ', ', |
310 | fn(featuredIds[2]), intl(' and '), |
311 | ids.length - 3, intl(' others') |
312 | ] |
313 | } else if (ids.length === 4) { |
314 | return [ |
315 | fn(featuredIds[0]), ', ', |
316 | fn(featuredIds[1]), ', ', |
317 | fn(featuredIds[2]), intl(' and '), |
318 | fn(featuredIds[3]) |
319 | ] |
320 | } else if (ids.length === 3) { |
321 | return [ |
322 | fn(featuredIds[0]), ', ', |
323 | fn(featuredIds[1]), intl(' and '), |
324 | fn(featuredIds[2]) |
325 | ] |
326 | } else if (ids.length === 2) { |
327 | return [ |
328 | fn(featuredIds[0]), intl(' and '), |
329 | fn(featuredIds[1]) |
330 | ] |
331 | } else { |
332 | return fn(featuredIds[0]) |
333 | } |
334 | } |
335 | } |
336 | |
337 | function getAuthors (items) { |
338 | return items.reduce((result, msg) => { |
339 | result.add(msg.value.author) |
340 | return result |
341 | }, new Set()) |
342 | } |
343 | |
344 | function getLikeAuthors (items) { |
345 | return items.reduce((result, msg) => { |
346 | if (msg.value.content.type === 'vote') { |
347 | if (msg.value.content && msg.value.content.vote && msg.value.content.vote.value === 1) { |
348 | result.add(msg.value.author) |
349 | } else { |
350 | result.delete(msg.value.author) |
351 | } |
352 | } |
353 | return result |
354 | }, new Set()) |
355 | } |
356 | |
357 | function isReply (msg) { |
358 | if (msg.value && msg.value.content) { |
359 | var type = msg.value.content.type |
360 | return type === 'post' || (type === 'about' && msg.value.content.attendee) |
361 | } |
362 | } |
363 | |
364 | function getType (msg) { |
365 | return msg && msg.value && msg.value.content && msg.value.content.type |
366 | } |
367 | |
368 | function returnTrue () { |
369 | return true |
370 | } |
371 | |
372 | function returnFalse () { |
373 | return false |
374 | } |
375 | |
376 | function byAssertedTime (a, b) { |
377 | return a.value.timestamp - b.value.timestamp |
378 | } |
379 | |
380 | function last (array) { |
381 | if (Array.isArray(array)) { |
382 | return array[array.length - 1] |
383 | } else { |
384 | return array |
385 | } |
386 | } |
387 |
Built with git-ssb-web