aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2024-05-30 14:32:26 +0200
committerCôme Chilliet <91878298+come-nc@users.noreply.github.com>2024-06-11 14:10:29 +0200
commit734aad8934cdbe072c7c880482e5caf88b61400b (patch)
tree74eeccb411e9903945e0706fc52f258100dc4db6 /lib
parentc3d4d2aad1539002e9ce31ad35e6df4801d93197 (diff)
downloadnextcloud-server-734aad8934cdbe072c7c880482e5caf88b61400b.tar.gz
nextcloud-server-734aad8934cdbe072c7c880482e5caf88b61400b.zip
feat: Make node events serializable
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/public/EventDispatcher/JsonSerializer.php42
-rw-r--r--lib/public/Files/Events/Node/AbstractNodeEvent.php13
-rw-r--r--lib/public/Files/Events/Node/AbstractNodesEvent.php12
5 files changed, 68 insertions, 1 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 8e1408e121e..0fe1314644f 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -281,6 +281,7 @@ return array(
'OCP\\EventDispatcher\\GenericEvent' => $baseDir . '/lib/public/EventDispatcher/GenericEvent.php',
'OCP\\EventDispatcher\\IEventDispatcher' => $baseDir . '/lib/public/EventDispatcher/IEventDispatcher.php',
'OCP\\EventDispatcher\\IEventListener' => $baseDir . '/lib/public/EventDispatcher/IEventListener.php',
+ 'OCP\\EventDispatcher\\JsonSerializer' => $baseDir . '/lib/public/EventDispatcher/JsonSerializer.php',
'OCP\\Exceptions\\AbortedEventException' => $baseDir . '/lib/public/Exceptions/AbortedEventException.php',
'OCP\\Exceptions\\AppConfigException' => $baseDir . '/lib/public/Exceptions/AppConfigException.php',
'OCP\\Exceptions\\AppConfigIncorrectTypeException' => $baseDir . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index d6939ae36ce..cf52b9026f1 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -322,6 +322,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\EventDispatcher\\GenericEvent' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/GenericEvent.php',
'OCP\\EventDispatcher\\IEventDispatcher' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventDispatcher.php',
'OCP\\EventDispatcher\\IEventListener' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventListener.php',
+ 'OCP\\EventDispatcher\\JsonSerializer' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/JsonSerializer.php',
'OCP\\Exceptions\\AbortedEventException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AbortedEventException.php',
'OCP\\Exceptions\\AppConfigException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigException.php',
'OCP\\Exceptions\\AppConfigIncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php',
diff --git a/lib/public/EventDispatcher/JsonSerializer.php b/lib/public/EventDispatcher/JsonSerializer.php
new file mode 100644
index 00000000000..1eb75ed7527
--- /dev/null
+++ b/lib/public/EventDispatcher/JsonSerializer.php
@@ -0,0 +1,42 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\EventDispatcher;
+
+use OCP\Files\FileInfo;
+use OCP\IUser;
+
+/**
+ * Contains helper static methods to serialize OCP classes into arrays to pass json_encode.
+ * Useful for events implementing \JsonSerializable.
+ *
+ * @since 30.0.0
+ */
+
+final class JsonSerializer {
+ /**
+ * @since 30.0.0
+ */
+ public static function serializeFileInfo(FileInfo $node): array {
+ return [
+ 'id' => $node->getId(),
+ 'path' => $node->getPath(),
+ ];
+ }
+
+ /**
+ * @since 30.0.0
+ */
+ public static function serializeUser(IUser $user): array {
+ return [
+ 'uid' => $user->getUID(),
+ 'displayName' => $user->getDisplayName(),
+ ];
+ }
+}
diff --git a/lib/public/Files/Events/Node/AbstractNodeEvent.php b/lib/public/Files/Events/Node/AbstractNodeEvent.php
index 1b290578ab9..cc27bdd8cfd 100644
--- a/lib/public/Files/Events/Node/AbstractNodeEvent.php
+++ b/lib/public/Files/Events/Node/AbstractNodeEvent.php
@@ -9,12 +9,13 @@ declare(strict_types=1);
namespace OCP\Files\Events\Node;
use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\JsonSerializer;
use OCP\Files\Node;
/**
* @since 20.0.0
*/
-abstract class AbstractNodeEvent extends Event {
+abstract class AbstractNodeEvent extends Event implements \JsonSerializable {
/**
* @since 20.0.0
*/
@@ -29,4 +30,14 @@ abstract class AbstractNodeEvent extends Event {
public function getNode(): Node {
return $this->node;
}
+
+ /**
+ * @since 30.0.0
+ */
+ public function jsonSerialize(): array {
+ return [
+ 'class' => static::class,
+ 'node' => JsonSerializer::serializeFileInfo($this->node),
+ ];
+ }
}
diff --git a/lib/public/Files/Events/Node/AbstractNodesEvent.php b/lib/public/Files/Events/Node/AbstractNodesEvent.php
index a5b058f18f5..b5fd1b75898 100644
--- a/lib/public/Files/Events/Node/AbstractNodesEvent.php
+++ b/lib/public/Files/Events/Node/AbstractNodesEvent.php
@@ -9,6 +9,7 @@ declare(strict_types=1);
namespace OCP\Files\Events\Node;
use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\JsonSerializer;
use OCP\Files\Node;
/**
@@ -37,4 +38,15 @@ abstract class AbstractNodesEvent extends Event {
public function getTarget(): Node {
return $this->target;
}
+
+ /**
+ * @since 30.0.0
+ */
+ public function jsonSerialize(): array {
+ return [
+ 'class' => static::class,
+ 'source' => JsonSerializer::serializeFileInfo($this->source),
+ 'target' => JsonSerializer::serializeFileInfo($this->target),
+ ];
+ }
}