git ssb

10+

Matt McKegg / patchwork



Tree: 59d3d0d716da753cf2d23af1ad3fab75bfc9dc93

Files: 59d3d0d716da753cf2d23af1ad3fab75bfc9dc93 / modules / page / html / render / public.js

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

Built with git-ssb-web