Fixed a few build errors and cases where the offset wasn't taken into account

This commit is contained in:
Morgan Pretty 2022-12-21 17:46:18 +11:00
parent f7199b4c44
commit a3da087352
7 changed files with 24 additions and 13 deletions

View File

@ -8,6 +8,7 @@
#import <QuartzCore/QuartzCore.h>
#import <SignalCoreKit/NSDate+OWS.h>
#import <SessionUtilitiesKit/NSTimer+Proxying.h>
#import <SessionSnodeKit/SessionSnodeKit.h>
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;

View File

@ -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

View File

@ -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)
}

View File

@ -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) }

View File

@ -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

View File

@ -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 {

View File

@ -19,15 +19,13 @@ public final class SnodeAPI {
internal static var snodePool: Atomic<Set<Snode>> = 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<Int64> = Atomic(0)
/// user's clock is incorrect
public static var clockOffsetMs: Atomic<Int64> = 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())
}
}