git ssb

10+

Matt McKegg / patchwork



Tree: 3eb1b173c39d9c87c4c84a786a93eb2059417ac2

Files: 3eb1b173c39d9c87c4c84a786a93eb2059417ac2 / modules / public.js

4972 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')
5
6var plugs = require('patchbay/plugs')
7var h = require('../lib/h')
8var message_compose = plugs.first(exports.message_compose = [])
9var sbot_log = plugs.first(exports.sbot_log = [])
10var feed_summary = plugs.first(exports.feed_summary = [])
11var obs_channels = plugs.first(exports.obs_channels = [])
12var obs_subscribed_channels = plugs.first(exports.obs_subscribed_channels = [])
13var get_id = plugs.first(exports.get_id = [])
14var publish = plugs.first(exports.sbot_publish = [])
15var obs_following = plugs.first(exports.obs_following = [])
16var obs_recently_updated_feeds = plugs.first(exports.obs_recently_updated_feeds = [])
17var avatar_image = plugs.first(exports.avatar_image = [])
18var avatar_name = plugs.first(exports.avatar_name = [])
19var obs_local = plugs.first(exports.obs_local = [])
20
21exports.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 windowSize: 200,
99 filter: (item) => {
100 return (
101 id === item.author ||
102 following().has(item.author) ||
103 subscribedChannels().has(item.channel) ||
104 (item.repliesFrom && item.repliesFrom.has(id))
105 )
106 },
107 bumpFilter: (msg, group) => {
108 if (!group.message) {
109 return (
110 isMentioned(id, msg.value.content.mentions) ||
111 msg.value.author === id || (
112 fromDay(msg, group.fromTime) && (
113 following().has(msg.value.author) ||
114 group.repliesFrom.has(id)
115 )
116 )
117 )
118 }
119 return true
120 }
121 })
122 ])
123 ])
124 }
125}
126
127function fromDay (msg, fromTime) {
128 return (fromTime - msg.timestamp) < (24 * 60 * 60e3)
129}
130
131function isMentioned (id, list) {
132 if (Array.isArray(list)) {
133 return list.includes(id)
134 } else {
135 return false
136 }
137}
138
139function subscribe (id) {
140 publish({
141 type: 'channel',
142 channel: id,
143 subscribed: true
144 })
145}
146
147function unsubscribe (id) {
148 publish({
149 type: 'channel',
150 channel: id,
151 subscribed: false
152 })
153}
154
155function arrayEq (a, b) {
156 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length && a !== b) {
157 return a.every((value, i) => value === b[i])
158 }
159}
160

Built with git-ssb-web