Files: 54f45b9e3a2114fd3c234a8de046349c9a3dab1c / src / components / AppSelector.js
4647 bytesRaw
1 | import './AppController.js' |
2 | import './FollowScuttleboot.js' |
3 | import { VotesManager } from '../VotesManager.js' |
4 | import { default as pull, paraMap, collect } from 'pull-stream' |
5 | |
6 | class AppSelector extends HTMLElement { |
7 | constructor() { |
8 | super(); |
9 | } |
10 | connectedCallback() { |
11 | const controllerArea = this.attachShadow({ mode: 'open' }) |
12 | const view = document.getElementById('view') |
13 | const opts = { |
14 | live: true, |
15 | reverse: false, |
16 | query: [ |
17 | { |
18 | $filter: { |
19 | value: { |
20 | content: { type: {$prefix: 'patchboot-'} } |
21 | } |
22 | } |
23 | }, |
24 | { |
25 | $filter: { |
26 | value: { |
27 | content: { type: {$in: ['patchboot-app','patchboot-webapp'] } } |
28 | } |
29 | } |
30 | } |
31 | ] |
32 | } |
33 | controllerArea.innerHTML = ` |
34 | <style> |
35 | * { |
36 | box-sizing: border-box; |
37 | overflow-wrap: anywhere; |
38 | } |
39 | app-controller { |
40 | --spacing: 0.5rem; |
41 | --lineColor: var(--lineColor2); |
42 | } |
43 | #apps { |
44 | border-radius: 0; |
45 | padding: 0; |
46 | min-height: 1rem; |
47 | max-height: 100%; |
48 | overflow-y: scroll; |
49 | } |
50 | .show-only-liked app-controller:not(.liked) { |
51 | display: none; |
52 | } |
53 | .top { |
54 | border-bottom: 1px solid gray; |
55 | width: 100%; |
56 | display: block; |
57 | } |
58 | </style> |
59 | <label class="top"><input type="checkbox" id="showLiked" />Show only apps I like</label>` |
60 | const appsGrid = document.createElement('div') |
61 | appsGrid.id = 'apps' |
62 | controllerArea.appendChild(appsGrid) |
63 | this.sbot.whoami().then(keys => this.sbot.friends.isFollowing({ |
64 | source: keys.id, |
65 | dest: '@luoZnBKHXeJl4kB39uIkZnQD4L0zl6Vd+Pe75gKS4fo=.ed25519' |
66 | })).then(followingSboot => { |
67 | if (!followingSboot) { |
68 | const followScuttleboot = document.createElement('follow-scuttleboot') |
69 | followScuttleboot.sbot = this.sbot |
70 | controllerArea.append(followScuttleboot) |
71 | } else { |
72 | console.log('Allready following scuttleboot.app') |
73 | } |
74 | }); |
75 | const showLikedcheckbox = controllerArea.getElementById('showLiked') |
76 | showLikedcheckbox.addEventListener('change', (e) => { |
77 | if (showLikedcheckbox.checked) { |
78 | appsGrid.classList.add('show-only-liked') |
79 | } else { |
80 | appsGrid.classList.remove('show-only-liked') |
81 | } |
82 | |
83 | }) |
84 | |
85 | let headObserver = null; |
86 | const sbot = this.sbot |
87 | pull(sbot.query.read(opts), pull.drain((msg) => { |
88 | if (!msg.value) { |
89 | return; |
90 | } |
91 | ensureNotRevoked(sbot, msg).then(() => { |
92 | const controller = document.createElement('app-controller'); |
93 | controller.msg = msg |
94 | controller.sbot = sbot |
95 | appsGrid.insertBefore(controller, appsGrid.firstChild); |
96 | const blobId = msg.value.content.link || msg.value.content.mentions[0].link; |
97 | controller.addEventListener('run', () => { |
98 | this.dispatchEvent(new CustomEvent('run', {detail: msg.value.content})) |
99 | }); |
100 | controller.addEventListener('view-source', () => { |
101 | this.dispatchEvent(new CustomEvent('show-source', {detail: msg.value.content})) |
102 | }) |
103 | controller.addEventListener('like', async () => { |
104 | try { |
105 | console.log(await VotesManager.getVotes(msg.key)); |
106 | } catch (e) { |
107 | console.log('error', e); |
108 | } |
109 | return true |
110 | }) |
111 | controller.addEventListener('unlike', () => { |
112 | //vote(msg.key, 0) |
113 | }) |
114 | }).catch(() => { }) |
115 | }, () => console.log('End of apps stream reached.'))) |
116 | |
117 | } |
118 | } |
119 | |
120 | function ensureNotRevoked(sbot, msg) { |
121 | return new Promise((resolve, reject) => { |
122 | const queryOpts = { |
123 | reverse: true, |
124 | query: [ |
125 | { |
126 | $filter: { |
127 | value: { |
128 | content: { |
129 | about: msg.key, |
130 | type: 'about', |
131 | status: 'revoked' |
132 | } |
133 | } |
134 | } |
135 | } |
136 | ], |
137 | limit: 1 |
138 | } |
139 | const backlinksOpts = { |
140 | reverse: true, |
141 | query: [ |
142 | { |
143 | $filter: { |
144 | dest: msg.key, |
145 | value: { |
146 | content: { |
147 | about: msg.key, |
148 | type: 'about', |
149 | status: 'revoked' |
150 | } |
151 | } |
152 | } |
153 | } |
154 | ], |
155 | limit: 1 |
156 | } |
157 | pull( |
158 | sbot.backlinks ? sbot.backlinks.read(backlinksOpts) : sbot.query.read(queryOpts), |
159 | pull.collect((err, revocations) => { |
160 | if (err) { |
161 | reject(err) |
162 | } else { |
163 | if (revocations.length > 0) { |
164 | reject() |
165 | } else { |
166 | resolve() |
167 | } |
168 | } |
169 | })) |
170 | }) |
171 | } |
172 | |
173 | |
174 | customElements.define("app-selector", AppSelector) |
Built with git-ssb-web