Files: 62a7ed9e9f40e4074cdf4a0c74cb32327bb5abdd / modules / page / html / render / public.js
9603 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 | '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 | |
39 | exports.gives = nest({ |
40 | 'page.html.render': true |
41 | }) |
42 | |
43 | exports.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 | return matchesSubscribedChannel(msg) || id === author || following().includes(author) |
97 | } |
98 | }, |
99 | rootFilter: function (msg) { |
100 | // skip messages that are directly replaced by the previous message |
101 | // e.g. follow / unfollow in quick succession |
102 | // THIS IS A TOTAL HACK!!! SHOULD BE REPLACED WITH A PROPER ROLLUP! |
103 | var isOutdated = isReplacementMessage(msg, lastMessage) |
104 | if (checkFeedFilter(msg) && !isOutdated) { |
105 | lastMessage = msg |
106 | return true |
107 | } |
108 | }, |
109 | compactFilter: function (msg, root) { |
110 | if (!root && api.message.sync.root(msg)) { |
111 | // msg has a root, but is being displayed as root (fork) |
112 | return true |
113 | } |
114 | }, |
115 | waitFor: computed([ |
116 | following.sync, |
117 | subscribedChannels.sync |
118 | ], (...x) => x.every(Boolean)) |
119 | }) |
120 | |
121 | // call reload whenever filters changes (equivalent to the refresh from inside rollup) |
122 | filters(feedView.reload) |
123 | |
124 | var result = h('div.SplitView', [ |
125 | h('div.side', [ |
126 | getSidebar() |
127 | ]), |
128 | h('div.main', feedView) |
129 | ]) |
130 | |
131 | result.pendingUpdates = feedView.pendingUpdates |
132 | result.reload = function () { |
133 | feedView.reload() |
134 | } |
135 | |
136 | return result |
137 | |
138 | function checkFeedFilter (root) { |
139 | if (filters()) { |
140 | if (filters().following && getType(root) === 'contact') return false |
141 | } |
142 | return true |
143 | } |
144 | |
145 | function matchesSubscribedChannel (msg) { |
146 | var channel = api.channel.sync.normalize(msg.value.content.channel) |
147 | var tagged = checkTag(msg.value.content.mentions) |
148 | var isSubscribed = channel ? subscribedChannels().has(channel) : false |
149 | return isSubscribed || tagged |
150 | } |
151 | |
152 | function checkTag (mentions) { |
153 | if (Array.isArray(mentions)) { |
154 | return mentions.some((mention) => { |
155 | if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) { |
156 | var channel = api.channel.sync.normalize(mention.link.slice(1)) |
157 | return channel ? subscribedChannels().has(channel) : false |
158 | } |
159 | }) |
160 | } |
161 | } |
162 | |
163 | function getSidebar () { |
164 | var whoToFollow = computed([api.profile.obs.recentlyUpdated(), following, blocking, localPeers], (recent, ...ignoreFeeds) => { |
165 | return recent.filter(x => x !== id && !ignoreFeeds.some(f => f.includes(x))).slice(0, 10) |
166 | }) |
167 | return [ |
168 | h('button -pub -full', { |
169 | 'ev-click': api.invite.sheet |
170 | }, i18n('+ Join Pub')), |
171 | when(loading, [ h('Loading') ], [ |
172 | when(computed(channels, x => x.length), h('h2', i18n('Active Channels'))), |
173 | h('div', { |
174 | classList: 'ChannelList', |
175 | hidden: loading |
176 | }, [ |
177 | map(channels, (channel) => { |
178 | var subscribed = subscribedChannels.has(channel) |
179 | return h('a.channel', { |
180 | href: `#${channel}`, |
181 | classList: [ |
182 | when(subscribed, '-subscribed') |
183 | ] |
184 | }, [ |
185 | h('span.name', '#' + channel), |
186 | when(subscribed, |
187 | h('a.-unsubscribe', { |
188 | 'ev-click': send(unsubscribe, channel) |
189 | }, i18n('Unsubscribe')), |
190 | h('a.-subscribe', { |
191 | 'ev-click': send(subscribe, channel) |
192 | }, i18n('Subscribe')) |
193 | ) |
194 | ]) |
195 | }, {maxTime: 5}), |
196 | h('a.channel -more', {href: '/channels'}, i18n('More Channels...')) |
197 | ]) |
198 | ]), |
199 | |
200 | PeerList(localPeers, i18n('Local')), |
201 | PeerList(connectedPubs, i18n('Connected Pubs')), |
202 | |
203 | when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))), |
204 | when(following.sync, |
205 | h('div', { |
206 | classList: 'ProfileList' |
207 | }, [ |
208 | map(whoToFollow, (id) => { |
209 | return h('a.profile', { |
210 | href: id |
211 | }, [ |
212 | h('div.avatar', [api.about.html.image(id)]), |
213 | h('div.main', [ |
214 | h('div.name', [ api.about.obs.name(id) ]) |
215 | ]) |
216 | ]) |
217 | }) |
218 | ]) |
219 | ) |
220 | ] |
221 | } |
222 | |
223 | function PeerList (ids, title) { |
224 | return [ |
225 | when(computed(ids, x => x.length), h('h2', title)), |
226 | h('div', { |
227 | classList: 'ProfileList' |
228 | }, [ |
229 | map(ids, (id) => { |
230 | var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id)) |
231 | return h('a.profile', { |
232 | classList: [ |
233 | when(connected, '-connected') |
234 | ], |
235 | href: id |
236 | }, [ |
237 | h('div.avatar', [api.about.html.image(id)]), |
238 | h('div.main', [ |
239 | h('div.name', [ api.about.obs.name(id) ]) |
240 | ]), |
241 | h('div.progress', [ |
242 | api.progress.html.peer(id) |
243 | ]), |
244 | h('div.controls', [ |
245 | h('a.disconnect', {href: '#disconnect', 'ev-click': send(disconnect, id), title: i18n('Force Disconnect')}, ['x']) |
246 | ]) |
247 | ]) |
248 | }) |
249 | ]) |
250 | ] |
251 | } |
252 | |
253 | function subscribe (id) { |
254 | api.message.async.publish({ |
255 | type: 'channel', |
256 | channel: id, |
257 | subscribed: true |
258 | }) |
259 | } |
260 | |
261 | function unsubscribe (id) { |
262 | api.message.async.publish({ |
263 | type: 'channel', |
264 | channel: id, |
265 | subscribed: false |
266 | }) |
267 | } |
268 | |
269 | function disconnect (id) { |
270 | onceTrue(api.sbot.obs.connection, (sbot) => { |
271 | sbot.patchwork.disconnect(id) |
272 | }) |
273 | } |
274 | } |
275 | } |
276 | |
277 | function getType (msg) { |
278 | return msg && msg.value && msg.value.content && msg.value.content.type |
279 | } |
280 | |
281 | function hasChannel (msg) { |
282 | return getType(msg) !== 'channel' && msg && msg.value && msg.value.content && !!msg.value.content.channel |
283 | } |
284 | |
285 | function arrayEq (a, b) { |
286 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) { |
287 | return a.every((value, i) => value === b[i]) |
288 | } |
289 | } |
290 | |
291 | function isReplacementMessage (msgA, msgB) { |
292 | if (msgA && msgB && msgA.value.content && msgB.value.content && msgA.value.content.type === msgB.value.content.type) { |
293 | if (msgA.key === msgB.key) return false |
294 | var type = msgA.value.content.type |
295 | if (type === 'contact') { |
296 | return msgA.value.author === msgB.value.author && msgA.value.content.contact === msgB.value.content.contact |
297 | } |
298 | } |
299 | } |
300 |
Built with git-ssb-web