Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 12, 2024
1 parent 6e466a2 commit 77cc852
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.AttachmentEntity
import com.flowcrypt.email.database.entity.MessageEntity
import com.flowcrypt.email.extensions.com.google.api.services.gmail.model.getRecipients
import com.flowcrypt.email.extensions.com.google.api.services.gmail.model.getSubject
import com.flowcrypt.email.extensions.com.google.api.services.gmail.model.getUniqueRecipients
import com.flowcrypt.email.extensions.contentId
import com.flowcrypt.email.extensions.disposition
Expand Down Expand Up @@ -390,17 +392,24 @@ class GmailApiHelper {
responseHeaders: HttpHeaders?
) {
t?.let { thread ->
val receiverEmail = accountEntity.email
val subject = thread.messages?.getOrNull(0)?.takeIf { message ->
message.getRecipients("From").any { internetAddress ->
internetAddress.address.equals(receiverEmail, true)
} || (thread.messages?.size?: 0) == 1
}?.getSubject()
?: thread.messages?.getOrNull(1)?.getSubject()
?: context.getString(R.string.no_subject)

thread.messages?.lastOrNull()?.let { lastMessageInThread ->
if (isTrash || lastMessageInThread.labelIds?.contains(LABEL_TRASH) != true) {
listResult.add(
GmailThreadInfo(
id = thread.id,
lastMessage = lastMessageInThread,
messagesCount = thread.messages?.size ?: 0,
recipients = thread.getUniqueRecipients(accountEntity.email),
subject = lastMessageInThread.payload?.headers?.firstOrNull { header ->
header.name == "Subject"
}?.value ?: context.getString(R.string.no_subject),
recipients = thread.getUniqueRecipients(receiverEmail),
subject = subject,
labels = thread.messages.flatMap { message ->
message.labelIds ?: emptyList()
}.toSortedSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package com.flowcrypt.email.extensions.com.google.api.services.gmail.model

import com.flowcrypt.email.api.email.EmailUtil
import com.flowcrypt.email.extensions.kotlin.asContentTypeOrNull
import com.flowcrypt.email.extensions.kotlin.asInternetAddresses
import com.google.api.services.gmail.model.Message
import jakarta.mail.internet.InternetAddress

/**
* @author Denys Bondarenko
Expand Down Expand Up @@ -40,4 +42,16 @@ fun Message.hasPgp(): Boolean {
|| isOpenPGPMimeSigned
|| isOpenPGPMimeEncrypted
|| hasEncryptedParts
}

fun Message.getRecipients(vararg recipientType: String): List<InternetAddress> {
return payload?.headers?.firstOrNull { header ->
header.name in recipientType
}?.value?.asInternetAddresses()?.toList() ?: emptyList()
}

fun Message.getSubject(): String? {
return payload?.headers?.firstOrNull { header ->
header.name == "Subject"
}?.value
}
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ class MessagesViewModel(application: Application) : AccountViewModel(application
val thread = gmailThreadInfoList.firstOrNull { it.id == message.threadId }
if (thread != null) {
messageEntity.copy(
subject = thread.subject,
threadMessagesCount = thread.messagesCount,
labelIds = thread.labels.joinToString(separator = LABEL_IDS_SEPARATOR),
hasAttachments = thread.hasAttachments,
Expand Down

0 comments on commit 77cc852

Please sign in to comment.