git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 8acd553f8fa30f4b8a87229695d165ff34d49efc

Files: 8acd553f8fa30f4b8a87229695d165ff34d49efc / modules / feed / html / rollup.js

9136 bytesRaw
1var nest = require('depnest')
2var {Value, Proxy, Array: MutantArray, h, computed, map, when, onceTrue, throttle} = 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.render': 'first',
25 'profile.html.person': 'first',
26 'message.html.link': 'first',
27 'message.sync.root': 'first',
28 'feed.pull.rollup': 'first',
29 'sbot.async.get': 'first',
30 'keys.sync.id': 'first',
31 'intl.sync.i18n': 'first',
32 'message.html.missing': 'first'
33})
34
35exports.gives = nest({
36 'feed.html.rollup': true
37})
38
39exports.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 [
184 // insert missing message marker (if can't be found)
185 api.message.html.missing(last(msg.value.content.branch), msg),
186 result
187 ]
188 })
189
190 var renderedMessage = api.message.html.render(item, {
191 inContext: true,
192 includeForks: false,
193 priority: highlightItems.has(item.key) ? 2 : 0
194 })
195
196 if (!renderedMessage) return h('div')
197 if (lastBumpType) {
198 var bumps = lastBumpType === 'vote'
199 ? getLikeAuthors(groupedBumps[lastBumpType])
200 : getAuthors(groupedBumps[lastBumpType])
201
202 var description = i18n(bumpMessages[lastBumpType] || 'added changes')
203 meta = h('div.meta', { title: names(bumps) }, [
204 many(bumps, api.profile.html.person, i18n), ' ', description
205 ])
206 }
207
208 return h('FeedEvent -post', {
209 attributes: {
210 'data-root-id': item.key
211 }
212 }, [
213 meta,
214 renderedMessage,
215 when(replyElements.length, [
216 when(replies.length > replyElements.length || partial,
217 h('a.full', {href: item.key, anchor: replies[0] && replies[0].key}, [i18n('View full thread') + ' (', replies.length, ')'])
218 ),
219 h('div.replies', replyElements)
220 ])
221 ])
222 }
223 })
224
225 function names (ids) {
226 var items = map(Array.from(ids), api.about.obs.name)
227 return computed([items], (names) => names.map((n) => `- ${n}`).join('\n'))
228 }
229
230 function LookupRoot () {
231 return paramap((msg, cb) => {
232 var rootId = api.message.sync.root(msg)
233 if (rootId) {
234 api.sbot.async.get(rootId, (_, value) => {
235 cb(null, extend(msg, {
236 root: {key: rootId, value}
237 }))
238 })
239 } else {
240 cb(null, msg)
241 }
242 })
243 }
244}
245
246function plural (value, single, many) {
247 return computed(value, (value) => {
248 if (value === 1) {
249 return single
250 } else {
251 return many
252 }
253 })
254}
255
256function many (ids, fn, intl) {
257 ids = Array.from(ids)
258 var featuredIds = ids.slice(0, 4)
259
260 if (ids.length) {
261 if (ids.length > 4) {
262 return [
263 fn(featuredIds[0]), ', ',
264 fn(featuredIds[1]), ', ',
265 fn(featuredIds[2]), intl(' and '),
266 ids.length - 3, intl(' others'),
267 ]
268 } else if (ids.length === 4) {
269 return [
270 fn(featuredIds[0]), ', ',
271 fn(featuredIds[1]), ', ',
272 fn(featuredIds[2]), intl(' and '),
273 fn(featuredIds[3])
274 ]
275 } else if (ids.length === 3) {
276 return [
277 fn(featuredIds[0]), ', ',
278 fn(featuredIds[1]), intl(' and '),
279 fn(featuredIds[2])
280 ]
281 } else if (ids.length === 2) {
282 return [
283 fn(featuredIds[0]), intl(' and '),
284 fn(featuredIds[1])
285 ]
286 } else {
287 return fn(featuredIds[0])
288 }
289 }
290}
291
292function getAuthors (items) {
293 return items.reduce((result, msg) => {
294 result.add(msg.value.author)
295 return result
296 }, new Set())
297}
298
299function getLikeAuthors (items) {
300 return items.reduce((result, msg) => {
301 if (msg.value.content.type === 'vote') {
302 if (msg.value.content && msg.value.content.vote && msg.value.content.vote.value === 1) {
303 result.add(msg.value.author)
304 } else {
305 result.delete(msg.value.author)
306 }
307 }
308 return result
309 }, new Set())
310}
311
312function isReply (msg) {
313 if (msg.value && msg.value.content) {
314 var type = msg.value.content.type
315 return type === 'post' || (type === 'about' && msg.value.content.attendee)
316 }
317}
318
319function getType (msg) {
320 return msg && msg.value && msg.value.content && msg.value.content.type
321}
322
323function returnTrue () {
324 return true
325}
326
327function byAssertedTime (a, b) {
328 return a.value.timestamp - b.value.timestamp
329}
330
331function last (array) {
332 if (Array.isArray(array)) {
333 return array[array.length - 1]
334 } else {
335 return array
336 }
337}
338

Built with git-ssb-web