1111use Psr \Http \Message \ResponseInterface as Response ;
1212use Psr \Http \Message \ServerRequestInterface as Request ;
1313use Throwable ;
14- use WebSocket \Client ;
1514use pietercolpaert \hardf \TriGWriter ;
1615use pietercolpaert \hardf \TriGParser ;
1716
@@ -55,8 +54,8 @@ class Server
5554 private $ filesystem ;
5655 /** @var Graph */
5756 private $ graph ;
58- /** @var string */
59- private $ pubsub ;
57+ /** @var SolidNotificationInterface */
58+ private $ notifications ;
6059 /** @var Response */
6160 private $ response ;
6261
@@ -85,19 +84,17 @@ final public function setBaseUrl($url)
8584 $ this ->basePath = $ serverRequest ->getUri ()->getPath ();
8685 }
8786
88- final public function setPubSubUrl ( $ url )
87+ final public function setNotifications ( SolidNotificationInterface $ notifications )
8988 {
90- $ this ->pubsub = $ url ;
89+ $ this ->notifications = $ notifications ;
9190 }
92-
9391 //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
9492
9593 // @TODO: The Graph should be injected by the caller
9694 final public function __construct (Filesystem $ filesystem , Response $ response , Graph $ graph = null )
9795 {
9896 $ this ->basePath = '' ;
9997 $ this ->baseUrl = '' ;
100- $ this ->pubsub = '' ;
10198 $ this ->filesystem = $ filesystem ;
10299 $ this ->graph = $ graph ?? new Graph ();
103100 $ this ->response = $ response ;
@@ -152,17 +149,6 @@ private function handle(string $method, string $path, $contents, $request): Resp
152149 // Lets assume the worst...
153150 $ response = $ response ->withStatus (500 );
154151
155- // Set Accept, Allow, and CORS headers
156- // $response = $response
157- // ->withHeader('Access-Control-Allow-Origin', '*')
158- // ->withHeader('Access-Control-Allow-Credentials','true')
159- // ->withHeader('Access-Control-Allow-Headers', '*, authorization, accept, content-type')
160- // @FIXME: Add correct headers to resources (for instance allow DELETE on a GET resource)
161- // ->withAddedHeader('Accept-Patch', 'text/ldpatch')
162- // ->withAddedHeader('Accept-Post', 'text/turtle, application/ld+json, image/bmp, image/jpeg')
163- // ->withHeader('Allow', 'GET, HEAD, OPTIONS, PATCH, POST, PUT')
164- //;
165-
166152 switch ($ method ) {
167153 case 'DELETE ' :
168154 $ response = $ this ->handleDeleteRequest ($ response , $ path , $ contents );
@@ -175,9 +161,6 @@ private function handle(string $method, string $path, $contents, $request): Resp
175161 $ response ->getBody ()->rewind ();
176162 $ response ->getBody ()->write ('' );
177163 $ response = $ response ->withStatus (204 ); // CHECKME: nextcloud will remove the updates-via header - any objections to give the 'HEAD' request a 'no content' response type?
178- if ($ this ->pubsub ) {
179- $ response = $ response ->withHeader ("updates-via " , $ this ->pubsub );
180- }
181164 }
182165 break ;
183166
@@ -350,7 +333,7 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
350333
351334 if ($ success ) {
352335 $ this ->removeLinkFromMetaFileFor ($ path );
353- $ this ->sendWebsocketUpdate ($ path );
336+ $ this ->sendNotificationUpdate ($ path, " Update " );
354337 }
355338 } catch (RdfException $ exception ) {
356339 $ response ->getBody ()->write (self ::ERROR_CAN_NOT_PARSE_FOR_PATCH );
@@ -501,7 +484,7 @@ private function handleN3Update(Response $response, string $path, $contents): Re
501484
502485 if ($ success ) {
503486 $ this ->removeLinkFromMetaFileFor ($ path );
504- $ this ->sendWebsocketUpdate ($ path );
487+ $ this ->sendNotificationUpdate ($ path, " Update " );
505488 }
506489 } catch (RdfException $ exception ) {
507490 $ response ->getBody ()->write (self ::ERROR_CAN_NOT_PARSE_FOR_PATCH );
@@ -551,7 +534,7 @@ private function handleCreateRequest(Response $response, string $path, $contents
551534 $ this ->removeLinkFromMetaFileFor ($ path );
552535 $ response = $ response ->withHeader ("Location " , $ this ->baseUrl . $ path );
553536 $ response = $ response ->withStatus (201 );
554- $ this ->sendWebsocketUpdate ($ path );
537+ $ this ->sendNotificationUpdate ($ path, " Create " );
555538 } else {
556539 $ response = $ response ->withStatus (500 );
557540 }
@@ -585,39 +568,21 @@ private function handleCreateDirectoryRequest(Response $response, string $path):
585568 $ response = $ response ->withStatus ($ success ? 201 : 500 );
586569 if ($ success ) {
587570 $ this ->removeLinkFromMetaFileFor ($ path );
588- $ this ->sendWebsocketUpdate ($ path );
571+ $ this ->sendNotificationUpdate ($ path, " Create " );
589572 }
590573 }
591574
592575 return $ response ;
593576 }
594577
595- private function sendWebsocketUpdate ($ path )
578+ private function sendNotificationUpdate ($ path, $ type )
596579 {
597- $ pubsub = $ this ->pubsub ;
598- if (!$ pubsub ) {
599- return ; // no pubsub server available, don't even try;
600- }
601-
602- $ pubsub = str_replace (["https:// " , "http:// " ], "ws:// " , $ pubsub );
603-
604580 $ baseUrl = $ this ->baseUrl ;
581+ $ this ->notifications ->send ($ baseUrl . $ path , $ type );
605582
606- $ client = new Client ($ pubsub , array (
607- 'headers ' => array (
608- 'Sec-WebSocket-Protocol ' => 'solid-0.1 '
609- )
610- ));
611-
612- try {
613- $ client ->send ("pub $ baseUrl$ path \n" );
614-
615- while ($ path !== "/ " ) {
616- $ path = $ this ->parentPath ($ path );
617- $ client ->send ("pub $ baseUrl$ path \n" );
618- }
619- } catch (\WebSocket \Exception $ exception ) {
620- throw new Exception ('Could not write to pub-sup server ' , 502 , $ exception );
583+ while ($ path !== "/ " ) {
584+ $ path = $ this ->parentPath ($ path );
585+ $ this ->notifications ->send ($ baseUrl . $ path , "Update " ); // checkme: delete on a directory triggers update notifications on parents
621586 }
622587 }
623588
@@ -637,15 +602,15 @@ private function handleDeleteRequest(Response $response, string $path, $contents
637602 } else {
638603 $ success = $ filesystem ->deleteDir ($ path );
639604 if ($ success ) {
640- $ this ->sendWebsocketUpdate ($ path );
605+ $ this ->sendNotificationUpdate ($ path, " Delete " );
641606 }
642607
643608 $ status = $ success ? 204 : 500 ;
644609 }
645610 } else {
646611 $ success = $ filesystem ->delete ($ path );
647612 if ($ success ) {
648- $ this ->sendWebsocketUpdate ($ path );
613+ $ this ->sendNotificationUpdate ($ path, " Delete " );
649614 }
650615 $ status = $ success ? 204 : 500 ;
651616 }
@@ -673,7 +638,7 @@ private function handleUpdateRequest(Response $response, string $path, string $c
673638 $ response = $ response ->withStatus ($ success ? 201 : 500 );
674639 if ($ success ) {
675640 $ this ->removeLinkFromMetaFileFor ($ path );
676- $ this ->sendWebsocketUpdate ($ path );
641+ $ this ->sendNotificationUpdate ($ path, " Update " );
677642 }
678643 }
679644
0 commit comments