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