git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: d4131d5fd9230e7811031665045b96e6b8c6eb1d

Files: d4131d5fd9230e7811031665045b96e6b8c6eb1d / modules / page / html / render / public.js

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

Built with git-ssb-web