From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Mon, 25 May 2026 04:05:46 +0400
Subject: prevent web preview from refetching the same link when typing

---
 .../java/org/telegram/ui/ChatActivity.java    | 32 ++++++++++++++-----
 .../ui/Components/ChatActivityEnterView.java  |  2 +-
 2 files changed, 25 insertions(+), 9 deletions(-)

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
@@ -13990,6 +13990,15 @@ public class ChatActivity extends BaseFragment implements
         }
     }
 
+    public void inu_clearLinkPreviewState() {
+        waitingForWebpageId++;
+        cancelSearchLinks();
+        linkSearchRequestId = 0;
+        pendingLinkSearchString = null;
+        foundUrls = null;
+        foundWebPage = null;
+    }
+
     private void editResetMediaManual() {
         if (editingMessageObject != null && editingMessageObject.messageOwner != null && editingMessageObject.messageOwner.media != null) {
             editingMessageObject.messageOwner.media.manual = false;
@@ -14032,7 +14041,7 @@ public class ChatActivity extends BaseFragment implements
             return;
         }
         checkEditLinkRemoved(charSequence);
-        if (force && foundWebPage != null) {
+        if (force && foundWebPage != null && !(foundWebPage instanceof TLRPC.TL_webPagePending)) {
             if (foundWebPage.url != null) {
                 int index = TextUtils.indexOf(charSequence, foundWebPage.url);
                 char lastChar = 0;
@@ -14065,12 +14074,6 @@ public class ChatActivity extends BaseFragment implements
         }
         final MessagesController messagesController = getMessagesController();
         Utilities.searchQueue.postRunnable(() -> {
-            boolean requestAnyway = false;
-            if (linkSearchRequestId != 0) {
-                getConnectionsManager().cancelRequest(linkSearchRequestId, true);
-                linkSearchRequestId = 0;
-                requestAnyway = true;
-            }
             ArrayList<CharSequence> urls = null;
             CharSequence textToCheck;
             try {
@@ -14104,10 +14107,14 @@ public class ChatActivity extends BaseFragment implements
                             clear = false;
                         }
                     }
-                    if (clear && !requestAnyway) {
+                    if (clear) {
                         return;
                     }
                 }
+                if (linkSearchRequestId != 0) {
+                    getConnectionsManager().cancelRequest(linkSearchRequestId, true);
+                    linkSearchRequestId = 0;
+                }
                 foundUrls = urls;
                 if (urls == null) {
                     AndroidUtilities.runOnUIThread(() -> {
@@ -14127,9 +14134,14 @@ public class ChatActivity extends BaseFragment implements
                     });
                     return;
                 }
+                if (linkSearchRequestId != 0) {
+                    getConnectionsManager().cancelRequest(linkSearchRequestId, true);
+                    linkSearchRequestId = 0;
+                }
                 textToCheck = charSequence;
             }
             final String firstUrl = urls == null || urls.isEmpty() ? null : urls.get(0).toString();
+            final ArrayList<CharSequence> requestUrls = urls;
 
             if (currentEncryptedChat != null && messagesController.secretWebpagePreview == 2) {
                 AndroidUtilities.runOnUIThread(() -> {
@@ -14165,6 +14177,10 @@ public class ChatActivity extends BaseFragment implements
                 if (waitingForWebpageId != myId) {
                     return;
                 }
+                CharSequence currentText = chatActivityEnterView != null ? chatActivityEnterView.getFieldText() : null;
+                if (TextUtils.isEmpty(currentText) || requestUrls != null && !desu.inugram.helpers.chat.WebPreviewHelper.allUrlsPresent(requestUrls, currentText) || requestUrls == null && !TextUtils.equals(currentText, charSequence)) {
+                    return;
+                }
                 if (success) {
                     foundWebPage = webpage;
                     foundWebPage.display_url = req.message;
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java
@@ -7816,7 +7816,7 @@ public class ChatActivityEnterView extends FrameLayout implements
                 }
                 if (parentFragment != null) {
                     parentFragment.editingMessageObject = null;
-                    parentFragment.foundWebPage = null;
+                    parentFragment.inu_clearLinkPreviewState();
                     if (parentFragment.messagePreviewParams != null) {
                         parentFragment.messagePreviewParams.updateLink(currentAccount, null, "", null, null, null);
                     }
