-
Notifications
You must be signed in to change notification settings - Fork 183
/
MessageManager.php
221 lines (198 loc) · 6.62 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace FOS\MessageBundle\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Query\Builder;
use FOS\MessageBundle\Document\Message;
use FOS\MessageBundle\Document\MessageMetadata;
use FOS\MessageBundle\Model\MessageInterface;
use FOS\MessageBundle\Model\ParticipantInterface;
use FOS\MessageBundle\Model\ReadableInterface;
use FOS\MessageBundle\Model\ThreadInterface;
use FOS\MessageBundle\ModelManager\MessageManager as BaseMessageManager;
/**
* 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;
/**
* @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 = $dm->getClassMetadata($metaClass)->name;
}
/**
* {@inheritdoc}
*/
public function getNbUnreadMessageByParticipant(ParticipantInterface $participant)
{
return $this->repository->createQueryBuilder()
->field('unreadForParticipants')->equals($participant->getId())
->getQuery()
->count();
}
/**
* {@inheritdoc}
*/
public function markAsReadByParticipant(ReadableInterface $readable, ParticipantInterface $participant)
{
$this->markIsReadByParticipant($readable, $participant, true);
}
/**
* {@inheritdoc}
*/
public function markAsUnreadByParticipant(ReadableInterface $readable, ParticipantInterface $participant)
{
$this->markIsReadByParticipant($readable, $participant, false);
}
/**
* Marks all messages of this thread as read by this participant.
*
* @param ThreadInterface $thread
* @param ParticipantInterface $participant
* @param bool $isRead
*/
public function markIsReadByThreadAndParticipant(ThreadInterface $thread, ParticipantInterface $participant, $isRead)
{
$this->markIsReadByCondition($participant, $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 $participant
* @param bool $isRead
*/
protected function markIsReadByParticipant(MessageInterface $message, ParticipantInterface $participant, $isRead)
{
$this->markIsReadByCondition($participant, $isRead, function (Builder $queryBuilder) use ($message) {
$queryBuilder->field('_id')->equals(new \MongoId($message->getId()));
});
}
/**
* Marks messages as read/unread
* by updating directly the storage.
*
* @param ParticipantInterface $participant
* @param bool $isRead
* @param \Closure $condition
*/
protected function markIsReadByCondition(ParticipantInterface $participant, $isRead, \Closure $condition)
{
$queryBuilder = $this->repository->createQueryBuilder();
$condition($queryBuilder);
$queryBuilder->update()
->field('metadata.participant.$id')->equals(new \MongoId($participant->getId()));
/* If marking the message as read for a participant, we should pull
* their ID out of the unreadForParticipants array. The same is not
* true for the inverse. We should only add a participant ID to this
* array if the message is not considered spam.
*/
if ($isRead) {
$queryBuilder->field('unreadForParticipants')->pull($participant->getId());
}
$queryBuilder
->field('metadata.$.isRead')->set((bool) $isRead)
->getQuery(array('multiple' => true))
->execute();
/* If marking the message as unread for a participant, add their ID to
* the unreadForParticipants array if the message is not spam. This must
* be done in a separate query, since the criteria is more selective.
*/
if (!$isRead) {
$queryBuilder = $this->repository->createQueryBuilder();
$condition($queryBuilder);
$queryBuilder->update()
->field('metadata.participant.$id')->equals(new \MongoId($participant->getId()))
->field('isSpam')->equals(false)
->field('unreadForParticipants')->addToSet($participant->getId())
->getQuery(array('multiple' => true))
->execute();
}
}
/**
* {@inheritdoc}
*/
public function saveMessage(MessageInterface $message, $andFlush = true)
{
$message->denormalize();
$this->dm->persist($message);
if ($andFlush) {
$this->dm->flush();
}
}
/**
* {@inheritdoc}
*/
public function getClass()
{
return $this->class;
}
/**
* Creates a new MessageMetadata instance.
*
* @return MessageMetadata
*/
protected function createMessageMetadata()
{
return new $this->metaClass();
}
/*
* DENORMALIZATION
*
* All following methods are relative to denormalization
*/
/**
* Performs denormalization tricks.
*
* @param Message $message
*/
public function denormalize(Message $message)
{
$this->doEnsureMessageMetadataExists($message);
$message->denormalize();
}
/**
* Ensures that the message has metadata for each thread participant.
*
* @param Message $message
*/
protected function doEnsureMessageMetadataExists(Message $message)
{
if (!$thread = $message->getThread()) {
throw new \InvalidArgumentException(sprintf('No thread is referenced in message with id "%s"', $message->getId()));
}
foreach ($thread->getParticipants() as $participant) {
if (!$meta = $message->getMetadataForParticipant($participant)) {
$meta = $this->createMessageMetadata();
$meta->setParticipant($participant);
$message->addMetadata($meta);
}
}
}
}