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