forked from FriendsOfSymfony/FOSMessageBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.php
228 lines (198 loc) · 6.5 KB
/
Thread.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
222
223
224
225
226
227
228
<?php
namespace FOS\MessageBundle\Document;
use FOS\MessageBundle\Model\Thread as AbstractThread;
use FOS\MessageBundle\Model\MessageInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\MessageBundle\Model\ParticipantInterface;
abstract class Thread extends AbstractThread
{
/**
* Date that the last message in this thread was created at
*
* This denormalization field is used for sorting threads in the inbox and
* sent list.
*
* @var DateTime
*/
protected $lastMessageDate;
/**
* All text contained in the thread messages
* Used for the full text search
*
* @var string
*/
protected $keywords = '';
/**
* The activeParticipants array is a union of the activeRecipients and
* activeSenders arrays.
*
* @var array of participant ID's
*/
protected $activeParticipants = array();
/**
* The activeRecipients array will contain a participant's ID if the thread
* is not deleted for the participant, the thread is not spam and at least
* one message in the thread is not created by the participant.
*
* @var array of participant ID's
*/
protected $activeRecipients = array();
/**
* The activeSenders array will contain a participant's ID if the thread is
* not deleted for the participant and at least one message in the thread
* is created by the participant.
*
* @var array of participant ID's
*/
protected $activeSenders = array();
/**
* Gets the users participating in this conversation
*
* @return array of ParticipantInterface
*/
public function getParticipants()
{
return $this->participants->toArray();
}
/**
* Adds a participant to the thread
* If it already exists, nothing is done.
*
* @param ParticipantInterface $participant
* @return null
*/
public function addParticipant(ParticipantInterface $participant)
{
if (!$this->isParticipant($participant)) {
$this->participants->add($participant);
}
}
/**
* Tells if the user participates to the conversation
*
* @param ParticipantInterface $participant
* @return boolean
*/
public function isParticipant(ParticipantInterface $participant)
{
return $this->participants->contains($participant);
}
/**
* DENORMALIZATION
*
* All following methods are relative to denormalization
*/
/**
* Performs denormalization tricks
*/
public function denormalize()
{
$this->doCreatedByAndAt();
$this->doLastMessageDate();
$this->doKeywords();
$this->doSpam();
$this->doMetadataLastMessageDates();
$this->doEnsureActiveParticipantArrays();
}
/**
* Ensures that the createdBy & createdAt properties are set
*/
protected function doCreatedByAndAt()
{
if (null !== $this->getCreatedBy()) {
return;
}
if (!$message = $this->getFirstMessage()) {
return;
}
$this->setCreatedBy($message->getSender());
$this->setCreatedAt($message->getCreatedAt());
}
/**
* Ensures that the lastMessageDate property is up to date
*/
protected function doLastMessageDate()
{
if (!$message = $this->getLastMessage()) {
return;
}
$this->lastMessageDate = $message->getCreatedAt();
}
/**
* Adds all messages contents to the keywords property
*/
protected function doKeywords()
{
$keywords = $this->getSubject();
foreach ($this->getMessages() as $message) {
$keywords .= ' '.$message->getBody();
}
// we only need each word once
$this->keywords = implode(' ', array_unique(str_word_count(mb_strtolower($keywords, 'UTF-8'), 1)));
}
/**
* Denormalizes the value of isSpam to messages
*/
protected function doSpam()
{
foreach ($this->getMessages() as $message) {
$message->setIsSpam($this->getIsSpam());
}
}
/**
* Ensures that metadata last message dates are up to date
*
* Precondition: metadata exists for all thread participants
*/
protected function doMetadataLastMessageDates()
{
foreach ($this->metadata as $meta) {
foreach ($this->getMessages() as $message) {
if ($meta->getParticipant()->getId() !== $message->getSender()->getId()) {
if (null === $meta->getLastMessageDate() || $meta->getLastMessageDate()->getTimestamp() < $message->getTimestamp()) {
$meta->setLastMessageDate($message->getCreatedAt());
}
} else {
if (null === $meta->getLastParticipantMessageDate() || $meta->getLastParticipantMessageDate()->getTimestamp() < $message->getTimestamp()) {
$meta->setLastParticipantMessageDate($message->getCreatedAt());
}
}
}
}
}
/**
* Ensures that active participant, recipient and sender arrays are updated.
*/
protected function doEnsureActiveParticipantArrays()
{
$this->activeParticipants = array();
$this->activeRecipients = array();
$this->activeSenders = array();
foreach ($this->getParticipants() as $participant) {
if ($this->isDeletedByParticipant($participant)) {
continue;
}
$participantIsActiveRecipient = $participantIsActiveSender = false;
foreach ($this->getMessages() as $message) {
if ($message->getSender()->getId() === $participant->getId()) {
$participantIsActiveSender = true;
} elseif (!$this->getIsSpam()) {
$participantIsActiveRecipient = true;
}
if ($participantIsActiveRecipient && $participantIsActiveSender) {
break;
}
}
if ($participantIsActiveSender) {
$this->activeSenders[] = $participant->getId();
}
if ($participantIsActiveRecipient) {
$this->activeRecipients[] = $participant->getId();
}
if ($participantIsActiveSender || $participantIsActiveRecipient) {
$this->activeParticipants[] = $participant->getId();
}
}
}
}