Skip to content

Commit

Permalink
Merge pull request #677 from Infomaniak/notifications-full-body
Browse files Browse the repository at this point in the history
feat(NotificationsHelper): Use real body instead of preview
  • Loading branch information
valentinperignon committed Apr 5, 2023
2 parents 1157e95 + c8cf76c commit 244d354
Showing 1 changed file with 24 additions and 1 deletion.
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
}
}
}

0 comments on commit 244d354

Please sign in to comment.