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

feat(NotificationsHelper): Use real body instead of preview #677

Merged
merged 2 commits into from
Apr 5, 2023
Merged
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
25 changes: 24 additions & 1 deletion MailCore/Utils/NotificationsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import InfomaniakCore
import InfomaniakCoreUI
import MailResources
import RealmSwift
import SwiftSoup
import UIKit
import UserNotifications

Expand Down Expand Up @@ -121,7 +122,7 @@ public enum NotificationsHelper {
content.title = MailResourcesStrings.Localizable.unknownRecipientTitle
}
content.subtitle = message.formattedSubject
content.body = message.preview
content.body = getCleanBodyFrom(message: message)
content.threadIdentifier = "\(mailboxId)_\(userId)"
content.targetContentIdentifier = "\(userId)_\(mailboxId)_\(message.uid)"
content.badge = getUnreadCount() as NSNumber
Expand All @@ -130,4 +131,26 @@ public enum NotificationsHelper {
NotificationsHelper.UserInfoKeys.messageUid: message.uid]
return content
}

private static func getCleanBodyFrom(message: Message) -> String {
guard let fullBody = message.body?.value,
let bodyType = message.body?.type,
let body = MessageBodyUtils.splitBodyAndQuote(messageBody: fullBody)?.messageBody else {
return message.preview
}

guard bodyType != "text/plain" else {
return body.trimmingCharacters(in: .whitespacesAndNewlines)
}

do {
let basicHtml = try SwiftSoup.clean(body, Whitelist.basic())!
let parsedBody = try SwiftSoup.parse(basicHtml)

let rawText = try parsedBody.text(trimAndNormaliseWhitespace: false)
return rawText.trimmingCharacters(in: .whitespacesAndNewlines)
} catch {
return message.preview
}
}
}