Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Debug wrong thread date #829

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Mail/Helpers/PreviewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import MailCore
import RealmSwift
import SwiftUI

#if DEBUG
enum PreviewHelper {
static let sampleMailboxManager = MailboxManager(mailbox: sampleMailbox, apiFetcher: MailApiFetcher())

Expand Down Expand Up @@ -65,7 +64,7 @@ enum PreviewHelper {
cc: [],
bcc: [],
subject: "Test thread",
date: Date(),
date: SentryDebug.knownDebugDate,
hasAttachments: true,
hasSwissTransferAttachments: false,
hasDrafts: false,
Expand All @@ -78,7 +77,7 @@ enum PreviewHelper {
msgId: "",
subject: "Test message",
priority: .normal,
date: Date(),
date: SentryDebug.knownDebugDate,
size: 0,
from: [sampleRecipient1],
to: [sampleRecipient2],
Expand Down Expand Up @@ -133,4 +132,3 @@ enum PreviewHelper {
expirationDate: Date()
))
}
#endif
16 changes: 10 additions & 6 deletions MailCore/Cache/MailboxManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class MailboxManager: ObservableObject {
let realmName = "\(mailbox.userId)-\(mailbox.mailboxId).realm"
realmConfiguration = Realm.Configuration(
fileURL: MailboxManager.constants.rootDocumentsURL.appendingPathComponent(realmName),
schemaVersion: 12,
schemaVersion: 13,
deleteRealmIfMigrationNeeded: true,
objectTypes: [
Folder.self,
Expand Down Expand Up @@ -761,31 +761,35 @@ public class MailboxManager: ObservableObject {
}

private func handleMessagesUids(messageUids: MessagesUids, folder: Folder) async throws {
let alreadyWrongIds = folder.fresh(using: getRealm())?.threads
.where { $0.date == SentryDebug.knownDebugDate }
let startDate = Date(timeIntervalSinceNow: -5 * 60)
let ignoredIds = folder.fresh(using: getRealm())?.threads
.where { $0.date > startDate }
.map { $0.uid } ?? []
await deleteMessages(uids: messageUids.deletedUids)
var shouldIgnoreNextEvents = SentryDebug.captureWrongDate(
step: "After delete",
startDate: startDate,
folder: folder,
alreadyWrongIds: alreadyWrongIds,
alreadyWrongIds: ignoredIds,
realm: getRealm()
)
await updateMessages(updates: messageUids.updated, folder: folder)
if !shouldIgnoreNextEvents {
shouldIgnoreNextEvents = SentryDebug.captureWrongDate(
step: "After updateMessages",
startDate: startDate,
folder: folder,
alreadyWrongIds: alreadyWrongIds,
alreadyWrongIds: ignoredIds,
realm: getRealm()
)
}
try await addMessages(shortUids: messageUids.addedShortUids, folder: folder, newCursor: messageUids.cursor)
if !shouldIgnoreNextEvents {
_ = SentryDebug.captureWrongDate(
step: "After addMessages",
startDate: startDate,
folder: folder,
alreadyWrongIds: alreadyWrongIds,
alreadyWrongIds: ignoredIds,
realm: getRealm()
)
}
Expand Down
3 changes: 1 addition & 2 deletions MailCore/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ public final class Message: Object, Decodable, Identifiable {
if let date = (try? values.decode(Date.self, forKey: .date)) {
self.date = date
} else {
// FIXME: Remove after thread date bug fix
date = SentryDebug.knownDebugDate
date = Date()
SentrySDK
.addBreadcrumb(SentryDebug.createBreadcrumb(
level: .warning,
Expand Down
15 changes: 10 additions & 5 deletions MailCore/Utils/SentryDebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import Foundation
import RealmSwift
import Sentry

enum SentryDebug {
static let knownDebugDate = Date(timeIntervalSince1970: 1_893_456_000)
public enum SentryDebug {
public static let knownDebugDate = Date(timeIntervalSince1970: 1_893_456_000)
static func sendMissingMessagesSentry(sentUids: [String], receivedMessages: [Message], folder: Folder, newCursor: String?) {
if receivedMessages.count != sentUids.count {
let receivedUids = Set(receivedMessages.map { Constants.shortUid(from: $0.uid) })
Expand Down Expand Up @@ -96,13 +96,18 @@ enum SentryDebug {
return crumb
}

static func captureWrongDate(step: String, folder: Folder, alreadyWrongIds: [String], realm: Realm) -> Bool {
static func captureWrongDate(step: String, startDate: Date, folder: Folder, alreadyWrongIds: [String], realm: Realm) -> Bool {
guard let freshFolder = folder.fresh(using: realm) else { return false }

let threads = freshFolder.threads.where { $0.date == knownDebugDate }.filter { !alreadyWrongIds.contains($0.uid) }
let threads = freshFolder.threads
.where { $0.date > startDate }
.filter { !alreadyWrongIds.contains($0.uid) }
.filter {
!$0.messages.map { $0.date }.contains($0.date)
}
guard !threads.isEmpty else { return false }

SentrySDK.capture(message: "Threads with wrong date on step \(step)") { scope in
SentrySDK.capture(message: "No corresponding message date for thread on step \(step)") { scope in
scope.setLevel(.error)
scope.setContext(value: ["threads": Array(threads).map {
[
Expand Down