From a3da087352f3b13dc873cfe4917b5d5181d7d600 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Wed, 21 Dec 2022 17:46:18 +1100 Subject: [PATCH] Fixed a few build errors and cases where the offset wasn't taken into account --- .../Conversations/Settings/OWSMessageTimerView.m | 3 ++- SessionMessagingKit/Calls/WebRTCSession.swift | 2 +- .../Jobs/Types/DisappearingMessagesJob.swift | 6 +++++- .../Sending & Receiving/MessageReceiver.swift | 2 +- .../Sending & Receiving/MessageSender.swift | 6 +++--- SessionSnodeKit/OnionRequestAPI.swift | 2 +- SessionSnodeKit/SnodeAPI.swift | 16 +++++++++++----- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Session/Conversations/Settings/OWSMessageTimerView.m b/Session/Conversations/Settings/OWSMessageTimerView.m index dc71b3b06..bfe57d7e3 100644 --- a/Session/Conversations/Settings/OWSMessageTimerView.m +++ b/Session/Conversations/Settings/OWSMessageTimerView.m @@ -8,6 +8,7 @@ #import #import #import +#import NS_ASSUME_NONNULL_BEGIN @@ -75,7 +76,7 @@ const CGFloat kDisappearingMessageIconSize = 12.f; return; } - uint64_t nowTimestamp = [NSDate ows_millisecondTimeStamp]; + uint64_t nowTimestamp = [SNSnodeAPI currentOffsetTimestampMs]; CGFloat secondsLeft = (self.expirationTimestamp > nowTimestamp ? (self.expirationTimestamp - nowTimestamp) / 1000.f : 0.f); CGFloat progress = 0.f; diff --git a/SessionMessagingKit/Calls/WebRTCSession.swift b/SessionMessagingKit/Calls/WebRTCSession.swift index eced676a7..e9c789158 100644 --- a/SessionMessagingKit/Calls/WebRTCSession.swift +++ b/SessionMessagingKit/Calls/WebRTCSession.swift @@ -180,7 +180,7 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate { uuid: uuid, kind: .offer, sdps: [ sdp.sdp ], - sentTimestampMs: SnodeAPI.currentTimestampMs() + sentTimestampMs: UInt64(SnodeAPI.currentOffsetTimestampMs()) ), interactionId: nil, in: thread diff --git a/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift b/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift index 8fdd407ff..d294c27b3 100644 --- a/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift +++ b/SessionMessagingKit/Jobs/Types/DisappearingMessagesJob.swift @@ -60,10 +60,14 @@ public extension DisappearingMessagesJob { guard let nextExpirationTimestampMs: Double = nextExpirationTimestampMs else { return nil } + /// The `expiresStartedAtMs` timestamp is now based on the `SnodeAPI.currentOffsetTimestampMs()` value + /// so we need to make sure offset the `nextRunTimestamp` accordingly to ensure it runs at the correct local time + let clockOffsetMs: Int64 = SnodeAPI.clockOffsetMs.wrappedValue + return try? Job .filter(Job.Columns.variant == Job.Variant.disappearingMessages) .fetchOne(db)? - .with(nextRunTimestamp: ceil(nextExpirationTimestampMs / 1000)) + .with(nextRunTimestamp: ceil((nextExpirationTimestampMs - Double(clockOffsetMs)) / 1000)) .saved(db) } diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index 88b9d4ea2..0c9196ac3 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -145,7 +145,7 @@ public enum MessageReceiver { message.sender = sender message.recipient = userPublicKey message.sentTimestamp = envelope.timestamp - message.receivedTimestamp = SnodeAPI.currentTimestampMs() + message.receivedTimestamp = UInt64(SnodeAPI.currentOffsetTimestampMs()) message.groupPublicKey = groupPublicKey message.openGroupServerMessageId = openGroupMessageServerId.map { UInt64($0) } diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 297e70732..ead951cbb 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -202,7 +202,7 @@ public final class MessageSender { recipient: message.recipient!, data: base64EncodedData, ttl: message.ttl, - timestampMs: UInt64(messageSendTimestamp + SnodeAPI.clockOffset.wrappedValue) + timestampMs: UInt64(messageSendTimestamp) ) SnodeAPI @@ -322,7 +322,7 @@ public final class MessageSender { // Set the timestamp, sender and recipient if message.sentTimestamp == nil { // Visible messages will already have their sent timestamp set - message.sentTimestamp = SnodeAPI.currentTimestampMs() + message.sentTimestamp = UInt64(SnodeAPI.currentOffsetTimestampMs()) } switch destination { @@ -472,7 +472,7 @@ public final class MessageSender { // Set the timestamp, sender and recipient if message.sentTimestamp == nil { // Visible messages will already have their sent timestamp set - message.sentTimestamp = SnodeAPI.currentTimestampMs() + message.sentTimestamp = UInt64(SnodeAPI.currentOffsetTimestampMs()) } message.sender = userPublicKey diff --git a/SessionSnodeKit/OnionRequestAPI.swift b/SessionSnodeKit/OnionRequestAPI.swift index d2a71b66c..65a3506ea 100644 --- a/SessionSnodeKit/OnionRequestAPI.swift +++ b/SessionSnodeKit/OnionRequestAPI.swift @@ -672,7 +672,7 @@ public enum OnionRequestAPI: OnionRequestAPIType { if let timestamp = body["t"] as? Int64 { let offset = timestamp - Int64(floor(Date().timeIntervalSince1970 * 1000)) - SnodeAPI.clockOffset.mutate { $0 = offset } + SnodeAPI.clockOffsetMs.mutate { $0 = offset } } guard 200...299 ~= statusCode else { diff --git a/SessionSnodeKit/SnodeAPI.swift b/SessionSnodeKit/SnodeAPI.swift index 4f0aff7f0..eff7c6632 100644 --- a/SessionSnodeKit/SnodeAPI.swift +++ b/SessionSnodeKit/SnodeAPI.swift @@ -19,15 +19,13 @@ public final class SnodeAPI { internal static var snodePool: Atomic> = Atomic([]) /// The offset between the user's clock and the Service Node's clock. Used in cases where the - /// user's clock is incorrect. - /// - /// - Note: Should only be accessed from `Threading.workQueue` to avoid race conditions. - public static var clockOffset: Atomic = Atomic(0) + /// user's clock is incorrect + public static var clockOffsetMs: Atomic = Atomic(0) public static func currentOffsetTimestampMs() -> Int64 { return ( Int64(floor(Date().timeIntervalSince1970 * 1000)) + - SnodeAPI.clockOffset.wrappedValue + SnodeAPI.clockOffsetMs.wrappedValue ) } @@ -1110,3 +1108,11 @@ public final class SnodeAPI { return nil } } + +@objc(SNSnodeAPI) +public final class SNSnodeAPI: NSObject { + @objc(currentOffsetTimestampMs) + public static func currentOffsetTimestampMs() -> UInt64 { + return UInt64(SnodeAPI.currentOffsetTimestampMs()) + } +}