git ssb

10+

Matt McKegg / patchwork



Tree: 35b3aece890ce9483986f9581925792a5bcd647d

Files: 35b3aece890ce9483986f9581925792a5bcd647d / modules / feed / html / rollup.js

12034 bytesRaw
1var nest = require('depnest')
2var {Value, Proxy, Array: MutantArray, h, computed, when, onceTrue, throttle, resolve} = require('mutant')
3var pull = require('pull-stream')
4var Abortable = require('pull-abortable')
5var Scroller = require('../../../lib/scroller')
6var nextStepper = require('../../../lib/next-stepper')
7var extend = require('xtend')
8var paramap = require('pull-paramap')
9
10var 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
19var rootBumpTypes = ['mention', 'channel-mention']
20
21exports.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
43exports.gives = nest({
44 'feed.html.rollup': true
45})
46
47exports.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.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 channelLink (channelName) {
290 return
291 }
292
293 function getPriority (msg) {
294 if (highlightItems.has(msg.key)) {
295 return 2
296 } else if (unreadIds.has(msg.key)) {
297 return 1
298 } else {
299 return 0
300 }
301 }
302 })
303
304 function LookupRoot () {
305 return paramap((msg, cb) => {
306 var rootId = api.message.sync.root(msg)
307 if (rootId) {
308 api.sbot.async.get(rootId, (_, value) => {
309 if (value && typeof value.content === 'string') {
310 // unbox private message
311 value = api.message.sync.unbox(value)
312 }
313 cb(null, extend(msg, {
314 root: {key: rootId, value}
315 }))
316 })
317 } else {
318 cb(null, msg)
319 }
320 })
321 }
322
323 function byAssertedTime (a, b) {
324 return api.message.sync.timestamp(a) - api.message.sync.timestamp(b)
325 }
326}
327
328function plural (value, single, many) {
329 return computed(value, (value) => {
330 if (value === 1) {
331 return single
332 } else {
333 return many
334 }
335 })
336}
337
338function many (ids, fn, intl) {
339 ids = Array.from(ids)
340 var featuredIds = ids.slice(0, 4)
341
342 if (ids.length) {
343 if (ids.length > 4) {
344 return [
345 fn(featuredIds[0]), ', ',
346 fn(featuredIds[1]), ', ',
347 fn(featuredIds[2]), intl(' and '),
348 ids.length - 3, intl(' others')
349 ]
350 } else if (ids.length === 4) {
351 return [
352 fn(featuredIds[0]), ', ',
353 fn(featuredIds[1]), ', ',
354 fn(featuredIds[2]), intl(' and '),
355 fn(featuredIds[3])
356 ]
357 } else if (ids.length === 3) {
358 return [
359 fn(featuredIds[0]), ', ',
360 fn(featuredIds[1]), intl(' and '),
361 fn(featuredIds[2])
362 ]
363 } else if (ids.length === 2) {
364 return [
365 fn(featuredIds[0]), intl(' and '),
366 fn(featuredIds[1])
367 ]
368 } else {
369 return fn(featuredIds[0])
370 }
371 }
372}
373
374function getAuthors (items) {
375 return items.reduce((result, msg) => {
376 result.add(msg.value.author)
377 return result
378 }, new Set())
379}
380
381function getLikeAuthors (items) {
382 return items.reduce((result, msg) => {
383 if (msg.value.content.type === 'vote') {
384 if (msg.value.content && msg.value.content.vote && msg.value.content.vote.value === 1) {
385 result.add(msg.value.author)
386 } else {
387 result.delete(msg.value.author)
388 }
389 }
390 return result
391 }, new Set())
392}
393
394function isReply (msg) {
395 if (msg.value && msg.value.content) {
396 var type = msg.value.content.type
397 return type === 'post' || (type === 'about' && msg.value.content.attendee)
398 }
399}
400
401function getType (msg) {
402 return msg && msg.value && msg.value.content && msg.value.content.type
403}
404
405function returnTrue () {
406 return true
407}
408
409function returnFalse () {
410 return false
411}
412
413function last (array) {
414 if (Array.isArray(array)) {
415 return array[array.length - 1]
416 } else {
417 return array
418 }
419}
420

Built with git-ssb-web