diff --git a/Sources/XXClient/Models/NotificationReport.swift b/Sources/XXClient/Models/NotificationReport.swift new file mode 100644 index 0000000000000000000000000000000000000000..31c106a34f5a083478522acf542d8b608544a333 --- /dev/null +++ b/Sources/XXClient/Models/NotificationReport.swift @@ -0,0 +1,47 @@ +import Foundation + +public struct NotificationReport: Equatable { + public enum ReportType: String, Equatable { + case `default` + case request + case reset + case confirm + case silent + case e2e + case group + case endFT + case groupRQ + } + + public init( + forMe: Bool, + type: NotificationReport.ReportType, + source: Data + ) { + self.forMe = forMe + self.type = type + self.source = source + } + + public var forMe: Bool + public var type: ReportType + public var source: Data +} + +extension NotificationReport.ReportType: Codable {} + +extension NotificationReport: Codable { + enum CodingKeys: String, CodingKey { + case forMe = "ForMe" + case type = "Type" + case source = "Source" + } + + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/XXClientTests/Models/NotificationReportTests.swift b/Tests/XXClientTests/Models/NotificationReportTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..9e99d82544e5631c26142d4a43b5a64ba96342bc --- /dev/null +++ b/Tests/XXClientTests/Models/NotificationReportTests.swift @@ -0,0 +1,31 @@ +import CustomDump +import XCTest +@testable import XXClient + +final class NotificationReportTests: XCTestCase { + func testCoding() throws { + let forMe = true + let type = NotificationReport.ReportType.default + let sourceB64 = "dGVzdGVyMTIzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + let jsonString = """ + { + "ForMe": true, + "Type": "\(type.rawValue)", + "Source": "\(sourceB64)" + } + """ + let jsonData = jsonString.data(using: .utf8)! + let model = try NotificationReport.decode(jsonData) + + XCTAssertNoDifference(model, NotificationReport( + forMe: forMe, + type: type, + source: Data(base64Encoded: sourceB64)! + )) + + let encodedModel = try model.encode() + let decodedModel = try NotificationReport.decode(encodedModel) + + XCTAssertNoDifference(decodedModel, model) + } +}