Nagram/TMessagesProj/src/main/java/tw/nekomimi/nekogram/MessageHelper.java

82 lines
3.3 KiB
Java
Raw Normal View History

2019-06-11 07:13:04 +00:00
package tw.nekomimi.nekogram;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.FileLog;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.UserConfig;
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.TLRPC;
import java.util.ArrayList;
public class MessageHelper {
private static volatile MessageHelper[] Instance = new MessageHelper[UserConfig.MAX_ACCOUNT_COUNT];
private int currentAccount;
private int lastReqId;
public MessageHelper(int num) {
currentAccount = num;
}
public static MessageHelper getInstance(int num) {
MessageHelper localInstance = Instance[num];
if (localInstance == null) {
synchronized (MessageHelper.class) {
localInstance = Instance[num];
if (localInstance == null) {
Instance[num] = localInstance = new MessageHelper(num);
}
}
}
return localInstance;
}
2019-12-09 05:43:57 +00:00
public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user) {
deleteUserChannelHistoryWithSearch(dialog_id, user, 0);
2019-06-11 07:13:04 +00:00
}
2019-12-09 05:43:57 +00:00
public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user, final int offset_id) {
final TLRPC.TL_messages_search req = new TLRPC.TL_messages_search();
req.peer = MessagesController.getInstance(currentAccount).getInputPeer((int) dialog_id);
if (req.peer == null) {
return;
2019-06-11 07:13:04 +00:00
}
2019-12-09 05:43:57 +00:00
req.limit = 100;
req.q = "";
req.offset_id = offset_id;
if (user != null) {
req.from_id = MessagesController.getInstance(currentAccount).getInputUser(user);
req.flags |= 1;
2019-06-11 07:13:04 +00:00
}
2019-12-09 05:43:57 +00:00
req.filter = new TLRPC.TL_inputMessagesFilterEmpty();
final int currentReqId = ++lastReqId;
ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
int lastMessageId = offset_id;
if (currentReqId == lastReqId) {
2019-06-11 07:13:04 +00:00
if (response != null) {
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
2019-12-09 05:43:57 +00:00
int size = res.messages.size();
if (size == 0) {
return;
}
ArrayList<Integer> ids = new ArrayList<>();
ArrayList<Long> random_ids = new ArrayList<>();
int channelId = 0;
for (int a = 0; a < res.messages.size(); a++) {
TLRPC.Message message = res.messages.get(a);
ids.add(message.id);
if (message.random_id != 0) {
random_ids.add(message.random_id);
2019-06-11 07:13:04 +00:00
}
2019-12-09 05:43:57 +00:00
channelId = message.to_id.channel_id;
lastMessageId = message.id;
2019-06-11 07:13:04 +00:00
}
2019-12-09 05:43:57 +00:00
MessagesController.getInstance(currentAccount).deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false);
deleteUserChannelHistoryWithSearch(dialog_id, user, lastMessageId);
2019-06-11 07:13:04 +00:00
}
2019-12-09 05:43:57 +00:00
}
}), ConnectionsManager.RequestFlagFailOnServerErrors);
2019-06-11 07:13:04 +00:00
}
}