|
| 1 | +import { S } from 'surplus' |
| 2 | +import { DataSignal } from 's-js/src/S' |
| 3 | + |
| 4 | +import solidUi from 'solid-ui' |
| 5 | +import $rdf, { NamedNode } from 'rdflib' |
| 6 | +import { TrustedApplication } from './trustedApplications.models' |
| 7 | +import { getStatementsToAdd, getStatementsToDelete } from './trustedApplications.service' |
| 8 | + |
| 9 | +const { authn, ns, store } = solidUi |
| 10 | + |
| 11 | +export class TrustedApplicationsController { |
| 12 | + public isLoading: DataSignal<boolean> |
| 13 | + public isLoggedIn: DataSignal<boolean> |
| 14 | + public isEditable: DataSignal<boolean> |
| 15 | + public applications: DataSignal<TrustedApplication[]> |
| 16 | + public newApplication: DataSignal<TrustedApplication> |
| 17 | + |
| 18 | + constructor (public subject: NamedNode) { |
| 19 | + this.isLoading = S.value(true) |
| 20 | + this.isLoggedIn = S.value(false) |
| 21 | + this.isEditable = S.value(false) |
| 22 | + this.applications = S.data([]) |
| 23 | + this.newApplication = S.value(TrustedApplication.createNew(subject)) |
| 24 | + |
| 25 | + authn.solidAuthClient.currentSession() |
| 26 | + .then((session: any) => { |
| 27 | + this.isLoggedIn(!!session) |
| 28 | + this.isEditable(!!store.updater.editable(subject.doc().uri, store)) |
| 29 | + this.isLoading(false) |
| 30 | + |
| 31 | + const applications = store.each(subject, ns.acl('trustedApp'), undefined, undefined) |
| 32 | + .flatMap((app: any) => { |
| 33 | + return store.each(app, ns.acl('origin'), undefined, undefined) |
| 34 | + .map((origin: any) => ({ appModes: store.each(app, ns.acl('mode'), undefined, undefined), origin })) |
| 35 | + }) |
| 36 | + .map(({ appModes, origin }: {appModes: NamedNode[], origin: NamedNode}) => TrustedApplication.fromNamedNodes(subject, origin, appModes)) |
| 37 | + .sort(this.sortApplications) |
| 38 | + this.applications(applications) |
| 39 | + }) |
| 40 | + .catch((err: any) => { |
| 41 | + this.isLoggedIn(false) |
| 42 | + this.isLoading(false) |
| 43 | + console.error('Error fetching currentSession:', err) |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + addOrEditApplication (appToAddOrEdit: TrustedApplication): void { |
| 48 | + let origin |
| 49 | + try { |
| 50 | + origin = $rdf.sym(appToAddOrEdit.origin()) |
| 51 | + } catch (err) { |
| 52 | + return alert('Please provide an application URL you want to trust') |
| 53 | + } |
| 54 | + |
| 55 | + const modes = appToAddOrEdit.modes |
| 56 | + .filter(checkbox => checkbox.isChecked()) |
| 57 | + .map(checkbox => checkbox.value) |
| 58 | + |
| 59 | + const deletions = getStatementsToDelete(origin, this.subject, store, ns) |
| 60 | + const additions = getStatementsToAdd(origin, this.generateRandomString(), modes, this.subject, ns) |
| 61 | + store.updater.update(deletions, additions, () => { |
| 62 | + let applications = this.applications() |
| 63 | + const addedOrUpdatedApp = TrustedApplication.copy(appToAddOrEdit) |
| 64 | + const index = applications.findIndex(app => app.origin() === appToAddOrEdit.origin()) |
| 65 | + if (index === -1) { |
| 66 | + applications.push(addedOrUpdatedApp) |
| 67 | + } else { |
| 68 | + applications.splice(index, 1, addedOrUpdatedApp) |
| 69 | + } |
| 70 | + this.applications(applications.sort(this.sortApplications)) |
| 71 | + this.newApplication(TrustedApplication.createNew(this.subject)) |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + removeApplication (appToRemove: TrustedApplication): void { |
| 76 | + let originToRemove |
| 77 | + try { |
| 78 | + originToRemove = $rdf.sym(appToRemove.origin()) |
| 79 | + } catch (err) { |
| 80 | + return alert('Please provide an application URL you want to remove trust from') |
| 81 | + } |
| 82 | + |
| 83 | + const deletions = getStatementsToDelete(originToRemove, this.subject, store, ns) |
| 84 | + store.updater.update(deletions, null, () => { |
| 85 | + const applications = this.applications().filter(app => app.origin() !== appToRemove.origin()) |
| 86 | + this.applications(applications) |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + private generateRandomString (): string { |
| 91 | + return Math.random().toString(36).substring(7) |
| 92 | + } |
| 93 | + |
| 94 | + private sortApplications (a: TrustedApplication, b: TrustedApplication): number { |
| 95 | + return a.origin() > b.origin() ? 1 : -1 |
| 96 | + } |
| 97 | +} |
0 commit comments