git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 3e9111306b522a46fa42a83d9e640509260fbf04

Files: 3e9111306b522a46fa42a83d9e640509260fbf04 / modules / page / html / render / public.js

8269 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 lastMessage = null
71
72 var filters = api.settings.obs.get('filters')
73 var feedView = api.feed.html.rollup(getStream, {
74 prepend,
75 prefiltered: true, // we've already filtered out the roots we don't want to include
76 updateStream: api.sbot.pull.stream(sbot => sbot.patchwork.latest({ids: [id]})),
77 bumpFilter: function (msg) {
78 // this needs to match the logic in sbot/roots so that we display the
79 // correct bump explainations
80 if (msg.value && msg.value.content && typeof msg.value.content === 'object') {
81 var type = msg.value.content.type
82 if (type === 'vote') return false
83
84 var author = msg.value.author
85 var channel = normalizeChannel(msg.value.content.channel)
86 var tagged = checkTag(msg.value.content.mentions)
87 var isSubscribed = channel ? subscribedChannels().has(channel) : false
88 return isSubscribed || id === author || following().includes(author) || tagged
89 }
90 },
91 rootFilter: function (msg) {
92 var filtered = filters() && filters().following && getType(msg) === 'contact'
93 // skip messages that are directly replaced by the previous message
94 // e.g. follow / unfollow in quick succession
95 var isOutdated = isReplacementMessage(msg, lastMessage)
96 if (!filtered && !isOutdated) {
97 lastMessage = msg
98 return true
99 }
100 },
101 waitFor: computed([
102 following.sync,
103 subscribedChannels.sync
104 ], (...x) => x.every(Boolean))
105 })
106
107 // call reload whenever filters changes (equivalent to the refresh from inside rollup)
108 filters(feedView.reload)
109
110 var result = h('div.SplitView', [
111 h('div.side', [
112 getSidebar()
113 ]),
114 h('div.main', feedView)
115 ])
116
117 result.pendingUpdates = feedView.pendingUpdates
118 result.reload = feedView.reload
119
120 return result
121
122 function checkTag (mentions) {
123 if (Array.isArray(mentions)) {
124 return mentions.some((mention) => {
125 if (mention && typeof mention.link === 'string' && mention.link.startsWith('#')) {
126 var channel = normalizeChannel(mention.link.slice(1))
127 return channel ? subscribedChannels().has(channel) : false
128 }
129 })
130 }
131 }
132
133 function getSidebar () {
134 var whoToFollow = computed([following, api.profile.obs.recentlyUpdated(), localPeers], (following, recent, peers) => {
135 return recent.filter(x => x !== id && !following.includes(x) && !peers.includes(x)).slice(0, 10)
136 })
137 return [
138 h('button -pub -full', {
139 'ev-click': api.invite.sheet
140 }, i18n('+ Join Pub')),
141 when(loading, [ h('Loading') ], [
142 when(computed(channels, x => x.length), h('h2', i18n('Active Channels'))),
143 h('div', {
144 classList: 'ChannelList',
145 hidden: loading
146 }, [
147 map(channels, (channel) => {
148 var subscribed = subscribedChannels.has(channel)
149 return h('a.channel', {
150 href: `#${channel}`,
151 classList: [
152 when(subscribed, '-subscribed')
153 ]
154 }, [
155 h('span.name', '#' + channel),
156 when(subscribed,
157 h('a.-unsubscribe', {
158 'ev-click': send(unsubscribe, channel)
159 }, i18n('Unsubscribe')),
160 h('a.-subscribe', {
161 'ev-click': send(subscribe, channel)
162 }, i18n('Subscribe'))
163 )
164 ])
165 }, {maxTime: 5}),
166 h('a.channel -more', {href: '/channels'}, i18n('More Channels...'))
167 ])
168 ]),
169
170 PeerList(localPeers, i18n('Local')),
171 PeerList(connectedPubs, i18n('Connected Pubs')),
172
173 when(computed(whoToFollow, x => x.length), h('h2', i18n('Who to follow'))),
174 when(following.sync,
175 h('div', {
176 classList: 'ProfileList'
177 }, [
178 map(whoToFollow, (id) => {
179 return h('a.profile', {
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 ])
187 })
188 ])
189 )
190 ]
191 }
192
193 function PeerList (ids, title) {
194 return [
195 when(computed(ids, x => x.length), h('h2', title)),
196 h('div', {
197 classList: 'ProfileList'
198 }, [
199 map(ids, (id) => {
200 var connected = computed([connectedPeers, id], (peers, id) => peers.includes(id))
201 return h('a.profile', {
202 classList: [
203 when(connected, '-connected')
204 ],
205 href: id
206 }, [
207 h('div.avatar', [api.about.html.image(id)]),
208 h('div.main', [
209 h('div.name', [ api.about.obs.name(id) ])
210 ]),
211 h('div.progress', [
212 api.progress.html.peer(id)
213 ])
214 ])
215 })
216 ])
217 ]
218 }
219
220 function subscribe (id) {
221 api.message.async.publish({
222 type: 'channel',
223 channel: id,
224 subscribed: true
225 })
226 }
227
228 function unsubscribe (id) {
229 api.message.async.publish({
230 type: 'channel',
231 channel: id,
232 subscribed: false
233 })
234 }
235 }
236}
237
238function getType (msg) {
239 return msg && msg.value && msg.value.content && msg.value.content.type
240}
241
242function arrayEq (a, b) {
243 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
244 return a.every((value, i) => value === b[i])
245 }
246}
247
248function isReplacementMessage (msgA, msgB) {
249 if (msgA && msgB && msgA.value.content && msgB.value.content && msgA.value.content.type === msgB.value.content.type) {
250 var type = msgA.value.content.type
251 if (type === 'contact') {
252 return msgA.value.author === msgB.value.author && msgA.value.content.contact === msgB.value.content.contact
253 }
254 }
255}
256

Built with git-ssb-web