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