1
1
import { DataFactory , foaf , IriString , RDFSerializer , schema } from '@openhps/rdf' ;
2
2
import { LocalRDFNode } from '../models/ldht/LocalRDFNode' ;
3
- import { LDHTAddNodeAction , LDHTPingAction , LDHTRemoveNodeAction , LDHTStoreValueAction } from '../models/ldht' ;
3
+ import { LDHTAction , LDHTAddNodeAction , LDHTPingAction , LDHTRemoveNodeAction , LDHTStoreValueAction } from '../models/ldht' ;
4
4
5
5
import { Activity , Collection , DatasetSubscription , SolidClientService } from '@openhps/solid' ;
6
6
import { DHTMemoryNetwork } from './DHTMemoryNetwork' ;
7
7
import { RemoteRDFNode } from '../models/ldht/RemoteRDFNode' ;
8
- import { Model } from '@openhps/core' ;
8
+ import { createChangeLog , Model } from '@openhps/core' ;
9
9
import { DHTService } from './DHTService' ;
10
10
11
11
/**
@@ -119,17 +119,18 @@ export class DHTRDFNetwork extends DHTMemoryNetwork {
119
119
const object = activity . object ;
120
120
if ( object ) {
121
121
// Handle actions
122
- this . service . logger ( 'debug' , `Received action: ${ object } ` ) ;
123
122
this . solidService . getDatasetStore ( this . solidService . session , object ) . then ( ( store ) => {
124
- const action = RDFSerializer . deserializeFromStore ( undefined , store ) ;
123
+ let action : LDHTAction = RDFSerializer . deserializeFromStore ( undefined , store ) ;
125
124
if ( action ) {
125
+ this . service . logger ( 'debug' , `Received (${ action . constructor . name } ) action: ${ object } ` ) ;
126
126
if ( action instanceof LDHTAddNodeAction ) {
127
127
// Add a node
128
128
// Fetch the remote node first
129
129
this . fetchRemoteNode ( action . object ) . then ( ( node ) => {
130
130
return this . addNode ( node ) ;
131
131
} ) . then ( ( ) => {
132
132
// Set action completed
133
+ action = createChangeLog ( action ) ;
133
134
action . actionStatus = schema . CompletedActionStatus ;
134
135
const changes = RDFSerializer . serializeToChangeLog ( action ) ;
135
136
store . additions = changes . additions ;
@@ -146,20 +147,28 @@ export class DHTRDFNetwork extends DHTMemoryNetwork {
146
147
return this . removeNode ( node ) ;
147
148
} ) . then ( ( ) => {
148
149
// Set action completed
150
+ action = createChangeLog ( action ) ;
149
151
action . actionStatus = schema . CompletedActionStatus ;
150
- // const store = RDFSerializer.serializeToStore(action);
151
- // return this.solidService.saveDataset(this.solidService.session, object, store);
152
+ const changes = RDFSerializer . serializeToChangeLog ( action ) ;
153
+ store . additions = changes . additions ;
154
+ store . deletions = changes . deletions ;
155
+ this . service . logger ( 'debug' , `Updating action status to completed for: ${ object } ` ) ;
156
+ return this . solidService . saveDataset ( this . solidService . session , object , store ) ;
152
157
} ) . catch ( ( err ) => {
153
158
this . service . logger ( 'error' , `Failed to fetch remote node: ${ err . message } ` ) ;
154
159
} ) ;
155
160
} else if ( action instanceof LDHTStoreValueAction ) {
156
161
// Store a value
157
162
this . storeValue ( action . object . identifier , action . object . value )
158
163
. then ( ( ) => {
159
- // Set action completed
160
- action . actionStatus = schema . CompletedActionStatus ;
161
- const store = RDFSerializer . serializeToStore ( action ) ;
162
- return this . solidService . saveDataset ( this . solidService . session , object , store ) ;
164
+ // Set action completed
165
+ action = createChangeLog ( action ) ;
166
+ action . actionStatus = schema . CompletedActionStatus ;
167
+ const changes = RDFSerializer . serializeToChangeLog ( action ) ;
168
+ store . additions = changes . additions ;
169
+ store . deletions = changes . deletions ;
170
+ this . service . logger ( 'debug' , `Updating action status to completed for: ${ object } ` ) ;
171
+ return this . solidService . saveDataset ( this . solidService . session , object , store ) ;
163
172
} ) . catch ( ( err ) => {
164
173
this . service . logger ( 'error' , `Failed to store value: ${ err . message } ` ) ;
165
174
} ) ;
@@ -300,23 +309,43 @@ export class DHTRDFNetwork extends DHTMemoryNetwork {
300
309
foaf . Agent ,
301
310
this . solidService . session ,
302
311
) ;
312
+ } ) . then ( ( ) => {
313
+ // Create the nodes and data store
314
+ this . service . logger ( 'debug' , `Creating nodes store at ${ nodesUri } ` ) ;
315
+ this . service . logger ( 'debug' , `Creating data store at ${ dataUri } ` ) ;
316
+ return Promise . all ( [
317
+ this . solidService . saveDataset ( this . solidService . session , nodesUri , this . solidService . createDataset ( ) ) ,
318
+ this . solidService . saveDataset ( this . solidService . session , dataUri , this . solidService . createDataset ( ) )
319
+ ] ) ;
320
+ } ) . then ( ( ) => {
321
+ this . service . logger ( 'debug' , `Setting access rights for ${ nodesUri } ` ) ;
322
+ this . service . logger ( 'debug' , `Setting access rights for ${ dataUri } ` ) ;
323
+ return Promise . all ( [
324
+ this . solidService . setAccess (
325
+ nodesUri ,
326
+ {
327
+ read : true ,
328
+ public : true ,
329
+ group : true
330
+ } ,
331
+ foaf . Agent ,
332
+ this . solidService . session ,
333
+ ) , this . solidService . setAccess (
334
+ dataUri ,
335
+ {
336
+ read : true ,
337
+ public : true ,
338
+ group : true
339
+ } ,
340
+ foaf . Agent ,
341
+ this . solidService . session ,
342
+ )
343
+ ] ) ;
303
344
} ) . then ( ( ) => {
304
345
resolve ( node ) ;
305
346
} ) . catch ( err => {
306
347
reject ( err ) ;
307
348
} ) ;
308
349
} ) ;
309
350
}
310
-
311
- remoteStore ( uri : IriString , key : number , values : string [ ] ) : Promise < void > {
312
- return new Promise ( ( resolve , reject ) => {
313
- this . solidService
314
- . getDatasetStore ( this . solidService . session , uri )
315
- . then ( ( store ) => {
316
- console . log ( store , key , values ) ;
317
- resolve ( ) ;
318
- } )
319
- . catch ( reject ) ;
320
- } ) ;
321
- }
322
351
}
0 commit comments