From 8a041e61f75a48587ffeb8a58cef315f5e7e9997 Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Thu, 15 Aug 2024 00:05:37 +0800 Subject: [PATCH] Stars improvements --- .../Sources/State/MessageReactions.swift | 18 ++++++++-- .../Messages/TelegramEngineMessages.swift | 2 +- .../Sources/ChatSendStarsScreen.swift | 33 ++++++++++++++----- ...ChatControllerOpenMessageContextMenu.swift | 2 +- .../TelegramUI/Sources/ChatController.swift | 2 +- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/submodules/TelegramCore/Sources/State/MessageReactions.swift b/submodules/TelegramCore/Sources/State/MessageReactions.swift index 55b47ce212..f4e704c406 100644 --- a/submodules/TelegramCore/Sources/State/MessageReactions.swift +++ b/submodules/TelegramCore/Sources/State/MessageReactions.swift @@ -172,7 +172,7 @@ public func updateMessageReactionsInteractively(account: Account, messageIds: [M |> ignoreValues } -public func sendStarsReactionsInteractively(account: Account, messageId: MessageId, count: Int, isAnonymous: Bool) -> Signal { +public func sendStarsReactionsInteractively(account: Account, messageId: MessageId, count: Int, isAnonymous: Bool?) -> Signal { return account.postbox.transaction { transaction -> Void in transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: SendStarsReactionsAction(randomId: Int64.random(in: Int64.min ... Int64.max))) transaction.updateMessage(messageId, update: { currentMessage in @@ -182,15 +182,28 @@ public func sendStarsReactionsInteractively(account: Account, messageId: Message } var mappedCount = Int32(count) var attributes = currentMessage.attributes + var resolvedIsAnonymous = false + for attribute in attributes { + if let attribute = attribute as? ReactionsMessageAttribute { + if let myReaction = attribute.topPeers.first(where: { $0.isMy }) { + resolvedIsAnonymous = myReaction.isAnonymous + } + } + } loop: for j in 0 ..< attributes.count { if let current = attributes[j] as? PendingStarsReactionsMessageAttribute { mappedCount += current.count + resolvedIsAnonymous = current.isAnonymous attributes.remove(at: j) break loop } } + + if let isAnonymous { + resolvedIsAnonymous = isAnonymous + } - attributes.append(PendingStarsReactionsMessageAttribute(accountPeerId: account.peerId, count: mappedCount, isAnonymous: isAnonymous)) + attributes.append(PendingStarsReactionsMessageAttribute(accountPeerId: account.peerId, count: mappedCount, isAnonymous: resolvedIsAnonymous)) return .update(StoreMessage(id: currentMessage.id, globallyUniqueId: currentMessage.globallyUniqueId, groupingKey: currentMessage.groupingKey, threadId: currentMessage.threadId, timestamp: currentMessage.timestamp, flags: StoreMessageFlags(currentMessage.flags), tags: currentMessage.tags, globalTags: currentMessage.globalTags, localTags: currentMessage.localTags, forwardInfo: storeForwardInfo, authorId: currentMessage.author?.id, text: currentMessage.text, attributes: attributes, media: currentMessage.media)) }) @@ -395,7 +408,6 @@ private func requestSendStarsReaction(postbox: Postbox, network: Network, stateM } |> mapToSignal { result -> Signal in stateManager.starsContext?.add(balance: Int64(-count), addTransaction: false) - //stateManager.starsContext?.load(force: true) return postbox.transaction { transaction -> Void in transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: UpdateMessageReactionsAction()) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift index a85b0c656c..8404b7fa1d 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift @@ -334,7 +334,7 @@ public extension TelegramEngine { ).startStandalone() } - public func sendStarsReaction(id: EngineMessage.Id, count: Int, isAnonymous: Bool) { + public func sendStarsReaction(id: EngineMessage.Id, count: Int, isAnonymous: Bool?) { let _ = sendStarsReactionsInteractively(account: self.account, messageId: id, count: count, isAnonymous: isAnonymous).startStandalone() } diff --git a/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift b/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift index 21f3a4b461..33fed16370 100644 --- a/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift +++ b/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift @@ -1652,11 +1652,14 @@ private final class ChatSendStarsScreenComponent: Component { myCountAddition = Int(self.amount.realValue) } myCount += myCountAddition - mappedTopPeers.append(ChatSendStarsScreen.TopPeer( - peer: self.isAnonymous ? nil : component.myPeer, - isMy: true, - count: myCount - )) + if myCount != 0 { + mappedTopPeers.append(ChatSendStarsScreen.TopPeer( + randomIndex: -1, + peer: self.isAnonymous ? nil : component.myPeer, + isMy: true, + count: myCount + )) + } mappedTopPeers.sort(by: { $0.count > $1.count }) if mappedTopPeers.count > 3 { mappedTopPeers = Array(mappedTopPeers.prefix(3)) @@ -2064,7 +2067,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { fileprivate final class TopPeer: Equatable { enum Id: Hashable { - case anonymous + case anonymous(Int) case my case peer(EnginePeer.Id) } @@ -2075,7 +2078,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { } else if let peer = self.peer { return .peer(peer.id) } else { - return .anonymous + return .anonymous(self.randomIndex) } } @@ -2083,17 +2086,22 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { return self.peer == nil } + let randomIndex: Int let peer: EnginePeer? let isMy: Bool let count: Int - init(peer: EnginePeer?, isMy: Bool, count: Int) { + init(randomIndex: Int, peer: EnginePeer?, isMy: Bool, count: Int) { + self.randomIndex = randomIndex self.peer = peer self.isMy = isMy self.count = count } static func ==(lhs: TopPeer, rhs: TopPeer) -> Bool { + if lhs.randomIndex != rhs.randomIndex { + return false + } if lhs.peer != rhs.peer { return false } @@ -2210,6 +2218,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { return nil } + var nextRandomIndex = 0 return InitialData( peer: peer, myPeer: myPeer, @@ -2218,7 +2227,10 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { currentSentAmount: currentSentAmount, topPeers: topPeers.compactMap { topPeer -> ChatSendStarsScreen.TopPeer? in guard let topPeerId = topPeer.peerId else { + let randomIndex = nextRandomIndex + nextRandomIndex += 1 return ChatSendStarsScreen.TopPeer( + randomIndex: randomIndex, peer: nil, isMy: topPeer.isMy, count: Int(topPeer.count) @@ -2230,7 +2242,10 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { guard let topPeerValue else { return nil } + let randomIndex = nextRandomIndex + nextRandomIndex += 1 return ChatSendStarsScreen.TopPeer( + randomIndex: randomIndex, peer: topPeer.isAnonymous ? nil : topPeerValue, isMy: topPeer.isMy, count: Int(topPeer.count) @@ -2239,6 +2254,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { myTopPeer: myTopPeer.flatMap { topPeer -> ChatSendStarsScreen.TopPeer? in guard let topPeerId = topPeer.peerId else { return ChatSendStarsScreen.TopPeer( + randomIndex: -1, peer: nil, isMy: topPeer.isMy, count: Int(topPeer.count) @@ -2251,6 +2267,7 @@ public class ChatSendStarsScreen: ViewControllerComponentContainer { return nil } return ChatSendStarsScreen.TopPeer( + randomIndex: -1, peer: topPeer.isAnonymous ? nil : topPeerValue, isMy: topPeer.isMy, count: Int(topPeer.count) diff --git a/submodules/TelegramUI/Sources/Chat/ChatControllerOpenMessageContextMenu.swift b/submodules/TelegramUI/Sources/Chat/ChatControllerOpenMessageContextMenu.swift index 8c8ffd7bb8..472af48ec3 100644 --- a/submodules/TelegramUI/Sources/Chat/ChatControllerOpenMessageContextMenu.swift +++ b/submodules/TelegramUI/Sources/Chat/ChatControllerOpenMessageContextMenu.swift @@ -424,7 +424,7 @@ extension ChatControllerImpl { return } - strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: false) + strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: nil) strongSelf.displayOrUpdateSendStarsUndo(messageId: message.id, count: 1) }) } else { diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index e6bf3d31f7..4c3824f63d 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -1729,7 +1729,7 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G return } - strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: false) + strongSelf.context.engine.messages.sendStarsReaction(id: message.id, count: 1, isAnonymous: nil) strongSelf.displayOrUpdateSendStarsUndo(messageId: message.id, count: 1) }) } else {