Files: 8acd553f8fa30f4b8a87229695d165ff34d49efc / modules / page / html / render / public.js
8064 bytesRaw
1 | var nest = require('depnest') |
2 | var extend = require('xtend') |
3 | var pull = require('pull-stream') |
4 | var normalizeChannel = require('../../../../lib/normalize-channel') |
5 | var { h, send, when, computed, map } = require('mutant') |
6 | |
7 | exports.needs = nest({ |
8 | sbot: { |
9 | obs: { |
10 | connectedPeers: 'first', |
11 | localPeers: '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 | 'progress.html.peer': 'first', |
23 | |
24 | 'feed.html.rollup': 'first', |
25 | 'profile.obs.recentlyUpdated': 'first', |
26 | 'contact.obs.following': 'first', |
27 | 'channel.obs': { |
28 | subscribed: 'first', |
29 | recent: 'first' |
30 | }, |
31 | 'keys.sync.id': 'first', |
32 | 'settings.obs.get': 'first', |
33 | 'intl.sync.i18n': 'first', |
34 | }) |
35 | |
36 | exports.gives = nest({ |
37 | 'page.html.render': true |
38 | }) |
39 | |
40 | exports.create = function (api) { |
41 | const i18n = api.intl.sync.i18n |
42 | return nest('page.html.render', page) |
43 | |
44 | function page (path) { |
45 | if (path !== '/public') return // "/" is a sigil for "page" |
46 | |
47 | var id = api.keys.sync.id() |
48 | var following = api.contact.obs.following(id) |
49 | var subscribedChannels = api.channel.obs.subscribed(id) |
50 | var recentChannels = api.channel.obs.recent() |
51 | var loading = computed([subscribedChannels.sync, recentChannels.sync], (...args) => !args.every(Boolean)) |
52 | var channels = computed(recentChannels, items => items.slice(0, 8), {comparer: arrayEq}) |
53 | var connectedPeers = api.sbot.obs.connectedPeers() |
54 | var localPeers = api.sbot.obs.localPeers() |
55 | var connectedPubs = computed([connectedPeers, localPeers], (c, l) => c.filter(x => !l.includes(x))) |
56 | |
57 | var prepend = [ |
58 | api.message.html.compose({ meta: { type: 'post' }, placeholder: i18n('Write a public message') }) |
59 | ] |
60 | |
61 | var getStream = (opts) => { |
62 | if (opts.lt != null && !opts.lt.marker) { |
63 | // if an lt has been specified that is not a marker, assume stream is finished |
64 | return pull.empty() |
65 | } else { |
66 | return api.sbot.pull.stream(sbot => sbot.patchwork.roots(extend(opts, { ids: [id] }))) |
67 | } |
68 | } |
69 | |
70 | var lastMessage = null |
71 | |
72 | var filters = api.settings.obs.get('filters') |
73 | var feedView = api.feed.html.rollup(getStream, { |
74 | prepend, |
75 | updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})), |
76 | bumpFilter: function (msg) { |
77 | if (msg.value && msg.value.content && typeof msg.value.content === 'object') { |
78 | var type = msg.value.content.type |
79 | if (type === 'vote') return false |
80 | |
81 | var author = msg.value.author |
82 | var channel = normalizeChannel(msg.value.content.channel) |
83 | var tagged = checkTag(msg.value.content.mentions) |
84 | var isSubscribed = channel ? subscribedChannels().has(channel) : false |
85 | return isSubscribed || id === author || following().includes(author) || tagged |
86 | } |
87 | }, |
88 | rootFilter: function (msg) { |
89 | var filtered = filters() && filters().following && getType(msg) === 'contact' |
90 | // skip messages that are directly replaced by the previous message |
91 | // e.g. follow / unfollow in quick succession |
92 | var isOutdated = isReplacementMessage(msg, lastMessage) |
93 | if (!filtered && !isOutdated) { |
94 | lastMessage = msg |
95 | return true |
96 | } |
97 | }, |
98 | waitFor: computed([ |
99 | following.sync, |
100 | subscribedChannels.sync |
101 | ], (...x) => x.every(Boolean)) |
102 | }) |
103 | |
104 | // call reload whenever filters changes (equivalent to the refresh from inside rollup) |
105 | filters(feedView.reload) |
106 | |
107 | var result = h('div.SplitView', [ |
108 | h('div.side', [ |
109 | getSidebar() |
110 | ]), |
111 | h('div.main', feedView) |
112 | ]) |
113 | |
114 | result.pendingUpdates = feedView.pendingUpdates |
115 | result.reload = feedView.reload |
116 | |
117 | return result |
118 | |
119 | function checkTag (mentions) { |
120 | if (Array.isArray(mentions)) { |
121 | return mentions.some((mention) => { |
122 | if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) { |
123 | var channel = normalizeChannel(mention.link.slice(1)) |
124 | return channel ? subscribedChannels().has(channel) : false |
125 | } |
126 | }) |
127 | } |
128 | } |
129 | |
130 | function getSidebar () { |
131 | var whoToFollow = computed([following, api.profile.obs.recentlyUpdated(), localPeers], (following, recent, peers) => { |
132 | return recent.filter(x => x !== id && !following.includes(x) && !peers.includes(x)).slice(0, 10) |
133 | }) |
134 | return [ |
135 | h('button -pub -full', { |
136 | 'ev-click': api.invite.sheet |
137 | }, i18n('+ Join Pub')), |
138 | when(loading, [ h("Loading") ], [ |
139 | when(computed(channels, x => x.length), h('h2', i18n("Active Channels"))), |
140 | h('div', { |
141 | classList: 'ChannelList', |
142 | hidden: loading |
143 | }, [ |
144 | map(channels, (channel) => { |
145 | var subscribed = subscribedChannels.has(channel) |
146 | return h('a.channel', { |
147 | href: `#${channel}`, |
148 | classList: [ |
149 | when(subscribed, '-subscribed') |
150 | ] |
151 | }, [ |
152 | h('span.name', '#' + channel), |
153 | when(subscribed, |
154 | h('a.-unsubscribe', { |
155 | 'ev-click': send(unsubscribe, channel) |
156 | }, i18n('Unsubscribe')), |
157 | h('a.-subscribe', { |
158 | 'ev-click': send(subscribe, channel) |
159 | }, i18n('Subscribe')) |
160 | ) |
161 | ]) |
162 | }, {maxTime: 5}), |
163 | h('a.channel -more', {href: '/channels'}, i18n('More Channels...')) |
164 | ]) |
165 | ]), |
166 | |
167 | PeerList(localPeers, i18n('Local')), |
168 | PeerList(connectedPubs, i18n('Connected Pubs')), |
169 | |
170 | when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))), |
171 | when(following.sync, |
172 | h('div', { |
173 | classList: 'ProfileList' |
174 | }, [ |
175 | map(whoToFollow, (id) => { |
176 | return h('a.profile', { |
177 | href: id |
178 | }, [ |
179 | h('div.avatar', [api.about.html.image(id)]), |
180 | h('div.main', [ |
181 | h('div.name', [ api.about.obs.name(id) ]) |
182 | ]) |
183 | ]) |
184 | }) |
185 | ]) |
186 | ) |
187 | ] |
188 | } |
189 | |
190 | function PeerList (ids, title) { |
191 | return [ |
192 | when(computed(ids, x => x.length), h('h2', title)), |
193 | h('div', { |
194 | classList: 'ProfileList' |
195 | }, [ |
196 | map(ids, (id) => { |
197 | var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id)) |
198 | return h('a.profile', { |
199 | classList: [ |
200 | when(connected, '-connected') |
201 | ], |
202 | href: id |
203 | }, [ |
204 | h('div.avatar', [api.about.html.image(id)]), |
205 | h('div.main', [ |
206 | h('div.name', [ api.about.obs.name(id) ]) |
207 | ]), |
208 | h('div.progress', [ |
209 | api.progress.html.peer(id) |
210 | ]) |
211 | ]) |
212 | }) |
213 | ]) |
214 | ] |
215 | } |
216 | |
217 | function subscribe (id) { |
218 | api.message.async.publish({ |
219 | type: 'channel', |
220 | channel: id, |
221 | subscribed: true |
222 | }) |
223 | } |
224 | |
225 | function unsubscribe (id) { |
226 | api.message.async.publish({ |
227 | type: 'channel', |
228 | channel: id, |
229 | subscribed: false |
230 | }) |
231 | } |
232 | } |
233 | } |
234 | |
235 | function getType (msg) { |
236 | return msg && msg.value && msg.value.content && msg.value.content.type |
237 | } |
238 | |
239 | function arrayEq (a, b) { |
240 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) { |
241 | return a.every((value, i) => value === b[i]) |
242 | } |
243 | } |
244 | |
245 | function isReplacementMessage (msgA, msgB) { |
246 | if (msgA && msgB && msgA.value.content && msgB.value.content && msgA.value.content.type === msgB.value.content.type) { |
247 | var type = msgA.value.content.type |
248 | if (type === 'contact') { |
249 | return msgA.value.author === msgB.value.author && msgA.value.content.contact === msgB.value.content.contact |
250 | } |
251 | } |
252 | } |
253 |
Built with git-ssb-web