git ssb

10+

Matt McKegg / patchwork



Tree: 2b4bd6561cd0949af862be8fed0c03013cd2d9d4

Files: 2b4bd6561cd0949af862be8fed0c03013cd2d9d4 / modules / feed / html / rollup.js

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

Built with git-ssb-web