-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNewMessageViewModel.kt
1003 lines (840 loc) · 40.6 KB
/
NewMessageViewModel.kt
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Infomaniak Mail - Android
* Copyright (C) 2022-2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.ui.newMessage
import android.app.Application
import android.content.ClipDescription
import android.content.Intent
import android.net.Uri
import android.os.Parcelable
import androidx.core.app.NotificationManagerCompat
import androidx.core.net.MailTo
import androidx.core.net.toUri
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.asLiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import com.infomaniak.lib.core.MatomoCore.TrackerAction
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.lib.core.utils.SingleLiveEvent
import com.infomaniak.lib.core.utils.getFileNameAndSize
import com.infomaniak.lib.core.utils.guessMimeType
import com.infomaniak.lib.core.utils.parcelableArrayListExtra
import com.infomaniak.lib.core.utils.parcelableExtra
import com.infomaniak.lib.core.utils.showToast
import com.infomaniak.mail.MatomoMail.OPEN_LOCAL_DRAFT
import com.infomaniak.mail.MatomoMail.trackExternalEvent
import com.infomaniak.mail.MatomoMail.trackNewMessageEvent
import com.infomaniak.mail.MatomoMail.trackSendingDraftEvent
import com.infomaniak.mail.R
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.RealmDatabase
import com.infomaniak.mail.data.cache.mailboxContent.DraftController
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.SignatureController
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.data.cache.userInfo.MergedContactController
import com.infomaniak.mail.data.models.Attachment
import com.infomaniak.mail.data.models.Attachment.UploadStatus
import com.infomaniak.mail.data.models.FeatureFlag
import com.infomaniak.mail.data.models.correspondent.Recipient
import com.infomaniak.mail.data.models.draft.Draft
import com.infomaniak.mail.data.models.draft.Draft.DraftAction
import com.infomaniak.mail.data.models.draft.Draft.DraftMode
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.data.models.message.Body
import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.data.models.signature.Signature
import com.infomaniak.mail.di.IoDispatcher
import com.infomaniak.mail.di.MainDispatcher
import com.infomaniak.mail.ui.main.SnackbarManager
import com.infomaniak.mail.ui.newMessage.NewMessageEditorManager.EditorAction
import com.infomaniak.mail.ui.newMessage.NewMessageRecipientFieldsManager.FieldType
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.SignatureScore.EXACT_MATCH
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.SignatureScore.EXACT_MATCH_AND_IS_DEFAULT
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.SignatureScore.NO_MATCH
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.SignatureScore.ONLY_EMAIL_MATCH
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.SignatureScore.ONLY_EMAIL_MATCH_AND_IS_DEFAULT
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.ContactUtils.arrangeMergedContacts
import com.infomaniak.mail.utils.LocalStorageUtils
import com.infomaniak.mail.utils.MessageBodyUtils
import com.infomaniak.mail.utils.SentryDebug
import com.infomaniak.mail.utils.SharedUtils
import com.infomaniak.mail.utils.SignatureUtils
import com.infomaniak.mail.utils.Utils
import com.infomaniak.mail.utils.coroutineContext
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.findSpecificAttachment
import com.infomaniak.mail.utils.extensions.appContext
import com.infomaniak.mail.utils.extensions.htmlToText
import com.infomaniak.mail.utils.extensions.isEmail
import com.infomaniak.mail.utils.extensions.textToHtml
import com.infomaniak.mail.utils.extensions.valueOrEmpty
import com.infomaniak.mail.utils.uploadAttachmentsWithMutex
import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.kotlin.MutableRealm
import io.realm.kotlin.Realm
import io.realm.kotlin.TypedRealm
import io.realm.kotlin.ext.copyFromRealm
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.types.RealmList
import io.sentry.Sentry
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
@HiltViewModel
class NewMessageViewModel @Inject constructor(
application: Application,
private val savedStateHandle: SavedStateHandle,
private val aiSharedData: AiSharedData,
private val draftController: DraftController,
private val globalCoroutineScope: CoroutineScope,
private val mailboxContentRealm: RealmDatabase.MailboxContent,
private val mailboxController: MailboxController,
private val mergedContactController: MergedContactController,
private val notificationManagerCompat: NotificationManagerCompat,
private val sharedUtils: SharedUtils,
private val signatureUtils: SignatureUtils,
private val snackbarManager: SnackbarManager,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
@MainDispatcher private val mainDispatcher: CoroutineDispatcher,
) : AndroidViewModel(application) {
private val ioCoroutineContext = viewModelScope.coroutineContext(ioDispatcher)
//region UI data
val fromLiveData = MutableLiveData<UiFrom>()
val toLiveData = MutableLiveData<UiRecipients>()
val ccLiveData = MutableLiveData<UiRecipients>()
val bccLiveData = MutableLiveData<UiRecipients>()
val attachmentsLiveData = MutableLiveData<List<Attachment>>()
val uiSignatureLiveData = MutableLiveData<String?>()
val uiQuoteLiveData = MutableLiveData<String?>()
//endregion
var lastOnStopSubjectValue = ""
var lastOnStopBodyValue = ""
var isAutoCompletionOpened = false
var isEditorExpanded = false
var isExternalBannerManuallyClosed = false
var shouldSendInsteadOfSave = false
var signaturesCount = 0
private var isNewMessage = false
private var snapshot: DraftSnapshot? = null
var otherRecipientsFieldsAreEmpty = MutableLiveData(true)
var initializeFieldsAsOpen = SingleLiveEvent<Boolean>()
val importAttachmentsLiveData = SingleLiveEvent<List<Uri>>()
val importAttachmentsResult = SingleLiveEvent<ImportationResult>()
val isSendingAllowed = SingleLiveEvent(false)
val externalRecipientCount = SingleLiveEvent<Pair<String?, Int>>()
// Boolean: For toggleable actions, `false` if the formatting has been removed and `true` if the formatting has been applied.
val editorAction = SingleLiveEvent<Pair<EditorAction, Boolean?>>()
// Needs to trigger every time the Fragment is recreated
val initResult = MutableLiveData<InitResult>()
val currentMailbox by lazy { mailboxController.getMailbox(AccountUtils.currentUserId, AccountUtils.currentMailboxId)!! }
val currentMailboxLive = mailboxController.getMailboxAsync(
AccountUtils.currentUserId,
AccountUtils.currentMailboxId,
).map { it.obj }.asLiveData(ioCoroutineContext)
val mergedContacts = liveData(ioCoroutineContext) {
val list = mergedContactController.getMergedContacts(sorted = true).copyFromRealm()
emit(list to arrangeMergedContacts(list))
}
private val arrivedFromExistingDraft
inline get() = savedStateHandle.get<Boolean>(NewMessageActivityArgs::arrivedFromExistingDraft.name) ?: false
private val draftLocalUuid
inline get() = savedStateHandle.get<String?>(NewMessageActivityArgs::draftLocalUuid.name)
private val draftMode
inline get() = savedStateHandle.get<DraftMode>(NewMessageActivityArgs::draftMode.name) ?: DraftMode.NEW_MAIL
private val draftResource
inline get() = savedStateHandle.get<String?>(NewMessageActivityArgs::draftResource.name)
private val mailToUri
inline get() = savedStateHandle.get<Uri?>(NewMessageActivityArgs::mailToUri.name)
private val messageUid
inline get() = savedStateHandle.get<String?>(NewMessageActivityArgs::messageUid.name)
private val notificationId
inline get() = savedStateHandle.get<Int>(NewMessageActivityArgs::notificationId.name) ?: -1
private val previousMessageUid
inline get() = savedStateHandle.get<String?>(NewMessageActivityArgs::previousMessageUid.name)
private val recipient
inline get() = savedStateHandle.get<Recipient?>(NewMessageActivityArgs::recipient.name)
private val shouldLoadDistantResources
inline get() = savedStateHandle.get<Boolean>(NewMessageActivityArgs::shouldLoadDistantResources.name) ?: false
fun arrivedFromExistingDraft() = arrivedFromExistingDraft
fun draftLocalUuid() = draftLocalUuid
fun draftMode() = draftMode
fun recipient() = recipient
fun shouldLoadDistantResources() = shouldLoadDistantResources
fun initDraftAndViewModel(intent: Intent): LiveData<Draft?> = liveData(ioCoroutineContext) {
val realm = mailboxContentRealm()
var signatures = emptyList<Signature>()
var draft: Draft? = null
runCatching {
signatures = SignatureController.getAllSignatures(realm)
.also { signaturesCount = it.count() }
.toMutableList()
.apply { add(index = 0, element = Signature.getDummySignature(appContext, email = currentMailbox.email)) }
isNewMessage = !arrivedFromExistingDraft && draftLocalUuid == null
draft = if (isNewMessage) {
getNewDraft(signatures, intent, realm) ?: return@runCatching
} else {
getExistingDraft(draftLocalUuid, realm) ?: return@runCatching
}
}.onFailure(Sentry::captureException)
draft?.let {
it.flagRecipientsAsAutomaticallyEntered()
dismissNotification()
markAsRead(currentMailbox, realm)
realm.writeBlocking { draftController.upsertDraft(it, realm = this) }
it.saveSnapshot()
it.initLiveData(signatures)
initResult.postValue(InitResult(it, signatures))
}
emit(draft)
}
private fun getExistingDraft(localUuid: String?, realm: Realm): Draft? {
return getLocalOrRemoteDraft(localUuid)?.also { draft ->
saveNavArgsToSavedState(draft.localUuid)
if (draft.identityId.isNullOrBlank()) {
draft.identityId = SignatureController.getDefaultSignatureWithFallback(realm, draftMode)?.id?.toString()
}
if (draft.body.isNotEmpty()) splitSignatureAndQuoteFromBody(draft)
}
}
private suspend fun getNewDraft(signatures: List<Signature>, intent: Intent, realm: Realm): Draft? = Draft().apply {
var previousMessage: Message? = null
initLocalValues(mimeType = ClipDescription.MIMETYPE_TEXT_HTML)
saveNavArgsToSavedState(localUuid)
when (draftMode) {
DraftMode.NEW_MAIL -> recipient?.let { to = realmListOf(it) }
DraftMode.REPLY, DraftMode.REPLY_ALL, DraftMode.FORWARD -> {
previousMessageUid
?.let { uid -> MessageController.getMessage(uid, realm) }
?.let { message ->
val (fullMessage, hasFailedFetching) = draftController.fetchHeavyDataIfNeeded(message, realm)
if (hasFailedFetching) return null
draftController.setPreviousMessage(draft = this, draftMode = draftMode, previousMessage = fullMessage)
val isAiEnabled = currentMailbox.featureFlags.contains(FeatureFlag.AI)
if (isAiEnabled) parsePreviousMailToAnswerWithAi(fullMessage.body!!, fullMessage.uid)
previousMessage = fullMessage
}
}
}
val defaultSignature = SignatureController.getDefaultSignature(realm, draftMode)
val shouldPreselectSignature = draftMode == DraftMode.REPLY || draftMode == DraftMode.REPLY_ALL
val signature = if (shouldPreselectSignature) {
defaultSignature ?: guessMostFittingSignature(previousMessage!!, signatures)
} else {
defaultSignature
}
signatureUtils.initSignature(
draft = this,
signature = signature ?: Signature.getDummySignature(appContext, email = currentMailbox.email, isDefault = true),
)
populateWithExternalMailDataIfNeeded(draft = this, intent)
body = getWholeBody()
}
private fun saveNavArgsToSavedState(localUuid: String) {
savedStateHandle[NewMessageActivityArgs::draftLocalUuid.name] = localUuid
// If the user put the app in background before we put the fetched Draft in Realm, and the system
// kill the app, then we won't be able to fetch the Draft anymore as the `draftResource` will be null.
savedStateHandle[NewMessageActivityArgs::draftResource.name] = draftResource
}
private fun splitSignatureAndQuoteFromBody(draft: Draft) {
fun Document.split(divClassName: String, defaultValue: String): Pair<String, String?> {
return getElementsByClass(divClassName).firstOrNull()?.let {
it.remove()
val first = body().html()
val second = if (it.html().isBlank()) null else it.outerHtml()
first to second
} ?: (defaultValue to null)
}
fun String.lastIndexOfOrMax(string: String): Int {
val index = lastIndexOf(string)
return if (index == -1) Int.MAX_VALUE else index
}
val doc = Jsoup.parse(draft.body).also { it.outputSettings().prettyPrint(false) }
val (bodyWithQuote, signature) = doc.split(MessageBodyUtils.INFOMANIAK_SIGNATURE_HTML_CLASS_NAME, draft.body)
val replyPosition = draft.body.lastIndexOfOrMax(MessageBodyUtils.INFOMANIAK_REPLY_QUOTE_HTML_CLASS_NAME)
val forwardPosition = draft.body.lastIndexOfOrMax(MessageBodyUtils.INFOMANIAK_FORWARD_QUOTE_HTML_CLASS_NAME)
val (body, quote) = if (replyPosition < forwardPosition) {
doc.split(MessageBodyUtils.INFOMANIAK_REPLY_QUOTE_HTML_CLASS_NAME, bodyWithQuote)
} else {
doc.split(MessageBodyUtils.INFOMANIAK_FORWARD_QUOTE_HTML_CLASS_NAME, bodyWithQuote)
}
draft.apply {
uiBody = body.htmlToText()
uiSignature = signature
uiQuote = quote
}
}
private fun populateWithExternalMailDataIfNeeded(draft: Draft, intent: Intent) {
when (intent.action) {
Intent.ACTION_SEND -> handleSingleSendIntent(draft, intent)
Intent.ACTION_SEND_MULTIPLE -> handleMultipleSendIntent(draft, intent)
Intent.ACTION_VIEW, Intent.ACTION_SENDTO -> handleMailTo(draft, intent.data, intent)
}
if (mailToUri != null) handleMailTo(draft, mailToUri)
SentryDebug.addAttachmentsBreadcrumb(draft, step = "populate Draft with external mail data")
}
private fun Draft.flagRecipientsAsAutomaticallyEntered() {
to.flagRecipientsAsAutomaticallyEntered()
cc.flagRecipientsAsAutomaticallyEntered()
bcc.flagRecipientsAsAutomaticallyEntered()
}
/**
* If we came from a Notification's action, we need to dismiss the Notification.
*/
private fun dismissNotification() {
if (notificationId == -1) return
notificationManagerCompat.cancel(notificationId)
}
/**
* If we are replying to a Message, we need to mark it as read.
*/
private suspend fun markAsRead(mailbox: Mailbox, realm: TypedRealm) {
val message = previousMessageUid?.let { MessageController.getMessage(it, realm) } ?: return
if (message.isSeen) return
sharedUtils.markAsSeen(
mailbox = mailbox,
threads = message.threads.filter { it.folderId == message.folderId },
message = message,
shouldRefreshThreads = false,
)
}
private fun Draft.saveSnapshot() {
snapshot = DraftSnapshot(
identityId = identityId,
to = to.toSet(),
cc = cc.toSet(),
bcc = bcc.toSet(),
subject = subject,
uiBody = uiBody,
attachmentsLocalUuids = attachments.map { it.localUuid }.toSet(),
)
}
private fun Draft.initLiveData(signatures: List<Signature>) {
fromLiveData.postValue(
UiFrom(
signature = signatures.single { it.id == identityId?.toInt() },
shouldUpdateBodySignature = false,
),
)
toLiveData.postValue(UiRecipients(recipients = to, otherFieldsAreEmpty = cc.isEmpty() && bcc.isEmpty()))
ccLiveData.postValue(UiRecipients(recipients = cc))
bccLiveData.postValue(UiRecipients(recipients = bcc))
attachmentsLiveData.postValue(attachments)
uiSignatureLiveData.postValue(uiSignature)
uiQuoteLiveData.postValue(uiQuote)
if (cc.isNotEmpty() || bcc.isNotEmpty()) {
otherRecipientsFieldsAreEmpty.postValue(false)
initializeFieldsAsOpen.postValue(true)
}
}
private fun getLocalOrRemoteDraft(localUuid: String?): Draft? {
@Suppress("UNUSED_PARAMETER")
fun trackOpenLocal(draft: Draft) { // Unused but required to use references inside the `also` block, used for readability
appContext.trackNewMessageEvent(OPEN_LOCAL_DRAFT, TrackerAction.DATA, value = 1.0f)
}
@Suppress("UNUSED_PARAMETER")
fun trackOpenRemote(draft: Draft) { // Unused but required to use references inside the `also` block, used for readability
appContext.trackNewMessageEvent(OPEN_LOCAL_DRAFT, TrackerAction.DATA, value = 0.0f)
}
return getLatestLocalDraft(localUuid)?.also(::trackOpenLocal) ?: fetchDraft()?.also(::trackOpenRemote)
}
private fun getLatestLocalDraft(localUuid: String?) = localUuid?.let(draftController::getDraft)?.copyFromRealm()
private fun fetchDraft(): Draft? {
return ApiRepository.getDraft(draftResource!!).data?.also {
/**
* If we are opening for the 1st time an existing Draft created somewhere else
* (ex: webmail), we need to create the link between the Draft and its Message.
* - The link in the Draft is added here, when creating the Draft.
* - The link in the Message is added later, when saving the Draft.
*/
it.initLocalValues(messageUid!!)
}
}
private suspend fun parsePreviousMailToAnswerWithAi(previousMessageBody: Body, messageUid: String) {
if (draftMode == DraftMode.REPLY || draftMode == DraftMode.REPLY_ALL) {
aiSharedData.previousMessageBodyPlainText = previousMessageBody.asPlainText(messageUid)
}
}
private suspend fun Body.asPlainText(messageUid: String): String = when (type) {
Utils.TEXT_HTML -> {
val splitBodyContent = MessageBodyUtils.splitContentAndQuote(this).content
val fullBody = MessageBodyUtils.mergeSplitBodyAndSubBodies(splitBodyContent, subBodies, messageUid)
fullBody.htmlToText()
}
else -> value
}
private fun guessMostFittingSignature(message: Message, signatures: List<Signature>): Signature? {
val signatureEmailsMap = signatures.groupBy { it.senderEmail }
return findSignatureInRecipients(message.to, signatureEmailsMap)
?: findSignatureInRecipients(message.from, signatureEmailsMap)
?: findSignatureInRecipients(message.cc, signatureEmailsMap)
}
private fun findSignatureInRecipients(
recipients: RealmList<Recipient>,
signatureEmailsMap: Map<String, List<Signature>>,
): Signature? {
val matchingEmailRecipients = recipients.filter { it.email in signatureEmailsMap }
if (matchingEmailRecipients.isEmpty()) return null // If no Recipient represents us, go to next Recipients
var bestScore = NO_MATCH
var bestSignature: Signature? = null
matchingEmailRecipients.forEach { recipient ->
val signatures = signatureEmailsMap[recipient.email] ?: return@forEach
val (score, signature) = computeScore(recipient, signatures)
when (score) {
EXACT_MATCH_AND_IS_DEFAULT -> return signature
else -> {
if (score.strictlyGreaterThan(bestScore)) {
bestScore = score
bestSignature = signature
}
}
}
}
return bestSignature
}
/**
* Only pass in Signatures that have the same email address as the Recipient
*/
private fun computeScore(recipient: Recipient, signatures: List<Signature>): Pair<SignatureScore, Signature> {
var bestScore: SignatureScore = NO_MATCH
var bestSignature: Signature? = null
signatures.forEach { signature ->
when (val score = computeScore(recipient, signature)) {
EXACT_MATCH_AND_IS_DEFAULT -> return score to signature
else -> if (score.strictlyGreaterThan(bestScore)) {
bestScore = score
bestSignature = signature
}
}
}
return bestScore to bestSignature!!
}
/**
* Only pass in a Signature that has the same email address as the Recipient
*/
private fun computeScore(recipient: Recipient, signature: Signature): SignatureScore {
val isExactMatch = recipient.name == signature.senderName
val isDefault = signature.isDefault
val score = when {
isExactMatch && isDefault -> EXACT_MATCH_AND_IS_DEFAULT
isExactMatch -> EXACT_MATCH
isDefault -> ONLY_EMAIL_MATCH_AND_IS_DEFAULT
else -> ONLY_EMAIL_MATCH
}
return score
}
private fun handleSingleSendIntent(draft: Draft, intent: Intent) = with(intent) {
if (hasExtra(Intent.EXTRA_EMAIL)) {
handleMailTo(draft, intent.data, intent)
} else if (hasExtra(Intent.EXTRA_TEXT)) {
getStringExtra(Intent.EXTRA_SUBJECT)?.let { draft.subject = it }
getStringExtra(Intent.EXTRA_TEXT)?.let { draft.uiBody = it }
}
if (hasExtra(Intent.EXTRA_STREAM)) {
(parcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)
?.let { importAttachments(currentAttachments = draft.attachments, uris = listOf(it)) }
?.let(draft.attachments::addAll)
}
}
private fun handleMultipleSendIntent(draft: Draft, intent: Intent) {
intent.parcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)
?.filterIsInstance<Uri>()
?.let { importAttachments(currentAttachments = draft.attachments, uris = it) }
?.let(draft.attachments::addAll)
}
/**
* Handle `mailTo` from [Intent.ACTION_VIEW], [Intent.ACTION_SENDTO] or [Intent.ACTION_SEND].
* Get [Intent.ACTION_VIEW] data with [MailTo] and [Intent.ACTION_SENDTO] or [Intent.ACTION_SEND] with [Intent].
*
* [Intent.ACTION_SEND] shouldn't be used for `mailTo` as it isn't meant to pass the Recipient of the mail,
* but some apps don't follow the guidelines, so we support it anyway.
*/
private fun handleMailTo(draft: Draft, uri: Uri?, intent: Intent? = null) {
/**
* Mailto grammar accept 'name_of_recipient<email>' for recipients
*/
fun parseEmailWithName(recipient: String): Recipient? {
val nameAndEmail = Regex("(.+)<(.+)>").find(recipient)?.destructured
return nameAndEmail?.let { (name, email) -> if (email.isEmail()) Recipient().initLocalValues(email, name) else null }
}
/**
* Each customer app is free to do what it wants, so we sometimes receive empty `mailTo` fields.
* Instead of ignoring them, we return `null` so we can check the recipients inside the Intent.
*
* If the string obtained through the mailto uri is blank, this method needs to make sure to return null.
*/
fun String.splitToRecipientList() = split(",", ";").mapNotNull {
val email = it.trim()
if (email.isEmail()) Recipient().initLocalValues(email, email) else parseEmailWithName(email)
}.takeIf { it.isNotEmpty() }
fun Intent.getRecipientsFromIntent(recipientsFlag: String): List<Recipient>? {
return getStringArrayExtra(recipientsFlag)?.map { Recipient().initLocalValues(it, it) }
}
val mailToIntent = runCatching { MailTo.parse(uri!!) }.getOrNull()
if (mailToIntent == null && intent?.hasExtra(Intent.EXTRA_EMAIL) != true) return
val splitTo = mailToIntent?.to?.splitToRecipientList()
?: intent?.getRecipientsFromIntent(Intent.EXTRA_EMAIL)
?: emptyList()
val splitCc = mailToIntent?.cc?.splitToRecipientList()
?: intent?.getRecipientsFromIntent(Intent.EXTRA_CC)
?: emptyList()
val splitBcc = mailToIntent?.bcc?.splitToRecipientList()
?: intent?.getRecipientsFromIntent(Intent.EXTRA_BCC)
?: emptyList()
draft.apply {
to.addAll(splitTo)
cc.addAll(splitCc)
bcc.addAll(splitBcc)
subject = mailToIntent?.subject?.takeIf(String::isNotEmpty) ?: intent?.getStringExtra(Intent.EXTRA_SUBJECT)
uiBody = mailToIntent?.body?.takeIf(String::isNotEmpty) ?: intent?.getStringExtra(Intent.EXTRA_TEXT) ?: ""
}
}
fun importNewAttachments(
currentAttachments: List<Attachment>,
uris: List<Uri>,
completion: (List<Attachment>) -> Unit,
) = viewModelScope.launch(ioCoroutineContext) {
completion(importAttachments(currentAttachments, uris))
}
private fun importAttachments(currentAttachments: List<Attachment>, uris: List<Uri>): List<Attachment> {
val newAttachments = mutableListOf<Attachment>()
var attachmentsSize = currentAttachments.sumOf { it.size }
var result = ImportationResult.SUCCESS
uris.forEach { uri ->
val availableSpace = ATTACHMENTS_MAX_SIZE - attachmentsSize
val (attachment, hasSizeLimitBeenReached) = importAttachment(uri, availableSpace)
if (hasSizeLimitBeenReached) {
result = ImportationResult.ATTACHMENTS_TOO_BIG
return@forEach
}
attachment?.let {
newAttachments.add(it)
attachmentsSize += it.size
}
}
importAttachmentsResult.postValue(result)
return newAttachments
}
private fun importAttachment(uri: Uri, availableSpace: Long): Pair<Attachment?, Boolean> {
val (fileName, fileSize) = appContext.getFileNameAndSize(uri) ?: return null to false
val attachment = Attachment()
return LocalStorageUtils.saveAttachmentToUploadDir(
context = appContext,
uri = uri,
fileName = fileName,
draftLocalUuid = draftLocalUuid!!,
attachmentLocalUuid = attachment.localUuid,
snackbarManager = snackbarManager,
)?.let { file ->
Pair(
attachment.initLocalValues(fileName, file.length(), file.path.guessMimeType(), file.toUri().toString()),
fileSize > availableSpace,
)
} ?: (null to false)
}
private fun RealmList<Recipient>.flagRecipientsAsAutomaticallyEntered() {
forEach { recipient ->
recipient.isManuallyEntered = false
}
}
fun addRecipientToField(recipient: Recipient, type: FieldType) {
if (type == FieldType.CC || type == FieldType.BCC) otherRecipientsFieldsAreEmpty.value = false
val recipientsLiveData = when (type) {
FieldType.TO -> toLiveData
FieldType.CC -> ccLiveData
FieldType.BCC -> bccLiveData
}
recipientsLiveData.addRecipientThenSetValue(recipient)
}
fun removeRecipientFromField(recipient: Recipient, type: FieldType) {
val recipientsLiveData = when (type) {
FieldType.TO -> toLiveData
FieldType.CC -> ccLiveData
FieldType.BCC -> bccLiveData
}
recipientsLiveData.removeRecipientThenSetValue(recipient)
appContext.trackNewMessageEvent("deleteRecipient")
if (recipient.isDisplayedAsExternal) appContext.trackExternalEvent("deleteRecipient")
}
fun deleteAttachment(position: Int) {
runCatching {
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
val attachment = attachments[position]
attachment.getUploadLocalFile()?.delete()
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)
attachments.removeAt(position)
attachmentsLiveData.value = attachments
}.onFailure { exception ->
// TODO: If we don't see this Sentry after mid-2024, we can remove it.
SentryLog.e(TAG, " Attachment $position doesn't exist", exception)
}
}
fun updateIsSendingAllowed(
attachments: List<Attachment> = attachmentsLiveData.valueOrEmpty(),
type: FieldType? = null,
recipients: List<Recipient> = emptyList(),
) {
val allRecipients = when (type) {
FieldType.TO -> recipients + ccLiveData.valueOrEmpty() + bccLiveData.valueOrEmpty()
FieldType.CC -> toLiveData.valueOrEmpty() + recipients + bccLiveData.valueOrEmpty()
FieldType.BCC -> toLiveData.valueOrEmpty() + ccLiveData.valueOrEmpty() + recipients
null -> toLiveData.valueOrEmpty() + ccLiveData.valueOrEmpty() + bccLiveData.valueOrEmpty()
}
isSendingAllowed.value = if (allRecipients.isNotEmpty()) {
var size = 0L
var isSizeCorrect = true
for (attachment in attachments) {
size += attachment.size
if (size > ATTACHMENTS_MAX_SIZE) {
isSizeCorrect = false
break
}
}
isSizeCorrect
} else {
false
}
}
fun updateOtherRecipientsFieldsAreEmpty(cc: List<Recipient>, bcc: List<Recipient>) {
if (cc.isEmpty() && bcc.isEmpty()) otherRecipientsFieldsAreEmpty.value = true
}
fun updateBodySignature(signature: Signature) {
uiSignatureLiveData.value = if (signature.isDummy) {
null
} else {
signatureUtils.encapsulateSignatureContentWithInfomaniakClass(signature.content)
}
}
fun uploadAttachmentsToServer(uiAttachments: List<Attachment>) = viewModelScope.launch(ioDispatcher) {
val localUuid = draftLocalUuid ?: return@launch
val localDraft = mailboxContentRealm().writeBlocking {
DraftController.getDraft(localUuid, realm = this)?.also {
it.updateDraftAttachmentsWithLiveData(
uiAttachments = uiAttachments,
step = "observeAttachments -> uploadAttachmentsToServer",
)
}
} ?: return@launch
runCatching {
uploadAttachmentsWithMutex(localDraft, currentMailbox, draftController, mailboxContentRealm())
}.onFailure(Sentry::captureException)
}
fun executeDraftActionWhenStopping(
action: DraftAction,
isFinishing: Boolean,
isTaskRoot: Boolean,
subjectValue: String,
uiBodyValue: String,
startWorkerCallback: () -> Unit,
) = globalCoroutineScope.launch(ioDispatcher) {
val localUuid = draftLocalUuid ?: return@launch
val subject = subjectValue.ifBlank { null }?.take(SUBJECT_MAX_LENGTH)
if (action == DraftAction.SAVE && isSnapshotTheSame(subject, uiBodyValue)) {
if (isFinishing && isNewMessage) removeDraftFromRealm(localUuid)
return@launch
}
val hasFailed = mailboxContentRealm().writeBlocking {
DraftController.getDraft(localUuid, realm = this)
?.updateDraftFromLiveData(action, isFinishing, subject, uiBodyValue, realm = this@writeBlocking)
?: return@writeBlocking true
return@writeBlocking false
}
if (hasFailed) return@launch
showDraftToastToUser(action, isFinishing, isTaskRoot)
startWorkerCallback()
appContext.trackSendingDraftEvent(
action = action,
to = toLiveData.valueOrEmpty(),
cc = ccLiveData.valueOrEmpty(),
bcc = bccLiveData.valueOrEmpty(),
externalMailFlagEnabled = currentMailbox.externalMailFlagEnabled,
)
}
override fun onCleared() {
draftLocalUuid?.let { LocalStorageUtils.deleteDraftUploadDir(appContext, draftLocalUuid = it) }
super.onCleared()
}
private fun Draft.updateDraftFromLiveData(
draftAction: DraftAction,
isFinishing: Boolean,
subjectValue: String?,
uiBodyValue: String,
realm: MutableRealm,
) {
action = draftAction
identityId = fromLiveData.value?.signature?.id.toString()
to = toLiveData.valueOrEmpty().toRealmList()
cc = ccLiveData.valueOrEmpty().toRealmList()
bcc = bccLiveData.valueOrEmpty().toRealmList()
updateDraftAttachmentsWithLiveData(
uiAttachments = attachmentsLiveData.valueOrEmpty(),
step = "executeDraftActionWhenStopping (action = ${draftAction.name}) -> updateDraftFromLiveData",
)
subject = subjectValue
uiBody = uiBodyValue
uiSignature = uiSignatureLiveData.value
uiQuote = uiQuoteLiveData.value
body = getWholeBody()
/**
* If we are opening for the 1st time an existing Draft created somewhere else
* (ex: webmail), we need to create the link between the Draft and its Message.
* - The link in the Draft is already added, when creating the Draft.
* - The link in the Message is added here, when saving the Draft.
*/
messageUid?.let { MessageController.getMessage(uid = it, realm)?.draftLocalUuid = localUuid }
// If we opened a text/plain draft, we will now convert it as text/html as we send it because we only support editing
// text/html drafts.
mimeType = Utils.TEXT_HTML
// Only if `!isFinishing`, because if we are finishing, well… We're out of here so we don't care about all of that.
if (!isFinishing) {
copyFromRealm()
.apply {
uiBody = [email protected]
uiSignature = [email protected]
uiQuote = [email protected]
}
.saveSnapshot()
isNewMessage = false
}
}
private fun Draft.updateDraftAttachmentsWithLiveData(uiAttachments: List<Attachment>, step: String) {
/**
* If :
* - we are in FORWARD mode,
* - none got added (all Attachments are already uploaded, meaning they are all from the original forwarded Message),
* - none got removed (their quantity is the same in UI and in Realm),
* Then it means the Attachments list hasn't been edited by the user, so we have nothing to do here.
*/
val isForwardingUneditedAttachmentsList = draftMode == DraftMode.FORWARD &&
uiAttachments.all { it.uploadStatus == UploadStatus.FINISHED } &&
uiAttachments.count() == attachments.count()
if (isForwardingUneditedAttachmentsList) return
val updatedAttachments = uiAttachments.map { uiAttachment ->
val localAttachment = attachments.findSpecificAttachment(uiAttachment)
/**
* The DraftsActionWorker will possibly start uploading the Attachments beforehand, so there will possibly already
* be some data for Attachments in Realm (for example, the `uuid`). If we don't take back the Realm version of the
* Attachment, this data will be lost forever and we won't be able to save/send the Draft.
*/
return@map if (localAttachment != null && localAttachment.uploadStatus != UploadStatus.AWAITING) {
localAttachment.copyFromRealm()
} else {
uiAttachment
}
}
attachments.apply {
clear()
addAll(updatedAttachments)
}
SentryDebug.addAttachmentsBreadcrumb(draft = this, step)
}
private fun Draft.getWholeBody(): String = uiBody.textToHtml() + (uiSignature ?: "") + (uiQuote ?: "")
private fun isSnapshotTheSame(subjectValue: String?, uiBodyValue: String): Boolean {
return snapshot?.let { draftSnapshot ->
draftSnapshot.identityId == fromLiveData.value?.signature?.id?.toString() &&
draftSnapshot.to == toLiveData.valueOrEmpty().toSet() &&
draftSnapshot.cc == ccLiveData.valueOrEmpty().toSet() &&
draftSnapshot.bcc == bccLiveData.valueOrEmpty().toSet() &&
draftSnapshot.subject == subjectValue &&
draftSnapshot.uiBody == uiBodyValue &&
draftSnapshot.attachmentsLocalUuids == attachmentsLiveData.valueOrEmpty().map { it.localUuid }.toSet()
} ?: false
}
private fun removeDraftFromRealm(localUuid: String) {
mailboxContentRealm().writeBlocking {
DraftController.getDraft(localUuid, realm = this)?.let(::delete)
}
}
private suspend fun showDraftToastToUser(
action: DraftAction,
isFinishing: Boolean,
isTaskRoot: Boolean,
) = withContext(mainDispatcher) {
when (action) {
DraftAction.SAVE -> {
if (isFinishing) {
if (isTaskRoot) appContext.showToast(R.string.snackbarDraftSaving)
} else {
appContext.showToast(R.string.snackbarDraftSaving)
}
}
DraftAction.SEND -> {
if (isTaskRoot) appContext.showToast(R.string.snackbarEmailSending)
}
}
}
private fun MutableLiveData<UiRecipients>.addRecipientThenSetValue(recipient: Recipient) {
updateRecipientsThenSetValue { it.add(recipient) }
}
private fun MutableLiveData<UiRecipients>.removeRecipientThenSetValue(recipient: Recipient) {
updateRecipientsThenSetValue { it.remove(recipient) }
}
private fun MutableLiveData<UiRecipients>.updateRecipientsThenSetValue(update: (MutableList<Recipient>) -> Unit) {
value = UiRecipients(
recipients = valueOrEmpty().toMutableList().apply { update(this) },
otherFieldsAreEmpty = value!!.otherFieldsAreEmpty,
)
}
enum class ImportationResult {
SUCCESS,
ATTACHMENTS_TOO_BIG,
}
enum class SignatureScore(private val weight: Int) {
EXACT_MATCH_AND_IS_DEFAULT(4),
EXACT_MATCH(3),
ONLY_EMAIL_MATCH_AND_IS_DEFAULT(2),
ONLY_EMAIL_MATCH(1),
NO_MATCH(0);
fun strictlyGreaterThan(other: SignatureScore): Boolean = weight > other.weight
}
data class InitResult(
val draft: Draft,
val signatures: List<Signature>,
)
data class UiFrom(
val signature: Signature,
val shouldUpdateBodySignature: Boolean = true,
)
data class UiRecipients(
val recipients: List<Recipient>,
val otherFieldsAreEmpty: Boolean = true,
)
private data class DraftSnapshot(
val identityId: String?,
val to: Set<Recipient>,
val cc: Set<Recipient>,
val bcc: Set<Recipient>,
var subject: String?,
var uiBody: String,
val attachmentsLocalUuids: Set<String>,
)
companion object {
private val TAG = NewMessageViewModel::class.java.simpleName
private const val ATTACHMENTS_MAX_SIZE = 25L * 1_024L * 1_024L // 25 MB