aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2024-09-17 13:57:33 +0200
committerCôme Chilliet <come.chilliet@nextcloud.com>2024-09-17 13:57:33 +0200
commit70a886ce831322ce4f50f7d06f43beb670239fbe (patch)
tree62cb3276302c83e0ced11d1596c9eb736828ffb9 /lib
parentf8dde2d254ded9760cfa7a24178e1f1ca141ba41 (diff)
downloadnextcloud-server-70a886ce831322ce4f50f7d06f43beb670239fbe.tar.gz
nextcloud-server-70a886ce831322ce4f50f7d06f43beb670239fbe.zip
feat: Add OCP interface to format richtext into string
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php2
-rw-r--r--lib/composer/composer/autoload_static.php2
-rw-r--r--lib/private/Activity/Event.php40
-rw-r--r--lib/private/Notification/Notification.php30
-rw-r--r--lib/private/RichObjectStrings/RichTextFormatter.php36
-rw-r--r--lib/public/RichObjectStrings/IRichTextFormatter.php25
6 files changed, 76 insertions, 59 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 78aca5fe883..578395a45b4 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -646,6 +646,7 @@ return array(
'OCP\\Remote\\IInstanceFactory' => $baseDir . '/lib/public/Remote/IInstanceFactory.php',
'OCP\\Remote\\IUser' => $baseDir . '/lib/public/Remote/IUser.php',
'OCP\\RichObjectStrings\\Definitions' => $baseDir . '/lib/public/RichObjectStrings/Definitions.php',
+ 'OCP\\RichObjectStrings\\IRichTextFormatter' => $baseDir . '/lib/public/RichObjectStrings/IRichTextFormatter.php',
'OCP\\RichObjectStrings\\IValidator' => $baseDir . '/lib/public/RichObjectStrings/IValidator.php',
'OCP\\RichObjectStrings\\InvalidObjectExeption' => $baseDir . '/lib/public/RichObjectStrings/InvalidObjectExeption.php',
'OCP\\Route\\IRoute' => $baseDir . '/lib/public/Route/IRoute.php',
@@ -1839,6 +1840,7 @@ return array(
'OC\\Repair\\RepairInvalidShares' => $baseDir . '/lib/private/Repair/RepairInvalidShares.php',
'OC\\Repair\\RepairLogoDimension' => $baseDir . '/lib/private/Repair/RepairLogoDimension.php',
'OC\\Repair\\RepairMimeTypes' => $baseDir . '/lib/private/Repair/RepairMimeTypes.php',
+ 'OC\\RichObjectStrings\\RichTextFormatter' => $baseDir . '/lib/private/RichObjectStrings/RichTextFormatter.php',
'OC\\RichObjectStrings\\Validator' => $baseDir . '/lib/private/RichObjectStrings/Validator.php',
'OC\\Route\\CachingRouter' => $baseDir . '/lib/private/Route/CachingRouter.php',
'OC\\Route\\Route' => $baseDir . '/lib/private/Route/Route.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 6a17bc4ff31..2e507ec7a5f 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -679,6 +679,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Remote\\IInstanceFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/IInstanceFactory.php',
'OCP\\Remote\\IUser' => __DIR__ . '/../../..' . '/lib/public/Remote/IUser.php',
'OCP\\RichObjectStrings\\Definitions' => __DIR__ . '/../../..' . '/lib/public/RichObjectStrings/Definitions.php',
+ 'OCP\\RichObjectStrings\\IRichTextFormatter' => __DIR__ . '/../../..' . '/lib/public/RichObjectStrings/IRichTextFormatter.php',
'OCP\\RichObjectStrings\\IValidator' => __DIR__ . '/../../..' . '/lib/public/RichObjectStrings/IValidator.php',
'OCP\\RichObjectStrings\\InvalidObjectExeption' => __DIR__ . '/../../..' . '/lib/public/RichObjectStrings/InvalidObjectExeption.php',
'OCP\\Route\\IRoute' => __DIR__ . '/../../..' . '/lib/public/Route/IRoute.php',
@@ -1872,6 +1873,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Repair\\RepairInvalidShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairInvalidShares.php',
'OC\\Repair\\RepairLogoDimension' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairLogoDimension.php',
'OC\\Repair\\RepairMimeTypes' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairMimeTypes.php',
+ 'OC\\RichObjectStrings\\RichTextFormatter' => __DIR__ . '/../../..' . '/lib/private/RichObjectStrings/RichTextFormatter.php',
'OC\\RichObjectStrings\\Validator' => __DIR__ . '/../../..' . '/lib/private/RichObjectStrings/Validator.php',
'OC\\Route\\CachingRouter' => __DIR__ . '/../../..' . '/lib/private/Route/CachingRouter.php',
'OC\\Route\\Route' => __DIR__ . '/../../..' . '/lib/private/Route/Route.php',
diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php
index 02cfd758058..810af5c0612 100644
--- a/lib/private/Activity/Event.php
+++ b/lib/private/Activity/Event.php
@@ -12,6 +12,7 @@ namespace OC\Activity;
use OCP\Activity\Exceptions\InvalidValueException;
use OCP\Activity\IEvent;
use OCP\RichObjectStrings\InvalidObjectExeption;
+use OCP\RichObjectStrings\IRichTextFormatter;
use OCP\RichObjectStrings\IValidator;
class Event implements IEvent {
@@ -60,14 +61,11 @@ class Event implements IEvent {
/** @var IEvent|null */
protected $child;
- /** @var IValidator */
- protected $richValidator;
- /**
- * @param IValidator $richValidator
- */
- public function __construct(IValidator $richValidator) {
- $this->richValidator = $richValidator;
+ public function __construct(
+ protected IValidator $richValidator,
+ protected IRichTextFormatter $richTextFormatter,
+ ) {
}
/**
@@ -217,7 +215,7 @@ class Event implements IEvent {
if ($this->subjectParsed === '') {
try {
- $this->subjectParsed = $this->richToParsed($subject, $parameters);
+ $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters);
} catch (\InvalidArgumentException $e) {
throw new InvalidValueException('richSubjectParameters', $e);
}
@@ -227,30 +225,6 @@ class Event implements IEvent {
}
/**
- * @throws \InvalidArgumentException if a parameter has no name or no type
- */
- private function richToParsed(string $message, array $parameters): string {
- $placeholders = [];
- $replacements = [];
- foreach ($parameters as $placeholder => $parameter) {
- $placeholders[] = '{' . $placeholder . '}';
- foreach (['name','type'] as $requiredField) {
- if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
- throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
- }
- }
- if ($parameter['type'] === 'user') {
- $replacements[] = '@' . $parameter['name'];
- } elseif ($parameter['type'] === 'file') {
- $replacements[] = $parameter['path'] ?? $parameter['name'];
- } else {
- $replacements[] = $parameter['name'];
- }
- }
- return str_replace($placeholders, $replacements, $message);
- }
-
- /**
* @return string
* @since 11.0.0
*/
@@ -317,7 +291,7 @@ class Event implements IEvent {
if ($this->messageParsed === '') {
try {
- $this->messageParsed = $this->richToParsed($message, $parameters);
+ $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters);
} catch (\InvalidArgumentException $e) {
throw new InvalidValueException('richMessageParameters', $e);
}
diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php
index 19c836c2044..1c624b50d44 100644
--- a/lib/private/Notification/Notification.php
+++ b/lib/private/Notification/Notification.php
@@ -12,6 +12,7 @@ use OCP\Notification\IAction;
use OCP\Notification\INotification;
use OCP\Notification\InvalidValueException;
use OCP\RichObjectStrings\InvalidObjectExeption;
+use OCP\RichObjectStrings\IRichTextFormatter;
use OCP\RichObjectStrings\IValidator;
class Notification implements INotification {
@@ -39,6 +40,7 @@ class Notification implements INotification {
public function __construct(
protected IValidator $richValidator,
+ protected IRichTextFormatter $richTextFormatter,
) {
$this->dateTime = new \DateTime();
$this->dateTime->setTimestamp(0);
@@ -187,7 +189,7 @@ class Notification implements INotification {
if ($this->subjectParsed === '') {
try {
- $this->subjectParsed = $this->richToParsed($subject, $parameters);
+ $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters);
} catch (\InvalidArgumentException $e) {
throw new InvalidValueException('richSubjectParameters', $e);
}
@@ -197,30 +199,6 @@ class Notification implements INotification {
}
/**
- * @throws \InvalidArgumentException if a parameter has no name or no type
- */
- private function richToParsed(string $message, array $parameters): string {
- $placeholders = [];
- $replacements = [];
- foreach ($parameters as $placeholder => $parameter) {
- $placeholders[] = '{' . $placeholder . '}';
- foreach (['name','type'] as $requiredField) {
- if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
- throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
- }
- }
- if ($parameter['type'] === 'user') {
- $replacements[] = '@' . $parameter['name'];
- } elseif ($parameter['type'] === 'file') {
- $replacements[] = $parameter['path'] ?? $parameter['name'];
- } else {
- $replacements[] = $parameter['name'];
- }
- }
- return str_replace($placeholders, $replacements, $message);
- }
-
- /**
* {@inheritDoc}
*/
public function getRichSubject(): string {
@@ -293,7 +271,7 @@ class Notification implements INotification {
if ($this->messageParsed === '') {
try {
- $this->messageParsed = $this->richToParsed($message, $parameters);
+ $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters);
} catch (\InvalidArgumentException $e) {
throw new InvalidValueException('richMessageParameters', $e);
}
diff --git a/lib/private/RichObjectStrings/RichTextFormatter.php b/lib/private/RichObjectStrings/RichTextFormatter.php
new file mode 100644
index 00000000000..9c9ddf94fa9
--- /dev/null
+++ b/lib/private/RichObjectStrings/RichTextFormatter.php
@@ -0,0 +1,36 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\RichObjectStrings;
+
+use OCP\RichObjectStrings\IRichTextFormatter;
+
+class RichTextFormatter implements IRichTextFormatter {
+ /**
+ * @throws \InvalidArgumentException if a parameter has no name or no type
+ */
+ public function richToParsed(string $message, array $parameters): string {
+ $placeholders = [];
+ $replacements = [];
+ foreach ($parameters as $placeholder => $parameter) {
+ $placeholders[] = '{' . $placeholder . '}';
+ foreach (['name','type'] as $requiredField) {
+ if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
+ throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
+ }
+ }
+ $replacements[] = match($parameter['type']) {
+ 'user' => '@' . $parameter['name'],
+ 'file' => $parameter['path'] ?? $parameter['name'],
+ default => $parameter['name'],
+ };
+ }
+ return str_replace($placeholders, $replacements, $message);
+ }
+}
diff --git a/lib/public/RichObjectStrings/IRichTextFormatter.php b/lib/public/RichObjectStrings/IRichTextFormatter.php
new file mode 100644
index 00000000000..1d72f480b07
--- /dev/null
+++ b/lib/public/RichObjectStrings/IRichTextFormatter.php
@@ -0,0 +1,25 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\RichObjectStrings;
+
+/**
+ * Parse rich text and format it with the richobjects
+ *
+ * @since 31.0.0
+ */
+interface IRichTextFormatter {
+ /**
+ * @since 31.0.0
+ * @param string $message
+ * @param array<string,array<string,string>> $parameters
+ * @throws \InvalidArgumentException if a parameter has no name or no type
+ */
+ public function richToParsed(string $message, array $parameters): string;
+}