mirror of https://github.com/NekoX-Dev/NekoX.git
200 lines
8.6 KiB
Java
200 lines
8.6 KiB
Java
package tw.nekomimi.nekogram;
|
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
|
import org.telegram.messenger.BaseController;
|
|
import org.telegram.messenger.MessageObject;
|
|
import org.telegram.messenger.UserConfig;
|
|
import org.telegram.tgnet.ConnectionsManager;
|
|
import org.telegram.tgnet.TLRPC;
|
|
import org.telegram.ui.Cells.ChatMessageCell;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
|
|
import tw.nekomimi.nekogram.utils.AlertUtil;
|
|
|
|
public class MessageHelper extends BaseController {
|
|
|
|
private static volatile MessageHelper[] Instance = new MessageHelper[UserConfig.MAX_ACCOUNT_COUNT];
|
|
private int lastReqId;
|
|
|
|
public MessageHelper(int num) {
|
|
super(num);
|
|
}
|
|
|
|
public static void resetMessageContent(MessageObject messageObject) {
|
|
if (messageObject.caption != null) {
|
|
messageObject.caption = null;
|
|
messageObject.generateCaption();
|
|
messageObject.forceUpdate = true;
|
|
}
|
|
messageObject.applyNewText();
|
|
messageObject.resetLayout();
|
|
}
|
|
|
|
public static void resetMessageContent(ChatMessageCell chatMessageCell) {
|
|
chatMessageCell.onAttachedToWindow();
|
|
chatMessageCell.requestLayout();
|
|
chatMessageCell.invalidate();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user) {
|
|
deleteUserChannelHistoryWithSearch(dialog_id, user, 0);
|
|
}
|
|
|
|
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 = getMessagesController().getInputPeer((int) dialog_id);
|
|
if (req.peer == null) {
|
|
return;
|
|
}
|
|
req.limit = 100;
|
|
req.q = "";
|
|
req.offset_id = offset_id;
|
|
if (user != null) {
|
|
req.from_id = getMessagesController().getInputUser(user);
|
|
req.flags |= 1;
|
|
}
|
|
req.filter = new TLRPC.TL_inputMessagesFilterEmpty();
|
|
final int currentReqId = ++lastReqId;
|
|
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
|
|
if (error == null) {
|
|
int lastMessageId = offset_id;
|
|
if (currentReqId == lastReqId) {
|
|
if (response != null) {
|
|
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
|
|
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);
|
|
}
|
|
if (message.to_id.channel_id != 0) {
|
|
channelId = message.to_id.channel_id;
|
|
}
|
|
if (message.id > lastMessageId) {
|
|
lastMessageId = message.id;
|
|
}
|
|
}
|
|
getMessagesController().deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false);
|
|
deleteUserChannelHistoryWithSearch(dialog_id, user, lastMessageId);
|
|
}
|
|
}
|
|
}
|
|
}), ConnectionsManager.RequestFlagFailOnServerErrors);
|
|
}
|
|
|
|
public void deleteChannelHistory(final long dialog_id, TLRPC.Chat chat, final int offset_id) {
|
|
|
|
final TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory();
|
|
req.peer = getMessagesController().getInputPeer((int) dialog_id);
|
|
if (req.peer == null) {
|
|
return;
|
|
}
|
|
req.limit = 100;
|
|
req.offset_id = offset_id;
|
|
final int currentReqId = ++lastReqId;
|
|
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
|
|
if (error == null) {
|
|
int lastMessageId = offset_id;
|
|
if (currentReqId == lastReqId) {
|
|
if (response != null) {
|
|
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
|
|
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);
|
|
}
|
|
if (message.to_id.channel_id != 0) {
|
|
channelId = message.to_id.channel_id;
|
|
}
|
|
if (message.id > lastMessageId) {
|
|
lastMessageId = message.id;
|
|
}
|
|
}
|
|
getMessagesController().deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false);
|
|
*/
|
|
HashSet<Integer> ids = new HashSet<>();
|
|
ArrayList<Integer> msgIds = 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.from_id > 0) {
|
|
ids.add(message.from_id);
|
|
} else {
|
|
msgIds.add(message.id);
|
|
if (message.random_id != 0) {
|
|
random_ids.add(message.random_id);
|
|
}
|
|
}
|
|
if (message.id > lastMessageId) {
|
|
lastMessageId = message.id;
|
|
}
|
|
}
|
|
for (int userId : ids) {
|
|
deleteUserChannelHistory(chat, userId, 0);
|
|
}
|
|
if (!msgIds.isEmpty()) {
|
|
getMessagesController().deleteMessages(msgIds, random_ids, null, dialog_id, channelId, true, false);
|
|
}
|
|
deleteChannelHistory(dialog_id, chat, lastMessageId);
|
|
|
|
}
|
|
}
|
|
} else {
|
|
AlertUtil.showToast(error.code + ": " + error.text);
|
|
}
|
|
}), ConnectionsManager.RequestFlagFailOnServerErrors);
|
|
}
|
|
|
|
public void deleteUserChannelHistory(final TLRPC.Chat chat, int userId, int offset) {
|
|
if (offset == 0) {
|
|
getMessagesStorage().deleteUserChannelHistory(chat.id, userId);
|
|
}
|
|
TLRPC.TL_channels_deleteUserHistory req = new TLRPC.TL_channels_deleteUserHistory();
|
|
req.channel = getMessagesController().getInputChannel(chat.id);
|
|
req.user_id = getMessagesController().getInputUser(userId);
|
|
getConnectionsManager().sendRequest(req, (response, error) -> {
|
|
if (error == null) {
|
|
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
|
|
if (res.offset > 0) {
|
|
deleteUserChannelHistory(chat, userId, res.offset);
|
|
}
|
|
getMessagesController().processNewChannelDifferenceParams(res.pts, res.pts_count, chat.id);
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|