-
Notifications
You must be signed in to change notification settings - Fork 5
/
rdflib-pg.html
221 lines (206 loc) · 9.23 KB
/
rdflib-pg.html
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
<!DOCTYPE html>
<html>
<head>
<title>RDFLIB Playground</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body class="container">
<div>
<h1>RDFLIB Playground</h1>
</div>
<!--<div>
<h3>Enter Raw VC Graph</h3>
<form id="vcform">
<textarea id="vc" rows=25 cols=100 form="vcform"></textarea>
<input type="submit" value="Parse VC" id="parse-vc">
</form>
</div>-->
<div>
<h3>Load Inbox From WebId</h3>
<input type="text" placeholder="eg.: https://SUBJECT.solid.community/profile/card#me" size=50 id="subject-id"><br>
<input type="submit" value="Load Inbox" id="load-inbox">
</div>
</body>
<!--<script src="./app/dist/app_test.bundle.js" charset="utf-8"></script>-->
<!--<script src="https://solid.github.io/solid-auth-client/dist/solid-auth-client.bundle.js"></script>-->
<script src="./node_modules/solid-auth-client/dist-lib/solid-auth-client.bundle.js"></script>
<script src="./node_modules/rdflib/dist/rdflib.min.js"></script>
<script>
var idp = 'https://kezike.solid.community/';
var webId = idp + 'profile/card#me';
var pub = idp + 'public/';
var inbox = idp + 'inbox/';
var me = $rdf.sym(webId);
var popupURI = 'popup.html';
var fetcher = $rdf.fetcher($rdf.graph());
var updateManager = new $rdf.UpdateManager(fetcher.store);
var FOAF = $rdf.Namespace('http://xmlns.com/foaf/0.1/');
var SVC = $rdf.Namespace('http://dig.csail.mit.edu/2018/svc#');
var SEC = $rdf.Namespace('https://w3id.org/security#');
var LDP = $rdf.Namespace('http://www.w3.org/ns/ldp#');
var RdflibPg = RdflibPg || {};
RdflibPg = {
loginOptions: {
username: 'kezike',
password: ''
},
getOptions: {
method: 'GET',
mode: 'cors',
credentials: 'include',
clearPreviousData: true
},
postOptions: {
method: 'POST',
headers: {
'content-type': 'text/n3'
},
mode: 'cors',
credentials: 'include',
body: "@prefix : <#>.\n@prefix c: <https://kezike17/solidtest.space/profile/card#>.\n@prefix n0: <http://xmlns.com/foaf/0.1/>.\n@prefix c0: <https://www.w3.org/People/Berners-Lee/card#>.\nc:me n0:knows c0:i."
},
patchOptions: {
method: 'PATCH',
headers: {
'content-type': 'text/n3'
},
mode: 'cors',
credentials: 'include',
deletions: [],
insertions: []
},
// Initialize app
init: function() {
RdflibPg.bindEvents();
RdflibPg.login();
},
// Login helper function
loginHelper: function(session) {
RdflibPg.session = session;
console.log("currentSession:", RdflibPg.session);
RdflibPg.fetcher = $rdf.fetcher($rdf.graph());
console.log("RdflibPg.fetcher:", RdflibPg.fetcher);
RdflibPg.updater = new $rdf.UpdateManager(RdflibPg.fetcher.store);
},
// Login to app
login: function() {
solid.auth.currentSession().then((currentSession) => {
if (!currentSession) {
solid.auth.popupLogin({popupUri: popupURI}).then((popupSession) => {
RdflibPg.loginHelper(popupSession);
}).catch((err) => {
console.error(err.name + ": " + err.message);
});
return;
}
RdflibPg.loginHelper(currentSession);
}).catch((err) => {
console.error(err.name + ": " + err.message);
});
},
// Bind events
bindEvents: function() {
// $(document).on('click', '#load-inbox', RdflibPg.loadInbox);
},
load: function(uri, options) {
fetcher.load(uri, options).then((response) => {
var store = fetcher.store;
var statements = store.statements;
var statementsMin = [];
console.log(statements);
console.log(response);
});
},
patch: function(uri, options) {
fetcher.load(uri, options).then((response) => {
updateManager.update(options.deletions, options.insertions, (uri, success, error) => {
console.log('uri:', uri);
console.log('success:', success);
console.log('error:', error);
});
});
},
createContainer: function(parentURI, folderName, data) {
fetcher.createContainer(parentURI, folderName, data);
},
// Discover the inbox of a target via LDN
discoverInbox: async function(target) {
var inboxPromise = new Promise((resolve, reject) => {
RdflibPg.fetcher.load(target, RdflibPg.getOptions).then((resp) => {
var inbox = RdflibPg.fetcher.store.any($rdf.sym(target), LDP('inbox'), undefined);
resolve(inbox);
}).catch((err) => {
console.error(err.name + ": " + err.message);
resolve(null);
});
});
var inboxResult = await inboxPromise;
return inboxResult;
},
// Load content of inbox
loadInbox: async function(inbox) {
var inboxPromise = new Promise((resolve, reject) => {
RdflibPg.fetcher.load(inbox, RdflibPg.getOptions).then((resp) => {
// resolve(resp);
resolve(resp['responseText']);
// var inboxContent = RdflibPg.fetcher.store.any($rdf.sym(inbox), LDP('contains'), undefined);
// resolve(inboxContent);
}).catch((err) => {
console.error(err.name + ": " + err.message);
resolve(null);
});
});
var inboxResult = await inboxPromise;
return inboxResult;
},
parseCredential: function(event) {
event.preventDefault();
var vc = $("#vc").val();
var store = $rdf.graph();
var base = me.value;
var type = "text/n3";
// var prefixPattern = /(@prefix (\w|\d)*: <.*>\.(\r|\n)*)*/;
// var prefixResult = prefixPattern.exec(vc);
// console.log("prefixes:\n", prefixResult);
// var prefixes = prefixResult[0];
// var body = vc.split(prefixes)[1];
// console.log("prefixes:\n" + prefixes);
// console.log("body:\n" + body);
$rdf.parse(vc, store, base, type, (errParse, resParse) => {
if (errParse) {
// console.error(errParse.name + ": " + errParse.message);
console.log("errParse:\n", errParse);
return;
}
console.log("resParse:\n", resParse);
$rdf.serialize(null, resParse, base, type, /*null*/ (errSer, resSer) => {
if (errSer) {
console.error(errSer.name + ": " + errSer.message);
return;
}
console.log("resSer:\n", resSer);
}, {});
});
}
}
async function main() {
// Main Program Setup
RdflibPg.init();
// LDN Test
var inbox = await RdflibPg.discoverInbox(webId);
// var inboxContent = await RdflibPg.loadInbox(inbox);
console.log("Inbox:\n" + inbox);
// console.log("Inbox Content:\n" + inboxContent);
// Patch Test
var me = $rdf.sym('https://kezike.solid.community/profile/card#me');
var doc = me.doc();
patchOptions.insertions.push($rdf.st(me, SEC('publicKey'), $rdf.Literal.fromValue('https://kezike.solid.community/public/keys/0516d000-1532-11e9-a29e-5d8e3e616ac9.txt'), doc));
RdflibPg.patch('https://kezike.solid.community/profile/card', patchOptions);
// Create Container Test
// RdflibPg.createContainer('https://kezike17.solidtest.space/public', 'dweep', "@prefix : <#>.\n@prefix c: <https://kezike17/solidtest.space/profile/card#>.\n@prefix n0: <http://xmlns.com/foaf/0.1/>.\n@prefix c0: <https://www.w3.org/People/Berners-Lee/card#>.\nc:me n0:knows c0:i.");
// Load Test
// RdflibPg.load(pub, getOptions);
}
main();
</script>
</html>