Skip to content
Merged
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
26 changes: 12 additions & 14 deletions apps/comments/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use OCP\L10N\IFactory;

class Provider implements IProvider {
protected ?IL10N $l = null;

public function __construct(
protected IFactory $languageFactory,
protected IURLGenerator $url,
Expand All @@ -42,9 +40,9 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null):
throw new UnknownActivityException();
}

$this->l = $this->languageFactory->get('comments', $language);

if ($event->getSubject() === 'add_comment_subject') {
$l = $this->languageFactory->get('comments', $language);

$this->parseMessage($event);
if ($this->activityManager->getRequirePNG()) {
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
Expand All @@ -54,13 +52,13 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null):

if ($this->activityManager->isFormattingFilteredObject()) {
try {
return $this->parseShortVersion($event);
return $this->parseShortVersion($event, $l);
} catch (UnknownActivityException) {
// Ignore and simply use the long version...
}
}

return $this->parseLongVersion($event);
return $this->parseLongVersion($event, $l);
}
throw new UnknownActivityException();

Expand All @@ -69,15 +67,15 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null):
/**
* @throws UnknownActivityException
*/
protected function parseShortVersion(IEvent $event): IEvent {
protected function parseShortVersion(IEvent $event, IL10N $l): IEvent {
$subjectParameters = $this->getSubjectParameters($event);

if ($event->getSubject() === 'add_comment_subject') {
if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
$event->setRichSubject($this->l->t('You commented'), []);
$event->setRichSubject($l->t('You commented'), []);
} else {
$author = $this->generateUserParameter($subjectParameters['actor']);
$event->setRichSubject($this->l->t('{author} commented'), [
$event->setRichSubject($l->t('{author} commented'), [
'author' => $author,
]);
}
Expand All @@ -91,24 +89,24 @@ protected function parseShortVersion(IEvent $event): IEvent {
/**
* @throws UnknownActivityException
*/
protected function parseLongVersion(IEvent $event): IEvent {
protected function parseLongVersion(IEvent $event, IL10N $l): IEvent {
$subjectParameters = $this->getSubjectParameters($event);

if ($event->getSubject() === 'add_comment_subject') {
if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
$event->setParsedSubject($this->l->t('You commented on %1$s', [
$event->setParsedSubject($l->t('You commented on %1$s', [
$subjectParameters['filePath'],
]))
->setRichSubject($this->l->t('You commented on {file}'), [
->setRichSubject($l->t('You commented on {file}'), [
'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
]);
} else {
$author = $this->generateUserParameter($subjectParameters['actor']);
$event->setParsedSubject($this->l->t('%1$s commented on %2$s', [
$event->setParsedSubject($l->t('%1$s commented on %2$s', [
$author['name'],
$subjectParameters['filePath'],
]))
->setRichSubject($this->l->t('{author} commented on {file}'), [
->setRichSubject($l->t('{author} commented on {file}'), [
'author' => $author,
'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
]);
Expand Down
6 changes: 3 additions & 3 deletions apps/comments/lib/Activity/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Setting extends ActivitySettings {
public function __construct(
protected IL10N $l,
protected readonly IL10N $l,
) {
}

Expand All @@ -23,11 +23,11 @@ public function getName(): string {
return $this->l->t('<strong>Comments</strong> for files');
}

public function getGroupIdentifier() {
public function getGroupIdentifier(): string {
return 'files';
}

public function getGroupName() {
public function getGroupName(): string {
return $this->l->t('Files');
}

Expand Down
4 changes: 4 additions & 0 deletions apps/comments/lib/Listener/CommentsEntityEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function handle(Event $event): void {
return;
}

if ($this->userId === null) {
return;
}

$event->addEntityCollection('files', function ($name): bool {
$nodes = $this->rootFolder->getUserFolder($this->userId)->getById((int)$name);
return !empty($nodes);
Expand Down
3 changes: 1 addition & 2 deletions apps/comments/lib/Listener/CommentsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function handle(Event $event): void {
}

$eventType = $event->getEvent();
if ($eventType === CommentsEvent::EVENT_ADD
) {
if ($eventType === CommentsEvent::EVENT_ADD) {
$this->notificationHandler($event);
$this->activityHandler($event);
return;
Expand Down
11 changes: 2 additions & 9 deletions apps/comments/tests/Unit/Activity/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\Comments\CommentsEvent;
use OCP\Comments\Events\CommentAddedEvent;
use OCP\Comments\IComment;
use OCP\Files\Config\ICachedMountFileInfo;
use OCP\Files\Config\IMountProviderCollection;
Expand Down Expand Up @@ -66,14 +66,7 @@ public function testCommentEvent(): void {
->method('getObjectType')
->willReturn('files');

/** @var CommentsEvent|MockObject $event */
$event = $this->createMock(CommentsEvent::class);
$event->expects($this->any())
->method('getComment')
->willReturn($comment);
$event->expects($this->any())
->method('getEvent')
->willReturn(CommentsEvent::EVENT_ADD);
$event = new CommentAddedEvent($comment);

/** @var IUser|MockObject $ownerUser */
$ownerUser = $this->createMock(IUser::class);
Expand Down
26 changes: 14 additions & 12 deletions apps/comments/tests/Unit/EventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use OCA\Comments\Listener\CommentsEventListener;
use OCA\Comments\Notification\Listener as NotificationListener;
use OCP\Comments\CommentsEvent;
use OCP\Comments\Events\BeforeCommentUpdatedEvent;
use OCP\Comments\Events\CommentAddedEvent;
use OCP\Comments\Events\CommentDeletedEvent;
use OCP\Comments\Events\CommentUpdatedEvent;
use OCP\Comments\IComment;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
Expand Down Expand Up @@ -50,10 +54,10 @@ public function testNotFiles(): void {

public static function handledProvider(): array {
return [
[CommentsEvent::EVENT_DELETE],
[CommentsEvent::EVENT_UPDATE],
[CommentsEvent::EVENT_PRE_UPDATE],
[CommentsEvent::EVENT_ADD]
['delete'],
['update'],
['pre_update'],
['add']
];
}

Expand All @@ -65,14 +69,12 @@ public function testHandled(string $eventType): void {
->method('getObjectType')
->willReturn('files');

/** @var CommentsEvent|MockObject $event */
$event = $this->createMock(CommentsEvent::class);
$event->expects($this->atLeastOnce())
->method('getComment')
->willReturn($comment);
$event->expects($this->atLeastOnce())
->method('getEvent')
->willReturn($eventType);
$event = match ($eventType) {
'add' => new CommentAddedEvent($comment),
'pre_update' => new BeforeCommentUpdatedEvent($comment),
'update' => new CommentUpdatedEvent($comment),
'delete' => new CommentDeletedEvent($comment),
};

$this->notificationListener->expects($this->once())
->method('evaluate')
Expand Down
52 changes: 22 additions & 30 deletions apps/comments/tests/Unit/Notification/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
namespace OCA\Comments\Tests\Unit\Notification;

use OCA\Comments\Notification\Listener;
use OCP\Comments\CommentsEvent;
use OCP\Comments\Events\BeforeCommentUpdatedEvent;
use OCP\Comments\Events\CommentAddedEvent;
use OCP\Comments\Events\CommentDeletedEvent;
use OCP\Comments\Events\CommentUpdatedEvent;
use OCP\Comments\IComment;
use OCP\IURLGenerator;
use OCP\IUserManager;
Expand Down Expand Up @@ -37,10 +40,10 @@ protected function setUp(): void {

public static function eventProvider(): array {
return [
[CommentsEvent::EVENT_ADD, 'notify'],
[CommentsEvent::EVENT_UPDATE, 'notify'],
[CommentsEvent::EVENT_PRE_UPDATE, 'markProcessed'],
[CommentsEvent::EVENT_DELETE, 'markProcessed']
['add', 'notify'],
['update', 'notify'],
['pre_update', 'markProcessed'],
['delete', 'markProcessed']
];
}

Expand All @@ -49,7 +52,7 @@ public static function eventProvider(): array {
* @param string $notificationMethod
*/
#[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')]
public function testEvaluate($eventType, $notificationMethod): void {
public function testEvaluate(string $eventType, $notificationMethod): void {
/** @var IComment|MockObject $comment */
$comment = $this->createMock(IComment::class);
$comment->expects($this->any())
Expand All @@ -72,14 +75,12 @@ public function testEvaluate($eventType, $notificationMethod): void {
->method('getId')
->willReturn('1234');

/** @var CommentsEvent|MockObject $event */
$event = $this->createMock(CommentsEvent::class);
$event->expects($this->once())
->method('getComment')
->willReturn($comment);
$event->expects(($this->any()))
->method(('getEvent'))
->willReturn($eventType);
$event = match ($eventType) {
'add' => new CommentAddedEvent($comment),
'pre_update' => new BeforeCommentUpdatedEvent($comment),
'update' => new CommentUpdatedEvent($comment),
'delete' => new CommentDeletedEvent($comment),
};

/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);
Expand Down Expand Up @@ -124,14 +125,12 @@ public function testEvaluateNoMentions(string $eventType): void {
->method('getMentions')
->willReturn([]);

/** @var CommentsEvent|MockObject $event */
$event = $this->createMock(CommentsEvent::class);
$event->expects($this->once())
->method('getComment')
->willReturn($comment);
$event->expects(($this->any()))
->method(('getEvent'))
->willReturn($eventType);
$event = match ($eventType) {
'add' => new CommentAddedEvent($comment),
'pre_update' => new BeforeCommentUpdatedEvent($comment),
'update' => new CommentUpdatedEvent($comment),
'delete' => new CommentDeletedEvent($comment),
};

$this->notificationManager->expects($this->never())
->method('createNotification');
Expand Down Expand Up @@ -162,14 +161,7 @@ public function testEvaluateUserDoesNotExist(): void {
->method('getId')
->willReturn('1234');

/** @var CommentsEvent|MockObject $event */
$event = $this->createMock(CommentsEvent::class);
$event->expects($this->once())
->method('getComment')
->willReturn($comment);
$event->expects(($this->any()))
->method(('getEvent'))
->willReturn(CommentsEvent::EVENT_ADD);
$event = new CommentAddedEvent($comment);

/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);
Expand Down
4 changes: 4 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => $baseDir . '/lib/public/Comments/CommentsEntityEvent.php',
'OCP\\Comments\\CommentsEvent' => $baseDir . '/lib/public/Comments/CommentsEvent.php',
'OCP\\Comments\\Events\\BeforeCommentUpdatedEvent' => $baseDir . '/lib/public/Comments/Events/BeforeCommentUpdatedEvent.php',
'OCP\\Comments\\Events\\CommentAddedEvent' => $baseDir . '/lib/public/Comments/Events/CommentAddedEvent.php',
'OCP\\Comments\\Events\\CommentDeletedEvent' => $baseDir . '/lib/public/Comments/Events/CommentDeletedEvent.php',
'OCP\\Comments\\Events\\CommentUpdatedEvent' => $baseDir . '/lib/public/Comments/Events/CommentUpdatedEvent.php',
'OCP\\Comments\\IComment' => $baseDir . '/lib/public/Comments/IComment.php',
'OCP\\Comments\\ICommentsEventHandler' => $baseDir . '/lib/public/Comments/ICommentsEventHandler.php',
'OCP\\Comments\\ICommentsManager' => $baseDir . '/lib/public/Comments/ICommentsManager.php',
Expand Down
4 changes: 4 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',
'OCP\\Comments\\CommentsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEntityEvent.php',
'OCP\\Comments\\CommentsEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEvent.php',
'OCP\\Comments\\Events\\BeforeCommentUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/Events/BeforeCommentUpdatedEvent.php',
'OCP\\Comments\\Events\\CommentAddedEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/Events/CommentAddedEvent.php',
'OCP\\Comments\\Events\\CommentDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/Events/CommentDeletedEvent.php',
'OCP\\Comments\\Events\\CommentUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/Events/CommentUpdatedEvent.php',
'OCP\\Comments\\IComment' => __DIR__ . '/../../..' . '/lib/public/Comments/IComment.php',
'OCP\\Comments\\ICommentsEventHandler' => __DIR__ . '/../../..' . '/lib/public/Comments/ICommentsEventHandler.php',
'OCP\\Comments\\ICommentsManager' => __DIR__ . '/../../..' . '/lib/public/Comments/ICommentsManager.php',
Expand Down
22 changes: 11 additions & 11 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\CommentsEvent;
use OCP\Comments\Events\BeforeCommentUpdatedEvent;
use OCP\Comments\Events\CommentAddedEvent;
use OCP\Comments\Events\CommentDeletedEvent;
use OCP\Comments\Events\CommentUpdatedEvent;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler;
use OCP\Comments\ICommentsManager;
Expand Down Expand Up @@ -902,7 +906,7 @@ public function delete($id) {
if ($comment->getVerb() === 'reaction_deleted') {
$this->deleteReaction($comment);
}
$this->sendEvent(CommentsEvent::EVENT_DELETE, $comment);
$this->sendEvent(new CommentDeletedEvent($comment));
}

return ($affectedRows > 0);
Expand Down Expand Up @@ -1147,7 +1151,7 @@ protected function insert(IComment $comment): bool {
if ($comment->getVerb() === 'reaction') {
$this->addReaction($comment);
}
$this->sendEvent(CommentsEvent::EVENT_ADD, $comment);
$this->sendEvent(new CommentAddedEvent($comment));
}

return $affectedRows > 0;
Expand Down Expand Up @@ -1233,11 +1237,11 @@ private function sumReactions(string $parentId): void {
* @return bool
* @throws NotFoundException
*/
protected function update(IComment $comment) {
protected function update(IComment $comment): bool {
// for properly working preUpdate Events we need the old comments as is
// in the DB and overcome caching. Also avoid that outdated information stays.
$this->uncache($comment->getId());
$this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId()));
$this->sendEvent(new BeforeCommentUpdatedEvent($this->get($comment->getId())));
$this->uncache($comment->getId());

$result = $this->updateQuery($comment);
Expand All @@ -1246,7 +1250,7 @@ protected function update(IComment $comment) {
$this->deleteReaction($comment);
}

$this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment);
$this->sendEvent(new CommentUpdatedEvent($comment));

return $result;
}
Expand Down Expand Up @@ -1528,14 +1532,10 @@ private function getEventHandlers() {
}

/**
* sends notifications to the registered entities
*
* @param $eventType
* @param IComment $comment
* Sends notifications to the registered entities
*/
private function sendEvent($eventType, IComment $comment) {
private function sendEvent(CommentsEvent $event): void {
$entities = $this->getEventHandlers();
$event = new CommentsEvent($eventType, $comment);
foreach ($entities as $entity) {
$entity->handle($event);
}
Expand Down
Loading
Loading