-
Notifications
You must be signed in to change notification settings - Fork 183
/
MessageManager.php
171 lines (155 loc) · 5.06 KB
/
MessageManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Ornicar\MessageBundle\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentManager;
use Ornicar\MessageBundle\Model\MessageInterface;
use Ornicar\MessageBundle\ModelManager\MessageManager as BaseMessageManager;
use Ornicar\MessageBundle\Model\ReadableInterface;
use Ornicar\MessageBundle\Model\ParticipantInterface;
use Ornicar\MessageBundle\Model\ThreadInterface;
use Doctrine\ODM\MongoDB\Query\Builder;
/**
* Default MongoDB MessageManager.
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class MessageManager extends BaseMessageManager
{
/**
* @var DocumentManager
*/
protected $dm;
/**
* @var DocumentRepository
*/
protected $repository;
/**
* @var string
*/
protected $class;
/**
* @var string
*/
protected $metaClass;
/**
* Constructor.
*
* @param DocumentManager $dm
* @param string $class
* @param string $metaClass
*/
public function __construct(DocumentManager $dm, $class, $metaClass)
{
$this->dm = $dm;
$this->repository = $dm->getRepository($class);
$this->class = $dm->getClassMetadata($class)->name;
$this->metaClass = $em->getClassMetadata($metaClass)->name;
}
/**
* Tells how many unread, non-spam, messages this participant has
*
* @param ParticipantInterface $participant
* @return int the number of unread messages
*/
public function getNbUnreadMessageByParticipant(ParticipantInterface $participant)
{
$queryBuilder = $this->repository->createQueryBuilder();
return $queryBuilder
->field('metadata')->elemMatch($queryBuilder->expr()
->field('participant.$id')->equals(new \MongoId($participant->getId()))
->field('isRead')->equals(false)
)
->field('isSpam')->equals(false)
->getQuery()
->count();
}
/**
* Marks the readable as read by this participant
* Must be applied directly to the storage,
* without modifying the readable state.
* We want to show the unread readables on the page,
* as well as marking the as read.
*
* @param ReadableInterface $readable
* @param ParticipantInterface $user
*/
public function markAsReadByParticipant(ReadableInterface $readable, ParticipantInterface $user)
{
return $this->markIsReadByParticipant($readable, $user, true);
}
/**
* Marks the readable as unread by this participant
*
* @param ReadableInterface $readable
* @param ParticipantInterface $user
*/
public function markAsUnreadByParticipant(ReadableInterface $readable, ParticipantInterface $user)
{
return $this->markIsReadByParticipant($readable, $user, false);
}
/**
* Marks all messages of this thread as read by this participant
*
* @param ThreadInterface $thread
* @param ParticipantInterface $user
* @param boolean $isRead
*/
public function markIsReadByThreadAndParticipant(ThreadInterface $thread, ParticipantInterface $user, $isRead)
{
$this->markIsReadByCondition($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 ParticipantInterface $user
* @param boolean $isRead
*/
protected function markIsReadByParticipant(MessageInterface $message, ParticipantInterface $user, $isRead)
{
$this->markIsReadByCondition($user, $isRead, function(Builder $queryBuilder) use ($message) {
$queryBuilder->field('id')->equals($message->getId());
});
}
/**
* Marks messages as read/unread
* by updating directly the storage
*
* @param ParticipantInterface $user
* @param boolean $isRead
* @param \Closure $condition
*/
protected function markIsReadByCondition(ParticipantInterface $user, $isRead, \Closure $condition)
{
$queryBuilder = $this->repository->createQueryBuilder();
$condition($queryBuilder);
$queryBuilder->update()
->field('metadata.participant.$id')->equals(new \MongoId($participant->getId()))
->field('metadata.$.isRead')->set((boolean) $isRead)
->getQuery(array('multiple' => true))
->execute();
}
/**
* Saves a message
*
* @param MessageInterface $message
* @param Boolean $andFlush Whether to flush the changes (default true)
*/
public function saveMessage(MessageInterface $message, $andFlush = true)
{
$this->dm->persist($message);
if ($andFlush) {
$this->dm->flush(array('safe' => true));
}
}
/**
* Returns the fully qualified comment thread class name
*
* @return string
**/
public function getClass()
{
return $this->class;
}
}