git ssb

10+

Matt McKegg / patchwork



Tree: 52922a4cd9a0eb4dfa4e61033be7a2fb037818ac

Files: 52922a4cd9a0eb4dfa4e61033be7a2fb037818ac / modules / page / html / render / public.js

7398 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 'intl.sync.i18n': 'first',
34})
35
36exports.gives = nest({
37 'page.html.render': true
38})
39
40exports.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 filters = api.settings.obs.get('filters')
71 var feedView = api.feed.html.rollup(getStream, {
72 prepend,
73 updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})),
74 bumpFilter: function (msg) {
75 if (msg.value && msg.value.content && typeof msg.value.content === 'object') {
76 var type = msg.value.content.type
77 if (type === 'vote') return false
78
79 var author = msg.value.author
80 var channel = normalizeChannel(msg.value.content.channel)
81 var tagged = checkTag(msg.value.content.mentions)
82 var isSubscribed = channel ? subscribedChannels().has(channel) : false
83 return isSubscribed || id === author || following().includes(author) || tagged
84 }
85 },
86 rootFilter: function (msg) {
87 if (!filters()) return true
88 return !(filters().following && getType(msg) === 'contact')
89 },
90 waitFor: computed([
91 following.sync,
92 subscribedChannels.sync
93 ], (...x) => x.every(Boolean))
94 })
95
96 // call reload whenever filters changes (equivalent to the refresh from inside rollup)
97 filters(feedView.reload)
98
99 var result = h('div.SplitView', [
100 h('div.side', [
101 getSidebar()
102 ]),
103 h('div.main', feedView)
104 ])
105
106 result.pendingUpdates = feedView.pendingUpdates
107 result.reload = feedView.reload
108
109 return result
110
111 function checkTag (mentions) {
112 if (Array.isArray(mentions)) {
113 return mentions.some((mention) => {
114 if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) {
115 var channel = normalizeChannel(mention.link.slice(1))
116 return channel ? subscribedChannels().has(channel) : false
117 }
118 })
119 }
120 }
121
122 function getSidebar () {
123 var whoToFollow = computed([following, api.profile.obs.recentlyUpdated(), localPeers], (following, recent, peers) => {
124 return recent.filter(x => x !== id && !following.includes(x) && !peers.includes(x)).slice(0, 10)
125 })
126 return [
127 h('button -pub -full', {
128 'ev-click': api.invite.sheet
129 }, i18n('+ Join Pub')),
130 when(loading, [ h("Loading") ], [
131 when(computed(channels, x => x.length), h('h2', i18n("Active Channels"))),
132 h('div', {
133 classList: 'ChannelList',
134 hidden: loading
135 }, [
136 map(channels, (channel) => {
137 var subscribed = subscribedChannels.has(channel)
138 return h('a.channel', {
139 href: `#${channel}`,
140 classList: [
141 when(subscribed, '-subscribed')
142 ]
143 }, [
144 h('span.name', '#' + channel),
145 when(subscribed,
146 h('a.-unsubscribe', {
147 'ev-click': send(unsubscribe, channel)
148 }, i18n('Unsubscribe')),
149 h('a.-subscribe', {
150 'ev-click': send(subscribe, channel)
151 }, i18n('Subscribe'))
152 )
153 ])
154 }, {maxTime: 5}),
155 h('a.channel -more', {href: '/channels'}, i18n('More Channels...'))
156 ])
157 ]),
158
159 PeerList(localPeers, i18n('Local')),
160 PeerList(connectedPubs, i18n('Connected Pubs')),
161
162 when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))),
163 when(following.sync,
164 h('div', {
165 classList: 'ProfileList'
166 }, [
167 map(whoToFollow, (id) => {
168 return h('a.profile', {
169 href: id
170 }, [
171 h('div.avatar', [api.about.html.image(id)]),
172 h('div.main', [
173 h('div.name', [ api.about.obs.name(id) ])
174 ])
175 ])
176 })
177 ])
178 )
179 ]
180 }
181
182 function PeerList (ids, title) {
183 return [
184 when(computed(ids, x => x.length), h('h2', title)),
185 h('div', {
186 classList: 'ProfileList'
187 }, [
188 map(ids, (id) => {
189 var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id))
190 return h('a.profile', {
191 classList: [
192 when(connected, '-connected')
193 ],
194 href: id
195 }, [
196 h('div.avatar', [api.about.html.image(id)]),
197 h('div.main', [
198 h('div.name', [ api.about.obs.name(id) ])
199 ]),
200 h('div.progress', [
201 api.progress.html.peer(id)
202 ])
203 ])
204 })
205 ])
206 ]
207 }
208
209 function subscribe (id) {
210 api.message.async.publish({
211 type: 'channel',
212 channel: id,
213 subscribed: true
214 })
215 }
216
217 function unsubscribe (id) {
218 api.message.async.publish({
219 type: 'channel',
220 channel: id,
221 subscribed: false
222 })
223 }
224 }
225}
226
227function getType (msg) {
228 return msg && msg.value && msg.value.content && msg.value.content.type
229}
230
231function arrayEq (a, b) {
232 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
233 return a.every((value, i) => value === b[i])
234 }
235}
236

Built with git-ssb-web