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