Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Add symfony events for comment app #30142

Merged
merged 1 commit into from
Jan 23, 2018
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
180 changes: 102 additions & 78 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Events\EventEmitterTrait;
use OCP\IDBConnection;
use OCP\IConfig;
use OCP\ILogger;
Expand All @@ -36,6 +37,7 @@

class Manager implements ICommentsManager {

use EventEmitterTrait;
/** @var IDBConnection */
protected $dbConn;

Expand Down Expand Up @@ -479,38 +481,43 @@ public function create($actorType, $actorId, $objectType, $objectId) {
* @since 9.0.0
*/
public function delete($id) {
if(!is_string($id)) {
throw new \InvalidArgumentException('Parameter must be string');
}
return $this->emittingCall(function () use (&$id) {
if(!is_string($id)) {
throw new \InvalidArgumentException('Parameter must be string');
}

try {
$comment = $this->get($id);
} catch (\Exception $e) {
// Ignore exceptions, we just don't fire a hook then
$comment = null;
}
try {
$comment = $this->get($id);
} catch (\Exception $e) {
// Ignore exceptions, we just don't fire a hook then
$comment = null;
}

$qb = $this->dbConn->getQueryBuilder();
$query = $qb->delete('comments')
->where($qb->expr()->eq('id', $qb->createParameter('id')))
->setParameter('id', $id);
$qb = $this->dbConn->getQueryBuilder();
$query = $qb->delete('comments')
->where($qb->expr()->eq('id', $qb->createParameter('id')))
->setParameter('id', $id);

try {
$affectedRows = $query->execute();
$this->uncache($id);
} catch (DriverException $e) {
$this->logger->logException($e, ['app' => 'core_comments']);
return false;
}
try {
$affectedRows = $query->execute();
$this->uncache($id);
} catch (DriverException $e) {
$this->logger->logException($e, ['app' => 'core_comments']);
return false;
}

if ($affectedRows > 0 && $comment instanceof IComment) {
$this->dispatcher->dispatch(CommentsEvent::EVENT_DELETE, new CommentsEvent(
CommentsEvent::EVENT_DELETE,
$comment
));
}
if ($affectedRows > 0 && $comment instanceof IComment) {
$this->dispatcher->dispatch(CommentsEvent::EVENT_DELETE, new CommentsEvent(
CommentsEvent::EVENT_DELETE,
$comment
));
}

return ($affectedRows > 0);
return ($affectedRows > 0);
}, [
'before' => ['commentId' => $id],
'after' => ['commentId' => $id, 'objectId' => $this->get($id)->getObjectId()]
], 'comment', 'delete');
}

/**
Expand All @@ -530,21 +537,28 @@ public function delete($id) {
* @since 9.0.0
*/
public function save(IComment $comment) {
if($this->prepareCommentForDatabaseWrite($comment)->getId() === '') {
$result = $this->insert($comment);
} else {
$result = $this->update($comment);
}
$databaseWrite = $this->prepareCommentForDatabaseWrite($comment)->getId();
$createOrUpdate = $databaseWrite === '' ? 'create' : 'update';
return $this->emittingCall(function () use (&$comment, &$databaseWrite) {
if($databaseWrite === '') {
$result = $this->insert($comment);
} else {
$result = $this->update($comment);
}

if($result && !!$comment->getParentId()) {
$this->updateChildrenInformation(
if($result && !!$comment->getParentId()) {
$this->updateChildrenInformation(
$comment->getParentId(),
$comment->getCreationDateTime()
);
$this->cache($comment);
}
);
$this->cache($comment);
}

return $result;
return $result;
}, [
'before' => ['objectId' => $comment->getObjectId(), 'commentId' => $comment->getId(), 'message' => $comment->getMessage(), 'status' => $createOrUpdate],
'after' => ['objectId' => $comment->getObjectId(), 'commentId' => $comment->getId() , 'message' => $comment->getMessage(), 'status' => $createOrUpdate]
], 'comment', 'save');
}

/**
Expand All @@ -554,34 +568,39 @@ public function save(IComment $comment) {
* @return bool
*/
protected function insert(IComment &$comment) {
$qb = $this->dbConn->getQueryBuilder();
$affectedRows = $qb
->insert('comments')
->values([
'parent_id' => $qb->createNamedParameter($comment->getParentId()),
'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
'actor_type' => $qb->createNamedParameter($comment->getActorType()),
'actor_id' => $qb->createNamedParameter($comment->getActorId()),
'message' => $qb->createNamedParameter($comment->getMessage()),
'verb' => $qb->createNamedParameter($comment->getVerb()),
'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
'object_type' => $qb->createNamedParameter($comment->getObjectType()),
'object_id' => $qb->createNamedParameter($comment->getObjectId()),
])
->execute();

if ($affectedRows > 0) {
$comment->setId(strval($qb->getLastInsertId()));
}
return $this->emittingCall(function () use (&$comment) {
$qb = $this->dbConn->getQueryBuilder();
$affectedRows = $qb
->insert('comments')
->values([
'parent_id' => $qb->createNamedParameter($comment->getParentId()),
'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
'actor_type' => $qb->createNamedParameter($comment->getActorType()),
'actor_id' => $qb->createNamedParameter($comment->getActorId()),
'message' => $qb->createNamedParameter($comment->getMessage()),
'verb' => $qb->createNamedParameter($comment->getVerb()),
'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
'object_type' => $qb->createNamedParameter($comment->getObjectType()),
'object_id' => $qb->createNamedParameter($comment->getObjectId()),
])
->execute();

if ($affectedRows > 0) {
$comment->setId(strval($qb->getLastInsertId()));
}

$this->dispatcher->dispatch(CommentsEvent::EVENT_ADD, new CommentsEvent(
CommentsEvent::EVENT_ADD,
$comment
));
$this->dispatcher->dispatch(CommentsEvent::EVENT_ADD, new CommentsEvent(
CommentsEvent::EVENT_ADD,
$comment
));

return $affectedRows > 0;
return $affectedRows > 0;
}, [
'before' => ['objectId' => $comment->getObjectId(), 'message' => $comment->getMessage()],
'after' => ['objectId' => $comment->getObjectId(), 'message' => $comment->getMessage()]
], 'comment', 'create');
}

/**
Expand All @@ -592,9 +611,10 @@ protected function insert(IComment &$comment) {
* @throws NotFoundException
*/
protected function update(IComment $comment) {
$qb = $this->dbConn->getQueryBuilder();
$affectedRows = $qb
->update('comments')
return $this->emittingCall(function () use (&$comment) {
$qb = $this->dbConn->getQueryBuilder();
$affectedRows = $qb
->update('comments')
->set('parent_id', $qb->createNamedParameter($comment->getParentId()))
->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId()))
->set('children_count', $qb->createNamedParameter($comment->getChildrenCount()))
Expand All @@ -606,20 +626,24 @@ protected function update(IComment $comment) {
->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
->set('object_id', $qb->createNamedParameter($comment->getObjectId()))
->where($qb->expr()->eq('id', $qb->createParameter('id')))
->setParameter('id', $comment->getId())
->execute();
->where($qb->expr()->eq('id', $qb->createParameter('id')))
->setParameter('id', $comment->getId())
->execute();

if($affectedRows === 0) {
throw new NotFoundException('Comment to update does ceased to exist');
}
if($affectedRows === 0) {
throw new NotFoundException('Comment to update does ceased to exist');
}

$this->dispatcher->dispatch(CommentsEvent::EVENT_UPDATE, new CommentsEvent(
CommentsEvent::EVENT_UPDATE,
$comment
));
$this->dispatcher->dispatch(CommentsEvent::EVENT_UPDATE, new CommentsEvent(
CommentsEvent::EVENT_UPDATE,
$comment
));

return $affectedRows > 0;
return $affectedRows > 0;
}, [
'before' => ['objectId' => $comment->getObjectId(), 'commentId' => $comment->getId(), 'message' => $comment->getMessage()],
'after' => ['objectId' => $comment->getObjectId(), 'commentId' => $comment->getId(), 'message' => $comment->getMessage()]
], 'comment', 'update');
}

/**
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/Comments/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OCP\Comments\ICommentsManager;
use OCP\IDBConnection;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
use Test\Traits\UserTrait;

Expand Down Expand Up @@ -428,6 +429,16 @@ public function testDelete() {
}

public function testSaveNew() {
$calledBeforeCreateEvent = [];
$calledAfterCreateEvent = [];
\OC::$server->getEventDispatcher()->addListener('comment.beforecreate', function (GenericEvent $event) use (&$calledBeforeCreateEvent) {
$calledBeforeCreateEvent[] = 'comment.beforesave';
$calledBeforeCreateEvent[] = $event;
});
\OC::$server->getEventDispatcher()->addListener('comment.aftercreate', function (GenericEvent $event) use (&$calledAfterCreateEvent) {
$calledAfterCreateEvent[] = 'comment.aftersave';
$calledAfterCreateEvent[] = $event;
});
$manager = $this->getManager();
$comment = new \OC\Comments\Comment();
$comment
Expand All @@ -441,13 +452,31 @@ public function testSaveNew() {
$this->assertTrue($comment->getId() !== '');
$this->assertTrue($comment->getId() !== '0');
$this->assertNotNull($comment->getCreationDateTime());
$this->assertInstanceOf(GenericEvent::class, $calledBeforeCreateEvent[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterCreateEvent[1]);
$this->assertEquals('comment.beforesave', $calledBeforeCreateEvent[0]);
$this->assertEquals('comment.aftersave', $calledAfterCreateEvent[0]);
$this->assertArrayHasKey('objectId', $calledBeforeCreateEvent[1]);
$this->assertArrayHasKey('message', $calledBeforeCreateEvent[1]);
$this->assertArrayHasKey('objectId', $calledAfterCreateEvent[1]);
$this->assertArrayHasKey('message', $calledAfterCreateEvent[1]);

$loadedComment = $manager->get($comment->getId());
$this->assertSame($comment->getMessage(), $loadedComment->getMessage());
$this->assertEquals($comment->getCreationDateTime()->format(\DateTime::ISO8601), $loadedComment->getCreationDateTime()->format(\DateTime::ISO8601));
}

public function testSaveUpdate() {
$calledBeforeUpdateEevnt = [];
$calledAfterUpdateEevnt = [];
\OC::$server->getEventDispatcher()->addListener('comment.beforeupdate', function (GenericEvent $event) use (&$calledBeforeUpdateEevnt) {
$calledBeforeUpdateEevnt[] = 'comment.beforeupdate';
$calledBeforeUpdateEevnt[] = $event;
});
\OC::$server->getEventDispatcher()->addListener('comment.afterupdate', function (GenericEvent $event) use (&$calledAfterUpdateEevnt) {
$calledAfterUpdateEevnt[] = 'comment.afterupdate';
$calledAfterUpdateEevnt[] = $event;
});
$manager = $this->getManager();
$comment = new \OC\Comments\Comment();
$comment
Expand All @@ -463,12 +492,34 @@ public function testSaveUpdate() {

$loadedComment = $manager->get($comment->getId());
$this->assertSame($comment->getMessage(), $loadedComment->getMessage());

$this->assertInstanceOf(GenericEvent::class, $calledAfterUpdateEevnt[1]);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeUpdateEevnt[1]);
$this->assertEquals('comment.beforeupdate', $calledBeforeUpdateEevnt[0]);
$this->assertEquals('comment.afterupdate', $calledAfterUpdateEevnt[0]);
$this->assertArrayHasKey('objectId', $calledAfterUpdateEevnt[1]);
$this->assertArrayHasKey('commentId', $calledAfterUpdateEevnt[1]);
$this->assertArrayHasKey('message', $calledAfterUpdateEevnt[1]);
$this->assertArrayHasKey('objectId', $calledBeforeUpdateEevnt[1]);
$this->assertArrayHasKey('commentId', $calledBeforeUpdateEevnt[1]);
$this->assertArrayHasKey('message', $calledBeforeUpdateEevnt[1]);
}

/**
* @expectedException \OCP\Comments\NotFoundException
*/
public function testSaveUpdateException() {
$calledBeforeDeleteEvent = [];
$calledAfterDeleteEvent = [];
\OC::$server->getEventDispatcher()->addListener('comment.beforedelete', function (GenericEvent $event) use (&$calledBeforeDeleteEvent) {
$calledBeforeDeleteEvent[] = 'comment.beforedelete';
$calledBeforeDeleteEvent[] = $event;
});
\OC::$server->getEventDispatcher()->addListener('comment.afterdelete', function (GenericEvent $event) use (&$calledAfterDeleteEvent) {
$calledAfterDeleteEvent[] = 'comment.afterdelete';
$calledAfterDeleteEvent[] = $event;
});

$manager = $this->getManager();
$comment = new \OC\Comments\Comment();
$comment
Expand All @@ -480,6 +531,14 @@ public function testSaveUpdateException() {
$manager->save($comment);

$manager->delete($comment->getId());
$this->assertInstanceOf(GenericEvent::class, $calledBeforeDeleteEvent[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterDeleteEvent[1]);
$this->assertEquals('comment.beforedelete', $calledBeforeDeleteEvent[0]);
$this->assertEquals('comment.afterdelete', $calledAfterDeleteEvent[0]);
$this->assertArrayHasKey('commentId', $calledBeforeDeleteEvent[1]);
$this->assertArrayHasKey('commentId', $calledAfterDeleteEvent[1]);
$this->assertArrayHasKey('objectId', $calledAfterDeleteEvent[1]);

$comment->setMessage('very beautiful, I am really so much impressed!');
$manager->save($comment);
}
Expand Down