if( $eventType === CommentsEvent::EVENT_ADD
&& $event instanceof CommentsEvent
) {
- $this->onAdd($event);
+ $this->notificationHandler($event);
+ $this->activityHandler($event);
+ return;
+ }
+
+ if( $eventType === CommentsEvent::EVENT_PRE_UPDATE
+ && $event instanceof CommentsEvent
+ ) {
+ $this->notificationHandler($event);
+ return;
+ }
+
+ if( $eventType === CommentsEvent::EVENT_UPDATE
+ && $event instanceof CommentsEvent
+ ) {
+ $this->notificationHandler($event);
return;
}
if( $eventType === CommentsEvent::EVENT_DELETE
&& $event instanceof CommentsEvent
) {
- $this->onDelete($event);
+ $this->notificationHandler($event);
return;
}
}
/**
* @param CommentsEvent $event
*/
- private function onAdd(CommentsEvent $event) {
+ private function activityHandler(CommentsEvent $event) {
$c = $this->app->getContainer();
- /** @var NotificationListener $notificationListener */
- $notificationListener = $c->query(NotificationListener::class);
- $notificationListener->evaluate($event);
-
/** @var ActivityListener $listener */
$activityListener = $c->query(ActivityListener::class);
$activityListener->commentEvent($event);
/**
* @param CommentsEvent $event
*/
- private function onDelete(CommentsEvent $event) {
+ private function notificationHandler(CommentsEvent $event) {
$c = $this->app->getContainer();
/** @var NotificationListener $notificationListener */
}
$notification->setUser($user);
- if($event->getEvent() === CommentsEvent::EVENT_DELETE) {
+ if( $event->getEvent() === CommentsEvent::EVENT_DELETE
+ || $event->getEvent() === CommentsEvent::EVENT_PRE_UPDATE)
+ {
$this->notificationManager->markProcessed($notification);
} else {
$this->notificationManager->notify($notification);
$this->eventHandler->handle($event);
}
- public function notHandledProvider() {
- return [
- [CommentsEvent::EVENT_UPDATE]
- ];
- }
-
- /**
- * @dataProvider notHandledProvider
- * @param string $eventType
- */
- public function testNotHandled($eventType) {
- /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
- $comment = $this->getMockBuilder(IComment::class)->getMock();
- $comment->expects($this->once())
- ->method('getObjectType')
- ->willReturn('files');
-
- /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
- $event = $this->getMockBuilder(CommentsEvent::class)
- ->disableOriginalConstructor()
- ->getMock();
- $event->expects($this->once())
- ->method('getComment')
- ->willReturn($comment);
- $event->expects($this->once())
- ->method('getEvent')
- ->willReturn($eventType);
-
- // further processing does not happen, because $event methods cannot be
- // access more than once.
- $this->eventHandler->handle($event);
- }
-
public function handledProvider() {
return [
[CommentsEvent::EVENT_DELETE],
+ [CommentsEvent::EVENT_UPDATE],
+ [CommentsEvent::EVENT_PRE_UPDATE],
[CommentsEvent::EVENT_ADD]
];
}
->withConsecutive([NotificationListener::class], [ActivityListener::class])
->willReturnOnConsecutiveCalls($notificationListener, $activityListener);
- $this->app->expects($this->once())
+ $this->app->expects($this->atLeastOnce())
->method('getContainer')
->willReturn($c);
public function eventProvider() {
return [
[CommentsEvent::EVENT_ADD, 'notify'],
+ [CommentsEvent::EVENT_UPDATE, 'notify'],
+ [CommentsEvent::EVENT_PRE_UPDATE, 'markProcessed'],
[CommentsEvent::EVENT_DELETE, 'markProcessed']
];
}
* @param $propertyValue
* @return bool
* @throws BadRequest
- * @throws Forbidden
+ * @throws \Exception
*/
public function updateComment($propertyValue) {
$this->checkWriteAccessOnComment();
* @throws NotFoundException
*/
protected function update(IComment $comment) {
+ // 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->uncache($comment->getId());
+
$qb = $this->dbConn->getQueryBuilder();
$affectedRows = $qb
->update('comments')
*/
class CommentsEvent extends Event {
- const EVENT_ADD = 'OCP\Comments\ICommentsManager::addComment';
- const EVENT_UPDATE = 'OCP\Comments\ICommentsManager::updateComment';
- const EVENT_DELETE = 'OCP\Comments\ICommentsManager::deleteComment';
+ const EVENT_ADD = 'OCP\Comments\ICommentsManager::addComment';
+ const EVENT_PRE_UPDATE = 'OCP\Comments\ICommentsManager::preUpdateComment';
+ const EVENT_UPDATE = 'OCP\Comments\ICommentsManager::updateComment';
+ const EVENT_DELETE = 'OCP\Comments\ICommentsManager::deleteComment';
/** @var string */
protected $event;
public function testSendEvent() {
$handler1 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
- $handler1->expects($this->exactly(3))
+ $handler1->expects($this->exactly(4))
->method('handle');
$handler2 = $this->getMockBuilder(ICommentsEventHandler::class)->getMock();
- $handler1->expects($this->exactly(3))
+ $handler1->expects($this->exactly(4))
->method('handle');
$manager = $this->getManager();