From 270d49557cb164d11e2ba0d4c0e7a2d9ff4097cc Mon Sep 17 00:00:00 2001 From: Ali <> Date: Tue, 7 Mar 2023 15:59:14 +0400 Subject: [PATCH] Support reaction timestamps --- submodules/TelegramApi/Sources/Api0.swift | 2 +- submodules/TelegramApi/Sources/Api14.swift | 22 +++++++++++-------- .../ApiUtils/ReactionsMessageAttribute.swift | 10 ++++----- .../Sources/State/MessageReactions.swift | 6 ++--- .../Sources/State/Serialization.swift | 2 +- .../SyncCore_ReactionsMessageAttribute.swift | 10 ++++++++- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/submodules/TelegramApi/Sources/Api0.swift b/submodules/TelegramApi/Sources/Api0.swift index ddb22241cb..b1e1b7034f 100644 --- a/submodules/TelegramApi/Sources/Api0.swift +++ b/submodules/TelegramApi/Sources/Api0.swift @@ -514,7 +514,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1618676578] = { return Api.MessageMedia.parse_messageMediaUnsupported($0) } dict[784356159] = { return Api.MessageMedia.parse_messageMediaVenue($0) } dict[-1557277184] = { return Api.MessageMedia.parse_messageMediaWebPage($0) } - dict[-1319698788] = { return Api.MessagePeerReaction.parse_messagePeerReaction($0) } + dict[-1938180548] = { return Api.MessagePeerReaction.parse_messagePeerReaction($0) } dict[182649427] = { return Api.MessageRange.parse_messageRange($0) } dict[1328256121] = { return Api.MessageReactions.parse_messageReactions($0) } dict[-2083123262] = { return Api.MessageReplies.parse_messageReplies($0) } diff --git a/submodules/TelegramApi/Sources/Api14.swift b/submodules/TelegramApi/Sources/Api14.swift index 01a381dfa4..5f7b972e35 100644 --- a/submodules/TelegramApi/Sources/Api14.swift +++ b/submodules/TelegramApi/Sources/Api14.swift @@ -1,15 +1,16 @@ public extension Api { enum MessagePeerReaction: TypeConstructorDescription { - case messagePeerReaction(flags: Int32, peerId: Api.Peer, reaction: Api.Reaction) + case messagePeerReaction(flags: Int32, peerId: Api.Peer, date: Int32, reaction: Api.Reaction) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { - case .messagePeerReaction(let flags, let peerId, let reaction): + case .messagePeerReaction(let flags, let peerId, let date, let reaction): if boxed { - buffer.appendInt32(-1319698788) + buffer.appendInt32(-1938180548) } serializeInt32(flags, buffer: buffer, boxed: false) peerId.serialize(buffer, true) + serializeInt32(date, buffer: buffer, boxed: false) reaction.serialize(buffer, true) break } @@ -17,8 +18,8 @@ public extension Api { public func descriptionFields() -> (String, [(String, Any)]) { switch self { - case .messagePeerReaction(let flags, let peerId, let reaction): - return ("messagePeerReaction", [("flags", flags as Any), ("peerId", peerId as Any), ("reaction", reaction as Any)]) + case .messagePeerReaction(let flags, let peerId, let date, let reaction): + return ("messagePeerReaction", [("flags", flags as Any), ("peerId", peerId as Any), ("date", date as Any), ("reaction", reaction as Any)]) } } @@ -29,15 +30,18 @@ public extension Api { if let signature = reader.readInt32() { _2 = Api.parse(reader, signature: signature) as? Api.Peer } - var _3: Api.Reaction? + var _3: Int32? + _3 = reader.readInt32() + var _4: Api.Reaction? if let signature = reader.readInt32() { - _3 = Api.parse(reader, signature: signature) as? Api.Reaction + _4 = Api.parse(reader, signature: signature) as? Api.Reaction } let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = _3 != nil - if _c1 && _c2 && _c3 { - return Api.MessagePeerReaction.messagePeerReaction(flags: _1!, peerId: _2!, reaction: _3!) + let _c4 = _4 != nil + if _c1 && _c2 && _c3 && _c4 { + return Api.MessagePeerReaction.messagePeerReaction(flags: _1!, peerId: _2!, date: _3!, reaction: _4!) } else { return nil diff --git a/submodules/TelegramCore/Sources/ApiUtils/ReactionsMessageAttribute.swift b/submodules/TelegramCore/Sources/ApiUtils/ReactionsMessageAttribute.swift index 773205e5cf..d2300cd98f 100644 --- a/submodules/TelegramCore/Sources/ApiUtils/ReactionsMessageAttribute.swift +++ b/submodules/TelegramCore/Sources/ApiUtils/ReactionsMessageAttribute.swift @@ -22,11 +22,11 @@ extension ReactionsMessageAttribute { if let recentReactions = recentReactions { parsedRecentReactions = recentReactions.compactMap { recentReaction -> ReactionsMessageAttribute.RecentPeer? in switch recentReaction { - case let .messagePeerReaction(flags, peerId, reaction): + case let .messagePeerReaction(flags, peerId, date, reaction): let isLarge = (flags & (1 << 0)) != 0 let isUnseen = (flags & (1 << 1)) != 0 if let reaction = MessageReaction.Reaction(apiReaction: reaction) { - return ReactionsMessageAttribute.RecentPeer(value: reaction, isLarge: isLarge, isUnseen: isUnseen, peerId: peerId.peerId) + return ReactionsMessageAttribute.RecentPeer(value: reaction, isLarge: isLarge, isUnseen: isUnseen, peerId: peerId.peerId, timestamp: date) } else { return nil } @@ -117,7 +117,7 @@ private func mergeReactions(reactions: [MessageReaction], recentPeers: [Reaction if let index = recentPeers.firstIndex(where: { $0.value == pendingReaction.value && $0.peerId == accountPeerId }) { recentPeers.remove(at: index) } - recentPeers.append(ReactionsMessageAttribute.RecentPeer(value: pendingReaction.value, isLarge: false, isUnseen: false, peerId: accountPeerId)) + recentPeers.append(ReactionsMessageAttribute.RecentPeer(value: pendingReaction.value, isLarge: false, isUnseen: false, peerId: accountPeerId, timestamp: Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970))) } for i in (0 ..< result.count).reversed() { @@ -184,11 +184,11 @@ extension ReactionsMessageAttribute { if let recentReactions = recentReactions { parsedRecentReactions = recentReactions.compactMap { recentReaction -> ReactionsMessageAttribute.RecentPeer? in switch recentReaction { - case let .messagePeerReaction(flags, peerId, reaction): + case let .messagePeerReaction(flags, peerId, date, reaction): let isLarge = (flags & (1 << 0)) != 0 let isUnseen = (flags & (1 << 1)) != 0 if let reaction = MessageReaction.Reaction(apiReaction: reaction) { - return ReactionsMessageAttribute.RecentPeer(value: reaction, isLarge: isLarge, isUnseen: isUnseen, peerId: peerId.peerId) + return ReactionsMessageAttribute.RecentPeer(value: reaction, isLarge: isLarge, isUnseen: isUnseen, peerId: peerId.peerId, timestamp: date) } else { return nil } diff --git a/submodules/TelegramCore/Sources/State/MessageReactions.swift b/submodules/TelegramCore/Sources/State/MessageReactions.swift index cde7906e09..ad811dbec4 100644 --- a/submodules/TelegramCore/Sources/State/MessageReactions.swift +++ b/submodules/TelegramCore/Sources/State/MessageReactions.swift @@ -338,7 +338,7 @@ public extension EngineMessageReactionListContext.State { for recentPeer in reactionsAttribute.recentPeers { if let peer = message.peers[recentPeer.peerId] { if reaction == nil || recentPeer.value == reaction { - items.append(EngineMessageReactionListContext.Item(peer: EnginePeer(peer), reaction: recentPeer.value, timestamp: readStats?.readTimestamps[peer.id])) + items.append(EngineMessageReactionListContext.Item(peer: EnginePeer(peer), reaction: recentPeer.value, timestamp: recentPeer.timestamp ?? readStats?.readTimestamps[peer.id])) } } } @@ -507,9 +507,9 @@ public final class EngineMessageReactionListContext { var items: [EngineMessageReactionListContext.Item] = [] for reaction in reactions { switch reaction { - case let .messagePeerReaction(_, peer, reaction): + case let .messagePeerReaction(_, peer, date, reaction): if let peer = transaction.getPeer(peer.peerId), let reaction = MessageReaction.Reaction(apiReaction: reaction) { - items.append(EngineMessageReactionListContext.Item(peer: EnginePeer(peer), reaction: reaction, timestamp: nil)) + items.append(EngineMessageReactionListContext.Item(peer: EnginePeer(peer), reaction: reaction, timestamp: date)) } } } diff --git a/submodules/TelegramCore/Sources/State/Serialization.swift b/submodules/TelegramCore/Sources/State/Serialization.swift index 1f6aa3fcb9..925e2a80ac 100644 --- a/submodules/TelegramCore/Sources/State/Serialization.swift +++ b/submodules/TelegramCore/Sources/State/Serialization.swift @@ -210,7 +210,7 @@ public class BoxedMessage: NSObject { public class Serialization: NSObject, MTSerialization { public func currentLayer() -> UInt { - return 154 + return 155 } public func parseMessage(_ data: Data!) -> Any! { diff --git a/submodules/TelegramCore/Sources/SyncCore/SyncCore_ReactionsMessageAttribute.swift b/submodules/TelegramCore/Sources/SyncCore/SyncCore_ReactionsMessageAttribute.swift index e17ecd5814..ed23a4220a 100644 --- a/submodules/TelegramCore/Sources/SyncCore/SyncCore_ReactionsMessageAttribute.swift +++ b/submodules/TelegramCore/Sources/SyncCore/SyncCore_ReactionsMessageAttribute.swift @@ -119,12 +119,14 @@ public final class ReactionsMessageAttribute: Equatable, MessageAttribute { public var isLarge: Bool public var isUnseen: Bool public var peerId: PeerId + public var timestamp: Int32? - public init(value: MessageReaction.Reaction, isLarge: Bool, isUnseen: Bool, peerId: PeerId) { + public init(value: MessageReaction.Reaction, isLarge: Bool, isUnseen: Bool, peerId: PeerId, timestamp: Int32?) { self.value = value self.isLarge = isLarge self.isUnseen = isUnseen self.peerId = peerId + self.timestamp = timestamp } public init(decoder: PostboxDecoder) { @@ -136,6 +138,7 @@ public final class ReactionsMessageAttribute: Equatable, MessageAttribute { self.isLarge = decoder.decodeInt32ForKey("l", orElse: 0) != 0 self.isUnseen = decoder.decodeInt32ForKey("u", orElse: 0) != 0 self.peerId = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) + self.timestamp = decoder.decodeOptionalInt32ForKey("ts") } public func encode(_ encoder: PostboxEncoder) { @@ -148,6 +151,11 @@ public final class ReactionsMessageAttribute: Equatable, MessageAttribute { encoder.encodeInt32(self.isLarge ? 1 : 0, forKey: "l") encoder.encodeInt32(self.isUnseen ? 1 : 0, forKey: "u") encoder.encodeInt64(self.peerId.toInt64(), forKey: "p") + if let timestamp = self.timestamp { + encoder.encodeInt32(timestamp, forKey: "ts") + } else { + encoder.encodeNil(forKey: "ts") + } } }