pEpForiOS/UI/EmailDisplayList/PreviewMessage.swift
author Xavier Algarra <xavier@pep-project.org>
Mon, 07 May 2018 11:11:24 +0200
branchIOS-1064
changeset 4550 c04fe6d5c76b
parent 4480 5fed9f39f57f
child 5069 4417bea77aaa
permissions -rw-r--r--
IOS-1064 some changes
     1 //
     2 //  PreviewMessage.swift
     3 //  pEp
     4 //
     5 //  Created by Andreas Buff on 02.10.17.
     6 //  Copyright © 2017 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import MessageModel
    10 import UIKit
    11 
    12 class PreviewMessage: Equatable {
    13     let uid: UInt
    14     private let uuid: MessageID
    15     private let address: String
    16     private let parentFolderName: String
    17     var pEpRating: Int
    18     let hasAttachments: Bool
    19     let from: Identity
    20     let to: String
    21     let subject: String
    22     var bodyPeek: String
    23     var isFlagged = false
    24     var isSeen = false
    25     let dateSent: Date
    26 
    27     var maxBodyPreviewCharacters = 120
    28 
    29     init(withMessage msg: Message) {
    30         uuid = msg.uuid
    31         uid = msg.uid
    32         address = msg.parent.account.user.address
    33         parentFolderName = msg.parent.name
    34         pEpRating = msg.pEpRatingInt ?? 0
    35         hasAttachments = msg.attachments.count > 0
    36         from =  msg.from ?? Identity(address: "unknown@unknown.com")
    37         to = msg.to.first?.userNameOrAddress ?? ""
    38         subject = msg.shortMessage ?? ""
    39         isFlagged = msg.imapFlags?.flagged ?? false
    40         isSeen = msg.imapFlags?.seen ?? false
    41         dateSent = msg.sent ?? Date()
    42         bodyPeek = ""
    43         bodyPeek = displayBody(fromMessage: msg)
    44     }
    45 
    46     private func displayBody(fromMessage msg: Message) -> String {
    47         var body: String?
    48         if let text = msg.longMessage {
    49             body = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
    50         } else if let html = msg.longMessageFormatted {
    51             // Limit the size of HTML to parse
    52             // That might result in a messy preview but valid messages use to offer a plaintext
    53             // version while certain spam mails have thousands of lines of invalid HTML, causing
    54             // the parser to take minutes to parse one message.
    55             let factorHtmlTags = 3
    56             let numChars = maxBodyPreviewCharacters * factorHtmlTags
    57             let truncatedHtml = html.prefix(ofLength: numChars)
    58             body = truncatedHtml.extractTextFromHTML()
    59             body = body?.replaceNewLinesWith(" ").trimmedWhiteSpace()
    60         }
    61         guard let saveBody = body else {
    62             return ""
    63         }
    64 
    65         let result: String
    66         if saveBody.count <= maxBodyPreviewCharacters {
    67             result = saveBody
    68         } else {
    69             let endIndex = saveBody.index(saveBody.startIndex, offsetBy: maxBodyPreviewCharacters)
    70             result = String(saveBody[..<endIndex])
    71         }
    72         return result
    73     }
    74 
    75     public func message() -> Message? {
    76         guard let msg = Message.by(uid: uid,
    77                                    uuid: uuid,
    78                                    folderName: parentFolderName,
    79                                    accountAddress: address)
    80             else {
    81                 // The model has changed.
    82                 return nil
    83         }
    84         return msg
    85     }
    86 
    87     public func flagsDiffer(previewMessage pvMsg: PreviewMessage) -> Bool {
    88         if self != pvMsg {
    89             return true
    90         }
    91         return self.isFlagged != pvMsg.isFlagged || self.isSeen != pvMsg.isSeen
    92     }
    93 
    94     static func ==(lhs: PreviewMessage, rhs: PreviewMessage) -> Bool {
    95         return lhs.uuid == rhs.uuid &&
    96             lhs.uid == rhs.uid &&
    97             lhs.parentFolderName == rhs.parentFolderName &&
    98             lhs.address == rhs.address
    99     }
   100     
   101     static func ==(lhs: PreviewMessage, rhs: Message) -> Bool {
   102         return lhs.uuid == rhs.uuid &&
   103             lhs.uid == rhs.uid &&
   104             lhs.parentFolderName == rhs.parent.name &&
   105             lhs.address == rhs.parent.account.user.address
   106     }
   107 }