use timestamp instead of date

pull/252/head
Ryan ZHAO 5 years ago
parent 80c4605530
commit d6604c10ff

@ -131,6 +131,7 @@ public final class PublicChatAPI : DotNetAPI {
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(message).") print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(message).")
return nil return nil
} }
let servertTimestamp = UInt64(date.timeIntervalSince1970) * 1000
var profilePicture: PublicChatMessage.ProfilePicture? = nil var profilePicture: PublicChatMessage.ProfilePicture? = nil
let displayName = user["name"] as? String ?? NSLocalizedString("Anonymous", comment: "") let displayName = user["name"] as? String ?? NSLocalizedString("Anonymous", comment: "")
if let userAnnotations = user["annotations"] as? [JSON], let profilePictureAnnotation = userAnnotations.first(where: { $0["type"] as? String == profilePictureType }), if let userAnnotations = user["annotations"] as? [JSON], let profilePictureAnnotation = userAnnotations.first(where: { $0["type"] as? String == profilePictureType }),
@ -170,7 +171,7 @@ public final class PublicChatAPI : DotNetAPI {
width: width, height: height, caption: caption, url: url, linkPreviewURL: linkPreviewURL, linkPreviewTitle: linkPreviewTitle) width: width, height: height, caption: caption, url: url, linkPreviewURL: linkPreviewURL, linkPreviewTitle: linkPreviewTitle)
} }
let result = PublicChatMessage(serverID: serverID, senderPublicKey: hexEncodedPublicKey, displayName: displayName, profilePicture: profilePicture, let result = PublicChatMessage(serverID: serverID, senderPublicKey: hexEncodedPublicKey, displayName: displayName, profilePicture: profilePicture,
body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature, serverTime: date) body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature, serverTimestamp: servertTimestamp)
guard result.hasValidSignature() else { guard result.hasValidSignature() else {
print("[Loki] Ignoring public chat message with invalid signature.") print("[Loki] Ignoring public chat message with invalid signature.")
return nil return nil
@ -184,7 +185,7 @@ public final class PublicChatAPI : DotNetAPI {
return nil return nil
} }
return result return result
}.sorted { $0.serverTime!.compare($1.serverTime!) == ComparisonResult.orderedAscending } }.sorted { $0.serverTimestamp < $1.serverTimestamp}
} }
} }
}.handlingInvalidAuthTokenIfNeeded(for: server) }.handlingInvalidAuthTokenIfNeeded(for: server)
@ -219,7 +220,7 @@ public final class PublicChatAPI : DotNetAPI {
throw DotNetAPIError.parsingFailed throw DotNetAPIError.parsingFailed
} }
let timestamp = UInt64(date.timeIntervalSince1970) * 1000 let timestamp = UInt64(date.timeIntervalSince1970) * 1000
return PublicChatMessage(serverID: serverID, senderPublicKey: getUserHexEncodedPublicKey(), displayName: displayName, profilePicture: signedMessage.profilePicture, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature, serverTime: date) return PublicChatMessage(serverID: serverID, senderPublicKey: getUserHexEncodedPublicKey(), displayName: displayName, profilePicture: signedMessage.profilePicture, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature, serverTimestamp: timestamp)
} }
} }
}.handlingInvalidAuthTokenIfNeeded(for: server) }.handlingInvalidAuthTokenIfNeeded(for: server)

