Skip to content

Commit

Permalink
Implement the ReadableManagerInterface by updating the storage directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 26, 2011
1 parent a971b85 commit 6b18742
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Composer/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Ornicar\MessageBundle\Composer;

use Ornicar\MessageBundle\Model\MessageManagerInterface;
use Ornicar\MessageBundle\ModelManager\MessageManagerInterface;
use Ornicar\MessageBundle\Sender\SenderInterface;

/**
Expand Down
53 changes: 51 additions & 2 deletions DocumentManager/MessageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Ornicar\MessageBundle\ModelManager\MessageManager as BaseMessageManager;
use Ornicar\MessageBundle\Model\ReadableInterface;
use FOS\UserBundle\Model\UserInterface;
use Ornicar\MessageBundle\Model\ThreadInterface;
use Doctrine\ODM\MongoDB\Query\Builder;

/**
* Default MongoDB MessageManager.
Expand Down Expand Up @@ -55,7 +57,7 @@ public function __construct(DocumentManager $dm, $class)
*/
public function markAsReadByParticipant(ReadableInterface $readable, UserInterface $user)
{
throw new \Exception('Implement me :)');
return $this->markIsReadByParticipant($readable, $user, true);
}

/**
Expand All @@ -66,7 +68,54 @@ public function markAsReadByParticipant(ReadableInterface $readable, UserInterfa
*/
public function markAsUnreadByParticipant(ReadableInterface $readable, UserInterface $user)
{
throw new \Exception('Implement me :)');
return $this->markIsReadByParticipant($readable, $user, false);
}

/**
* Marks all messages of this thread as read by this participant
*
* @param ThreadInterface $thread
* @param UserInterface $user
* @param boolean $isRead
*/
public function markIsReadByThreadAndParticipant(ThreadInterface $thread, UserInterface $user, $isRead)
{
$this->doMarkIsReadByParticipant($user, $isRead, function(Builder $queryBuilder) use ($thread) {
$queryBuilder->field('thread.$id')->equals(new \MongoId($thread->getId()));
});
}

/**
* Marks the message as read or unread by this participant
*
* @param MessageInterface $message
* @param UserInterface $user
* @param boolean $isRead
*/
protected function markIsReadByParticipant(MessageInterface $message, UserInterface $user, $isRead)
{
$this->doMarkIsReadByParticipant($user, $isRead, function(Builder $queryBuilder) use ($message) {
$queryBuilder->field('id')->equals($message->getId());
});
}

/**
* Marks messages as read/unread
* by updating directly the storage
*
* @param UserInterface $user
* @param boolean $isRead
* @param \Closure $condition
*/
protected function doMarkIsReadByParticipant(UserInterface $user, $isRead, \Closure $condition)
{
$isReadByParticipantFieldName = sprintf('isReadByParticipant.%s', $user->getId());
$queryBuilder = $this->repository->createQueryBuilder();
$condition($queryBuilder);
$queryBuilder->update()
->field($isReadByParticipantFieldName)->set((boolean) $isRead)
->getQuery(array('multiple' => true))
->execute();
}

/**
Expand Down
24 changes: 18 additions & 6 deletions DocumentManager/ThreadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@ class ThreadManager extends BaseThreadManager
protected $repository;

/**
* The model class
*
* @var string
*/
protected $class;

/**
* The message manager, required to mark
* the messages of a thread as read/unread
*
* @var MessageManager
*/
protected $messageManager;

/**
* Constructor.
*
* @param DocumentManager $dm
* @param string $class
* @param MessageManager $messageManager
*/
public function __construct(DocumentManager $dm, $class)
public function __construct(DocumentManager $dm, $class, MessageManager $messageManager)
{
$this->dm = $dm;
$this->repository = $dm->getRepository($class);
$this->class = $dm->getClassMetadata($class)->name;
$this->dm = $dm;
$this->repository = $dm->getRepository($class);
$this->class = $dm->getClassMetadata($class)->name;
$this->messageManager = $messageManager;
}

/**
Expand Down Expand Up @@ -129,7 +141,7 @@ public function findUserSentThreads(UserInterface $user)
*/
public function markAsReadByParticipant(ReadableInterface $readable, UserInterface $user)
{
throw new \Exception('Implement me :)');
return $this->messageManager->markIsReadByThreadAndParticipant($readable, $user, true);
}

/**
Expand All @@ -140,7 +152,7 @@ public function markAsReadByParticipant(ReadableInterface $readable, UserInterfa
*/
public function markAsUnreadByParticipant(ReadableInterface $readable, UserInterface $user)
{
throw new \Exception('Implement me :)');
return $this->messageManager->markIsReadByThreadAndParticipant($readable, $user, false);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Ornicar\MessageBundle\Model\ThreadManagerInterface;
use Ornicar\MessageBundle\ModelManager\ThreadManagerInterface;
use Ornicar\MessageBundle\Authorizer\AuthorizerInterface;
use Ornicar\MessageBundle\Reader\ReaderInterface;

Expand Down Expand Up @@ -59,6 +59,9 @@ public function getThread($threadId)
if (!$this->authorizer->canSeeThread($thread)) {
throw new AccessDeniedException('You are not allowed to see this thread');
}
// Load the thread messages before marking them as read
// because we want to see the unread messages
$thread->getMessages();
$this->threadReader->markAsRead($thread);

return $thread;
Expand Down
1 change: 1 addition & 0 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<service id="ornicar_message.thread_manager.default" class="Ornicar\MessageBundle\DocumentManager\ThreadManager" public="false">
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument>%ornicar_message.thread_class%</argument>
<argument type="service" id="ornicar_message.message_manager" />
</service>

</services>
Expand Down
4 changes: 2 additions & 2 deletions Sender/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Ornicar\MessageBundle\Sender;

use Ornicar\MessageBundle\Model\MessageManagerInterface;
use Ornicar\MessageBundle\Model\ThreadManagerInterface;
use Ornicar\MessageBundle\ModelManager\MessageManagerInterface;
use Ornicar\MessageBundle\ModelManager\ThreadManagerInterface;
use Ornicar\MessageBundle\Model\MessageInterface;
use Ornicar\MessageBundle\Model\ThreadInterface;
use FOS\UserBundle\Model\UserInterface;
Expand Down

0 comments on commit 6b18742

Please sign in to comment.