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