git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 141e43f7286b7d2209c0ce6e7f6d225c81a744e4

Files: 141e43f7286b7d2209c0ce6e7f6d225c81a744e4 / modules / feed-summary.js

5634 bytesRaw
1var Value = require('@mmckegg/mutant/value')
2var h = require('@mmckegg/mutant/html-element')
3var when = require('@mmckegg/mutant/when')
4var computed = require('@mmckegg/mutant/computed')
5var MutantArray = require('@mmckegg/mutant/array')
6var Abortable = require('pull-abortable')
7var Scroller = require('../lib/pull-scroll')
8var FeedSummary = require('../lib/feed-summary')
9var onceTrue = require('../lib/once-true')
10
11var m = require('../lib/h')
12
13var pull = require('pull-stream')
14
15var plugs = require('patchbay/plugs')
16var message_render = plugs.first(exports.message_render = [])
17var message_link = plugs.first(exports.message_link = [])
18var person = plugs.first(exports.person = [])
19var many_people = plugs.first(exports.many_people = [])
20var people_names = plugs.first(exports.people_names = [])
21var sbot_get = plugs.first(exports.sbot_get = [])
22
23exports.feed_summary = function (getStream, prefix, opts) {
24 var sync = Value(false)
25 var updates = Value(0)
26
27 var filter = opts && opts.filter
28 var bumpFilter = opts && opts.bumpFilter
29 var windowSize = opts && opts.windowSize
30 var waitFor = opts && opts.waitFor || true
31
32 var updateLoader = m('a Notifier -loader', {
33 href: '#',
34 'ev-click': refresh
35 }, [
36 'Show ',
37 h('strong', [updates]), ' ',
38 when(computed(updates, a => a === 1), 'update', 'updates')
39 ])
40
41 var content = h('div.column.scroller__content')
42
43 var scrollElement = h('div.column.scroller', {
44 style: {
45 'overflow': 'auto'
46 }
47 }, [
48 h('div.scroller__wrapper', [
49 prefix, content
50 ])
51 ])
52
53 setTimeout(refresh, 10)
54
55 onceTrue(waitFor, () => {
56 pull(
57 getStream({old: false}),
58 pull.drain((item) => {
59 var type = item && item.value && item.value.content.type
60 if (type && type !== 'vote') {
61 if (filter) {
62 var update = (item.value.content.type === 'post' && item.value.content.root) ? {
63 type: 'message',
64 messageId: item.value.content.root,
65 channel: item.value.content.channel
66 } : {
67 type: 'message',
68 author: item.value.author,
69 channel: item.value.content.channel,
70 messageId: item.key
71 }
72
73 ensureAuthor(update, (err, update) => {
74 if (!err) {
75 if (filter(update)) {
76 updates.set(updates() + 1)
77 }
78 }
79 })
80 } else {
81 updates.set(updates() + 1)
82 }
83 }
84 })
85 )
86 })
87
88 var abortLastFeed = null
89
90 return MutantArray([
91 when(updates, updateLoader),
92 when(sync, scrollElement, m('Loading -large'))
93 ])
94
95 // scoped
96
97 function refresh () {
98 if (abortLastFeed) {
99 abortLastFeed()
100 }
101 updates.set(0)
102 sync.set(false)
103 content.innerHTML = ''
104
105 var abortable = Abortable()
106 abortLastFeed = abortable.abort
107
108 pull(
109 FeedSummary(getStream, {windowSize, bumpFilter}, () => {
110 sync.set(true)
111 }),
112 pull.asyncMap(ensureAuthor),
113 pull.filter((item) => {
114 if (filter) {
115 return filter(item)
116 } else {
117 return true
118 }
119 }),
120 abortable,
121 Scroller(scrollElement, content, renderItem, false, false)
122 )
123 }
124}
125
126function ensureAuthor (item, cb) {
127 if (item.type === 'message' && !item.message) {
128 sbot_get(item.messageId, (_, value) => {
129 if (value) {
130 item.author = value.author
131 }
132 cb(null, item)
133 })
134 } else {
135 cb(null, item)
136 }
137}
138
139function renderItem (item) {
140 if (item.type === 'message') {
141 var meta = null
142 var previousId = item.messageId
143 var replies = item.replies.slice(-4).map((msg) => {
144 var result = message_render(msg, {inContext: true, inSummary: true, previousId})
145 previousId = msg.key
146 return result
147 })
148 var renderedMessage = item.message ? message_render(item.message, {inContext: true}) : null
149 if (renderedMessage) {
150 if (item.lastUpdateType === 'reply' && item.repliesFrom.size) {
151 meta = m('div.meta', {
152 title: people_names(item.repliesFrom)
153 }, [
154 many_people(item.repliesFrom), ' replied'
155 ])
156 } else if (item.lastUpdateType === 'dig' && item.digs.size) {
157 meta = m('div.meta', {
158 title: people_names(item.digs)
159 }, [
160 many_people(item.digs), ' dug this message'
161 ])
162 }
163
164 return m('FeedEvent', [
165 meta,
166 renderedMessage,
167 when(replies.length, [
168 when(item.replies.length > replies.length,
169 m('a.full', {href: `#${item.messageId}`}, ['View full thread'])
170 ),
171 m('div.replies', replies)
172 ])
173 ])
174 } else {
175 if (item.lastUpdateType === 'reply' && item.repliesFrom.size) {
176 meta = m('div.meta', {
177 title: people_names(item.repliesFrom)
178 }, [
179 many_people(item.repliesFrom), ' replied to ', message_link(item.messageId)
180 ])
181 } else if (item.lastUpdateType === 'dig' && item.digs.size) {
182 meta = m('div.meta', {
183 title: people_names(item.digs)
184 }, [
185 many_people(item.digs), ' dug ', message_link(item.messageId)
186 ])
187 }
188
189 if (meta || replies.length) {
190 return m('FeedEvent', [
191 meta, m('div.replies', replies)
192 ])
193 }
194 }
195 } else if (item.type === 'follow') {
196 return m('FeedEvent -follow', [
197 m('div.meta', {
198 title: people_names(item.contacts)
199 }, [
200 person(item.id), ' followed ', many_people(item.contacts)
201 ])
202 ])
203 }
204
205 return h('div')
206}
207

Built with git-ssb-web