Files: a97b2ac07374b443c84bbd1f8d2c42f40456a4bb / modules / feed / html / rollup.js
8792 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).filter(displayFilter).sort(byAssertedTime) |
175 | var replyElements = replies.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: replies[0] && replies[0].key}, [i18n('View full thread') + ' (', replies.length, ')']) |
213 | ), |
214 | h('div.replies', replyElements) |
215 | ]) |
216 | ]) |
217 | } |
218 | }) |
219 | |
220 | function names (ids) { |
221 | var items = map(Array.from(ids), api.about.obs.name) |
222 | return computed([items], (names) => names.map((n) => `- ${n}`).join('\n')) |
223 | } |
224 | |
225 | function LookupRoot () { |
226 | return paramap((msg, cb) => { |
227 | var rootId = api.message.sync.root(msg) |
228 | if (rootId) { |
229 | api.sbot.async.get(rootId, (_, value) => { |
230 | cb(null, extend(msg, { |
231 | root: {key: rootId, value} |
232 | })) |
233 | }) |
234 | } else { |
235 | cb(null, msg) |
236 | } |
237 | }) |
238 | } |
239 | } |
240 | |
241 | function plural (value, single, many) { |
242 | return computed(value, (value) => { |
243 | if (value === 1) { |
244 | return single |
245 | } else { |
246 | return many |
247 | } |
248 | }) |
249 | } |
250 | |
251 | function many (ids, fn, intl) { |
252 | ids = Array.from(ids) |
253 | var featuredIds = ids.slice(0, 4) |
254 | |
255 | if (ids.length) { |
256 | if (ids.length > 4) { |
257 | return [ |
258 | fn(featuredIds[0]), ', ', |
259 | fn(featuredIds[1]), ', ', |
260 | fn(featuredIds[2]), intl(' and '), |
261 | ids.length - 3, intl(' others'), |
262 | ] |
263 | } else if (ids.length === 4) { |
264 | return [ |
265 | fn(featuredIds[0]), ', ', |
266 | fn(featuredIds[1]), ', ', |
267 | fn(featuredIds[2]), intl(' and '), |
268 | fn(featuredIds[3]) |
269 | ] |
270 | } else if (ids.length === 3) { |
271 | return [ |
272 | fn(featuredIds[0]), ', ', |
273 | fn(featuredIds[1]), intl(' and '), |
274 | fn(featuredIds[2]) |
275 | ] |
276 | } else if (ids.length === 2) { |
277 | return [ |
278 | fn(featuredIds[0]), intl(' and '), |
279 | fn(featuredIds[1]) |
280 | ] |
281 | } else { |
282 | return fn(featuredIds[0]) |
283 | } |
284 | } |
285 | } |
286 | |
287 | function getAuthors (items) { |
288 | return items.reduce((result, msg) => { |
289 | result.add(msg.value.author) |
290 | return result |
291 | }, new Set()) |
292 | } |
293 | |
294 | function getLikeAuthors (items) { |
295 | return items.reduce((result, msg) => { |
296 | if (msg.value.content.type === 'vote') { |
297 | if (msg.value.content && msg.value.content.vote && msg.value.content.vote.value === 1) { |
298 | result.add(msg.value.author) |
299 | } else { |
300 | result.delete(msg.value.author) |
301 | } |
302 | } |
303 | return result |
304 | }, new Set()) |
305 | } |
306 | |
307 | function isReply (msg) { |
308 | if (msg.value && msg.value.content) { |
309 | var type = msg.value.content.type |
310 | return type === 'post' || (type === 'about' && msg.value.content.attendee) |
311 | } |
312 | } |
313 | |
314 | function getType (msg) { |
315 | return msg && msg.value && msg.value.content && msg.value.content.type |
316 | } |
317 | |
318 | function returnTrue () { |
319 | return true |
320 | } |
321 | |
322 | function byAssertedTime (a, b) { |
323 | return a.value.timestamp - b.value.timestamp |
324 | } |
325 |
Built with git-ssb-web