git ssb

2+

mixmix / ticktack



Tree: 140bd2aa4c9baa0075ccea378b2376a4c90cf8fc

Files: 140bd2aa4c9baa0075ccea378b2376a4c90cf8fc / app / page / channelSubscriptions.js

3076 bytesRaw
1const nest = require('depnest')
2const { h, when, Value, Array: MutantArray, onceTrue, watch, computed, map: mutantMap } = require('mutant')
3const sortBy = require('lodash/sortBy')
4const map = require('lodash/map')
5const difference = require('lodash/difference')
6
7exports.gives = nest('app.page.channelSubscriptions')
8
9exports.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.pluginWarnings': '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
26exports.create = (api) => {
27 const otherChannels = 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.pluginWarnings(),
56 isWarnings => {
57 if (isWarnings) {
58 return
59 }
60 onceTrue(api.sbot.obs.connection, getChannels)
61 }
62 )
63
64 const showMoreCounter = Value(1)
65 const newChannels = computed([otherChannels, mySubs, showMoreCounter], (other, mine, more) => {
66 return difference(other, mine)
67 .slice(0, 10 * more)
68 })
69
70 return h('Page -channelSubscriptions', { title: strings.home }, [
71 api.app.html.sideNav(location),
72 h('div.content', [
73 when(otherChannels,
74 [
75 mutantMap(newChannels, api.app.html.channelCard),
76 h('Button', { 'ev-click': () => showMoreCounter.set(showMoreCounter() + 1) },
77 strings.showMore
78 )
79 ],
80 h('p', strings.loading)
81 )
82 ])
83 ])
84 }
85 })
86
87 function getChannels (sbot) {
88 console.log('fetching channel subscriptions')
89 sbot.channel.subscriptions((err, c) => {
90 if (err) throw err
91 let b = map(c, (v,k) => {return {channel: k, users: v}})
92 b = sortBy(b, o => o.users.length)
93 let res = b.reverse()
94
95 otherChannels.set(res.map(c => c.channel))
96 })
97 }
98}
99
100

Built with git-ssb-web