Files: 8bd323651fb1b0b76a2af4d86690309d221f6ab1 / modules / public.js
4230 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 | |
6 | var plugs = require('patchbay/plugs') |
7 | var h = require('../lib/h') |
8 | var message_compose = plugs.first(exports.message_compose = []) |
9 | var sbot_log = plugs.first(exports.sbot_log = []) |
10 | var feed_summary = plugs.first(exports.feed_summary = []) |
11 | var obs_channels = plugs.first(exports.obs_channels = []) |
12 | var obs_subscribed_channels = plugs.first(exports.obs_subscribed_channels = []) |
13 | var get_id = plugs.first(exports.get_id = []) |
14 | var publish = plugs.first(exports.sbot_publish = []) |
15 | var obs_following = plugs.first(exports.obs_following = []) |
16 | var obs_recently_updated_feeds = plugs.first(exports.obs_recently_updated_feeds = []) |
17 | var avatar_image = plugs.first(exports.avatar_image = []) |
18 | var avatar_name = plugs.first(exports.avatar_name = []) |
19 | var obs_local = plugs.first(exports.obs_local = []) |
20 | |
21 | exports.screen_view = function (path, sbot) { |
22 | if (path === '/public') { |
23 | var id = get_id() |
24 | var channels = computed(obs_channels(), items => items.slice(0, 6), {comparer: arrayEq}) |
25 | var subscribedChannels = obs_subscribed_channels(id) |
26 | var loading = computed(subscribedChannels.sync, x => !x) |
27 | var localPeers = obs_local() |
28 | var following = obs_following(id) |
29 | |
30 | var whoToFollow = computed([obs_following(id), obs_recently_updated_feeds(200)], (following, recent) => { |
31 | return Array.from(recent).filter(x => x !== id && !following.has(x)).slice(0, 20) |
32 | }) |
33 | |
34 | return h('SplitView', [ |
35 | h('div.side', [ |
36 | h('h2', 'Active Channels'), |
37 | when(loading, [ h('Loading') ]), |
38 | h('ChannelList', { |
39 | hidden: loading |
40 | }, [ |
41 | MutantMap(channels, (channel) => { |
42 | var subscribed = subscribedChannels.has(channel.id) |
43 | return h('a.channel', { |
44 | href: `##${channel.id}`, |
45 | classList: [ |
46 | when(subscribed, '-subscribed') |
47 | ] |
48 | }, [ |
49 | h('span.name', '#' + channel.id), |
50 | when(subscribed, |
51 | h('a -unsubscribe', { |
52 | 'ev-click': send(unsubscribe, channel.id) |
53 | }, 'Unsubscribe'), |
54 | h('a -subscribe', { |
55 | 'ev-click': send(subscribe, channel.id) |
56 | }, 'Subscribe') |
57 | ) |
58 | ]) |
59 | }, {maxTime: 5}) |
60 | ]), |
61 | |
62 | when(computed(localPeers, x => x.length), h('h2', 'Local')), |
63 | h('ProfileList', [ |
64 | MutantMap(localPeers, (id) => { |
65 | return h('a.profile', { |
66 | href: `#${id}` |
67 | }, [ |
68 | h('div.avatar', [avatar_image(id)]), |
69 | h('div.main', [ |
70 | h('div.name', [ avatar_name(id) ]) |
71 | ]) |
72 | ]) |
73 | }) |
74 | ]), |
75 | |
76 | when(computed(whoToFollow, x => x.length), h('h2', 'Who to follow')), |
77 | h('ProfileList', [ |
78 | MutantMap(whoToFollow, (id) => { |
79 | return h('a.profile', { |
80 | href: `#${id}` |
81 | }, [ |
82 | h('div.avatar', [avatar_image(id)]), |
83 | h('div.main', [ |
84 | h('div.name', [ avatar_name(id) ]) |
85 | ]) |
86 | ]) |
87 | }) |
88 | ]) |
89 | ]), |
90 | h('div.main', [ |
91 | feed_summary(sbot_log, [ |
92 | message_compose({type: 'post'}, {placeholder: 'Write a public message'}) |
93 | ], { |
94 | waitUntil: computed([ |
95 | following.sync, |
96 | subscribedChannels.sync |
97 | ], x => x.every(Boolean)), |
98 | filter: (item) => { |
99 | return id === item.author || |
100 | following().has(item.author) || |
101 | subscribedChannels().has(item.channel) || |
102 | (item.repliesFrom && item.repliesFrom.has(id)) |
103 | } |
104 | }) |
105 | ]) |
106 | ]) |
107 | } |
108 | } |
109 | |
110 | function subscribe (id) { |
111 | publish({ |
112 | type: 'channel', |
113 | channel: id, |
114 | subscribed: true |
115 | }) |
116 | } |
117 | |
118 | function unsubscribe (id) { |
119 | publish({ |
120 | type: 'channel', |
121 | channel: id, |
122 | subscribed: false |
123 | }) |
124 | } |
125 | |
126 | function arrayEq (a, b) { |
127 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) { |
128 | return a.every((value, i) => value === b[i]) |
129 | } |
130 | } |
131 |
Built with git-ssb-web