git ssb

10+

Matt McKegg / patchwork



Tree: 7d39f00f5d8cc46d121b4197b45fa1dc145ec3da

Files: 7d39f00f5d8cc46d121b4197b45fa1dc145ec3da / modules / page / html / render / public.js

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

Built with git-ssb-web