git ssb

10+

Matt McKegg / patchwork



Tree: e1c938e079d739f9e8511262531b1d04e30effab

Files: e1c938e079d739f9e8511262531b1d04e30effab / modules / page / html / render / public.js

11018 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 // skip messages that are directly replaced by the previous message
111 // e.g. follow / unfollow in quick succession
112 // THIS IS A TOTAL HACK!!! SHOULD BE REPLACED WITH A PROPER ROLLUP!
113 var isOutdated = isReplacementMessage(msg, lastMessage)
114 if (checkFeedFilter(msg) && !isOutdated) {
115 lastMessage = msg
116 return true
117 }
118 },
119 compactFilter: function (msg, root) {
120 if (!root && api.message.sync.root(msg)) {
121 // msg has a root, but is being displayed as root (fork)
122 return true
123 }
124 },
125 waitFor: computed([
126 following.sync,
127 subscribedChannels.sync
128 ], (...x) => x.every(Boolean))
129 })
130
131 // call reload whenever filters changes (equivalent to the refresh from inside rollup)
132 filters(feedView.reload)
133
134 var result = h('div.SplitView', [
135 h('div.side', [
136 getSidebar()
137 ]),
138 h('div.main', feedView)
139 ])
140
141 result.pendingUpdates = feedView.pendingUpdates
142 result.reload = function () {
143 feedView.reload()
144 }
145
146 return result
147
148 function checkFeedFilter (root) {
149 const filterObj = filters()
150 if (filterObj) {
151 const rootType = getType(root)
152 if (
153 (filterObj.following && rootType === 'contact') ||
154 (filterObj.subscriptions && rootType === 'channel') ||
155 (filterObj.onlySubscribed && rootType === 'post' && !matchesSubscribedChannel(root))
156 ) {
157 return false
158 }
159 }
160 return true
161 }
162
163 function matchesSubscribedChannel (msg) {
164 if (msg.filterResult) {
165 return msg.filterResult.matchesChannel || msg.filterResult.matchingTags.length
166 } else {
167 var channel = api.channel.sync.normalize(msg.value.content.channel)
168 var tagged = checkTag(msg.value.content.mentions)
169 var isSubscribed = channel ? subscribedChannels().has(channel) : false
170 return isSubscribed || tagged
171 }
172 }
173
174 function checkTag (mentions) {
175 if (Array.isArray(mentions)) {
176 return mentions.some((mention) => {
177 if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) {
178 var channel = api.channel.sync.normalize(mention.link.slice(1))
179 return channel ? subscribedChannels().has(channel) : false
180 }
181 })
182 }
183 }
184
185 function getSidebar () {
186 var whoToFollow = computed([api.profile.obs.recentlyUpdated(), following, blocking, localPeers], (recent, ...ignoreFeeds) => {
187 return recent.filter(x => x !== id && !ignoreFeeds.some(f => f.includes(x))).slice(0, 10)
188 })
189 return [
190 h('button -pub -full', {
191 'ev-click': api.invite.sheet
192 }, i18n('+ Join Pub')),
193 when(loading, [ h('Loading') ], [
194 when(computed(channels, x => x.length), h('h2', i18n('Active Channels'))),
195 h('div', {
196 classList: 'ChannelList',
197 hidden: loading
198 }, [
199 map(channels, (channel) => {
200 var subscribed = subscribedChannels.has(channel)
201 return h('a.channel', {
202 href: `#${channel}`,
203 classList: [
204 when(subscribed, '-subscribed')
205 ]
206 }, [
207 h('span.name', '#' + channel)
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: '#', 'ev-click': send(disconnect, id), title: i18n('Force Disconnect')}, ['x'])
260 ])
261 ])
262 })
263 ])
264 ]
265 }
266
267 function noVisibleNewPostsWarning () {
268 const explanation = i18n('You may not be able to see new content until you follow some users or pubs.')
269
270 const shownWhen = computed([loading, contact.isNotFollowingAnybody],
271 (isLoading, isNotFollowingAnybody) => !isLoading && isNotFollowingAnybody
272 )
273
274 return api.feed.html.followWarning(shownWhen, explanation)
275 }
276
277 function noFollowersWarning () {
278 const explanation = i18n(
279 '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.'
280 )
281
282 // We only show this if the user has followed someone as the first warning ('You are not following anyone')
283 // should be sufficient to get the user to join a pub. However, pubs have been buggy and not followed back on occassion.
284 // Additionally, someone onboarded on a local network might follow someone on the network, but not be followed back by
285 // them, so we begin to show this warning if the user has followed someone, but has no followers.
286 const shownWhen = computed([loading, contact.hasNoFollowers, contact.isNotFollowingAnybody],
287 (isLoading, hasNoFollowers, isNotFollowingAnybody) =>
288 !isLoading && (hasNoFollowers && !isNotFollowingAnybody)
289 )
290
291 return api.feed.html.followerWarning(shownWhen, explanation)
292 }
293
294 function disconnect (id) {
295 onceTrue(api.sbot.obs.connection, (sbot) => {
296 sbot.patchwork.disconnect(id)
297 })
298 }
299 }
300}
301
302function getType (msg) {
303 return msg && msg.value && msg.value.content && msg.value.content.type
304}
305
306function arrayEq (a, b) {
307 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
308 return a.every((value, i) => value === b[i])
309 }
310}
311
312function isReplacementMessage (msgA, msgB) {
313 if (msgA && msgB && msgA.value.content && msgB.value.content && msgA.value.content.type === msgB.value.content.type) {
314 if (msgA.key === msgB.key) return false
315 var type = msgA.value.content.type
316 if (type === 'contact') {
317 return msgA.value.author === msgB.value.author && msgA.value.content.contact === msgB.value.content.contact
318 }
319 }
320}
321

Built with git-ssb-web