Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions lib/DigestSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,27 +220,6 @@ public function sendDigestForUser(IUser $user, int $now, string $timezone, strin
* @return string
*/
protected function getHTMLSubject(IEvent $event): string {
if ($event->getRichSubject() === '') {
return htmlspecialchars($event->getParsedSubject());
}

$placeholders = $replacements = [];
foreach ($event->getRichSubjectParameters() as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';

if ($parameter['type'] === 'file') {
$replacement = (string)$parameter['path'];
} else {
$replacement = (string)$parameter['name'];
}

if (isset($parameter['link'])) {
$replacements[] = '<a href="' . $parameter['link'] . '">' . htmlspecialchars($replacement) . '</a>';
} else {
$replacements[] = '<strong>' . htmlspecialchars($replacement) . '</strong>';
}
}

return str_replace($placeholders, $replacements, $event->getRichSubject());
return HTMLSubjectFormatter::format($event);
}
}
37 changes: 37 additions & 0 deletions lib/HTMLSubjectFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Activity;

use OCP\Activity\IEvent;

class HTMLSubjectFormatter {
public static function format(IEvent $event): string {
if ($event->getRichSubject() === '') {
return htmlspecialchars($event->getParsedSubject());
}

$placeholders = $replacements = [];
foreach ($event->getRichSubjectParameters() as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';

$replacement = $parameter['type'] === 'file'
? (string)$parameter['path']
: (string)$parameter['name'];

if (isset($parameter['link'])) {
$replacements[] = '<a href="' . $parameter['link'] . '">' . htmlspecialchars($replacement) . '</a>';
} else {
$replacements[] = '<strong>' . htmlspecialchars($replacement) . '</strong>';
}
}

return str_replace($placeholders, $replacements, $event->getRichSubject());
}
}
23 changes: 1 addition & 22 deletions lib/MailQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,28 +379,7 @@ function ($event) use ($timezone, $l) {
}

protected function getHTMLSubject(IEvent $event): string {
if ($event->getRichSubject() === '') {
return htmlspecialchars($event->getParsedSubject());
}

$placeholders = $replacements = [];
foreach ($event->getRichSubjectParameters() as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';

if ($parameter['type'] === 'file') {
$replacement = (string)$parameter['path'];
} else {
$replacement = (string)$parameter['name'];
}

if (isset($parameter['link'])) {
$replacements[] = '<a href="' . $parameter['link'] . '">' . htmlspecialchars($replacement) . '</a>';
} else {
$replacements[] = '<strong>' . htmlspecialchars($replacement) . '</strong>';
}
}

return str_replace($placeholders, $replacements, $event->getRichSubject());
return HTMLSubjectFormatter::format($event);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/HTMLSubjectFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Activity\Tests;

use OCA\Activity\HTMLSubjectFormatter;
use OCP\Activity\IEvent;
use PHPUnit\Framework\MockObject\MockObject;

class HTMLSubjectFormatterTest extends TestCase {
private function getEvent(string $richSubject, array $richParams, string $parsedSubject): IEvent&MockObject {
$event = $this->createMock(IEvent::class);
$event->method('getRichSubject')->willReturn($richSubject);
$event->method('getRichSubjectParameters')->willReturn($richParams);
$event->method('getParsedSubject')->willReturn($parsedSubject);
return $event;
}

public function testNoRichSubjectFallsToParsed(): void {
$event = $this->getEvent('', [], 'Plain <subject>');
$this->assertSame('Plain &lt;subject&gt;', HTMLSubjectFormatter::format($event));
}

public function testFileParameterUsesPath(): void {
$event = $this->getEvent(
'File {file} was changed',
['file' => ['type' => 'file', 'path' => '/my/file.txt', 'name' => 'file.txt']],
'',
);
$this->assertSame('File <strong>/my/file.txt</strong> was changed', HTMLSubjectFormatter::format($event));
}

public function testNonFileParameterUsesName(): void {
$event = $this->getEvent(
'{user} shared',
['user' => ['type' => 'user', 'name' => 'Alice & Bob']],
'',
);
$this->assertSame('<strong>Alice &amp; Bob</strong> shared', HTMLSubjectFormatter::format($event));
}

public function testParameterWithLinkRendersAnchor(): void {
$event = $this->getEvent(
'See {file}',
['file' => ['type' => 'file', 'path' => 'report.pdf', 'name' => 'report.pdf', 'link' => 'https://example.com/report.pdf']],
'',
);
$this->assertSame('See <a href="https://example.com/report.pdf">report.pdf</a>', HTMLSubjectFormatter::format($event));
}
}
Loading