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