git ssb

10+

Matt McKegg / patchwork



Tree: 2d704dd69f4a5dc68cb09a69665d8ac4ff93b64b

Files: 2d704dd69f4a5dc68cb09a69665d8ac4ff93b64b / modules / page / html / render / public.js

7439 bytesRaw
1var nest = require('depnest')
2var { h, send, when, computed, map } = require('mutant')
3var extend = require('xtend')
4var pull = require('pull-stream')
5
6exports.needs = nest({
7 sbot: {
8 pull: {
9 log: 'first',
10 feed: 'first',
11 userFeed: 'first'
12 },
13 obs: {
14 connectedPeers: 'first',
15 localPeers: 'first'
16 }
17 },
18 'about.html.image': 'first',
19 'about.obs.name': 'first',
20 'invite.sheet': 'first',
21
22 'message.html.compose': 'first',
23 'message.async.publish': 'first',
24 'progress.html.peer': 'first',
25
26 'feed.html.rollup': 'first',
27 'profile.obs.recentlyUpdated': 'first',
28 'contact.obs.following': 'first',
29 'channel.obs': {
30 subscribed: 'first',
31 recent: 'first'
32 },
33 'keys.sync.id': 'first'
34})
35
36exports.gives = nest({
37 'page.html.render': true
38})
39
40exports.create = function (api) {
41 return nest('page.html.render', page)
42
43 function page (path) {
44 if (path !== '/public') return // "/" is a sigil for "page"
45
46 var id = api.keys.sync.id()
47 var following = api.contact.obs.following(id)
48 var subscribedChannels = api.channel.obs.subscribed(id)
49 var loading = computed(subscribedChannels.sync, x => !x)
50 var channels = computed(api.channel.obs.recent(), 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 feedView = api.feed.html.rollup(getFeed, {
60 prepend,
61 waitFor: computed([
62 following.sync,
63 subscribedChannels.sync
64 ], (...x) => x.every(Boolean)),
65 windowSize: 1000,
66 filter: (item) => {
67 return !item.boxed && (item.lastUpdateType !== 'post' || item.message) && (
68 id === item.author ||
69 (item.author && following().has(item.author)) ||
70 (item.type === 'message' && subscribedChannels().has(item.channel)) ||
71 (item.type === 'subscribe' && item.subscribers.size) ||
72 (item.repliesFrom && item.repliesFrom.has(id)) ||
73 item.likes && item.likes.has(id)
74 )
75 },
76 bumpFilter: (msg, group) => {
77 if (group.type === 'subscribe') {
78 removeStrangers(group.subscribers)
79 }
80
81 if (group.type === 'message') {
82 removeStrangers(group.likes)
83 removeStrangers(group.repliesFrom)
84
85 if (!group.message) {
86 // if message is old, only show replies from friends
87 group.replies = group.replies.filter(x => {
88 return (x.value.author === id || following().has(x.value.author))
89 })
90 }
91 }
92
93 if (!group.message) {
94 return (
95 isMentioned(id, msg.value.content.mentions) ||
96 msg.value.author === id || (
97 fromDay(msg, group.fromTime) && (
98 following().has(msg.value.author) ||
99 group.repliesFrom.has(id)
100 )
101 )
102 )
103 }
104 return true
105 }
106 })
107
108 var result = h('div.SplitView', [
109 h('div.side', [
110 getSidebar()
111 ]),
112 h('div.main', feedView)
113 ])
114
115 result.pendingUpdates = feedView.pendingUpdates
116 result.reload = feedView.reload
117
118 return result
119
120 function removeStrangers (set) {
121 if (set) {
122 Array.from(set).forEach(key => {
123 if (!following().has(key) && key !== id) {
124 set.delete(key)
125 }
126 })
127 }
128 }
129
130 function getSidebar () {
131 var whoToFollow = computed([following, api.profile.obs.recentlyUpdated(), localPeers], (following, recent, peers) => {
132 return Array.from(recent).filter(x => x !== id && !following.has(x) && !peers.includes(x)).slice(0, 10)
133 })
134 return [
135 h('button -pub -full', {
136 'ev-click': api.invite.sheet
137 }, '+ Join Pub'),
138 when(computed(channels, x => x.length),
139 h('h2',
140 h('a', {href: '/channels'}, 'Active Channels')
141 )
142 ),
143 when(loading, [ h('Loading') ]),
144 h('div', {
145 classList: 'ChannelList',
146 hidden: loading
147 }, [
148 map(channels, (channel) => {
149 var subscribed = subscribedChannels.has(channel)
150 return h('a.channel', {
151 href: `#${channel}`,
152 classList: [
153 when(subscribed, '-subscribed')
154 ]
155 }, [
156 h('span.name', '#' + channel),
157 when(subscribed,
158 h('a.-unsubscribe', {
159 'ev-click': send(unsubscribe, channel)
160 }, 'Unsubscribe'),
161 h('a.-subscribe', {
162 'ev-click': send(subscribe, channel)
163 }, 'Subscribe')
164 )
165 ])
166 }, {maxTime: 5})
167 ]),
168
169 PeerList(localPeers, 'Local'),
170 PeerList(connectedPubs, 'Connected Pubs'),
171
172 when(computed(whoToFollow, x => x.length), h('h2', 'Who to follow')),
173 when(following.sync,
174 h('div', {
175 classList: 'ProfileList'
176 }, [
177 map(whoToFollow, (id) => {
178 return h('a.profile', {
179 href: id
180 }, [
181 h('div.avatar', [api.about.html.image(id)]),
182 h('div.main', [
183 h('div.name', [ api.about.obs.name(id) ])
184 ])
185 ])
186 })
187 ])
188 )
189 ]
190 }
191
192 function PeerList (ids, title) {
193 return [
194 when(computed(ids, x => x.length), h('h2', title)),
195 h('div', {
196 classList: 'ProfileList'
197 }, [
198 map(ids, (id) => {
199 return h('a.profile', {
200 classList: [ '-connected' ],
201 href: id
202 }, [
203 h('div.avatar', [api.about.html.image(id)]),
204 h('div.main', [
205 h('div.name', [ api.about.obs.name(id) ])
206 ]),
207 h('div.progress', [
208 api.progress.html.peer(id)
209 ])
210 ])
211 })
212 ])
213 ]
214 }
215
216 function getFeed (opts) {
217 if (opts.lt) {
218 opts = extend(opts, {lt: parseInt(opts.lt, 10)})
219 }
220
221 return pull(
222 api.sbot.pull.feed(opts),
223 pull.map((msg) => {
224 if (msg.sync) return msg
225 return {key: msg.key, value: msg.value, timestamp: msg.value.timestamp}
226 })
227 )
228 }
229
230 function getFirstMessage (feedId, cb) {
231 api.sbot.pull.userFeed({id: feedId, gte: 0, limit: 1})(null, cb)
232 }
233
234 function subscribe (id) {
235 api.message.async.publish({
236 type: 'channel',
237 channel: id,
238 subscribed: true
239 })
240 }
241
242 function unsubscribe (id) {
243 api.message.async.publish({
244 type: 'channel',
245 channel: id,
246 subscribed: false
247 })
248 }
249 }
250}
251
252function isMentioned (id, list) {
253 if (Array.isArray(list)) {
254 return list.includes(id)
255 } else {
256 return false
257 }
258}
259
260function fromDay (msg, fromTime) {
261 return (fromTime - msg.timestamp) < (24 * 60 * 60e3)
262}
263
264function arrayEq (a, b) {
265 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
266 return a.every((value, i) => value === b[i])
267 }
268}
269

Built with git-ssb-web