Files: 0ae3757980ed868524769abdc7b07da937191e78 / modules / page / html / render / public.js
6055 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 | 'progress.html.peer': 'first', |
22 | |
23 | 'feed.html.rollup': 'first', |
24 | 'profile.obs.recentlyUpdated': 'first', |
25 | 'contact.obs.following': 'first', |
26 | 'channel.obs': { |
27 | subscribed: 'first', |
28 | recent: 'first' |
29 | }, |
30 | 'keys.sync.id': 'first' |
31 | }) |
32 | |
33 | exports.gives = nest({ |
34 | 'page.html.render': true |
35 | }) |
36 | |
37 | exports.create = function (api) { |
38 | return nest('page.html.render', page) |
39 | |
40 | function page (path) { |
41 | if (path !== '/public') return // "/" is a sigil for "page" |
42 | |
43 | var id = api.keys.sync.id() |
44 | var following = api.contact.obs.following(id) |
45 | var subscribedChannels = api.channel.obs.subscribed(id) |
46 | var recentChannels = api.channel.obs.recent() |
47 | var loading = computed([subscribedChannels.sync, recentChannels.sync], (...args) => !args.every(Boolean)) |
48 | var channels = computed(recentChannels, items => items.slice(0, 8), {comparer: arrayEq}) |
49 | var connectedPeers = api.sbot.obs.connectedPeers() |
50 | var localPeers = api.sbot.obs.localPeers() |
51 | var connectedPubs = computed([connectedPeers, localPeers], (c, l) => c.filter(x => !l.includes(x))) |
52 | |
53 | var prepend = [ |
54 | api.message.html.compose({ meta: { type: 'post' }, placeholder: 'Write a public message' }) |
55 | ] |
56 | |
57 | var getStream = (opts) => { |
58 | if (opts.lt != null && !opts.lt.marker) { |
59 | // if an lt has been specified that is not a marker, assume stream is finished |
60 | return pull.empty() |
61 | } else { |
62 | return api.sbot.pull.stream(sbot => sbot.patchwork.roots(extend(opts, { ids: [id] }))) |
63 | } |
64 | } |
65 | |
66 | var feedView = api.feed.html.rollup(getStream, { |
67 | prepend, |
68 | updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})), |
69 | bumpFilter: function (msg) { |
70 | if (msg.value && msg.value.content && typeof msg.value.content === 'object') { |
71 | var author = msg.value.author |
72 | return id === author || following().has(author) |
73 | } |
74 | }, |
75 | waitFor: computed([ |
76 | following.sync, |
77 | subscribedChannels.sync |
78 | ], (...x) => x.every(Boolean)) |
79 | }) |
80 | |
81 | var result = h('div.SplitView', [ |
82 | h('div.side', [ |
83 | getSidebar() |
84 | ]), |
85 | h('div.main', feedView) |
86 | ]) |
87 | |
88 | result.pendingUpdates = feedView.pendingUpdates |
89 | result.reload = feedView.reload |
90 | |
91 | return result |
92 | |
93 | function getSidebar () { |
94 | var whoToFollow = computed([following, api.profile.obs.recentlyUpdated(), localPeers], (following, recent, peers) => { |
95 | return Array.from(recent).filter(x => x !== id && !following.has(x) && !peers.includes(x)).slice(0, 10) |
96 | }) |
97 | return [ |
98 | h('button -pub -full', { |
99 | 'ev-click': api.invite.sheet |
100 | }, '+ Join Pub'), |
101 | when(loading, [ h('Loading') ], [ |
102 | when(computed(channels, x => x.length), h('h2', 'Active Channels')), |
103 | h('div', { |
104 | classList: 'ChannelList', |
105 | hidden: loading |
106 | }, [ |
107 | map(channels, (channel) => { |
108 | var subscribed = subscribedChannels.has(channel) |
109 | return h('a.channel', { |
110 | href: `#${channel}`, |
111 | classList: [ |
112 | when(subscribed, '-subscribed') |
113 | ] |
114 | }, [ |
115 | h('span.name', '#' + channel), |
116 | when(subscribed, |
117 | h('a.-unsubscribe', { |
118 | 'ev-click': send(unsubscribe, channel) |
119 | }, 'Unsubscribe'), |
120 | h('a.-subscribe', { |
121 | 'ev-click': send(subscribe, channel) |
122 | }, 'Subscribe') |
123 | ) |
124 | ]) |
125 | }, {maxTime: 5}), |
126 | h('a.channel -more', {href: '/channels'}, 'More Channels...') |
127 | ]) |
128 | ]), |
129 | |
130 | PeerList(localPeers, 'Local'), |
131 | PeerList(connectedPubs, 'Connected Pubs'), |
132 | |
133 | when(computed(whoToFollow, x => x.length), h('h2', 'Who to follow')), |
134 | when(following.sync, |
135 | h('div', { |
136 | classList: 'ProfileList' |
137 | }, [ |
138 | map(whoToFollow, (id) => { |
139 | return h('a.profile', { |
140 | href: id |
141 | }, [ |
142 | h('div.avatar', [api.about.html.image(id)]), |
143 | h('div.main', [ |
144 | h('div.name', [ api.about.obs.name(id) ]) |
145 | ]) |
146 | ]) |
147 | }) |
148 | ]) |
149 | ) |
150 | ] |
151 | } |
152 | |
153 | function PeerList (ids, title) { |
154 | return [ |
155 | when(computed(ids, x => x.length), h('h2', title)), |
156 | h('div', { |
157 | classList: 'ProfileList' |
158 | }, [ |
159 | map(ids, (id) => { |
160 | var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id)) |
161 | return h('a.profile', { |
162 | classList: [ |
163 | when(connected, '-connected') |
164 | ], |
165 | href: id |
166 | }, [ |
167 | h('div.avatar', [api.about.html.image(id)]), |
168 | h('div.main', [ |
169 | h('div.name', [ api.about.obs.name(id) ]) |
170 | ]), |
171 | h('div.progress', [ |
172 | api.progress.html.peer(id) |
173 | ]) |
174 | ]) |
175 | }) |
176 | ]) |
177 | ] |
178 | } |
179 | |
180 | function subscribe (id) { |
181 | api.message.async.publish({ |
182 | type: 'channel', |
183 | channel: id, |
184 | subscribed: true |
185 | }) |
186 | } |
187 | |
188 | function unsubscribe (id) { |
189 | api.message.async.publish({ |
190 | type: 'channel', |
191 | channel: id, |
192 | subscribed: false |
193 | }) |
194 | } |
195 | } |
196 | } |
197 | |
198 | function arrayEq (a, b) { |
199 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) { |
200 | return a.every((value, i) => value === b[i]) |
201 | } |
202 | } |
203 |
Built with git-ssb-web