From 7bb3894bb2b0ecca2ca1fa5859e904870552bd40 Mon Sep 17 00:00:00 2001 From: Erik Everson <32311723+ErikEverson@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:11:29 -0600 Subject: [PATCH] Fixed TAK rendering bug for chats (#30) --- .../DittoChatPackage/Data/DittoService.swift | 10 ++---- Sources/DittoChatPackage/Models/Message.swift | 32 ++++++++++++------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Sources/DittoChatPackage/Data/DittoService.swift b/Sources/DittoChatPackage/Data/DittoService.swift index 529a95a..7bf9dc8 100644 --- a/Sources/DittoChatPackage/Data/DittoService.swift +++ b/Sources/DittoChatPackage/Data/DittoService.swift @@ -255,16 +255,10 @@ extension DittoService { } func createMessage(for room: Room, text: String) { - guard let userId = privateStore.currentUserId else { - return - } - - guard let room = self.room(for: room) else { - return - } + guard let userId = privateStore.currentUserId else { return } + guard let room = self.room(for: room) else { return } let message = Message(roomId: room.id, message: text, userName: userId, userId: userId).docDictionary() - try! ditto.store.collection(room.messagesId).upsert(message) } diff --git a/Sources/DittoChatPackage/Models/Message.swift b/Sources/DittoChatPackage/Models/Message.swift index b436982..f280938 100644 --- a/Sources/DittoChatPackage/Models/Message.swift +++ b/Sources/DittoChatPackage/Models/Message.swift @@ -71,7 +71,7 @@ extension Message { self.roomId = document[roomIdKey].stringValue self.schver = document[schverKey].intValue self.takUid = document[takUidKey].stringValue - self.timeMs = Date(timeIntervalSince1970: document[timeMsKey].doubleValue / 1000) + self.timeMs = Date(timeIntervalSince1970InMilliSeconds: document[timeMsKey].intValue) self.hasBeenConverted = document[hasBeenConvertedKey].bool if let hasBeenConverted, hasBeenConverted == true { @@ -170,7 +170,7 @@ extension Message { // Used for creating new chat types for upload init( id: String? = nil, - createdOn: Date? = nil, + createdOn: Date = .now, roomId: String, message: String = "", userName: String, @@ -180,13 +180,12 @@ extension Message { archivedMessage: String? = nil, isArchived: Bool = false, parent: String = "RootContactGroup", - pks: String = "", - room: String = "Ditto", + room: String = "ditto", schver: Int = 1, hasBeenConverted: Bool = true ) { self.id = id ?? UUID().uuidString - self.createdOn = createdOn ?? Date() + self.createdOn = createdOn self.roomId = roomId self.text = message self.userId = userId @@ -197,15 +196,16 @@ extension Message { self.authorCs = userName self.authorId = userId - self.authorLoc = "" - self.authorType = "" + self.authorLoc = "0.0,0.0,NaN,HAE,NaN,NaN" + self.authorType = "a-f-G-U-C" self.msg = message self.parent = parent - self.pks = pks + let peerKey = DittoInstance.shared.ditto.presence.graph.localPeer.peerKeyString + self.pks = peerKey self.room = room self.schver = schver - self.takUid = userId - self.timeMs = createdOn ?? Date() + self.takUid = UUID().uuidString + self.timeMs = createdOn } } @@ -231,8 +231,18 @@ extension Message { roomKey: room, schverKey: schver, takUidKey: takUid, - timeMsKey: (timeMs.timeIntervalSince1970 * 1000), + timeMsKey: timeMs.timeIntervalSince1970InMilliSeconds, hasBeenConvertedKey: hasBeenConverted, ] } } + +extension Date { + init(timeIntervalSince1970InMilliSeconds: Int) { + self = Date(timeIntervalSince1970: Double(timeIntervalSince1970InMilliSeconds) / 1000) + } + + var timeIntervalSince1970InMilliSeconds: Int { + Int(self.timeIntervalSince1970 * 1000) + } +}