git ssb

10+

Matt McKegg / patchwork



Tree: 122c4f16e01a425e17ad901fecec876393951017

Files: 122c4f16e01a425e17ad901fecec876393951017 / modules / page / html / render / public.js

7423 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), h('h2', 'Active Channels')),
139 when(loading, [ h('Loading') ]),
140 h('div', {
141 classList: 'ChannelList',
142 hidden: loading
143 }, [
144 map(channels, (channel) => {
145 var subscribed = subscribedChannels.has(channel)
146 return h('a.channel', {
147 href: `#${channel}`,
148 classList: [
149 when(subscribed, '-subscribed')
150 ]
151 }, [
152 h('span.name', '#' + channel),
153 when(subscribed,
154 h('a.-unsubscribe', {
155 'ev-click': send(unsubscribe, channel)
156 }, 'Unsubscribe'),
157 h('a.-subscribe', {
158 'ev-click': send(subscribe, channel)
159 }, 'Subscribe')
160 )
161 ])
162 }, {maxTime: 5}),
163 h('a.channel', {href: '/channels'}, 'More channels..')
164 ]),
165
166 PeerList(localPeers, 'Local'),
167 PeerList(connectedPubs, 'Connected Pubs'),
168
169 when(computed(whoToFollow, x => x.length), h('h2', 'Who to follow')),
170 when(following.sync,
171 h('div', {
172 classList: 'ProfileList'
173 }, [
174 map(whoToFollow, (id) => {
175 return h('a.profile', {
176 href: id
177 }, [
178 h('div.avatar', [api.about.html.image(id)]),
179 h('div.main', [
180 h('div.name', [ api.about.obs.name(id) ])
181 ])
182 ])
183 })
184 ])
185 )
186 ]
187 }
188
189 function PeerList (ids, title) {
190 return [
191 when(computed(ids, x => x.length), h('h2', title)),
192 h('div', {
193 classList: 'ProfileList'
194 }, [
195 map(ids, (id) => {
196 return h('a.profile', {
197 classList: [ '-connected' ],
198 href: id
199 }, [
200 h('div.avatar', [api.about.html.image(id)]),
201 h('div.main', [
202 h('div.name', [ api.about.obs.name(id) ])
203 ]),
204 h('div.progress', [
205 api.progress.html.peer(id)
206 ])
207 ])
208 })
209 ])
210 ]
211 }
212
213 function getFeed (opts) {
214 if (opts.lt) {
215 opts = extend(opts, {lt: parseInt(opts.lt, 10)})
216 }
217
218 return pull(
219 api.sbot.pull.feed(opts),
220 pull.map((msg) => {
221 if (msg.sync) return msg
222 return {key: msg.key, value: msg.value, timestamp: msg.value.timestamp}
223 })
224 )
225 }
226
227 function getFirstMessage (feedId, cb) {
228 api.sbot.pull.userFeed({id: feedId, gte: 0, limit: 1})(null, cb)
229 }
230
231 function subscribe (id) {
232 api.message.async.publish({
233 type: 'channel',
234 channel: id,
235 subscribed: true
236 })
237 }
238
239 function unsubscribe (id) {
240 api.message.async.publish({
241 type: 'channel',
242 channel: id,
243 subscribed: false
244 })
245 }
246 }
247}
248
249function isMentioned (id, list) {
250 if (Array.isArray(list)) {
251 return list.includes(id)
252 } else {
253 return false
254 }
255}
256
257function fromDay (msg, fromTime) {
258 return (fromTime - msg.timestamp) < (24 * 60 * 60e3)
259}
260
261function arrayEq (a, b) {
262 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
263 return a.every((value, i) => value === b[i])
264 }
265}
266

Built with git-ssb-web