forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-agent-buttons.ts
More file actions
286 lines (255 loc) · 10.6 KB
/
Copy pathadd-agent-buttons.ts
File metadata and controls
286 lines (255 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Contains the [[AddAgentButtons]] class
* @packageDocumentation
*/
import { NamedNode, Store } from 'rdflib'
import { AuthenticationContext } from 'solid-logic'
import * as debug from '../debug'
import { icons } from '../iconBase'
import { ensureLoadedProfile } from '../login/login'
import * as ns from '../ns'
import * as utils from '../utils'
import * as widgets from '../widgets'
import * as style from '../style'
import { AccessGroups } from './access-groups'
/**
* Renders the Sharing pane's "+" button and the menus behind it,
* see https://github.com/solidos/userguide/blob/main/views/sharing/userguide.md#add
*/
export class AddAgentButtons {
private readonly rootElement: HTMLElement
private readonly barElement: HTMLElement
private isExpanded: boolean = false
constructor (private groupList: AccessGroups) {
this.rootElement = groupList.controller.dom.createElement('div')
this.barElement = groupList.controller.dom.createElement('div')
}
public render (): HTMLElement {
this.rootElement.innerHTML = ''
this.rootElement.appendChild(this.renderAddButton())
this.rootElement.appendChild(this.barElement)
return this.rootElement
}
private renderAddButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
`${icons.iconBase}noun_34653_green.svg`,
'Add ...',
() => {
this.toggleBar()
this.renderBar()
}
)
}
private renderBar (): void {
this.barElement.innerHTML = ''
if (!this.isExpanded) {
return
}
this.barElement.appendChild(this.renderPersonButton())
this.barElement.appendChild(this.renderGroupButton())
this.barElement.appendChild(this.renderPublicButton())
this.barElement.appendChild(this.renderAuthenticatedAgentButton())
this.barElement.appendChild(this.renderBotButton())
this.barElement.appendChild(this.renderAppsButton())
}
private renderSimplifiedBar (button: EventTarget | null) {
Array.from(this.barElement.children)
.filter(element => element !== button)
.forEach(element => this.barElement.removeChild(element))
}
private renderPersonButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
icons.iconBase + widgets.iconForClass['vcard:Individual'],
'Add Person',
event => {
this.renderSimplifiedBar(event.target)
this.renderNameForm(ns.vcard('Individual'), 'person')
.then(name => this.addPerson(name))
.then(() => this.renderCleanup())
.catch(error => this.groupList.controller.renderStatus(error))
}
)
}
private renderGroupButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
icons.iconBase + widgets.iconForClass['vcard:Group'],
'Add Group',
event => {
this.renderSimplifiedBar(event.target)
this.renderNameForm(ns.vcard('Group'), 'group')
.then(name => this.addGroup(name))
.then(() => this.renderCleanup())
.catch(error => this.groupList.controller.renderStatus(error))
}
)
}
private renderNameForm (type: NamedNode, noun: string): Promise<string | undefined> {
return widgets.askName(
this.groupList.controller.dom,
this.groupList.store,
this.barElement,
ns.vcard('URI'),
type,
noun
)
}
private renderPublicButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
icons.iconBase + widgets.iconForClass['foaf:Agent'],
'Add Everyone',
() => this.addAgent(ns.foaf('Agent').uri)
.then(() => this.groupList.controller.renderTemporaryStatus('Adding the general public to those who can read. Drag the globe to a different level to give them more access.'))
.then(() => this.renderCleanup()))
}
private renderAuthenticatedAgentButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
`${icons.iconBase}noun_99101.svg`,
'Anyone logged In',
() => this.addAgent(ns.acl('AuthenticatedAgent').uri)
.then(() => this.groupList.controller.renderTemporaryStatus('Adding anyone logged in to those who can read. Drag the ID icon to a different level to give them more access.'))
.then(() => this.renderCleanup()))
}
private renderBotButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
icons.iconBase + 'noun_Robot_849764.svg',
'A Software Agent (bot)',
event => {
this.renderSimplifiedBar(event.target)
this.renderNameForm(ns.schema('Application'), 'bot')
.then(name => this.addBot(name))
.then(() => this.renderCleanup())
})
}
private renderAppsButton (): HTMLElement {
return widgets.button(
this.groupList.controller.dom,
`${icons.iconBase}noun_15177.svg`,
'A Web App (origin)',
event => {
this.renderSimplifiedBar(event.target)
const eventContext = {
div: this.barElement,
dom: this.groupList.controller.dom
}
const existingApps = this.renderAppsTable(eventContext)
.catch(error => this.groupList.controller.renderStatus(error))
this.renderAppsView()
const newApp = this.renderNameForm(ns.schema('WebApplication'), 'webapp domain')
.then(name => this.getOriginFromName(name))
Promise.race([
existingApps,
newApp
])
.then(origin => {
if (origin) {
this.groupList.addNewURI(origin)
}
})
.then(() => this.renderCleanup())
}
)
}
private renderAppsView (): void {
const trustedApplications = this.groupList.controller.context.session.paneRegistry.byName('trustedApplications')
if (trustedApplications) {
const trustedApplicationsElement = trustedApplications.render(null, this.groupList.controller.context)
trustedApplicationsElement.setAttribute('style', style.trustedAppController)
const cancelButton = widgets.cancelButton(this.groupList.controller.dom, () => this.renderCleanup())
cancelButton.setAttribute('style', style.trustedAppCancelButton)
trustedApplicationsElement.insertBefore(cancelButton, trustedApplicationsElement.firstChild)
this.barElement.appendChild(trustedApplicationsElement)
}
}
private async renderAppsTable (eventContext: AuthenticationContext): Promise<string> {
await ensureLoadedProfile(eventContext)
const trustedApps = (this.groupList.store as Store).each(eventContext.me, ns.acl('trustedApp')) as Array<NamedNode> // @@ TODO fix as
const trustedOrigins = trustedApps.flatMap(app => (this.groupList.store as Store).each(app, ns.acl('origin'))) // @@ TODO fix as
this.barElement.appendChild(this.groupList.controller.dom.createElement('p')).textContent = `You have ${trustedOrigins.length} selected web apps.`
return new Promise((resolve, reject) => {
const appsTable = this.barElement.appendChild(this.groupList.controller.dom.createElement('table'))
appsTable.setAttribute('style', style.trustedAppAddApplicationsTable)
trustedApps.forEach(app => {
const origin = (this.groupList.store as Store).any(app, ns.acl('origin')) // @@ TODO fix as
if (!origin) {
reject(new Error(`Unable to pick app: ${app.value}`))
}
const thingTR = widgets.personTR(this.groupList.controller.dom, ns.acl('origin'), origin, {})
const innerTable = this.groupList.controller.dom.createElement('table')
const innerRow = innerTable.appendChild(this.groupList.controller.dom.createElement('tr'))
const innerLeftColumn = innerRow.appendChild(this.groupList.controller.dom.createElement('td'))
innerLeftColumn.appendChild(thingTR)
const innerMiddleColumn = innerRow.appendChild(this.groupList.controller.dom.createElement('td'))
innerMiddleColumn.textContent = `Give access to ${this.groupList.controller.noun} ${utils.label(this.groupList.controller.subject)}?`
const innerRightColumn = innerRow.appendChild(this.groupList.controller.dom.createElement('td'))
innerRightColumn.appendChild(widgets.continueButton(this.groupList.controller.dom, () => resolve(origin!.value)))
appsTable.appendChild(innerTable)
})
})
}
private renderCleanup (): void {
this.renderBar()
this.groupList.render()
}
private async addPerson (name?: string): Promise<void> {
if (!name) return this.toggleBar() // user cancelled
const domainNameRegexp = /^https?:/i
if (!name.match(domainNameRegexp)) {
// @@ enforce in user input live like a form element
return Promise.reject(new Error('Not a http URI'))
}
// @@ check it actually is a person and has an owner who agrees they own it
debug.log(`Adding to ACL person: ${name}`)
await this.groupList.addNewURI(name)
this.toggleBar()
}
private async addGroup (name?: string): Promise<void> {
if (!name) return this.toggleBar() // user cancelled
const domainNameRegexp = /^https?:/i
if (!name.match(domainNameRegexp)) {
// @@ enforce in user input live like a form element
return Promise.reject(new Error('Not a http URI'))
}
// @@ check it actually is a group and has an owner who agrees they own it
debug.log('Adding to ACL group: ' + name)
await this.groupList.addNewURI(name)
this.toggleBar()
}
private async addAgent (agentUri: string): Promise<void> {
await this.groupList.addNewURI(agentUri)
this.toggleBar()
}
private async addBot (name?: string): Promise<void> {
if (!name) return this.toggleBar() // user cancelled
const domainNameRegexp = /^https?:/i
if (!name.match(domainNameRegexp)) {
// @@ enforce in user input live like a form element
return Promise.reject(new Error('Not a http URI'))
}
// @@ check it actually is a bot and has an owner who agrees they own it
debug.log('Adding to ACL bot: ' + name)
await this.groupList.addNewURI(name)
this.toggleBar()
}
private async getOriginFromName (name?: string): Promise<string | void> {
if (!name) return Promise.resolve() // user cancelled
const domainNameRegexp = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i
// https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch08s15.html
if (!name.match(domainNameRegexp)) {
// @@ enforce in user input live like a form element
return Promise.reject(new Error('Not a domain name'))
}
const origin = 'https://' + name
debug.log('Adding to ACL origin: ' + origin)
this.toggleBar()
return origin
}
private toggleBar (): void {
this.isExpanded = !this.isExpanded
}
}