| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import org.telegram.messenger.AndroidUtilities; |
| 4 | import org.telegram.messenger.BaseController; |
| 5 | import org.telegram.messenger.MessageObject; |
| 6 | import org.telegram.messenger.UserConfig; |
| 7 | import org.telegram.tgnet.ConnectionsManager; |
| 8 | import org.telegram.tgnet.TLRPC; |
| 9 | import org.telegram.ui.Cells.ChatMessageCell; |
| 10 | |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.HashSet; |
| 13 | |
| 14 | import tw.nekomimi.nekogram.utils.AlertUtil; |
| 15 | |
| 16 | public class MessageHelper extends BaseController { |
| 17 | |
| 18 | private static volatile MessageHelper[] Instance = new MessageHelper[UserConfig.MAX_ACCOUNT_COUNT]; |
| 19 | private int lastReqId; |
| 20 | |
| 21 | public MessageHelper(int num) { |
| 22 | super(num); |
| 23 | } |
| 24 | |
| 25 | public static void resetMessageContent(MessageObject messageObject) { |
| 26 | if (messageObject.caption != null) { |
| 27 | messageObject.caption = null; |
| 28 | messageObject.generateCaption(); |
| 29 | messageObject.forceUpdate = true; |
| 30 | } |
| 31 | messageObject.applyNewText(); |
| 32 | messageObject.resetLayout(); |
| 33 | } |
| 34 | |
| 35 | public static void resetMessageContent(ChatMessageCell chatMessageCell) { |
| 36 | chatMessageCell.onAttachedToWindow(); |
| 37 | chatMessageCell.requestLayout(); |
| 38 | chatMessageCell.invalidate(); |
| 39 | } |
| 40 | |
| 41 | public static MessageHelper getInstance(int num) { |
| 42 | MessageHelper localInstance = Instance[num]; |
| 43 | if (localInstance == null) { |
| 44 | synchronized (MessageHelper.class) { |
| 45 | localInstance = Instance[num]; |
| 46 | if (localInstance == null) { |
| 47 | Instance[num] = localInstance = new MessageHelper(num); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | return localInstance; |
| 52 | } |
| 53 | |
| 54 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user) { |
| 55 | deleteUserChannelHistoryWithSearch(dialog_id, user, 0); |
| 56 | } |
| 57 | |
| 58 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user, final int offset_id) { |
| 59 | final TLRPC.TL_messages_search req = new TLRPC.TL_messages_search(); |
| 60 | req.peer = getMessagesController().getInputPeer((int) dialog_id); |
| 61 | if (req.peer == null) { |
| 62 | return; |
| 63 | } |
| 64 | req.limit = 100; |
| 65 | req.q = ""; |
| 66 | req.offset_id = offset_id; |
| 67 | if (user != null) { |
| 68 | req.from_id = getMessagesController().getInputUser(user); |
| 69 | req.flags |= 1; |
| 70 | } |
| 71 | req.filter = new TLRPC.TL_inputMessagesFilterEmpty(); |
| 72 | final int currentReqId = ++lastReqId; |
| 73 | getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> { |
| 74 | if (error == null) { |
| 75 | int lastMessageId = offset_id; |
| 76 | if (currentReqId == lastReqId) { |
| 77 | if (response != null) { |
| 78 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 79 | int size = res.messages.size(); |
| 80 | if (size == 0) { |
| 81 | return; |
| 82 | } |
| 83 | ArrayList<Integer> ids = new ArrayList<>(); |
| 84 | ArrayList<Long> random_ids = new ArrayList<>(); |
| 85 | int channelId = 0; |
| 86 | for (int a = 0; a < res.messages.size(); a++) { |
| 87 | TLRPC.Message message = res.messages.get(a); |
| 88 | ids.add(message.id); |
| 89 | if (message.random_id != 0) { |
| 90 | random_ids.add(message.random_id); |
| 91 | } |
| 92 | if (message.to_id.channel_id != 0) { |
| 93 | channelId = message.to_id.channel_id; |
| 94 | } |
| 95 | if (message.id > lastMessageId) { |
| 96 | lastMessageId = message.id; |
| 97 | } |
| 98 | } |
| 99 | getMessagesController().deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false); |
| 100 | deleteUserChannelHistoryWithSearch(dialog_id, user, lastMessageId); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | }), ConnectionsManager.RequestFlagFailOnServerErrors); |
| 105 | } |
| 106 | |
| 107 | public void deleteChannelHistory(final long dialog_id, TLRPC.Chat chat, final int offset_id) { |
| 108 | |
| 109 | final TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory(); |
| 110 | req.peer = getMessagesController().getInputPeer((int) dialog_id); |
| 111 | if (req.peer == null) { |
| 112 | return; |
| 113 | } |
| 114 | req.limit = 100; |
| 115 | req.offset_id = offset_id; |
| 116 | final int currentReqId = ++lastReqId; |
| 117 | getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> { |
| 118 | if (error == null) { |
| 119 | int lastMessageId = offset_id; |
| 120 | if (currentReqId == lastReqId) { |
| 121 | if (response != null) { |
| 122 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 123 | int size = res.messages.size(); |
| 124 | if (size == 0) { |
| 125 | return; |
| 126 | } |
| 127 | /* |
| 128 | ArrayList<Integer> ids = new ArrayList<>(); |
| 129 | ArrayList<Long> random_ids = new ArrayList<>(); |
| 130 | int channelId = 0; |
| 131 | for (int a = 0; a < res.messages.size(); a++) { |
| 132 | TLRPC.Message message = res.messages.get(a); |
| 133 | ids.add(message.id); |
| 134 | if (message.random_id != 0) { |
| 135 | random_ids.add(message.random_id); |
| 136 | } |
| 137 | if (message.to_id.channel_id != 0) { |
| 138 | channelId = message.to_id.channel_id; |
| 139 | } |
| 140 | if (message.id > lastMessageId) { |
| 141 | lastMessageId = message.id; |
| 142 | } |
| 143 | } |
| 144 | getMessagesController().deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false); |
| 145 | */ |
| 146 | HashSet<Integer> ids = new HashSet<>(); |
| 147 | ArrayList<Integer> msgIds = new ArrayList<>(); |
| 148 | ArrayList<Long> random_ids = new ArrayList<>(); |
| 149 | int channelId = 0; |
| 150 | for (int a = 0; a < res.messages.size(); a++) { |
| 151 | TLRPC.Message message = res.messages.get(a); |
| 152 | ids.add(message.id); |
| 153 | if (message.from_id > 0) { |
| 154 | ids.add(message.from_id); |
| 155 | } else { |
| 156 | msgIds.add(message.id); |
| 157 | if (message.random_id != 0) { |
| 158 | random_ids.add(message.random_id); |
| 159 | } |
| 160 | } |
| 161 | if (message.id > lastMessageId) { |
| 162 | lastMessageId = message.id; |
| 163 | } |
| 164 | } |
| 165 | for (int userId : ids) { |
| 166 | deleteUserChannelHistory(chat, userId, 0); |
| 167 | } |
| 168 | if (!msgIds.isEmpty()) { |
| 169 | getMessagesController().deleteMessages(msgIds, random_ids, null, dialog_id, channelId, true, false); |
| 170 | } |
| 171 | deleteChannelHistory(dialog_id, chat, lastMessageId); |
| 172 | |
| 173 | } |
| 174 | } |
| 175 | } else { |
| 176 | AlertUtil.showToast(error.code + ": " + error.text); |
| 177 | } |
| 178 | }), ConnectionsManager.RequestFlagFailOnServerErrors); |
| 179 | } |
| 180 | |
| 181 | public void deleteUserChannelHistory(final TLRPC.Chat chat, int userId, int offset) { |
| 182 | if (offset == 0) { |
| 183 | getMessagesStorage().deleteUserChannelHistory(chat.id, userId); |
| 184 | } |
| 185 | TLRPC.TL_channels_deleteUserHistory req = new TLRPC.TL_channels_deleteUserHistory(); |
| 186 | req.channel = getMessagesController().getInputChannel(chat.id); |
| 187 | req.user_id = getMessagesController().getInputUser(userId); |
| 188 | getConnectionsManager().sendRequest(req, (response, error) -> { |
| 189 | if (error == null) { |
| 190 | TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response; |
| 191 | if (res.offset > 0) { |
| 192 | deleteUserChannelHistory(chat, userId, res.offset); |
| 193 | } |
| 194 | getMessagesController().processNewChannelDifferenceParams(res.pts, res.pts_count, chat.id); |
| 195 | } |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |