aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Activity/Event.php33
-rw-r--r--lib/private/Notification/Notification.php33
2 files changed, 64 insertions, 2 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;
}