| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | |
| 5 | import org.telegram.messenger.AndroidUtilities; |
| 6 | import org.telegram.messenger.BaseController; |
| 7 | import org.telegram.messenger.LocaleController; |
| 8 | import org.telegram.messenger.MessageObject; |
| 9 | import org.telegram.messenger.R; |
| 10 | import org.telegram.messenger.UserConfig; |
| 11 | import org.telegram.tgnet.ConnectionsManager; |
| 12 | import org.telegram.tgnet.TLRPC; |
| 13 | import org.telegram.ui.ActionBar.AlertDialog; |
| 14 | import org.telegram.ui.Cells.ChatMessageCell; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | |
| 18 | import tw.nekomimi.nekogram.translator.TranslateBottomSheet; |
| 19 | import tw.nekomimi.nekogram.translator.Translator; |
| 20 | |
| 21 | public class MessageHelper extends BaseController { |
| 22 | |
| 23 | private static volatile MessageHelper[] Instance = new MessageHelper[UserConfig.MAX_ACCOUNT_COUNT]; |
| 24 | private static AlertDialog progressDialog; |
| 25 | private int lastReqId; |
| 26 | |
| 27 | public MessageHelper(int num) { |
| 28 | super(num); |
| 29 | } |
| 30 | |
| 31 | public static void setMessageContent(MessageObject messageObject, ChatMessageCell chatMessageCell, String message) { |
| 32 | messageObject.messageOwner.message = message; |
| 33 | if (messageObject.caption != null) { |
| 34 | messageObject.caption = null; |
| 35 | messageObject.generateCaption(); |
| 36 | messageObject.forceUpdate = true; |
| 37 | } |
| 38 | messageObject.applyNewText(); |
| 39 | messageObject.resetLayout(); |
| 40 | chatMessageCell.requestLayout(); |
| 41 | chatMessageCell.invalidate(); |
| 42 | } |
| 43 | |
| 44 | public static void showTranslateDialog(Context context, String query) { |
| 45 | if (NekoConfig.translationProvider < 0) { |
| 46 | TranslateBottomSheet.show(context, query); |
| 47 | } else { |
| 48 | if (progressDialog != null) { |
| 49 | progressDialog.dismiss(); |
| 50 | } |
| 51 | Translator.translate(query, new Translator.TranslateCallBack() { |
| 52 | @Override |
| 53 | public void onSuccess(String translation) { |
| 54 | if (progressDialog != null) { |
| 55 | progressDialog.dismiss(); |
| 56 | } |
| 57 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 58 | builder.setMessage(translation); |
| 59 | builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null); |
| 60 | builder.setNeutralButton(LocaleController.getString("Copy", R.string.Copy), (dialog, which) -> AndroidUtilities.addToClipboard(translation)); |
| 61 | builder.show(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onError() { |
| 66 | if (progressDialog != null) { |
| 67 | progressDialog.dismiss(); |
| 68 | } |
| 69 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 70 | builder.setMessage(LocaleController.getString("TranslateFailed", R.string.TranslateFailed)); |
| 71 | builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null); |
| 72 | builder.show(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onUnsupported() { |
| 77 | if (progressDialog != null) { |
| 78 | progressDialog.dismiss(); |
| 79 | } |
| 80 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 81 | builder.setMessage(LocaleController.getString("TranslateApiUnsupported", R.string.TranslateApiUnsupported)); |
| 82 | builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null); |
| 83 | builder.show(); |
| 84 | } |
| 85 | }); |
| 86 | progressDialog = new AlertDialog(context, 3); |
| 87 | progressDialog.showDelayed(400); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public static MessageHelper getInstance(int num) { |
| 92 | MessageHelper localInstance = Instance[num]; |
| 93 | if (localInstance == null) { |
| 94 | synchronized (MessageHelper.class) { |
| 95 | localInstance = Instance[num]; |
| 96 | if (localInstance == null) { |
| 97 | Instance[num] = localInstance = new MessageHelper(num); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return localInstance; |
| 102 | } |
| 103 | |
| 104 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user) { |
| 105 | deleteUserChannelHistoryWithSearch(dialog_id, user, 0); |
| 106 | } |
| 107 | |
| 108 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user, final int offset_id) { |
| 109 | final TLRPC.TL_messages_search req = new TLRPC.TL_messages_search(); |
| 110 | req.peer = getMessagesController().getInputPeer((int) dialog_id); |
| 111 | if (req.peer == null) { |
| 112 | return; |
| 113 | } |
| 114 | req.limit = 100; |
| 115 | req.q = ""; |
| 116 | req.offset_id = offset_id; |
| 117 | if (user != null) { |
| 118 | req.from_id = getMessagesController().getInputUser(user); |
| 119 | req.flags |= 1; |
| 120 | } |
| 121 | req.filter = new TLRPC.TL_inputMessagesFilterEmpty(); |
| 122 | final int currentReqId = ++lastReqId; |
| 123 | getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> { |
| 124 | if (error == null) { |
| 125 | int lastMessageId = offset_id; |
| 126 | if (currentReqId == lastReqId) { |
| 127 | if (response != null) { |
| 128 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 129 | int size = res.messages.size(); |
| 130 | if (size == 0) { |
| 131 | return; |
| 132 | } |
| 133 | ArrayList<Integer> ids = new ArrayList<>(); |
| 134 | ArrayList<Long> random_ids = new ArrayList<>(); |
| 135 | int channelId = 0; |
| 136 | for (int a = 0; a < res.messages.size(); a++) { |
| 137 | TLRPC.Message message = res.messages.get(a); |
| 138 | ids.add(message.id); |
| 139 | if (message.random_id != 0) { |
| 140 | random_ids.add(message.random_id); |
| 141 | } |
| 142 | if (message.to_id.channel_id != 0) { |
| 143 | channelId = message.to_id.channel_id; |
| 144 | } |
| 145 | if (message.id > lastMessageId) { |
| 146 | lastMessageId = message.id; |
| 147 | } |
| 148 | } |
| 149 | getMessagesController().deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false); |
| 150 | deleteUserChannelHistoryWithSearch(dialog_id, user, lastMessageId); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | }), ConnectionsManager.RequestFlagFailOnServerErrors); |
| 155 | } |
| 156 | } |
| 157 | |