Files: 5e26b5c3a8536e6be48e4c3324963f127b1e3fb0 / modules / feed / html / rollup.js
8821 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 | includeForks: false, |
189 | priority: highlightItems.has(item.key) ? 2 : 0 |
190 | }) |
191 | |
192 | if (!renderedMessage) return h('div') |
193 | if (lastBumpType) { |
194 | var bumps = lastBumpType === 'vote' |
195 | ? getLikeAuthors(groupedBumps[lastBumpType]) |
196 | : getAuthors(groupedBumps[lastBumpType]) |
197 | |
198 | var description = i18n(bumpMessages[lastBumpType] || 'added changes') |
199 | meta = h('div.meta', { title: names(bumps) }, [ |
200 | many(bumps, api.profile.html.person, i18n), ' ', description |
201 | ]) |
202 | } |
203 | |
204 | return h('FeedEvent -post', { |
205 | attributes: { |
206 | 'data-root-id': item.key |
207 | } |
208 | }, [ |
209 | meta, |
210 | renderedMessage, |
211 | when(replyElements.length, [ |
212 | when(replies.length > replyElements.length || partial, |
213 | h('a.full', {href: item.key, anchor: replies[0] && replies[0].key}, [i18n('View full thread') + ' (', replies.length, ')']) |
214 | ), |
215 | h('div.replies', replyElements) |
216 | ]) |
217 | ]) |
218 | } |
219 | }) |
220 | |
221 | function names (ids) { |
222 | var items = map(Array.from(ids), api.about.obs.name) |
223 | return computed([items], (names) => names.map((n) => `- ${n}`).join('\n')) |
224 | } |
225 | |
226 | function LookupRoot () { |
227 | return paramap((msg, cb) => { |
228 | var rootId = api.message.sync.root(msg) |
229 | if (rootId) { |
230 | api.sbot.async.get(rootId, (_, value) => { |
231 | cb(null, extend(msg, { |
232 | root: {key: rootId, value} |
233 | })) |
234 | }) |
235 | } else { |
236 | cb(null, msg) |
237 | } |
238 | }) |
239 | } |
240 | } |
241 | |
242 | function plural (value, single, many) { |
243 | return computed(value, (value) => { |
244 | if (value === 1) { |
245 | return single |
246 | } else { |
247 | return many |
248 | } |
249 | }) |
250 | } |
251 | |
252 | function many (ids, fn, intl) { |
253 | ids = Array.from(ids) |
254 | var featuredIds = ids.slice(0, 4) |
255 | |
256 | if (ids.length) { |
257 | if (ids.length > 4) { |
258 | return [ |
259 | fn(featuredIds[0]), ', ', |
260 | fn(featuredIds[1]), ', ', |
261 | fn(featuredIds[2]), intl(' and '), |
262 | ids.length - 3, intl(' others'), |
263 | ] |
264 | } else if (ids.length === 4) { |
265 | return [ |
266 | fn(featuredIds[0]), ', ', |
267 | fn(featuredIds[1]), ', ', |
268 | fn(featuredIds[2]), intl(' and '), |
269 | fn(featuredIds[3]) |
270 | ] |
271 | } else if (ids.length === 3) { |
272 | return [ |
273 | fn(featuredIds[0]), ', ', |
274 | fn(featuredIds[1]), intl(' and '), |
275 | fn(featuredIds[2]) |
276 | ] |
277 | } else if (ids.length === 2) { |
278 | return [ |
279 | fn(featuredIds[0]), intl(' and '), |
280 | fn(featuredIds[1]) |
281 | ] |
282 | } else { |
283 | return fn(featuredIds[0]) |
284 | } |
285 | } |
286 | } |
287 | |
288 | function getAuthors (items) { |
289 | return items.reduce((result, msg) => { |
290 | result.add(msg.value.author) |
291 | return result |
292 | }, new Set()) |
293 | } |
294 | |
295 | function getLikeAuthors (items) { |
296 | return items.reduce((result, msg) => { |
297 | if (msg.value.content.type === 'vote') { |
298 | if (msg.value.content && msg.value.content.vote && msg.value.content.vote.value === 1) { |
299 | result.add(msg.value.author) |
300 | } else { |
301 | result.delete(msg.value.author) |
302 | } |
303 | } |
304 | return result |
305 | }, new Set()) |
306 | } |
307 | |
308 | function isReply (msg) { |
309 | if (msg.value && msg.value.content) { |
310 | var type = msg.value.content.type |
311 | return type === 'post' || (type === 'about' && msg.value.content.attendee) |
312 | } |
313 | } |
314 | |
315 | function getType (msg) { |
316 | return msg && msg.value && msg.value.content && msg.value.content.type |
317 | } |
318 | |
319 | function returnTrue () { |
320 | return true |
321 | } |
322 | |
323 | function byAssertedTime (a, b) { |
324 | return a.value.timestamp - b.value.timestamp |
325 | } |
326 |
Built with git-ssb-web