Files: cc9b1a7a5f4f79d8e0f7c4a2491a50f440ac9153 / app / page / channelSubscriptions.js
3101 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, when, Value, Array: MutantArray, onceTrue, watch, computed, map: mutantMap } = require('mutant') |
3 | const sortBy = require('lodash/sortBy') |
4 | const map = require('lodash/map') |
5 | const difference = require('lodash/difference') |
6 | |
7 | exports.gives = nest('app.page.channelSubscriptions') |
8 | |
9 | exports.needs = nest({ |
10 | 'app.html.sideNav': 'first', |
11 | 'app.html.topNav': 'first', |
12 | 'app.html.scroller': 'first', |
13 | 'app.html.channelCard': 'first', |
14 | 'app.obs.pluginsOk': 'first', |
15 | 'history.sync.push': 'first', |
16 | 'keys.sync.id': 'first', |
17 | 'channel.obs.subscribed': 'first', |
18 | 'channel.obs.recent': 'first', |
19 | 'channel.html.link': 'first', |
20 | 'translations.sync.strings': 'first', |
21 | 'sbot.async.friendsGet': 'first', |
22 | 'sbot.pull.userFeed': 'first', |
23 | 'sbot.obs.connection': 'first' |
24 | }) |
25 | |
26 | exports.create = (api) => { |
27 | const allChannels = MutantArray() |
28 | |
29 | return nest('app.page.channelSubscriptions', function (location) { |
30 | const strings = api.translations.sync.strings() |
31 | const myId = api.keys.sync.id() |
32 | |
33 | const rawSubs = api.channel.obs.subscribed(myId) |
34 | const mySubs = computed(rawSubs, myChannels => [...myChannels.values()].reverse()) |
35 | |
36 | if (location.scope === 'user') { |
37 | return h('Page -channelSubscriptions', { title: strings.home }, [ |
38 | api.app.html.sideNav(location), |
39 | h('div.content', [ |
40 | when(rawSubs.sync, |
41 | [ |
42 | computed(mySubs, mys => mys.length === 0 ? strings.subscriptions.state.noSubscriptions : ''), |
43 | mutantMap(mySubs, api.app.html.channelCard) |
44 | ], |
45 | h('p', strings.loading) |
46 | ) |
47 | ]) |
48 | ]) |
49 | } |
50 | |
51 | if (location.scope === 'friends') { |
52 | // update list of other all channels |
53 | // NOTE can't use onceTrue right now, because warnings are true/ false |
54 | watch( |
55 | api.app.obs.pluginsOk(), |
56 | ok => { |
57 | if (!ok) return |
58 | onceTrue(api.sbot.obs.connection, getChannels) |
59 | } |
60 | ) |
61 | // TODO - refactor this to use the cache in channel.obs.subscribed |
62 | |
63 | const showMoreCounter = Value(1) |
64 | const newChannels = computed([allChannels, mySubs, showMoreCounter], (all, mine, more) => { |
65 | return difference(all, mine) |
66 | .slice(0, 10 * more) |
67 | }) |
68 | |
69 | return h('Page -channelSubscriptions', { title: strings.home }, [ |
70 | api.app.html.sideNav(location), |
71 | h('div.content', [ |
72 | when(allChannels, |
73 | [ |
74 | mutantMap(newChannels, api.app.html.channelCard), |
75 | h('Button', { 'ev-click': () => showMoreCounter.set(showMoreCounter() + 1) }, |
76 | strings.showMore |
77 | ) |
78 | ], |
79 | h('p', strings.loading) |
80 | ) |
81 | ]) |
82 | ]) |
83 | } |
84 | }) |
85 | |
86 | function getChannels (sbot) { |
87 | console.log('fetching channel subscriptions') |
88 | sbot.channel.subscriptions((err, c) => { |
89 | if (err) throw err |
90 | let b = map(c, (v,k) => {return {channel: k, users: v.map(e=> e[0]) }}) |
91 | b = sortBy(b, o => o.users.length) |
92 | let res = b.reverse() |
93 | |
94 | allChannels.set(res.map(c => c.channel)) |
95 | }) |
96 | } |
97 | } |
98 | |
99 |
Built with git-ssb-web