Files: a224084e5f0cf22563242ad706400c3e4a22d5c5 / modules / page / html / render / public.js
8836 bytesRaw
1 | var nest = require('depnest') |
2 | var extend = require('xtend') |
3 | var pull = require('pull-stream') |
4 | var { h, send, when, computed, map } = require('mutant') |
5 | |
6 | exports.needs = nest({ |
7 | sbot: { |
8 | obs: { |
9 | connectedPeers: 'first', |
10 | localPeers: 'first' |
11 | } |
12 | }, |
13 | 'sbot.pull.stream': 'first', |
14 | 'feed.pull.public': 'first', |
15 | 'about.html.image': 'first', |
16 | 'about.obs.name': 'first', |
17 | 'invite.sheet': 'first', |
18 | |
19 | 'message.html.compose': 'first', |
20 | 'message.async.publish': 'first', |
21 | 'message.sync.root': 'first', |
22 | 'progress.html.peer': 'first', |
23 | |
24 | 'feed.html.rollup': 'first', |
25 | 'profile.obs.recentlyUpdated': 'first', |
26 | 'contact.obs.following': 'first', |
27 | 'contact.obs.blocking': 'first', |
28 | 'channel.obs': { |
29 | subscribed: 'first', |
30 | recent: 'first' |
31 | }, |
32 | 'channel.sync.normalize': 'first', |
33 | 'keys.sync.id': 'first', |
34 | 'settings.obs.get': 'first', |
35 | 'intl.sync.i18n': 'first' |
36 | }) |
37 | |
38 | exports.gives = nest({ |
39 | 'page.html.render': true |
40 | }) |
41 | |
42 | exports.create = function (api) { |
43 | const i18n = api.intl.sync.i18n |
44 | return nest('page.html.render', page) |
45 | |
46 | function page (path) { |
47 | if (path !== '/public') return // "/" is a sigil for "page" |
48 | |
49 | var id = api.keys.sync.id() |
50 | var following = api.contact.obs.following(id) |
51 | var blocking = api.contact.obs.blocking(id) |
52 | var subscribedChannels = api.channel.obs.subscribed(id) |
53 | var recentChannels = api.channel.obs.recent() |
54 | var loading = computed([subscribedChannels.sync, recentChannels.sync], (...args) => !args.every(Boolean)) |
55 | var channels = computed(recentChannels, items => items.slice(0, 8), {comparer: arrayEq}) |
56 | var connectedPeers = api.sbot.obs.connectedPeers() |
57 | var localPeers = api.sbot.obs.localPeers() |
58 | var connectedPubs = computed([connectedPeers, localPeers], (c, l) => c.filter(x => !l.includes(x))) |
59 | |
60 | var prepend = [ |
61 | api.message.html.compose({ meta: { type: 'post' }, placeholder: i18n('Write a public message') }) |
62 | ] |
63 | |
64 | var lastMessage = null |
65 | |
66 | var getStream = (opts) => { |
67 | if (!opts.lt) { |
68 | // HACK: reset the isReplacementMessage check |
69 | lastMessage = null |
70 | } |
71 | if (opts.lt != null && !opts.lt.marker) { |
72 | // if an lt has been specified that is not a marker, assume stream is finished |
73 | return pull.empty() |
74 | } else { |
75 | return api.sbot.pull.stream(sbot => sbot.patchwork.roots(extend(opts, { ids: [id] }))) |
76 | } |
77 | } |
78 | |
79 | var filters = api.settings.obs.get('filters') |
80 | var feedView = api.feed.html.rollup(getStream, { |
81 | prepend, |
82 | prefiltered: true, // we've already filtered out the roots we don't want to include |
83 | updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})), |
84 | bumpFilter: function (msg) { |
85 | // this needs to match the logic in sbot/roots so that we display the |
86 | // correct bump explainations |
87 | if (msg.value && msg.value.content && typeof msg.value.content === 'object') { |
88 | var type = msg.value.content.type |
89 | if (type === 'vote') return false |
90 | |
91 | var author = msg.value.author |
92 | var channel = api.channel.sync.normalize(msg.value.content.channel) |
93 | var tagged = checkTag(msg.value.content.mentions) |
94 | var isSubscribed = channel ? subscribedChannels().has(channel) : false |
95 | return isSubscribed || id === author || following().includes(author) || tagged |
96 | } |
97 | }, |
98 | rootFilter: function (msg) { |
99 | var filtered = filters() && filters().following && getType(msg) === 'contact' |
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 (!filtered && !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 checkTag (mentions) { |
139 | if (Array.isArray(mentions)) { |
140 | return mentions.some((mention) => { |
141 | if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) { |
142 | var channel = api.channel.sync.normalize(mention.link.slice(1)) |
143 | return channel ? subscribedChannels().has(channel) : false |
144 | } |
145 | }) |
146 | } |
147 | } |
148 | |
149 | function getSidebar () { |
150 | var whoToFollow = computed([api.profile.obs.recentlyUpdated(), following, blocking, localPeers], (recent, ...ignoreFeeds) => { |
151 | return recent.filter(x => x !== id && !ignoreFeeds.some(f => f.includes(x))).slice(0, 10) |
152 | }) |
153 | return [ |
154 | h('button -pub -full', { |
155 | 'ev-click': api.invite.sheet |
156 | }, i18n('+ Join Pub')), |
157 | when(loading, [ h('Loading') ], [ |
158 | when(computed(channels, x => x.length), h('h2', i18n('Active Channels'))), |
159 | h('div', { |
160 | classList: 'ChannelList', |
161 | hidden: loading |
162 | }, [ |
163 | map(channels, (channel) => { |
164 | var subscribed = subscribedChannels.has(channel) |
165 | return h('a.channel', { |
166 | href: `#${channel}`, |
167 | classList: [ |
168 | when(subscribed, '-subscribed') |
169 | ] |
170 | }, [ |
171 | h('span.name', '#' + channel), |
172 | when(subscribed, |
173 | h('a.-unsubscribe', { |
174 | 'ev-click': send(unsubscribe, channel) |
175 | }, i18n('Unsubscribe')), |
176 | h('a.-subscribe', { |
177 | 'ev-click': send(subscribe, channel) |
178 | }, i18n('Subscribe')) |
179 | ) |
180 | ]) |
181 | }, {maxTime: 5}), |
182 | h('a.channel -more', {href: '/channels'}, i18n('More Channels...')) |
183 | ]) |
184 | ]), |
185 | |
186 | PeerList(localPeers, i18n('Local')), |
187 | PeerList(connectedPubs, i18n('Connected Pubs')), |
188 | |
189 | when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))), |
190 | when(following.sync, |
191 | h('div', { |
192 | classList: 'ProfileList' |
193 | }, [ |
194 | map(whoToFollow, (id) => { |
195 | return h('a.profile', { |
196 | href: id |
197 | }, [ |
198 | h('div.avatar', [api.about.html.image(id)]), |
199 | h('div.main', [ |
200 | h('div.name', [ api.about.obs.name(id) ]) |
201 | ]) |
202 | ]) |
203 | }) |
204 | ]) |
205 | ) |
206 | ] |
207 | } |
208 | |
209 | function PeerList (ids, title) { |
210 | return [ |
211 | when(computed(ids, x => x.length), h('h2', title)), |
212 | h('div', { |
213 | classList: 'ProfileList' |
214 | }, [ |
215 | map(ids, (id) => { |
216 | var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id)) |
217 | return h('a.profile', { |
218 | classList: [ |
219 | when(connected, '-connected') |
220 | ], |
221 | href: id |
222 | }, [ |
223 | h('div.avatar', [api.about.html.image(id)]), |
224 | h('div.main', [ |
225 | h('div.name', [ api.about.obs.name(id) ]) |
226 | ]), |
227 | h('div.progress', [ |
228 | api.progress.html.peer(id) |
229 | ]) |
230 | ]) |
231 | }) |
232 | ]) |
233 | ] |
234 | } |
235 | |
236 | function subscribe (id) { |
237 | api.message.async.publish({ |
238 | type: 'channel', |
239 | channel: id, |
240 | subscribed: true |
241 | }) |
242 | } |
243 | |
244 | function unsubscribe (id) { |
245 | api.message.async.publish({ |
246 | type: 'channel', |
247 | channel: id, |
248 | subscribed: false |
249 | }) |
250 | } |
251 | } |
252 | } |
253 | |
254 | function getType (msg) { |
255 | return msg && msg.value && msg.value.content && msg.value.content.type |
256 | } |
257 | |
258 | function arrayEq (a, b) { |
259 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) { |
260 | return a.every((value, i) => value === b[i]) |
261 | } |
262 | } |
263 | |
264 | function isReplacementMessage (msgA, msgB) { |
265 | if (msgA && msgB && msgA.value.content && msgB.value.content && msgA.value.content.type === msgB.value.content.type) { |
266 | if (msgA.key === msgB.key) return false |
267 | var type = msgA.value.content.type |
268 | if (type === 'contact') { |
269 | return msgA.value.author === msgB.value.author && msgA.value.content.contact === msgB.value.content.contact |
270 | } |
271 | } |
272 | } |
273 |
Built with git-ssb-web