Skip to content

Commit

Permalink
Add Thread.createdAt & Thread.createdBy
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 29, 2011
1 parent 3b21d19 commit 65ee102
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 6 deletions.
77 changes: 74 additions & 3 deletions Document/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ abstract class Thread extends AbstractThread
*/
protected $participants;

/**
* Participant that created the thread
*
* @var ParticipantInterface
*/
protected $createdBy;

/**
* Date this thread was created at
*
* @var \DateTime
*/
protected $createdAt;

/**
* Tells, for each participant, if the message is deleted
*
Expand Down Expand Up @@ -90,6 +104,45 @@ public function addMessage(MessageInterface $message)
$this->messages->add($message);
}

/**
* Gets the participant that created the thread
* Generally the sender of the first message
*
* @return ParticipantInterface
*/
public function getCreatedBy()
{
return $this->createdBy;
}

/**
* Sets the participant that created the thread
* Generally the sender of the first message
*
* @parm ParticipantInterface
*/
public function setCreatedBy(ParticipantInterface $participant)
{
$this->createdBy = $participant;
}

/**
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* @param \DateTime
* @return null
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}

/**
* Gets the users participating in this conversation
*
Expand Down Expand Up @@ -117,12 +170,12 @@ public function addParticipant(ParticipantInterface $participant)
/**
* Tells if the user participates to the conversation
*
* @param ParticipantInterface $user
* @param ParticipantInterface $participant
* @return boolean
*/
public function isParticipant(ParticipantInterface $user)
public function isParticipant(ParticipantInterface $participant)
{
return $this->participants->contains($user);
return $this->participants->contains($participant);
}

/**
Expand Down Expand Up @@ -162,6 +215,7 @@ public function setIsDeletedByParticipant(ParticipantInterface $participant, $is
public function denormalize()
{
$this->doParticipants();
$this->doCreatedByAndAt();
$this->doKeywords();
$this->doEnsureMessagesIsRead();
$this->doDatesOfLastMessageWrittenByParticipant();
Expand All @@ -179,6 +233,23 @@ protected function doParticipants()
}
}

/**
* Ensures that the createdBy & createdAt properties are set
*/
protected function doCreatedByAndAt()
{
if (isset($this->createdBy)) {
return;
}
$messages = $this->getMessages();
if (empty($messages)) {
return;
}
$firstMessage = reset($messages);
$thread->setCreatedBy($firstMessage->getSender());
$thread->setCreatedAt($firstMessage->getCreatedAt());
}

/**
* Adds all messages contents to the keywords property
*/
Expand Down
14 changes: 14 additions & 0 deletions DocumentManager/ThreadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ public function findParticipantThreadsBySearch(participantinterface $participant
return $this->getParticipantThreadsBySearchQueryBuilder($participant, $search)->getQuery()->execute();
}

/**
* Gets threads created by a participant
*
* @param ParticipantInterface $participant
* @return array of ThreadInterface
*/
public function findThreadsCreatedBy(ParticipantInterface $participant)
{
return $this->repository->createQueryBuilder()
->field('createdBy.$id')->equals(new \MongoId($participant->getId()))
->getQuery()
->execute();
}

/**
* Marks the readable as read by this participant
* Must be applied directly to the storage,
Expand Down
36 changes: 34 additions & 2 deletions Model/ThreadInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,38 @@ function addMessage(MessageInterface $message);
*/
function getLastMessage();

/**
* Gets the participant that created the thread
* Generally the sender of the first message
*
* @return ParticipantInterface
*/
function getCreatedBy();

/**
* Sets the participant that created the thread
* Generally the sender of the first message
*
* @parm ParticipantInterface
*/
function setCreatedBy(ParticipantInterface $participant);

/**
* Gets the date this thread was created at
* Generally the date of the first message
*
* @return \DateTime
*/
function getCreatedAt();

/**
* Sets the date this thread was created at
* Generally the date of the first message
*
* @param \DateTime
*/
function setCreatedAt(\DateTime $createdAt);

/**
* Gets the users participating in this conversation
*
Expand All @@ -55,10 +87,10 @@ function getParticipants();
/**
* Tells if the user participates to the conversation
*
* @param ParticipantInterface $user
* @param ParticipantInterface $participant
* @return boolean
*/
function isParticipant(ParticipantInterface $user);
function isParticipant(ParticipantInterface $participant);

/**
* Adds a participant to the thread
Expand Down
10 changes: 9 additions & 1 deletion ModelManager/ThreadManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ function getParticipantThreadsBySearchQueryBuilder(ParticipantInterface $partici
* @param string $search
* @return array of ThreadInterface
*/
function findparticipantthreadsbysearch(participantinterface $participant, $search);
function findParticipantThreadsBySearch(ParticipantInterface $participant, $search);

/**
* Gets threads created by a participant
*
* @param ParticipantInterface $participant
* @return array of ThreadInterface
*/
function findThreadsCreatedBy(ParticipantInterface $participant);

/**
* Creates an empty comment thread instance
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/doctrine/Thread.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

<!--<reference-many field="messages" target-document="Acme\MessageBundle\Document\Message" />-->

<!--<reference-one field="createdBy" target-document="Acme\UserBundle\Document\User" />-->

<field name="createdAt" fieldName="createdAt" type="date" />

<field name="isSpam" fieldName="isSpam" type="boolean" />

<field name="isDeletedByParticipant" fieldName="isDeletedByParticipant" type="hash" />
Expand All @@ -28,6 +32,9 @@
</lifecycle-callbacks>

<indexes>
<index unique="false">
<key name="createdAt" order="desc" />
</index>
<index unique="false">
<key name="isSpam" order="asc" />
</index>
Expand Down
5 changes: 5 additions & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Thread class::
* @MongoDB\ReferenceMany(targetDocument="Acme\UserBundle\Document\User")
*/
protected $participants;

/**
* @MongoDB\ReferenceOne(targetDocument="Acme\UserBundle\Document\User")
*/
protected $createdBy;
}

Message class::
Expand Down

0 comments on commit 65ee102

Please sign in to comment.