| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import org.telegram.messenger.AndroidUtilities; |
| 4 | import org.telegram.messenger.FileLog; |
| 5 | import org.telegram.messenger.MessagesController; |
| 6 | import org.telegram.messenger.UserConfig; |
| 7 | import org.telegram.tgnet.ConnectionsManager; |
| 8 | import org.telegram.tgnet.TLRPC; |
| 9 | |
| 10 | import java.util.ArrayList; |
| 11 | |
| 12 | public class MessageHelper { |
| 13 | |
| 14 | private static volatile MessageHelper[] Instance = new MessageHelper[UserConfig.MAX_ACCOUNT_COUNT]; |
| 15 | private int currentAccount; |
| 16 | private int reqId; |
| 17 | private int mergeReqId; |
| 18 | private int lastReqId; |
| 19 | private int[] messagesSearchCount = new int[]{0, 0}; |
| 20 | |
| 21 | public MessageHelper(int num) { |
| 22 | currentAccount = num; |
| 23 | } |
| 24 | |
| 25 | public static MessageHelper getInstance(int num) { |
| 26 | MessageHelper localInstance = Instance[num]; |
| 27 | if (localInstance == null) { |
| 28 | synchronized (MessageHelper.class) { |
| 29 | localInstance = Instance[num]; |
| 30 | if (localInstance == null) { |
| 31 | Instance[num] = localInstance = new MessageHelper(num); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | return localInstance; |
| 36 | } |
| 37 | |
| 38 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user, boolean scheduled) { |
| 39 | deleteUserChannelHistoryWithSearch(dialog_id, user, scheduled, false); |
| 40 | } |
| 41 | |
| 42 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user, boolean scheduled, final boolean internal) { |
| 43 | boolean firstQuery = !internal; |
| 44 | if (reqId != 0) { |
| 45 | ConnectionsManager.getInstance(currentAccount).cancelRequest(reqId, true); |
| 46 | reqId = 0; |
| 47 | } |
| 48 | if (mergeReqId != 0) { |
| 49 | ConnectionsManager.getInstance(currentAccount).cancelRequest(mergeReqId, true); |
| 50 | mergeReqId = 0; |
| 51 | } |
| 52 | if (firstQuery) { |
| 53 | TLRPC.InputPeer inputPeer = MessagesController.getInstance(currentAccount).getInputPeer((int) dialog_id); |
| 54 | if (inputPeer == null) { |
| 55 | return; |
| 56 | } |
| 57 | final TLRPC.TL_messages_search req = new TLRPC.TL_messages_search(); |
| 58 | req.peer = inputPeer; |
| 59 | req.limit = 1; |
| 60 | req.q = ""; |
| 61 | if (user != null) { |
| 62 | req.from_id = MessagesController.getInstance(currentAccount).getInputUser(user); |
| 63 | req.flags |= 1; |
| 64 | } |
| 65 | req.filter = new TLRPC.TL_inputMessagesFilterEmpty(); |
| 66 | mergeReqId = ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> { |
| 67 | mergeReqId = 0; |
| 68 | if (response != null) { |
| 69 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 70 | messagesSearchCount[0] = res.count; |
| 71 | FileLog.d("total " + messagesSearchCount[0]); |
| 72 | deleteUserChannelHistoryWithSearch(dialog_id, user, true); |
| 73 | } |
| 74 | }), ConnectionsManager.RequestFlagFailOnServerErrors); |
| 75 | } else { |
| 76 | final TLRPC.TL_messages_search req = new TLRPC.TL_messages_search(); |
| 77 | req.peer = MessagesController.getInstance(currentAccount).getInputPeer((int) dialog_id); |
| 78 | if (req.peer == null) { |
| 79 | return; |
| 80 | } |
| 81 | req.limit = 100; |
| 82 | req.q = ""; |
| 83 | req.offset_id = 0; |
| 84 | if (user != null) { |
| 85 | req.from_id = MessagesController.getInstance(currentAccount).getInputUser(user); |
| 86 | req.flags |= 1; |
| 87 | } |
| 88 | req.filter = new TLRPC.TL_inputMessagesFilterEmpty(); |
| 89 | final int currentReqId = ++lastReqId; |
| 90 | reqId = ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> { |
| 91 | if (currentReqId == lastReqId) { |
| 92 | reqId = 0; |
| 93 | if (response != null) { |
| 94 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 95 | messagesSearchCount[1] = res.messages.size(); |
| 96 | for (int a = 0; a < res.messages.size(); a++) { |
| 97 | TLRPC.Message message = res.messages.get(a); |
| 98 | if (message instanceof TLRPC.TL_messageEmpty || message.action instanceof TLRPC.TL_messageActionHistoryClear || message instanceof TLRPC.TL_messageService) { |
| 99 | res.messages.remove(a); |
| 100 | a--; |
| 101 | } |
| 102 | } |
| 103 | ArrayList<Integer> ids = new ArrayList<>(); |
| 104 | ArrayList<Long> random_ids = new ArrayList<>(); |
| 105 | int channelId = 0; |
| 106 | for (int a = 0; a < res.messages.size(); a++) { |
| 107 | TLRPC.Message message = res.messages.get(a); |
| 108 | ids.add(message.id); |
| 109 | if (message.random_id != 0) { |
| 110 | random_ids.add(message.random_id); |
| 111 | } |
| 112 | channelId = message.to_id.channel_id; |
| 113 | } |
| 114 | MessagesController.getInstance(currentAccount).deleteMessages(ids, random_ids, null, dialog_id, channelId, true, scheduled); |
| 115 | messagesSearchCount[0] = messagesSearchCount[0] - messagesSearchCount[1]; |
| 116 | FileLog.d("found " + messagesSearchCount[1]); |
| 117 | FileLog.d("after " + messagesSearchCount[0]); |
| 118 | if (messagesSearchCount[0] > 0) { |
| 119 | try { |
| 120 | Thread.sleep(1000); |
| 121 | } catch (InterruptedException e) { |
| 122 | e.printStackTrace(); |
| 123 | } |
| 124 | deleteUserChannelHistoryWithSearch(dialog_id, user, true); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | }), ConnectionsManager.RequestFlagFailOnServerErrors); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |