From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Thu, 11 Jun 2026 16:32:07 +0400
Subject: hopefully fix cancelling sending media

---
 .../messenger/SendMessagesHelper.java         | 20 ++++++++++++++-----
 .../java/org/telegram/ui/ChatActivity.java    |  4 +---
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/SendMessagesHelper.java b/TMessagesProj/src/main/java/org/telegram/messenger/SendMessagesHelper.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/messenger/SendMessagesHelper.java
+++ b/TMessagesProj/src/main/java/org/telegram/messenger/SendMessagesHelper.java
@@ -6972,12 +6972,14 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
         if (inputMedia != null) {
             TLRPC.TL_messages_uploadMedia req = new TLRPC.TL_messages_uploadMedia();
             req.media = inputMedia;
+            TLRPC.Message sendingMessage = null;
             if (message.sendRequest instanceof TLRPC.TL_messages_sendMultiMedia) {
                 TLRPC.TL_messages_sendMultiMedia multiMedia = (TLRPC.TL_messages_sendMultiMedia) message.sendRequest;
                 req.peer = multiMedia.peer;
                 for (int a = 0; a < multiMedia.multi_media.size(); a++) {
                     if (multiMedia.multi_media.get(a).media == inputMedia) {
-                        putToSendingMessages(message.messages.get(a), message.scheduled);
+                        sendingMessage = message.messages.get(a);
+                        putToSendingMessages(sendingMessage, message.scheduled);
                         getNotificationCenter().postNotificationName(NotificationCenter.fileUploadProgressChanged, key, -1L, -1L, false);
                         break;
                     }
@@ -6988,7 +6990,8 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
                 TLRPC.TL_inputMediaPaidMedia multiMedia = (TLRPC.TL_inputMediaPaidMedia) sendMedia.media;
                 for (int a = 0; a < multiMedia.extended_media.size(); a++) {
                     if (multiMedia.extended_media.get(a) == inputMedia) {
-                        putToSendingMessages(message.messages.get(a), message.scheduled);
+                        sendingMessage = message.messages.get(a);
+                        putToSendingMessages(sendingMessage, message.scheduled);
                         getNotificationCenter().postNotificationName(NotificationCenter.fileUploadProgressChanged, key, -1L, -1L, false);
                         break;
                     }
@@ -7000,7 +7003,8 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
                 final int mediaPackIndex = PollAttachedMediaPack.findInputMedia(multiMedia, inputMedia);
                 for (int a = 0; a < message.pollIndexes.size(); a++) {
                     if (message.pollIndexes.get(a) == mediaPackIndex) {
-                        putToSendingMessages(message.messages.get(a), message.scheduled);
+                        sendingMessage = message.messages.get(a);
+                        putToSendingMessages(sendingMessage, message.scheduled);
                         getNotificationCenter().postNotificationName(NotificationCenter.fileUploadProgressChanged, key, -1L, -1L, false);
                         break;
                     }
@@ -7029,7 +7033,7 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
                     }
                 }
             }
-            getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
+            int reqId = getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                 TLRPC.InputMedia newInputMedia = null;
                 if (response != null) {
                     final TLRPC.MessageMedia messageMedia = (TLRPC.MessageMedia) response;
@@ -7120,6 +7124,9 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
                     message.markAsError();
                 }
             }));
+            if (sendingMessage != null) {
+                sendingMessage.reqId = reqId;
+            }
         } else if (inputEncryptedFile != null) {
             TLRPC.TL_messages_sendEncryptedMultiMedia multiMedia = (TLRPC.TL_messages_sendEncryptedMultiMedia) message.sendEncryptedRequest;
             for (int a = 0; a < multiMedia.files.size(); a++) {
@@ -7450,7 +7457,7 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
             return;
         }
 
-        getConnectionsManager().sendRequest(request, (response, error) -> {
+        int reqId = getConnectionsManager().sendRequest(request, (response, error) -> {
             if (error != null && FileRefController.isFileRefError(error.text)) {
                 final int fileRefIndex = FileRefController.getFileRefErrorIndex(error.text);
                 if (parentObjects != null) {
@@ -7740,6 +7747,9 @@ public class SendMessagesHelper extends BaseController implements NotificationCe
                 }
             });
         }, null, ConnectionsManager.RequestFlagCanCompress | ConnectionsManager.RequestFlagInvokeAfter);
+        for (int a = 0, size = msgObjs.size(); a < size; a++) {
+            msgObjs.get(a).messageOwner.reqId = reqId;
+        }
     }
 
     private void performSendMessageRequest(final TLObject req, final MessageObject msgObj, final String originalPath, DelayedMessage delayedMessage, Object parentObject, HashMap<String, String> params, boolean scheduled) {
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java
@@ -33901,9 +33901,7 @@ public class ChatActivity extends BaseFragment implements
                 if (selectedObject.isEditing() || selectedObject.isSending() && selectedObjectGroup == null) {
                     getSendMessagesHelper().cancelSendingMessage(selectedObject);
                 } else if (selectedObject.isSending() && selectedObjectGroup != null) {
-                    for (int a = 0; a < selectedObjectGroup.messages.size(); a++) {
-                        getSendMessagesHelper().cancelSendingMessage(new ArrayList<>(selectedObjectGroup.messages));
-                    }
+                    getSendMessagesHelper().cancelSendingMessage(new ArrayList<>(selectedObjectGroup.messages));
                 }
                 break;
             }
