diff options
author | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2022-12-19 10:46:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-19 10:46:39 +0100 |
commit | cbbb0712db0f33a41ef66ae59d5c70ba1e23748f (patch) | |
tree | db09b311f4a6a62451dd25db25ae9f8e80a3064c /lib | |
parent | 884da19198d15a764487e6a6d615968c0e63203d (diff) | |
parent | e553cd1bb9cec6bf6200298bcca6637e81163953 (diff) | |
download | nextcloud-server-cbbb0712db0f33a41ef66ae59d5c70ba1e23748f.tar.gz nextcloud-server-cbbb0712db0f33a41ef66ae59d5c70ba1e23748f.zip |
Merge pull request #34807 from nextcloud/fix/compute-notification-parsed-subject
Compute notification parsed subject from rich subject when possible
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Activity/Event.php | 33 | ||||
-rw-r--r-- | lib/private/Notification/Notification.php | 33 | ||||
-rw-r--r-- | lib/public/Activity/IEvent.php | 12 | ||||
-rw-r--r-- | lib/public/Notification/INotification.php | 12 |
4 files changed, 68 insertions, 22 deletions
diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php index fd0c0afd9d8..cc1135cf179 100644 --- a/lib/private/Activity/Event.php +++ b/lib/private/Activity/Event.php @@ -33,7 +33,6 @@ use OCP\RichObjectStrings\InvalidObjectExeption; use OCP\RichObjectStrings\IValidator; class Event implements IEvent { - /** @var string */ protected $app = ''; /** @var string */ @@ -269,10 +268,38 @@ class Event implements IEvent { $this->subjectRich = $subject; $this->subjectRichParameters = $parameters; + if ($this->subjectParsed === '') { + $this->subjectParsed = $this->richToParsed($subject, $parameters); + } + return $this; } /** + * @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 */ @@ -350,6 +377,10 @@ class Event implements IEvent { $this->messageRich = $message; $this->messageRichParameters = $parameters; + if ($this->messageParsed === '') { + $this->messageParsed = $this->richToParsed($message, $parameters); + } + return $this; } diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php index 3e5cf1d6934..2291c4ae34f 100644 --- a/lib/private/Notification/Notification.php +++ b/lib/private/Notification/Notification.php @@ -32,7 +32,6 @@ use OCP\RichObjectStrings\InvalidObjectExeption; use OCP\RichObjectStrings\IValidator; class Notification implements INotification { - /** @var IValidator */ protected $richValidator; @@ -296,10 +295,38 @@ class Notification implements INotification { $this->subjectRich = $subject; $this->subjectRichParameters = $parameters; + if ($this->subjectParsed === '') { + $this->subjectParsed = $this->richToParsed($subject, $parameters); + } + return $this; } /** + * @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 */ @@ -386,6 +413,10 @@ class Notification implements INotification { $this->messageRich = $message; $this->messageRichParameters = $parameters; + if ($this->messageParsed === '') { + $this->messageParsed = $this->richToParsed($message, $parameters); + } + return $this; } diff --git a/lib/public/Activity/IEvent.php b/lib/public/Activity/IEvent.php index 1e3c1deef26..4c47cc47a3f 100644 --- a/lib/public/Activity/IEvent.php +++ b/lib/public/Activity/IEvent.php @@ -103,9 +103,7 @@ interface IEvent { * HTML is not allowed in the parsed subject and will be escaped * automatically by the clients. You can use the RichObjectString system * provided by the Nextcloud server to highlight important parameters via - * the setRichSubject method, but make sure, that a plain text message is - * always set via setParsedSubject, to support clients which can not handle - * rich strings. + * the setRichSubject method. * * See https://github.com/nextcloud/server/issues/1706 for more information. * @@ -128,8 +126,6 @@ interface IEvent { * HTML is not allowed in the rich subject and will be escaped automatically * by the clients, but you can use the RichObjectString system provided by * the Nextcloud server to highlight important parameters. - * Also make sure, that a plain text subject is always set via - * setParsedSubject, to support clients which can not handle rich strings. * * See https://github.com/nextcloud/server/issues/1706 for more information. * @@ -170,9 +166,7 @@ interface IEvent { * HTML is not allowed in the parsed message and will be escaped * automatically by the clients. You can use the RichObjectString system * provided by the Nextcloud server to highlight important parameters via - * the setRichMessage method, but make sure, that a plain text message is - * always set via setParsedMessage, to support clients which can not handle - * rich strings. + * the setRichMessage method. * * See https://github.com/nextcloud/server/issues/1706 for more information. * @@ -195,8 +189,6 @@ interface IEvent { * HTML is not allowed in the rich message and will be escaped automatically * by the clients, but you can use the RichObjectString system provided by * the Nextcloud server to highlight important parameters. - * Also make sure, that a plain text message is always set via - * setParsedMessage, to support clients which can not handle rich strings. * * See https://github.com/nextcloud/server/issues/1706 for more information. * diff --git a/lib/public/Notification/INotification.php b/lib/public/Notification/INotification.php index 511f65955f5..0c6625e346d 100644 --- a/lib/public/Notification/INotification.php +++ b/lib/public/Notification/INotification.php @@ -121,9 +121,7 @@ interface INotification { * HTML is not allowed in the parsed subject and will be escaped * automatically by the clients. You can use the RichObjectString system * provided by the Nextcloud server to highlight important parameters via - * the setRichSubject method, but make sure, that a plain text message is - * always set via setParsedSubject, to support clients which can not handle - * rich strings. + * the setRichSubject method. * * See https://github.com/nextcloud/server/issues/1706 for more information. * @@ -146,8 +144,6 @@ interface INotification { * HTML is not allowed in the rich subject and will be escaped automatically * by the clients, but you can use the RichObjectString system provided by * the Nextcloud server to highlight important parameters. - * Also make sure, that a plain text subject is always set via - * setParsedSubject, to support clients which can not handle rich strings. * * See https://github.com/nextcloud/server/issues/1706 for more information. * @@ -198,9 +194,7 @@ interface INotification { * HTML is not allowed in the parsed message and will be escaped * automatically by the clients. You can use the RichObjectString system * provided by the Nextcloud server to highlight important parameters via - * the setRichMessage method, but make sure, that a plain text message is - * always set via setParsedMessage, to support clients which can not handle - * rich strings. + * the setRichMessage method. * * See https://github.com/nextcloud/server/issues/1706 for more information. * @@ -223,8 +217,6 @@ interface INotification { * HTML is not allowed in the rich message and will be escaped automatically * by the clients, but you can use the RichObjectString system provided by * the Nextcloud server to highlight important parameters. - * Also make sure, that a plain text message is always set via - * setParsedMessage, to support clients which can not handle rich strings. * * See https://github.com/nextcloud/server/issues/1706 for more information. * |