git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 4085b6135d2bff24ac3c2b0818dedc49e03a87dd

Files: 4085b6135d2bff24ac3c2b0818dedc49e03a87dd / modules / public.js

6848 bytesRaw
1var MutantMap = require('@mmckegg/mutant/map')
2var computed = require('@mmckegg/mutant/computed')
3var when = require('@mmckegg/mutant/when')
4var send = require('@mmckegg/mutant/send')
5var pull = require('pull-stream')
6var extend = require('xtend')
7
8var plugs = require('patchbay/plugs')
9var h = require('../lib/h')
10var message_compose = plugs.first(exports.message_compose = [])
11var sbot_log = plugs.first(exports.sbot_log = [])
12var sbot_feed = plugs.first(exports.sbot_feed = [])
13var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
14
15var feed_summary = plugs.first(exports.feed_summary = [])
16var obs_channels = plugs.first(exports.obs_channels = [])
17var obs_subscribed_channels = plugs.first(exports.obs_subscribed_channels = [])
18var get_id = plugs.first(exports.get_id = [])
19var publish = plugs.first(exports.sbot_publish = [])
20var obs_following = plugs.first(exports.obs_following = [])
21var obs_recently_updated_feeds = plugs.first(exports.obs_recently_updated_feeds = [])
22var avatar_image = plugs.first(exports.avatar_image = [])
23var avatar_name = plugs.first(exports.avatar_name = [])
24var obs_local = plugs.first(exports.obs_local = [])
25var obs_connected = plugs.first(exports.obs_connected = [])
26
27exports.screen_view = function (path, sbot) {
28 if (path === '/public') {
29 var id = get_id()
30 var channels = computed(obs_channels(), items => items.slice(0, 8), {comparer: arrayEq})
31 var subscribedChannels = obs_subscribed_channels(id)
32 var loading = computed(subscribedChannels.sync, x => !x)
33 var connectedPeers = obs_connected()
34 var localPeers = obs_local()
35 var connectedPubs = computed([connectedPeers, localPeers], (c, l) => c.filter(x => !l.includes(x)))
36 var following = obs_following(id)
37
38 var oldest = Date.now() - (2 * 24 * 60 * 60e3)
39 getFirstMessage(id, (_, msg) => {
40 if (msg) {
41 // fall back to timestamp stream before this, give 48 hrs for feeds to stabilize
42 if (msg.value.timestamp > oldest) {
43 oldest = Date.now()
44 }
45 }
46 })
47
48 var whoToFollow = computed([obs_following(id), obs_recently_updated_feeds(200)], (following, recent) => {
49 return Array.from(recent).filter(x => x !== id && !following.has(x)).slice(0, 10)
50 })
51
52 return h('SplitView', [
53 h('div.side', [
54 h('h2', 'Active Channels'),
55 when(loading, [ h('Loading') ]),
56 h('ChannelList', {
57 hidden: loading
58 }, [
59 MutantMap(channels, (channel) => {
60 var subscribed = subscribedChannels.has(channel.id)
61 return h('a.channel', {
62 href: `##${channel.id}`,
63 classList: [
64 when(subscribed, '-subscribed')
65 ]
66 }, [
67 h('span.name', '#' + channel.id),
68 when(subscribed,
69 h('a -unsubscribe', {
70 'ev-click': send(unsubscribe, channel.id)
71 }, 'Unsubscribe'),
72 h('a -subscribe', {
73 'ev-click': send(subscribe, channel.id)
74 }, 'Subscribe')
75 )
76 ])
77 }, {maxTime: 5})
78 ]),
79
80 when(computed(localPeers, x => x.length), h('h2', 'Local')),
81 h('ProfileList', [
82 MutantMap(localPeers, (id) => {
83 return h('a.profile', {
84 classList: [
85 when(computed([connectedPeers, id], (p, id) => p.includes(id)), '-connected')
86 ],
87 href: `#${id}`
88 }, [
89 h('div.avatar', [avatar_image(id)]),
90 h('div.main', [
91 h('div.name', [ avatar_name(id) ])
92 ])
93 ])
94 })
95 ]),
96
97 when(computed(whoToFollow, x => x.length), h('h2', 'Who to follow')),
98 h('ProfileList', [
99 MutantMap(whoToFollow, (id) => {
100 return h('a.profile', {
101 href: `#${id}`
102 }, [
103 h('div.avatar', [avatar_image(id)]),
104 h('div.main', [
105 h('div.name', [ avatar_name(id) ])
106 ])
107 ])
108 })
109 ]),
110
111 when(computed(connectedPubs, x => x.length), h('h2', 'Connected Pubs')),
112 h('ProfileList', [
113 MutantMap(connectedPubs, (id) => {
114 return h('a.profile', {
115 classList: [ '-connected' ],
116 href: `#${id}`
117 }, [
118 h('div.avatar', [avatar_image(id)]),
119 h('div.main', [
120 h('div.name', [ avatar_name(id) ])
121 ])
122 ])
123 })
124 ])
125 ]),
126 h('div.main', [
127 feed_summary(getFeed, [
128 message_compose({type: 'post'}, {placeholder: 'Write a public message'})
129 ], {
130 waitUntil: computed([
131 following.sync,
132 subscribedChannels.sync
133 ], x => x.every(Boolean)),
134 windowSize: 500,
135 filter: (item) => {
136 return (
137 id === item.author ||
138 following().has(item.author) ||
139 subscribedChannels().has(item.channel) ||
140 (item.repliesFrom && item.repliesFrom.has(id)) ||
141 item.digs && item.digs.has(id)
142 )
143 },
144 bumpFilter: (msg, group) => {
145 if (!group.message) {
146 return (
147 isMentioned(id, msg.value.content.mentions) ||
148 msg.value.author === id || (
149 fromDay(msg, group.fromTime) && (
150 following().has(msg.value.author) ||
151 group.repliesFrom.has(id)
152 )
153 )
154 )
155 }
156 return true
157 }
158 })
159 ])
160 ])
161 }
162
163 // scoped
164
165 function getFeed (opts) {
166 if (opts.lt && opts.lt < oldest) {
167 opts = extend(opts, {lt: parseInt(opts.lt, 10)})
168 return pull(
169 sbot_feed(opts),
170 pull.map((msg) => {
171 if (msg.sync) {
172 return msg
173 } else {
174 return {key: msg.key, value: msg.value, timestamp: msg.value.timestamp}
175 }
176 })
177 )
178 } else {
179 return sbot_log(opts)
180 }
181 }
182}
183
184function fromDay (msg, fromTime) {
185 return (fromTime - msg.timestamp) < (24 * 60 * 60e3)
186}
187
188function isMentioned (id, list) {
189 if (Array.isArray(list)) {
190 return list.includes(id)
191 } else {
192 return false
193 }
194}
195
196function subscribe (id) {
197 publish({
198 type: 'channel',
199 channel: id,
200 subscribed: true
201 })
202}
203
204function unsubscribe (id) {
205 publish({
206 type: 'channel',
207 channel: id,
208 subscribed: false
209 })
210}
211
212function arrayEq (a, b) {
213 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
214 return a.every((value, i) => value === b[i])
215 }
216}
217
218function getFirstMessage (feedId, cb) {
219 sbot_user_feed({id: feedId, gte: 0, limit: 1})(null, cb)
220}
221

Built with git-ssb-web