-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmintNewAddressBook.js
171 lines (152 loc) · 5.63 KB
/
mintNewAddressBook.js
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
import * as UI from 'solid-ui'
import { solidLogicSingleton } from 'solid-logic'
const { setACLUserPublic } = solidLogicSingleton.acl
// const mime = require('mime-types')
// const toolsPane0 = require('./toolsPane')
// const toolsPane = toolsPane0.toolsPane
const $rdf = UI.rdf
// const ns = UI.ns
// const utils = UI.utils
// Mint a new address book
export function mintNewAddressBook (dataBrowserContext, context) {
return new Promise(function (resolve, reject) {
UI.login.ensureLoadedProfile(context).then(
context => {
// 20180713
console.log('Logged in as ' + context.me)
const me = context.me
const dom = context.dom
const div = context.div
const kb = dataBrowserContext.session.store
const ns = UI.ns
const newBase = context.newBase || context.newInstance.dir().uri
const instanceClass = context.instanceClass || ns.vcard('AddressBook')
if (instanceClass.sameTerm(ns.vcard('Group'))) {
// Make a group not an address book
const g =
context.newInstance || kb.sym(context.newBase + 'index.ttl#this')
const doc = g.doc()
kb.add(g, ns.rdf('type'), ns.vcard('Group'), doc)
kb.add(
g,
ns.vcard('fn'),
context.instanceName || 'untitled group',
doc
) // @@ write doc back
kb.fetcher
.putBack(doc, { contentType: 'text/turtle' })
.then(function (_xhr) {
resolve(context)
})
.catch(function (err) {
reject(
new Error('Error creating document for new group ' + err)
)
})
return
}
const appInstanceNoun = 'address book'
function complain (message) {
div.appendChild(UI.widgets.errorMessageBlock(dom, message, 'pink'))
}
let bookContents = `@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
@prefix ab: <http://www.w3.org/ns/pim/ab#>.
@prefix dc: <http://purl.org/dc/elements/1.1/>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
<#this> a vcard:AddressBook;
dc:title "New address Book";
vcard:nameEmailIndex <people.ttl>;
vcard:groupIndex <groups.ttl>.
`
bookContents +=
'<#this> <http://www.w3.org/ns/auth/acl#owner> <' +
me.uri +
'>.\n\n'
const newAppInstance = kb.sym(newBase + 'index.ttl#this')
const toBeWritten = [
{
to: 'index.ttl',
content: bookContents,
contentType: 'text/turtle'
},
{ to: 'groups.ttl', content: '', contentType: 'text/turtle' },
{ to: 'people.ttl', content: '', contentType: 'text/turtle' },
{ to: '', existing: true, aclOptions: { defaultForNew: true } }
]
// @@ Ask user abut ACLs?
//
// @@ Add header to PUT If-None-Match: * to prevent overwrite
//
function claimSuccess (newAppInstance, appInstanceNoun) {
// @@ delete or grey other stuff
console.log(`New ${appInstanceNoun} created at ${newAppInstance}`)
const p = div.appendChild(dom.createElement('p'))
p.setAttribute('style', 'font-size: 140%;')
p.innerHTML =
"Your <a href='" +
newAppInstance.uri +
"'><b>new " +
appInstanceNoun +
'</b></a> is ready. ' +
"<br/><br/><a href='" +
newAppInstance.uri +
"'>Go to new " +
appInstanceNoun +
'</a>'
const newContext = Object.assign(
{ newInstance: newAppInstance },
context
)
resolve(newContext)
}
function doNextTask () {
function checkOKSetACL (uri, ok) {
if (!ok) {
complain('Error writing new file ' + task.to)
return reject(new Error('Error writing new file ' + task.to))
}
setACLUserPublic(dest, me, aclOptions)
.then(() => doNextTask())
.catch(err => {
const message =
'Error setting access permissions for ' +
task.to +
' : ' +
err.message
complain(message)
return reject(new Error(message))
})
}
if (toBeWritten.length === 0) {
claimSuccess(newAppInstance, appInstanceNoun)
} else {
var task = toBeWritten.shift() /* eslint-disable-line no-var */
console.log('Creating new file ' + task.to + ' in new instance ')
var dest = $rdf.uri.join(task.to, newBase) /* eslint-disable-line no-var */
var aclOptions = task.aclOptions || {} /* eslint-disable-line no-var */
if ('content' in task) {
kb.fetcher
.webOperation('PUT', dest, {
data: task.content,
saveMetadata: true,
contentType: task.contentType
})
.then(() => checkOKSetACL(dest, true))
} else if ('existing' in task) {
checkOKSetACL(dest, true)
} else {
reject(new Error('copy not expected buiding new app!!'))
// const from = task.from || task.to // default source to be same as dest
// UI.widgets.webCopy(base + from, dest, task.contentType, checkOKSetACL)
}
}
}
doNextTask()
},
err => {
// log in then
context.div.appendChild(UI.widgets.errorMessageBlock(err))
}
)
})
}