From 4cb2d81670797e8d92bbbce9359ce116528a09bd Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 11 Aug 2022 11:32:29 +1000 Subject: [PATCH] clean up --- Podfile.lock | 2 +- .../Common Networking/QueryParam.swift | 1 - SessionMessagingKit/Messages/Message.swift | 77 ++++++++++++------- .../Open Groups/Models/SOGSMessage.swift | 4 +- .../Open Groups/OpenGroupAPI.swift | 5 +- .../Open Groups/OpenGroupManager.swift | 12 ++- .../Sending & Receiving/MessageSender.swift | 5 +- 7 files changed, 62 insertions(+), 44 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 5e750d933..37f4ac9c5 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -244,4 +244,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: f0857369c4831b2e5c1946345e76e493f3286805 -COCOAPODS: 1.11.2 +COCOAPODS: 1.11.3 diff --git a/SessionMessagingKit/Common Networking/QueryParam.swift b/SessionMessagingKit/Common Networking/QueryParam.swift index 184c6d4da..28280b495 100644 --- a/SessionMessagingKit/Common Networking/QueryParam.swift +++ b/SessionMessagingKit/Common Networking/QueryParam.swift @@ -10,6 +10,5 @@ enum QueryParam: String { case limit // For messages - number between 1 and 256 (default is 100) case platform // For file server session version check - case reactions = "t" case reactors = "reactors" } diff --git a/SessionMessagingKit/Messages/Message.swift b/SessionMessagingKit/Messages/Message.swift index 117670484..a8267acae 100644 --- a/SessionMessagingKit/Messages/Message.swift +++ b/SessionMessagingKit/Messages/Message.swift @@ -292,10 +292,10 @@ public extension Message { dependencies: SMKDependencies = SMKDependencies() ) throws -> ProcessedMessage? { // Need a sender in order to process the message - guard let sender: String = message.sender else { return nil } + guard let sender: String = message.sender, let timestamp = message.posted else { return nil } // Note: The `posted` value is in seconds but all messages in the database use milliseconds for timestamps - let envelopeBuilder = SNProtoEnvelope.builder(type: .sessionMessage, timestamp: UInt64(floor(message.posted * 1000))) + let envelopeBuilder = SNProtoEnvelope.builder(type: .sessionMessage, timestamp: UInt64(floor(timestamp * 1000))) envelopeBuilder.setContent(data) envelopeBuilder.setSource(sender) @@ -367,34 +367,55 @@ public extension Message { rawReaction.count > 0, let reactors = rawReaction.reactors { - var count = rawReaction.count - let sortId: Int64 = rawReaction.index - for reactor in reactors { - if reactor == blindedUserPublicKey { continue } // Will add a reaction for this case outside of the loop - let reaction = Reaction( - interactionId: message.id, - serverHash: nil, - timestampMs: Int64(floor((Date().timeIntervalSince1970 * 1000))), - authorId: reactor, - emoji: emoji, - count: count, - sortId: sortId + let timestampMs: Int64 = Int64(floor((Date().timeIntervalSince1970 * 1000))) + let desiredReactorIds: [String] = reactors + .filter { $0 != blindedUserPublicKey } + + results = results + .appending( // Add the first reaction (with the count) + desiredReactorIds.first + .map { reactor in + Reaction( + interactionId: message.id, + serverHash: nil, + timestampMs: timestampMs, + authorId: reactor, + emoji: emoji, + count: rawReaction.count, + sortId: rawReaction.index + ) + } ) - count = 0 // Only insert the first reaction with the total count of this emoji - results.append(reaction) - } - if rawReaction.you && !reactors.contains(userPublicKey) { - let reaction = Reaction( - interactionId: message.id, - serverHash: nil, - timestampMs: Int64(floor((Date().timeIntervalSince1970 * 1000))), - authorId: userPublicKey, - emoji: emoji, - count: count, - sortId: sortId + .appending( // Add all other reactions + contentsOf: desiredReactorIds.count <= 1 ? + [] : + desiredReactorIds + .suffix(from: 1) + .map { reactor in + Reaction( + interactionId: message.id, + serverHash: nil, + timestampMs: timestampMs, + authorId: reactor, + emoji: emoji, + count: 0, // Only want this on the first reaction + sortId: rawReaction.index + ) + } + ) + .appending( // Add the current user reaction (if applicable and not already included) + !rawReaction.you || reactors.contains(userPublicKey) ? + nil : + Reaction( + interactionId: message.id, + serverHash: nil, + timestampMs: timestampMs, + authorId: userPublicKey, + emoji: emoji, + count: (desiredReactorIds.isEmpty ? rawReaction.count : 0), + sortId: rawReaction.index + ) ) - results.append(reaction) - } } } return results diff --git a/SessionMessagingKit/Open Groups/Models/SOGSMessage.swift b/SessionMessagingKit/Open Groups/Models/SOGSMessage.swift index 9bb7bb7ec..c30dfcf3a 100644 --- a/SessionMessagingKit/Open Groups/Models/SOGSMessage.swift +++ b/SessionMessagingKit/Open Groups/Models/SOGSMessage.swift @@ -24,7 +24,7 @@ extension OpenGroupAPI { public let id: Int64 public let sender: String? - public let posted: TimeInterval + public let posted: TimeInterval? public let edited: TimeInterval? public let deleted: Bool? public let seqNo: Int64 @@ -98,7 +98,7 @@ extension OpenGroupAPI.Message { self = OpenGroupAPI.Message( id: try container.decode(Int64.self, forKey: .id), sender: try? container.decode(String.self, forKey: .sender), - posted: ((try? container.decode(TimeInterval.self, forKey: .posted)) ?? Date().timeIntervalSince1970), // Reaction updates don't include posted + posted: try? container.decode(TimeInterval.self, forKey: .posted), edited: try? container.decode(TimeInterval.self, forKey: .edited), deleted: try? container.decode(Bool.self, forKey: .deleted), seqNo: try container.decode(Int64.self, forKey: .seqNo), diff --git a/SessionMessagingKit/Open Groups/OpenGroupAPI.swift b/SessionMessagingKit/Open Groups/OpenGroupAPI.swift index 6191b4ded..518cec16c 100644 --- a/SessionMessagingKit/Open Groups/OpenGroupAPI.swift +++ b/SessionMessagingKit/Open Groups/OpenGroupAPI.swift @@ -97,8 +97,7 @@ public enum OpenGroupAPI { .roomMessagesRecent(openGroup.roomToken) : .roomMessagesSince(openGroup.roomToken, seqNo: openGroup.sequenceNumber) ), - queryParameters: [.reactions: "r", - .reactors: "20"] + queryParameters: [.reactors: "20"] ), responseType: [Failable].self ) @@ -621,7 +620,7 @@ public enum OpenGroupAPI { request: Request( server: server, endpoint: .roomMessagesSince(roomToken, seqNo: seqNo), - queryParameters: [.reactions : "r"] + queryParameters: [.reactors: "20"] ), using: dependencies ) diff --git a/SessionMessagingKit/Open Groups/OpenGroupManager.swift b/SessionMessagingKit/Open Groups/OpenGroupManager.swift index 84f4cd7cc..7bee7652e 100644 --- a/SessionMessagingKit/Open Groups/OpenGroupManager.swift +++ b/SessionMessagingKit/Open Groups/OpenGroupManager.swift @@ -564,13 +564,11 @@ public final class OpenGroupManager: NSObject { dependencies: dependencies ) - if !reactions.isEmpty { - try MessageReceiver.handleOpenGroupReactions( - db, - openGroupMessageServerId: message.id, - openGroupReactions: reactions - ) - } + try MessageReceiver.handleOpenGroupReactions( + db, + openGroupMessageServerId: message.id, + openGroupReactions: reactions + ) } catch { SNLog("Couldn't handle open group reactions due to error: \(error).") diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 242f37372..8235b9417 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -433,7 +433,8 @@ public final class MessageSender { ) .done(on: DispatchQueue.global(qos: .default)) { responseInfo, data in message.openGroupServerMessageId = UInt64(data.id) - + let serverTimestampMs: UInt64? = (data.posted == nil) ? nil : UInt64(floor(data.posted! * 1000)) + dependencies.storage.write { db in // The `posted` value is in seconds but we sent it in ms so need that for de-duping try MessageSender.handleSuccessfulMessageSend( @@ -441,7 +442,7 @@ public final class MessageSender { message: message, to: destination, interactionId: interactionId, - serverTimestampMs: UInt64(floor(data.posted * 1000)) + serverTimestampMs: serverTimestampMs ) seal.fulfill(()) }