]> git.mxchange.org Git - friendica.git/commitdiff
Compact using a predefined context to fix content from Pleroma
authorMichael <heluecht@pirati.ca>
Sun, 16 May 2021 19:31:10 +0000 (19:31 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 16 May 2021 19:31:10 +0000 (19:31 +0000)
src/Util/JsonLD.php

index 7692cefc2f38080dfef20bde809cd0224225663a..ee5e0aea97cbb39666273f4b2f40e0638e4e21f3 100644 (file)
@@ -26,6 +26,7 @@ use Friendica\Core\Logger;
 use Exception;
 use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Protocol\ActivityPub;
 
 /**
  * This class contain methods to work with JsonLD data
@@ -161,6 +162,26 @@ class JsonLD
         * @throws Exception
         */
        public static function compact($json)
+       {
+               $context = $json['@context'] ?? [];
+               $json['@context'] = ActivityPub::CONTEXT;
+
+               $compacted = self::internalCompact($json);
+               if (empty($compacted)) {
+                       Logger::info('Failed to compact with our context');
+                       $json['@context'] = $context;
+                       $compacted = self::internalCompact($json);
+                       if (empty($compacted)) {
+                               Logger::info('Failed to compact with original context');
+                       } else {
+                               Logger::info('Successful compacted with original context');
+                       }
+               }
+
+               return $compacted;
+       }
+
+       private static function internalCompact($json)
        {
                $json = self::fixContext($json);
 
@@ -192,9 +213,7 @@ class JsonLD
                }
                catch (Exception $e) {
                        $compacted = false;
-                       Logger::error('compacting error', ['callstack' => System::callstack(20)]);
-                       // Sooner or later we should log some details as well - but currently this leads to memory issues
-                       // Logger::log('compacting error:' . substr(print_r($e, true), 0, 10000), Logger::DEBUG);
+                       Logger::error('compacting error', ['line' => $e->getLine(), 'message' => $e->getMessage(),'callstack' => System::callstack(20)]);
                }
 
                $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);