-
Notifications
You must be signed in to change notification settings - Fork 263
/
MessagesController.php
executable file
Β·813 lines (735 loc) Β· 22.2 KB
/
MessagesController.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<?php
declare(strict_types=1);
/**
* @author Alexander Weidinger <alexwegoo@gmail.com>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Christoph Wurst <wurst.christoph@gmail.com>
* @author Jakob Sack <jakob@owncloud.org>
* @author Jan-Christoph Borchardt <hey@jancborchardt.net>
* @author Lukas Reschke <lukas@owncloud.com>
* @author Thomas Imbreckx <zinks@iozero.be>
* @author Thomas MΓΌller <thomas.mueller@tmit.eu>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Mail\Controller;
use Exception;
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
use OCA\Mail\Contracts\IMailTransmission;
use OCA\Mail\Contracts\ITrustedSenderService;
use OCA\Mail\Db\Message;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\AttachmentDownloadResponse;
use OCA\Mail\Http\HtmlResponse;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\ItineraryService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\ZipResponse;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
use function array_map;
class MessagesController extends Controller {
/** @var AccountService */
private $accountService;
/** @var IMailManager */
private $mailManager;
/** @var IMailSearch */
private $mailSearch;
/** @var ItineraryService */
private $itineraryService;
/** @var ?string */
private $currentUserId;
/** @var LoggerInterface */
private $logger;
/** @var Folder */
private $userFolder;
/** @var IMimeTypeDetector */
private $mimeTypeDetector;
/** @var IL10N */
private $l10n;
/** @var IURLGenerator */
private $urlGenerator;
/** @var ContentSecurityPolicyNonceManager */
private $nonceManager;
/** @var ITrustedSenderService */
private $trustedSenderService;
/** @var IMailTransmission */
private $mailTransmission;
/**
* @param string $appName
* @param IRequest $request
* @param AccountService $accountService
* @param IMailManager $mailManager
* @param IMailSearch $mailSearch
* @param ItineraryService $itineraryService
* @param string $UserId
* @param $userFolder
* @param LoggerInterface $logger
* @param IL10N $l10n
* @param IMimeTypeDetector $mimeTypeDetector
* @param IURLGenerator $urlGenerator
* @param ContentSecurityPolicyNonceManager $nonceManager
* @param ITrustedSenderService $trustedSenderService
* @param IMailTransmission $mailTransmission
*/
public function __construct(string $appName,
IRequest $request,
AccountService $accountService,
IMailManager $mailManager,
IMailSearch $mailSearch,
ItineraryService $itineraryService,
?string $UserId,
$userFolder,
LoggerInterface $logger,
IL10N $l10n,
IMimeTypeDetector $mimeTypeDetector,
IURLGenerator $urlGenerator,
ContentSecurityPolicyNonceManager $nonceManager,
ITrustedSenderService $trustedSenderService,
IMailTransmission $mailTransmission) {
parent::__construct($appName, $request);
$this->accountService = $accountService;
$this->mailManager = $mailManager;
$this->mailSearch = $mailSearch;
$this->itineraryService = $itineraryService;
$this->currentUserId = $UserId;
$this->userFolder = $userFolder;
$this->logger = $logger;
$this->l10n = $l10n;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->urlGenerator = $urlGenerator;
$this->nonceManager = $nonceManager;
$this->trustedSenderService = $trustedSenderService;
$this->mailTransmission = $mailTransmission;
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $mailboxId
* @param int $cursor
* @param string $filter
* @param int|null $limit
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function index(int $mailboxId,
int $cursor = null,
string $filter = null,
int $limit = null): JSONResponse {
try {
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $mailboxId);
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->logger->debug("loading messages of folder <$mailboxId>");
return new JSONResponse(
$this->mailSearch->findMessages(
$account,
$mailbox,
$filter === '' ? null : $filter,
$cursor,
$limit
)
);
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $accountId
* @param string $folderId
* @param int $id
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function show(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->logger->debug("loading message <$id>");
return new JSONResponse(
$this->mailSearch->findMessage(
$account,
$mailbox,
$message
)
);
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function getBody(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$json = $this->mailManager->getImapMessage(
$account,
$mailbox,
$message->getUid(),
true
)->getFullMessage($id);
$itineraries = $this->itineraryService->getCached($account, $mailbox, $message->getUid());
if ($itineraries) {
$json['itineraries'] = $itineraries;
}
$json['attachments'] = array_map(function ($a) use ($id) {
return $this->enrichDownloadUrl(
$id,
$a
);
}, $json['attachments']);
$json['accountId'] = $account->getId();
$json['mailboxId'] = $mailbox->getId();
$json['databaseId'] = $message->getId();
$json['isSenderTrusted'] = $this->isSenderTrusted($message);
$response = new JSONResponse($json);
// Enable caching
$response->cacheFor(60 * 60);
return $response;
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
*
* @return JSONResponse
*
* @throws ClientException
*/
public function getItineraries(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
return new JsonResponse($this->itineraryService->extract($account, $mailbox, $message->getUid()));
}
private function isSenderTrusted(Message $message): bool {
$from = $message->getFrom();
$first = $from->first();
if ($first === null) {
return false;
}
$email = $first->getEmail();
if ($email === null) {
return false;
}
return $this->trustedSenderService->isTrusted(
$this->currentUserId,
$email
);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @TrapError
*
* @param int $id
*
* @return JSONResponse
* @throws ClientException
*/
public function getThread(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
return new JSONResponse($this->mailManager->getThread($account, $message->getThreadRootId()));
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param int $destFolderId
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function move(int $id, int $destFolderId): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$srcMailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$dstMailbox = $this->mailManager->getMailbox($this->currentUserId, $destFolderId);
$srcAccount = $this->accountService->find($this->currentUserId, $srcMailbox->getAccountId());
$dstAccount = $this->accountService->find($this->currentUserId, $dstMailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->moveMessage(
$srcAccount,
$srcMailbox->getName(),
$message->getUid(),
$dstAccount,
$dstMailbox->getName()
);
return new JSONResponse();
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function mdn(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
if ($message->getFlagMdnsent()) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
try {
$this->mailTransmission->sendMdn($account, $mailbox, $message);
$this->mailManager->flagMessage($account, $mailbox->getName(), $message->getUid(), 'mdnsent', true);
} catch (ServiceException $ex) {
$this->logger->error('Sending mdn failed: ' . $ex->getMessage());
throw $ex;
}
return new JSONResponse();
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @TrapError
*
* @param int $accountId
* @param string $folderId
* @param int $messageId
*
* @return JSONResponse
* @throws ServiceException
*/
public function getSource(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$response = new JSONResponse([
'source' => $this->mailManager->getSource(
$account,
$mailbox->getName(),
$message->getUid()
)
]);
// Enable caching
$response->cacheFor(60 * 60);
return $response;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @TrapError
*
* @param int $id
* @param bool $plain do not inject scripts if true (default=false)
*
* @return HtmlResponse|TemplateResponse
*
* @throws ClientException
*/
public function getHtmlBody(int $id, bool $plain = false): Response {
try {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new TemplateResponse(
$this->appName,
'error',
['message' => 'Not allowed'],
'none'
);
}
$html = $this->mailManager->getImapMessage(
$account,
$mailbox,
$message->getUid(),
true
)->getHtmlBody(
$id
);
$htmlResponse = $plain ?
HtmlResponse::plain($html) :
HtmlResponse::withResizer(
$html,
$this->nonceManager->getNonce(),
$this->urlGenerator->getAbsoluteURL(
$this->urlGenerator->linkTo('mail', 'js/htmlresponse.js')
)
);
// Harden the default security policy
$policy = new ContentSecurityPolicy();
$policy->allowEvalScript(false);
$policy->disallowScriptDomain('\'self\'');
$policy->disallowConnectDomain('\'self\'');
$policy->disallowFontDomain('\'self\'');
$policy->disallowMediaDomain('\'self\'');
$htmlResponse->setContentSecurityPolicy($policy);
// Enable caching
$htmlResponse->cacheFor(60 * 60);
return $htmlResponse;
} catch (Exception $ex) {
return new TemplateResponse(
$this->appName,
'error',
['message' => $ex->getMessage()],
'none'
);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @TrapError
*
* @param int $accountId
* @param string $folderId
* @param int $id
* @param string $attachmentId
*
* @return Response
*
* @throws ClientException
* @throws ServiceException
*/
public function downloadAttachment(int $id,
string $attachmentId): Response {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$folder = $account->getMailbox($mailbox->getName());
$attachment = $folder->getAttachment($message->getUid(), $attachmentId);
// Body party and embedded messages do not have a name
if ($attachment->getName() === null) {
return new AttachmentDownloadResponse(
$attachment->getContents(),
$this->l10n->t('Embedded message %s', [
$attachmentId,
]) . '.eml',
$attachment->getType()
);
}
return new AttachmentDownloadResponse(
$attachment->getContents(),
$attachment->getName(),
$attachment->getType()
);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @TrapError
*
* @param int $id the message id
* @param string $attachmentId
*
* @return ZipResponse|JSONResponse
*
* @throws ClientException
* @throws ServiceException
* @throws DoesNotExistException
*/
public function downloadAttachments(int $id): Response {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$attachments = $this->mailManager->getMailAttachments($account, $mailbox, $message);
$zip = new ZipResponse($this->request, 'attachments');
foreach ($attachments as $attachment) {
$fileName = $attachment['name'];
$fh = fopen("php://temp", 'r+');
fputs($fh, $attachment['content']);
$size = (int)$attachment['size'];
rewind($fh);
$zip->addResource($fh, $fileName, $size);
}
return $zip;
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param string $attachmentId
* @param string $targetPath
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function saveAttachment(int $id,
string $attachmentId,
string $targetPath) {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$folder = $account->getMailbox($mailbox->getName());
if ($attachmentId === '0') {
// Save all attachments
/* @var $m IMAPMessage */
$m = $folder->getMessage($message->getUid());
$attachmentIds = array_map(function ($a) {
return $a['id'];
}, $m->attachments);
} else {
$attachmentIds = [$attachmentId];
}
foreach ($attachmentIds as $aid) {
$attachment = $folder->getAttachment($message->getUid(), $aid);
$fileName = $attachment->getName() ?? $this->l10n->t('Embedded message %s', [
$aid,
]) . '.eml';
$fileParts = pathinfo($fileName);
$fileName = $fileParts['filename'];
$fileExtension = $fileParts['extension'];
$fullPath = "$targetPath/$fileName.$fileExtension";
$counter = 2;
while ($this->userFolder->nodeExists($fullPath)) {
$fullPath = "$targetPath/$fileName ($counter).$fileExtension";
$counter++;
}
$newFile = $this->userFolder->newFile($fullPath);
$newFile->putContent($attachment->getContents());
}
return new JSONResponse();
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param array $flags
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function setFlags(int $id, array $flags): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
foreach ($flags as $flag => $value) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
$this->mailManager->flagMessage($account, $mailbox->getName(), $message->getUid(), $flag, $value);
}
return new JSONResponse();
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param string $imapLabel
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function setTag(int $id, string $imapLabel): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
try {
$tag = $this->mailManager->getTagByImapLabel($imapLabel, $this->currentUserId);
} catch (ClientException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->tagMessage($account, $mailbox->getName(), $message, $tag, true);
return new JSONResponse($tag);
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param string $imapLabel
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function removeTag(int $id, string $imapLabel): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
try {
$tag = $this->mailManager->getTagByImapLabel($imapLabel, $this->currentUserId);
} catch (ClientException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->mailManager->tagMessage($account, $mailbox->getName(), $message, $tag, false);
return new JSONResponse($tag);
}
/**
* @NoAdminRequired
* @TrapError
*
* @param int $accountId
* @param string $folderId
* @param int $id
*
* @return JSONResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function destroy(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$this->logger->debug("deleting message <$id>");
$this->mailManager->deleteMessage(
$account,
$mailbox->getName(),
$message->getUid()
);
return new JSONResponse();
}
/**
* @param int $id
* @param array $attachment
*
* @return array
*/
private function enrichDownloadUrl(int $id,
array $attachment) {
$downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachment',
[
'id' => $id,
'attachmentId' => $attachment['id'],
]);
$downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl);
$attachment['downloadUrl'] = $downloadUrl;
$attachment['mimeUrl'] = $this->mimeTypeDetector->mimeTypeIcon($attachment['mime']);
$attachment['isImage'] = $this->attachmentIsImage($attachment);
$attachment['isCalendarEvent'] = $this->attachmentIsCalendarEvent($attachment);
return $attachment;
}
/**
* Determines if the content of this attachment is an image
*
* @param array $attachment
*
* @return boolean
*/
private function attachmentIsImage(array $attachment): bool {
return in_array(
$attachment['mime'], [
'image/jpeg',
'image/png',
'image/gif'
]);
}
/**
* @param array $attachment
*
* @return boolean
*/
private function attachmentIsCalendarEvent(array $attachment): bool {
return in_array($attachment['mime'], ['text/calendar', 'application/ics'], true);
}
}