summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/ProblemReportInteractor.swift
blob: 744eedcbac70fc5dd69e71aaa13851efbba59880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
//  ProblemReportInteractor.swift
//  MullvadVPN
//
//  Created by pronebird on 25/10/2022.
//  Copyright © 2022 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadREST
import MullvadTypes
import Operations

final class ProblemReportInteractor {
    private let apiProxy: REST.APIProxy
    private let tunnelManager: TunnelManager

    private lazy var consolidatedLog: ConsolidatedApplicationLog = {
        let securityGroupIdentifier = ApplicationConfiguration.securityGroupIdentifier

        // TODO: make sure we redact old tokens

        let redactStrings = [tunnelManager.deviceState.accountData?.number].compactMap { $0 }

        let report = ConsolidatedApplicationLog(
            redactCustomStrings: redactStrings,
            redactContainerPathsForSecurityGroupIdentifiers: [securityGroupIdentifier]
        )

        report.addLogFiles(fileURLs: ApplicationConfiguration.logFileURLs, includeLogBackups: true)

        return report
    }()

    init(apiProxy: REST.APIProxy, tunnelManager: TunnelManager) {
        self.apiProxy = apiProxy
        self.tunnelManager = tunnelManager
    }

    func sendReport(
        email: String,
        message: String,
        completion: @escaping (OperationCompletion<Void, REST.Error>) -> Void
    ) -> Cancellable {
        let request = REST.ProblemReportRequest(
            address: email,
            message: message,
            log: consolidatedLog.string,
            metadata: consolidatedLog.metadata.reduce(into: [:]) { output, entry in
                output[entry.key.rawValue] = entry.value
            }
        )

        return apiProxy.sendProblemReport(
            request,
            retryStrategy: .default,
            completionHandler: completion
        )
    }

    var reportString: String {
        return consolidatedLog.string
    }
}