git ssb

10+

Matt McKegg / patchwork



Tree: 13b1df716aa0e61f57f567bce7d3a64e14243907

Files: 13b1df716aa0e61f57f567bce7d3a64e14243907 / modules / page / html / render / public.js

9997 bytesRaw
1var nest = require('depnest')
2var extend = require('xtend')
3var pull = require('pull-stream')
4var { h, send, when, computed, map, onceTrue } = require('mutant')
5
6exports.needs = nest({
7 sbot: {
8 obs: {
9 connectedPeers: 'first',
10 localPeers: 'first',
11 connection: 'first'
12 }
13 },
14 'sbot.pull.stream': 'first',
15 'feed.pull.public': 'first',
16 'about.html.image': 'first',
17 'about.obs.name': 'first',
18 'invite.sheet': 'first',
19
20 'message.html.compose': 'first',
21 'message.async.publish': 'first',
22 'message.sync.root': 'first',
23 'progress.html.peer': 'first',
24
25 'feed.html.rollup': 'first',
26 'profile.obs.recentlyUpdated': 'first',
27 'contact.obs.following': 'first',
28 'contact.obs.blocking': 'first',
29 'channel.obs': {
30 subscribed: 'first',
31 recent: 'first'
32 },
33 'channel.sync.normalize': 'first',
34 'keys.sync.id': 'first',
35 'settings.obs.get': 'first',
36 'intl.sync.i18n': 'first'
37})
38
39exports.gives = nest({
40 'page.html.render': true
41})
42
43exports.create = function (api) {
44 const i18n = api.intl.sync.i18n
45 return nest('page.html.render', page)
46
47 function page (path) {
48 if (path !== '/public') return // "/" is a sigil for "page"
49
50 var id = api.keys.sync.id()
51 var following = api.contact.obs.following(id)
52 var blocking = api.contact.obs.blocking(id)
53 var subscribedChannels = api.channel.obs.subscribed(id)
54 var recentChannels = api.channel.obs.recent()
55 var loading = computed([subscribedChannels.sync, recentChannels.sync], (...args) => !args.every(Boolean))
56 var channels = computed(recentChannels, items => items.slice(0, 8), {comparer: arrayEq})
57 var connectedPeers = api.sbot.obs.connectedPeers()
58 var localPeers = api.sbot.obs.localPeers()
59 var connectedPubs = computed([connectedPeers, localPeers], (c, l) => c.filter(x => !l.includes(x)))
60
61 var prepend = [
62 api.message.html.compose({ meta: { type: 'post' }, placeholder: i18n('Write a public message') })
63 ]
64
65 var lastMessage = null
66
67 var getStream = (opts) => {
68 if (!opts.lt) {
69 // HACK: reset the isReplacementMessage check
70 lastMessage = null
71 }
72 if (opts.lt != null && !opts.lt.marker) {
73 // if an lt has been specified that is not a marker, assume stream is finished
74 return pull.empty()
75 } else {
76 return api.sbot.pull.stream(sbot => sbot.patchwork.roots(extend(opts, {
77 ids: [id],
78 onlySubscribedChannels: filters() && filters().onlySubscribed
79 })))
80 }
81 }
82
83 var filters = api.settings.obs.get('filters')
84 var feedView = api.feed.html.rollup(getStream, {
85 prepend,
86 prefiltered: true, // we've already filtered out the roots we don't want to include
87 updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})),
88 bumpFilter: function (msg) {
89 // this needs to match the logic in sbot/roots so that we display the
90 // correct bump explainations
91 if (msg.value && msg.value.content && typeof msg.value.content === 'object') {
92 var type = msg.value.content.type
93 if (type === 'vote') return false
94
95 var author = msg.value.author
96
97 if (id === author || following().includes(author)) {
98 return true
99 } else if (matchesSubscribedChannel(msg)) {
100 return 'matches-channel'
101 }
102 }
103 },
104 rootFilter: function (msg) {
105 // skip messages that are directly replaced by the previous message
106 // e.g. follow / unfollow in quick succession
107 // THIS IS A TOTAL HACK!!! SHOULD BE REPLACED WITH A PROPER ROLLUP!
108 var isOutdated = isReplacementMessage(msg, lastMessage)
109 if (checkFeedFilter(msg) && !isOutdated) {
110 lastMessage = msg
111 return true
112 }
113 },
114 compactFilter: function (msg, root) {
115 if (!root && api.message.sync.root(msg)) {
116 // msg has a root, but is being displayed as root (fork)
117 return true
118 }
119 },
120 waitFor: computed([
121 following.sync,
122 subscribedChannels.sync
123 ], (...x) => x.every(Boolean))
124 })
125
126 // call reload whenever filters changes (equivalent to the refresh from inside rollup)
127 filters(feedView.reload)
128
129 var result = h('div.SplitView', [
130 h('div.side', [
131 getSidebar()
132 ]),
133 h('div.main', feedView)
134 ])
135
136 result.pendingUpdates = feedView.pendingUpdates
137 result.reload = function () {
138 feedView.reload()
139 }
140
141 return result
142
143 function checkFeedFilter (root) {
144 const filterObj = filters()
145 if (filterObj) {
146 const rootType = getType(root)
147 if (
148 filterObj.following && rootType === 'contact' ||
149 filterObj.subscriptions && rootType === 'channel'
150 ) return false
151 }
152 return true
153 }
154
155 function matchesSubscribedChannel (msg) {
156 if (msg.filterResult) {
157 return msg.filterResult.matchesChannel || msg.filterResult.matchingTags.length
158 } else {
159 var channel = api.channel.sync.normalize(msg.value.content.channel)
160 var tagged = checkTag(msg.value.content.mentions)
161 var isSubscribed = channel ? subscribedChannels().has(channel) : false
162 return isSubscribed || tagged
163 }
164 }
165
166 function checkTag (mentions) {
167 if (Array.isArray(mentions)) {
168 return mentions.some((mention) => {
169 if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) {
170 var channel = api.channel.sync.normalize(mention.link.slice(1))
171 return channel ? subscribedChannels().has(channel) : false
172 }
173 })
174 }
175 }
176
177 function getSidebar () {
178 var whoToFollow = computed([api.profile.obs.recentlyUpdated(), following, blocking, localPeers], (recent, ...ignoreFeeds) => {
179 return recent.filter(x => x !== id && !ignoreFeeds.some(f => f.includes(x))).slice(0, 10)
180 })
181 return [
182 h('button -pub -full', {
183 'ev-click': api.invite.sheet
184 }, i18n('+ Join Pub')),
185 when(loading, [ h('Loading') ], [
186 when(computed(channels, x => x.length), h('h2', i18n('Active Channels'))),
187 h('div', {
188 classList: 'ChannelList',
189 hidden: loading
190 }, [
191 map(channels, (channel) => {
192 var subscribed = subscribedChannels.has(channel)
193 return h('a.channel', {
194 href: `#${channel}`,
195 classList: [
196 when(subscribed, '-subscribed')
197 ]
198 }, [
199 h('span.name', '#' + channel),
200 when(subscribed,
201 h('a.-unsubscribe', {
202 'ev-click': send(unsubscribe, channel)
203 }, i18n('Unsubscribe')),
204 h('a.-subscribe', {
205 'ev-click': send(subscribe, channel)
206 }, i18n('Subscribe'))
207 )
208 ])
209 }, {maxTime: 5}),
210 h('a.channel -more', {href: '/channels'}, i18n('More Channels...'))
211 ])
212 ]),
213
214 PeerList(localPeers, i18n('Local')),
215 PeerList(connectedPubs, i18n('Connected Pubs')),
216
217 when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))),
218 when(following.sync,
219 h('div', {
220 classList: 'ProfileList'
221 }, [
222 map(whoToFollow, (id) => {
223 return h('a.profile', {
224 href: id
225 }, [
226 h('div.avatar', [api.about.html.image(id)]),
227 h('div.main', [
228 h('div.name', [ api.about.obs.name(id) ])
229 ])
230 ])
231 })
232 ])
233 )
234 ]
235 }
236
237 function PeerList (ids, title) {
238 return [
239 when(computed(ids, x => x.length), h('h2', title)),
240 h('div', {
241 classList: 'ProfileList'
242 }, [
243 map(ids, (id) => {
244 var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id))
245 return h('a.profile', {
246 classList: [
247 when(connected, '-connected')
248 ],
249 href: id
250 }, [
251 h('div.avatar', [api.about.html.image(id)]),
252 h('div.main', [
253 h('div.name', [ api.about.obs.name(id) ])
254 ]),
255 h('div.progress', [
256 api.progress.html.peer(id)
257 ]),
258 h('div.controls', [
259 h('a.disconnect', {href: '#disconnect', 'ev-click': send(disconnect, id), title: i18n('Force Disconnect')}, ['x'])
260 ])
261 ])
262 })
263 ])
264 ]
265 }
266
267 function subscribe (id) {
268 api.message.async.publish({
269 type: 'channel',
270 channel: id,
271 subscribed: true
272 })
273 }
274
275 function unsubscribe (id) {
276 api.message.async.publish({
277 type: 'channel',
278 channel: id,
279 subscribed: false
280 })
281 }
282
283 function disconnect (id) {
284 onceTrue(api.sbot.obs.connection, (sbot) => {
285 sbot.patchwork.disconnect(id)
286 })
287 }
288 }
289}
290
291function getType (msg) {
292 return msg && msg.value && msg.value.content && msg.value.content.type
293}
294
295function hasChannel (msg) {
296 return getType(msg) !== 'channel' && msg && msg.value && msg.value.content && !!msg.value.content.channel
297}
298
299function arrayEq (a, b) {
300 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
301 return a.every((value, i) => value === b[i])
302 }
303}
304
305function isReplacementMessage (msgA, msgB) {
306 if (msgA && msgB && msgA.value.content && msgB.value.content && msgA.value.content.type === msgB.value.content.type) {
307 if (msgA.key === msgB.key) return false
308 var type = msgA.value.content.type
309 if (type === 'contact') {
310 return msgA.value.author === msgB.value.author && msgA.value.content.contact === msgB.value.content.contact
311 }
312 }
313}
314

Built with git-ssb-web