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