Fix negative peer ids

This commit is contained in:
Ali 2021-04-01 00:30:04 +04:00
parent 1ebf1b78d7
commit 99a34ac2fd
2 changed files with 23 additions and 13 deletions

View File

@ -1,7 +1,7 @@
import Foundation import Foundation
public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable { public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
public struct Namespace: Comparable, Hashable, Codable { public struct Namespace: Comparable, Hashable, Codable, CustomStringConvertible {
public static var max: Namespace { public static var max: Namespace {
return Namespace(rawValue: 0x7) return Namespace(rawValue: 0x7)
} }
@ -24,6 +24,10 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
} }
} }
public var description: String {
return "\(self.rawValue)"
}
fileprivate init(rawValue: UInt32) { fileprivate init(rawValue: UInt32) {
precondition((rawValue | 0x7) == 0x7) precondition((rawValue | 0x7) == 0x7)
@ -52,11 +56,11 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
return Id(rawValue: 0x000000007fffffff) return Id(rawValue: 0x000000007fffffff)
} }
fileprivate var rawValue: UInt32 fileprivate var rawValue: Int32
var predecessor: Id { var predecessor: Id {
if self.rawValue != 0 { if self.rawValue != 0 {
return Id(rawValue: UInt64(self.rawValue - 1)) return Id(rawValue: self.rawValue - 1)
} else { } else {
return self return self
} }
@ -64,24 +68,28 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
var successor: Id { var successor: Id {
if self.rawValue != Id.max.rawValue { if self.rawValue != Id.max.rawValue {
return Id(rawValue: UInt64(self.rawValue + 1)) return Id(rawValue: self.rawValue + 1)
} else { } else {
return self return self
} }
} }
fileprivate init(rawValue: UInt64) { public var description: String {
precondition((rawValue | 0x000FFFFFFFFFFFFF) == 0x000FFFFFFFFFFFFF) return "\(self.rawValue)"
}
self.rawValue = UInt32(rawValue) fileprivate init(rawValue: Int32) {
//precondition((rawValue | 0x000FFFFFFFFFFFFF) == 0x000FFFFFFFFFFFFF)
self.rawValue = rawValue
} }
public static func _internalFromInt32Value(_ value: Int32) -> Id { public static func _internalFromInt32Value(_ value: Int32) -> Id {
return Id(rawValue: UInt64(UInt32(bitPattern: value))) return Id(rawValue: value)
} }
public func _internalGetInt32Value() -> Int32 { public func _internalGetInt32Value() -> Int32 {
return Int32(clamping: self.rawValue) return self.rawValue
} }
public static func <(lhs: Id, rhs: Id) -> Bool { public static func <(lhs: Id, rhs: Id) -> Bool {
@ -137,7 +145,7 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
if legacyNamespaceBits == 0x7fffffff && idLowBits == 0 { if legacyNamespaceBits == 0x7fffffff && idLowBits == 0 {
self.namespace = .max self.namespace = .max
self.id = Id(rawValue: idLowBits) self.id = Id(rawValue: Int32(bitPattern: UInt32(clamping: idLowBits)))
} else { } else {
let namespaceBits = ((data >> 32) & 0x7) let namespaceBits = ((data >> 32) & 0x7)
self.namespace = Namespace(rawValue: UInt32(namespaceBits)) self.namespace = Namespace(rawValue: UInt32(namespaceBits))
@ -145,12 +153,12 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
let idHighBits = (data >> (32 + 3)) & 0xffffffff let idHighBits = (data >> (32 + 3)) & 0xffffffff
assert(idHighBits == 0) assert(idHighBits == 0)
self.id = Id(rawValue: idLowBits) self.id = Id(rawValue: Int32(bitPattern: UInt32(clamping: idLowBits)))
} }
} }
public func toInt64() -> Int64 { public func toInt64() -> Int64 {
let idLowBits = self.id.rawValue & 0xffffffff let idLowBits = UInt32(bitPattern: self.id.rawValue)
let result: Int64 let result: Int64
if self.namespace == .max && self.id.rawValue == 0 { if self.namespace == .max && self.id.rawValue == 0 {
@ -166,7 +174,8 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
var data: UInt64 = 0 var data: UInt64 = 0
data |= UInt64(self.namespace.rawValue) << 32 data |= UInt64(self.namespace.rawValue) << 32
let idHighBits = (self.id.rawValue >> 32) & 0x3FFFFFFF let idValue = UInt32(bitPattern: self.id.rawValue)
let idHighBits = (idValue >> 32) & 0x3FFFFFFF
assert(idHighBits == 0) assert(idHighBits == 0)
data |= UInt64(idLowBits) data |= UInt64(idLowBits)

View File

@ -184,6 +184,7 @@ private func requestActivity(postbox: Postbox, network: Network, accountPeerId:
return .complete() return .complete()
} }
} else if let peer = peer as? TelegramSecretChat, activity == .typingText { } else if let peer = peer as? TelegramSecretChat, activity == .typingText {
let _ = PeerId(peer.id.toInt64())
return network.request(Api.functions.messages.setEncryptedTyping(peer: .inputEncryptedChat(chatId: peer.id.id._internalGetInt32Value(), accessHash: peer.accessHash), typing: .boolTrue)) return network.request(Api.functions.messages.setEncryptedTyping(peer: .inputEncryptedChat(chatId: peer.id.id._internalGetInt32Value(), accessHash: peer.accessHash), typing: .boolTrue))
|> `catch` { _ -> Signal<Api.Bool, NoError> in |> `catch` { _ -> Signal<Api.Bool, NoError> in
return .single(.boolFalse) return .single(.boolFalse)