Newer
Older
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
<?php declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Mail\Db;
use Horde_Imap_Client;
use OCA\Mail\Address;
use OCA\Mail\AddressList;
use OCA\Mail\Service\Search\SearchQuery;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use function array_combine;
use function array_keys;
use function array_map;
class MessageMapper extends QBMapper {
/** @var ITimeFactory */
private $timeFactory;
public function __construct(IDBConnection $db,
ITimeFactory $timeFactory) {
parent::__construct($db, 'mail_messages');
$this->timeFactory = $timeFactory;
}
public function findAllUids(Mailbox $mailbox): array {
$query = $this->db->getQueryBuilder();
$query->select('uid')
->from($this->getTableName())
->where($query->expr()->eq('mailbox_id', $query->createNamedParameter($mailbox->getId())));
$result = $query->execute();
$uids = array_map(function (array $row) {
return (int) $row['uid'];
}, $result->fetchAll());
$result->closeCursor();
return $uids;
}
public function insertBulk(Message ...$messages): void {
$this->db->beginTransaction();
$qb1 = $this->db->getQueryBuilder();
$qb1->insert($this->getTableName());
$qb1->setValue('uid', $qb1->createParameter('uid'));
$qb1->setValue('message_id', $qb1->createParameter('message_id'));
$qb1->setValue('mailbox_id', $qb1->createParameter('mailbox_id'));
$qb1->setValue('subject', $qb1->createParameter('subject'));
$qb1->setValue('sent_at', $qb1->createParameter('sent_at'));
$qb1->setValue('flag_answered', $qb1->createParameter('flag_answered'));
$qb1->setValue('flag_deleted', $qb1->createParameter('flag_deleted'));
$qb1->setValue('flag_draft', $qb1->createParameter('flag_draft'));
$qb1->setValue('flag_flagged', $qb1->createParameter('flag_flagged'));
$qb1->setValue('flag_seen', $qb1->createParameter('flag_seen'));
$qb1->setValue('flag_forwarded', $qb1->createParameter('flag_forwarded'));
$qb1->setValue('flag_junk', $qb1->createParameter('flag_junk'));
$qb1->setValue('flag_notjunk', $qb1->createParameter('flag_notjunk'));
$qb2 = $this->db->getQueryBuilder();
$qb2->insert('mail_recipients')
->setValue('message_id', $qb2->createParameter('message_id'))
->setValue('type', $qb2->createParameter('type'))
->setValue('label', $qb2->createParameter('label'))
->setValue('email', $qb2->createParameter('email'));
foreach ($messages as $message) {
$qb1->setParameter('uid', $message->getUid(), IQueryBuilder::PARAM_INT);
$qb1->setParameter('message_id', $message->getMessageId(), IQueryBuilder::PARAM_STR);
$qb1->setParameter('mailbox_id', $message->getMailboxId(), IQueryBuilder::PARAM_INT);
$qb1->setParameter('subject', $message->getSubject(), IQueryBuilder::PARAM_STR);
$qb1->setParameter('sent_at', $message->getSentAt(), IQueryBuilder::PARAM_INT);
$qb1->setParameter('flag_answered', $message->getFlagAnswered(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_deleted', $message->getFlagDeleted(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_draft', $message->getFlagDraft(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_flagged', $message->getFlagFlagged(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_seen', $message->getFlagSeen(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_forwarded', $message->getFlagForwarded(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_junk', $message->getFlagJunk(), IQueryBuilder::PARAM_BOOL);
$qb1->setParameter('flag_notjunk', $message->getFlagNotjunk(), IQueryBuilder::PARAM_BOOL);
$qb1->execute();
$messageId = $qb1->getLastInsertId();
$recipientTypes = [
Address::TYPE_FROM => $message->getFrom(),
Address::TYPE_TO => $message->getTo(),
Address::TYPE_CC => $message->getCc(),
Address::TYPE_BCC => $message->getBcc(),
];
foreach ($recipientTypes as $type => $recipients) {
/** @var AddressList $recipients */
foreach ($recipients->iterate() as $recipient) {
/** @var Address $recipient */
if ($recipient->getEmail() === null) {
// If for some reason the e-mail is not set we should ignore this entry
continue;
}
$qb2->setParameter('message_id', $messageId, IQueryBuilder::PARAM_INT);
$qb2->setParameter('type', $type, IQueryBuilder::PARAM_INT);
$qb2->setParameter('label', $recipient->getLabel(), IQueryBuilder::PARAM_STR);
$qb2->setParameter('email', $recipient->getEmail(), IQueryBuilder::PARAM_STR);
$qb2->execute();
}
}
}
$this->db->commit();
}
public function updateBulk(Message ...$messages): void {
$this->db->beginTransaction();
$query = $this->db->getQueryBuilder();
$query->update($this->getTableName())
->set('flag_answered', $query->createParameter('flag_answered'))
->set('flag_deleted', $query->createParameter('flag_deleted'))
->set('flag_draft', $query->createParameter('flag_draft'))
->set('flag_flagged', $query->createParameter('flag_flagged'))
->set('flag_seen', $query->createParameter('flag_seen'))
->set('flag_forwarded', $query->createParameter('flag_forwarded'))
->set('flag_junk', $query->createParameter('flag_junk'))
->set('flag_notjunk', $query->createParameter('flag_notjunk'))
->set('updated_at', $query->createNamedParameter($this->timeFactory->getTime()))
->where($query->expr()->andX(
$query->expr()->eq('uid', $query->createParameter('uid')),
$query->expr()->eq('mailbox_id', $query->createParameter('mailbox_id'))
));
foreach ($messages as $message) {
$query->setParameter('uid', $message->getUid(), IQueryBuilder::PARAM_INT);
$query->setParameter('mailbox_id', $message->getMailboxId(), IQueryBuilder::PARAM_INT);
$query->setParameter('flag_answered', $message->getFlagAnswered(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_deleted', $message->getFlagDeleted(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_draft', $message->getFlagDraft(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_flagged', $message->getFlagFlagged(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_seen', $message->getFlagSeen(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_forwarded', $message->getFlagForwarded(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_junk', $message->getFlagJunk(), IQueryBuilder::PARAM_BOOL);
$query->setParameter('flag_notjunk', $message->getFlagNotjunk(), IQueryBuilder::PARAM_BOOL);
$query->execute();
}
$this->db->commit();
}
public function deleteAll(Mailbox $mailbox): void {
$query = $this->db->getQueryBuilder();
$query->delete($this->getTableName())
->where($query->expr()->eq('mailbox_id', $query->createNamedParameter($mailbox->getId())));
$query->execute();
}
public function deleteByUid(Mailbox $mailbox, int ...$uids): void {
$query = $this->db->getQueryBuilder();
$query->delete($this->getTableName())
->where(
$query->expr()->eq('mailbox_id', $query->createNamedParameter($mailbox->getId())),
$query->expr()->in('uid', $query->createNamedParameter($uids, IQueryBuilder::PARAM_INT_ARRAY))
);
$query->execute();
}
/**
* @param Mailbox $mailbox
* @param SearchQuery $query
*
* @return int[]
*/
public function findUidsByQuery(Mailbox $mailbox, SearchQuery $query, array $uids = null): array {
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
229
230
231
232
233
234
235
$qb = $this->db->getQueryBuilder();
$select = $qb
->selectDistinct('m.uid')
->from($this->getTableName(), 'm');
if (!empty($query->getFrom())) {
$select->innerJoin('m', 'mail_recipients', 'r0', 'm.id = r0.message_id');
}
if (!empty($query->getTo())) {
$select->innerJoin('m', 'mail_recipients', 'r1', 'm.id = r1.message_id');
}
if (!empty($query->getCc())) {
$select->innerJoin('m', 'mail_recipients', 'r2', 'm.id = r2.message_id');
}
if (!empty($query->getBcc())) {
$select->innerJoin('m', 'mail_recipients', 'r3', 'm.id = r3.message_id');
}
$select->where(
$qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailbox->getId()), IQueryBuilder::PARAM_INT)
);
if (!empty($query->getFrom())) {
$select->andWhere(
$qb->expr()->in('r0.email', $qb->createNamedParameter($query->getFrom(), IQueryBuilder::PARAM_STR_ARRAY))
);
}
if (!empty($query->getTo())) {
$select->andWhere(
$qb->expr()->in('r1.email', $qb->createNamedParameter($query->getTo(), IQueryBuilder::PARAM_STR_ARRAY))
);
}
if (!empty($query->getCc())) {
$select->andWhere(
$qb->expr()->in('r2.email', $qb->createNamedParameter($query->getCc(), IQueryBuilder::PARAM_STR_ARRAY))
);
}
if (!empty($query->getBcc())) {
$select->andWhere(
$qb->expr()->in('r3.email', $qb->createNamedParameter($query->getBcc(), IQueryBuilder::PARAM_STR_ARRAY))
);
}
if ($query->getCursor() !== null) {
$select->andWhere(
$qb->expr()->lt('sent_at', $qb->createNamedParameter($query->getCursor(), IQueryBuilder::PARAM_INT))
);
}
if ($uids !== null) {
$select->andWhere(
$qb->expr()->in('uid', $qb->createNamedParameter($uids, IQueryBuilder::PARAM_INT_ARRAY))
);
}
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
$flags = $query->getFlags();
$flagKeys = array_keys($flags);
foreach ([
Horde_Imap_Client::FLAG_ANSWERED,
Horde_Imap_Client::FLAG_DELETED,
Horde_Imap_Client::FLAG_DRAFT,
Horde_Imap_Client::FLAG_FLAGGED,
Horde_Imap_Client::FLAG_RECENT,
Horde_Imap_Client::FLAG_SEEN,
Horde_Imap_Client::FLAG_FORWARDED,
Horde_Imap_Client::FLAG_JUNK,
Horde_Imap_Client::FLAG_NOTJUNK,
] as $flag) {
if (in_array($flag, $flagKeys, true)) {
$key = ltrim($flag, '\\');
$select->andWhere($qb->expr()->eq("flag_$key", $qb->createNamedParameter($flags[$flag], IQueryBuilder::PARAM_BOOL)));
}
}
$select = $select
->orderBy('sent_at', 'desc')
->setMaxResults(20);
return array_map(function (Message $message) {
return $message->getUid();
}, $this->findEntities($select));
}
/**
* @param Mailbox $mailbox
* @param int[] $uids
*
* @return Message[]
*/
public function findByUids(Mailbox $mailbox, array $uids): array {
$qb = $this->db->getQueryBuilder();
$select = $qb
->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailbox->getId()), IQueryBuilder::PARAM_INT),
$qb->expr()->in('uid', $qb->createNamedParameter($uids, IQueryBuilder::PARAM_INT_ARRAY))
)
->orderBy('sent_at', 'desc');
return $this->findRecipients($this->findEntities($select));
}
/**
* @param Message[] $messages
* @return Message[]
*/
private function findRecipients(array $messages): array {
/** @var Message[] $indexedMessages */
$indexedMessages = array_combine(
array_map(function (Message $msg) {
return $msg->getId();
}, $messages),
$messages
);
$qb2 = $this->db->getQueryBuilder();
$qb2->select('label', 'email', 'type', 'message_id')
->from('mail_recipients')
->where(
$qb2->expr()->in('message_id', $qb2->createNamedParameter(array_keys($indexedMessages), IQueryBuilder::PARAM_INT_ARRAY))
);
$recipientsResult = $qb2->execute();
foreach ($recipientsResult->fetchAll() as $recipient) {
$message = $indexedMessages[(int)$recipient['message_id']];
switch ($recipient['type']) {
case Address::TYPE_FROM:
$message->setFrom(
$message->getFrom()->merge(AddressList::fromRow($recipient))
);
break;
case Address::TYPE_TO:
$message->setTo(
$message->getTo()->merge(AddressList::fromRow($recipient))
);
break;
case Address::TYPE_CC:
$message->setCc(
$message->getCc()->merge(AddressList::fromRow($recipient))
);
break;
case Address::TYPE_BCC:
$message->setFrom(
$message->getFrom()->merge(AddressList::fromRow($recipient))
);
break;
}
}
$recipientsResult->closeCursor();
return $messages;
}
public function findNew(Mailbox $mailbox, int $highest): array {
$qb = $this->db->getQueryBuilder();
$select = $qb
->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailbox->getId(), IQueryBuilder::PARAM_INT)),
$qb->expr()->gt('uid', $qb->createNamedParameter($highest, IQueryBuilder::PARAM_INT))
);
return $this->findRecipients($this->findEntities($select));
}
public function findChanged(Mailbox $mailbox, int $since): array {
$qb = $this->db->getQueryBuilder();
$select = $qb
->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailbox->getId(), IQueryBuilder::PARAM_INT)),
$qb->expr()->gt('updated_at', $qb->createNamedParameter($since, IQueryBuilder::PARAM_INT))
);
return $this->findRecipients($this->findEntities($select));
}
}