@ -14,8 +14,8 @@ public final class PublicChatMessage : NSObject {
public var attachments: [Attachment] = [] public var attachments: [Attachment] = []
public let signature: Signature? public let signature: Signature?
// MARK: Server Time (use for sorting) // MARK: Server Timestamp (use for sorting)
public let serverTime: Date? public let serverTimestamp: UInt64
@objc(serverID) @objc(serverID)
public var objc_serverID: UInt64 { return serverID ?? 0 } public var objc_serverID: UInt64 { return serverID ?? 0 }
@ -75,7 +75,7 @@ public final class PublicChatMessage : NSObject {
} }
// MARK: Initialization // MARK: Initialization
public init(serverID: UInt64?, senderPublicKey: String, displayName: String, profilePicture: ProfilePicture?, body: String, type: String, timestamp: UInt64, quote: Quote?, attachments: [Attachment], signature: Signature?, serverTime: Date? = nil) { public init(serverID: UInt64?, senderPublicKey: String, displayName: String, profilePicture: ProfilePicture?, body: String, type: String, timestamp: UInt64, quote: Quote?, attachments: [Attachment], signature: Signature?, serverTimestamp: UInt64) {
self.serverID = serverID self.serverID = serverID
self.senderPublicKey = senderPublicKey self.senderPublicKey = senderPublicKey
self.displayName = displayName self.displayName = displayName
@ -86,11 +86,11 @@ public final class PublicChatMessage : NSObject {
self.quote = quote self.quote = quote
self.attachments = attachments self.attachments = attachments
self.signature = signature self.signature = signature
self.serverTime = serverTime self.serverTimestamp = serverTimestamp
super.init() super.init()
} }
@objc public convenience init(senderPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64, quotedMessageTimestamp: UInt64, quoteePublicKey: String?, quotedMessageBody: String?, quotedMessageServerID: UInt64, signatureData: Data?, signatureVersion: UInt64, serverTime: Date? = nil) { @objc public convenience init(senderPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64, quotedMessageTimestamp: UInt64, quoteePublicKey: String?, quotedMessageBody: String?, quotedMessageServerID: UInt64, signatureData: Data?, signatureVersion: UInt64, serverTimestamp: UInt64) {
let quote: Quote? let quote: Quote?
if quotedMessageTimestamp != 0, let quoteeHexEncodedPublicKey = quoteePublicKey, let quotedMessageBody = quotedMessageBody { if quotedMessageTimestamp != 0, let quoteeHexEncodedPublicKey = quoteePublicKey, let quotedMessageBody = quotedMessageBody {
let quotedMessageServerID = (quotedMessageServerID != 0) ? quotedMessageServerID : nil let quotedMessageServerID = (quotedMessageServerID != 0) ? quotedMessageServerID : nil
@ -104,7 +104,7 @@ public final class PublicChatMessage : NSObject {
} else { } else {
signature = nil signature = nil
} }
self.init(serverID: nil, senderPublicKey: senderPublicKey, displayName: displayName, profilePicture: nil, body: body, type: type, timestamp: timestamp, quote: quote, attachments: [], signature: signature, serverTime: serverTime) self.init(serverID: nil, senderPublicKey: senderPublicKey, displayName: displayName, profilePicture: nil, body: body, type: type, timestamp: timestamp, quote: quote, attachments: [], signature: signature, serverTimestamp: serverTimestamp)
} }
// MARK: Crypto // MARK: Crypto
@ -119,7 +119,7 @@ public final class PublicChatMessage : NSObject {
return nil return nil
} }
let signature = Signature(data: signatureData, version: signatureVersion) let signature = Signature(data: signatureData, version: signatureVersion)
return PublicChatMessage(serverID: serverID, senderPublicKey: senderPublicKey, displayName: displayName, profilePicture: profilePicture, body: body, type: type, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature, serverTime: serverTime) return PublicChatMessage(serverID: serverID, senderPublicKey: senderPublicKey, displayName: displayName, profilePicture: profilePicture, body: body, type: type, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature, serverTimestamp: serverTimestamp)
} }
internal func hasValidSignature() -> Bool { internal func hasValidSignature() -> Bool {

@ -75,7 +75,7 @@ public final class PublicChatPoller : NSObject {
} }
*/ */
// Sorting the messages by timestamp before importing them fixes an issue where messages that quote older messages can't find those older messages // Sorting the messages by timestamp before importing them fixes an issue where messages that quote older messages can't find those older messages
messages.sorted { $0.serverTime!.compare($1.serverTime!) == ComparisonResult.orderedAscending }.forEach { message in messages.sorted { $0.serverTimestamp < $1.serverTimestamp }.forEach { message in
var wasSentByCurrentUser = false var wasSentByCurrentUser = false
OWSPrimaryStorage.shared().dbReadConnection.read { transaction in OWSPrimaryStorage.shared().dbReadConnection.read { transaction in
wasSentByCurrentUser = LokiDatabaseUtilities.isUserLinkedDevice(message.senderPublicKey, transaction: transaction) wasSentByCurrentUser = LokiDatabaseUtilities.isUserLinkedDevice(message.senderPublicKey, transaction: transaction)

@ -1043,7 +1043,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
NSString *body = (message.body != nil && message.body.length > 0) ? message.body : [NSString stringWithFormat:@"%@", @(message.timestamp)]; // Workaround for the fact that the back-end doesn't accept messages without a body NSString *body = (message.body != nil && message.body.length > 0) ? message.body : [NSString stringWithFormat:@"%@", @(message.timestamp)]; // Workaround for the fact that the back-end doesn't accept messages without a body
LKPublicChatMessage *groupMessage = [[LKPublicChatMessage alloc] initWithSenderPublicKey:userPublicKey displayName:displayName body:body type:LKPublicChatAPI.publicChatMessageType LKPublicChatMessage *groupMessage = [[LKPublicChatMessage alloc] initWithSenderPublicKey:userPublicKey displayName:displayName body:body type:LKPublicChatAPI.publicChatMessageType
timestamp:message.timestamp quotedMessageTimestamp:quoteID quoteePublicKey:quoteePublicKey quotedMessageBody:quote.body quotedMessageServerID:quotedMessageServerID signatureData:nil signatureVersion:0 serverTime:nil]; timestamp:message.timestamp quotedMessageTimestamp:quoteID quoteePublicKey:quoteePublicKey quotedMessageBody:quote.body quotedMessageServerID:quotedMessageServerID signatureData:nil signatureVersion:0 serverTimestamp:0];
OWSLinkPreview *linkPreview = message.linkPreview; OWSLinkPreview *linkPreview = message.linkPreview;
if (linkPreview != nil) { if (linkPreview != nil) {
TSAttachmentStream *attachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId]; TSAttachmentStream *attachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId];

Loading…
Cancel
Save