Disable bot keyboard.

This commit is contained in:
B. Petersen
2016-10-20 18:14:28 +02:00
parent 3956623de2
commit 36718c9323
7 changed files with 35 additions and 216 deletions
@@ -21,7 +21,7 @@ import android.util.SparseIntArray;
import android.widget.Toast;
import org.telegram.SQLite.SQLiteCursor;
import org.telegram.messenger.query.BotQuery;
//import org.telegram.messenger.query.BotQuery; -- EDIT BY MR
import org.telegram.messenger.query.DraftQuery;
import org.telegram.messenger.query.MessagesQuery;
import org.telegram.messenger.query.SearchQuery;
@@ -1007,10 +1007,12 @@ public class MessagesController implements NotificationCenter.NotificationCenter
@Override
public void run() {
applyDialogNotificationsSettings(-chat_id, res.full_chat.notify_settings);
/* EDIT BY MR
for (int a = 0; a < res.full_chat.bot_info.size(); a++) {
TLRPC.BotInfo botInfo = res.full_chat.bot_info.get(a);
BotQuery.putBotInfo(botInfo);
}
*/
exportedChats.put(chat_id, res.full_chat.exported_invite);
loadingFullChats.remove((Integer) chat_id);
loadedFullChats.add(chat_id);
@@ -1056,9 +1058,11 @@ public class MessagesController implements NotificationCenter.NotificationCenter
public void run() {
TLRPC.TL_userFull userFull = (TLRPC.TL_userFull) response;
applyDialogNotificationsSettings(user.id, userFull.notify_settings);
/* EDIT BY MR
if (userFull.bot_info instanceof TLRPC.TL_botInfo) {
BotQuery.putBotInfo(userFull.bot_info);
}
*/
if (userFull.about != null && userFull.about.length() > 0) {
fullUsersAbout.put(user.id, userFull.about);
} else {
@@ -18,7 +18,7 @@ import org.telegram.PhoneFormat.PhoneFormat;
import org.telegram.SQLite.SQLiteCursor;
import org.telegram.SQLite.SQLiteDatabase;
import org.telegram.SQLite.SQLitePreparedStatement;
import org.telegram.messenger.query.BotQuery;
//import org.telegram.messenger.query.BotQuery; -- EDIT BY MR
import org.telegram.messenger.query.MessagesQuery;
import org.telegram.messenger.query.SharedMediaQuery;
import org.telegram.tgnet.ConnectionsManager;
@@ -1308,7 +1308,7 @@ public class MessagesStorage {
database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
BotQuery.clearBotKeyboard(did, null);
//BotQuery.clearBotKeyboard(did, null); -- EDIT BY MR
SQLitePreparedStatement state5 = database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
SQLitePreparedStatement state6 = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
@@ -1329,7 +1329,7 @@ public class MessagesStorage {
database.executeFast("DELETE FROM media_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM messages_holes WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
BotQuery.clearBotKeyboard(did, null);
//BotQuery.clearBotKeyboard(did, null); -- EDIT BY MR
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
@@ -3771,7 +3771,7 @@ public class MessagesStorage {
database.executeFast("DELETE FROM media_v2 WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM messages_holes WHERE uid = " + did).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
BotQuery.clearBotKeyboard(did, null);
//BotQuery.clearBotKeyboard(did, null); -- EDIT BY MR
TLRPC.TL_messages_dialogs dialogs = new TLRPC.TL_messages_dialogs();
dialogs.chats.addAll(difference.chats);
@@ -3844,9 +3844,11 @@ public class MessagesStorage {
});
}
/* EDIT BY MR
private boolean isValidKeyboardToSave(TLRPC.Message message) {
return message.reply_markup != null && !(message.reply_markup instanceof TLRPC.TL_replyInlineMarkup) && (!message.reply_markup.selective || message.mentioned);
}
*/
private void putMessagesInternal(final ArrayList<TLRPC.Message> messages, final boolean withTransaction, final boolean doNotUpdateDialogDate, final int downloadMask, boolean ifNoLastMessage) {
try {
@@ -3942,17 +3944,21 @@ public class MessagesStorage {
messagesMediaIdsMap.put(messageId, message.dialog_id);
mediaTypes.put(messageId, SharedMediaQuery.getMediaType(message));
}
/* EDIT BY MR
if (isValidKeyboardToSave(message)) {
TLRPC.Message oldMessage = botKeyboards.get(message.dialog_id);
if (oldMessage == null || oldMessage.id < message.id) {
botKeyboards.put(message.dialog_id, message);
}
}
*/
}
/* EDIT BY MR
for (HashMap.Entry<Long, TLRPC.Message> entry : botKeyboards.entrySet()) {
BotQuery.putBotKeyboard(entry.getKey(), entry.getValue());
}
*/
if (messageMediaIds != null) {
SQLiteCursor cursor = database.queryFinalized("SELECT mid FROM media_v2 WHERE mid IN(" + messageMediaIds.toString() + ")");
@@ -4746,7 +4752,7 @@ public class MessagesStorage {
database.executeFast(String.format(Locale.US, "DELETE FROM messages_seq WHERE mid IN(%s)", ids)).stepThis().dispose();
database.executeFast(String.format(Locale.US, "DELETE FROM media_v2 WHERE mid IN(%s)", ids)).stepThis().dispose();
database.executeFast("DELETE FROM media_counts_v2 WHERE 1").stepThis().dispose();
BotQuery.clearBotKeyboard(0, messages);
//BotQuery.clearBotKeyboard(0, messages); -- EDIT BY MR
} catch (Exception e) {
FileLog.e("tmessages", e);
}
@@ -5245,20 +5251,25 @@ public class MessagesStorage {
state5.step();
}
/* EDIT BY MR
if (load_type == 0 && isValidKeyboardToSave(message)) {
if (botKeyboard == null || botKeyboard.id < message.id) {
botKeyboard = message;
}
}
*/
}
state.dispose();
state2.dispose();
if (state5 != null) {
state5.dispose();
}
/* EDIT BY MR
if (botKeyboard != null) {
BotQuery.putBotKeyboard(dialog_id, botKeyboard);
}
*/
putUsersInternal(messages.users);
putChatsInternal(messages.chats);
@@ -5548,9 +5559,11 @@ public class MessagesStorage {
if (message != null) {
messageDate = Math.max(message.date, messageDate);
/* EDIT BY MR
if (isValidKeyboardToSave(message)) {
BotQuery.putBotKeyboard(dialog.id, message);
}
*/
fixUnsupportedMedia(message);
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
@@ -1,207 +0,0 @@
/*
* This is the source code of Telegram for Android v. 3.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2016.
*/
package org.telegram.messenger.query;
import org.telegram.SQLite.SQLiteCursor;
import org.telegram.SQLite.SQLitePreparedStatement;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.MessagesStorage;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.FileLog;
import org.telegram.tgnet.NativeByteBuffer;
import org.telegram.tgnet.TLRPC;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
public class BotQuery {
private static HashMap<Integer, TLRPC.BotInfo> botInfos = new HashMap<>();
private static HashMap<Long, TLRPC.Message> botKeyboards = new HashMap<>();
private static HashMap<Integer, Long> botKeyboardsByMids = new HashMap<>();
public static void cleanup() {
botInfos.clear();
botKeyboards.clear();
botKeyboardsByMids.clear();
}
public static void clearBotKeyboard(final long did, final ArrayList<Integer> messages) {
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
if (messages != null) {
for (int a = 0; a < messages.size(); a++) {
Long did = botKeyboardsByMids.get(messages.get(a));
if (did != null) {
botKeyboards.remove(did);
botKeyboardsByMids.remove(messages.get(a));
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botKeyboardDidLoaded, null, did);
}
}
} else {
botKeyboards.remove(did);
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botKeyboardDidLoaded, null, did);
}
}
});
}
public static void loadBotKeyboard(final long did) {
TLRPC.Message keyboard = botKeyboards.get(did);
if (keyboard != null) {
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botKeyboardDidLoaded, keyboard, did);
return;
}
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
@Override
public void run() {
try {
TLRPC.Message botKeyboard = null;
SQLiteCursor cursor = MessagesStorage.getInstance().getDatabase().queryFinalized(String.format(Locale.US, "SELECT info FROM bot_keyboard WHERE uid = %d", did));
if (cursor.next()) {
NativeByteBuffer data;
if (!cursor.isNull(0)) {
data = cursor.byteBufferValue(0);
if (data != null) {
botKeyboard = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
}
}
cursor.dispose();
if (botKeyboard != null) {
final TLRPC.Message botKeyboardFinal = botKeyboard;
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botKeyboardDidLoaded, botKeyboardFinal, did);
}
});
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
});
}
public static void loadBotInfo(final int uid, boolean cache, final int classGuid) {
if (cache) {
TLRPC.BotInfo botInfo = botInfos.get(uid);
if (botInfo != null) {
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botInfoDidLoaded, botInfo, classGuid);
return;
}
}
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
@Override
public void run() {
try {
TLRPC.BotInfo botInfo = null;
SQLiteCursor cursor = MessagesStorage.getInstance().getDatabase().queryFinalized(String.format(Locale.US, "SELECT info FROM bot_info WHERE uid = %d", uid));
if (cursor.next()) {
NativeByteBuffer data;
if (!cursor.isNull(0)) {
data = cursor.byteBufferValue(0);
if (data != null) {
botInfo = TLRPC.BotInfo.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
}
}
cursor.dispose();
if (botInfo != null) {
final TLRPC.BotInfo botInfoFinal = botInfo;
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botInfoDidLoaded, botInfoFinal, classGuid);
}
});
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
});
}
public static void putBotKeyboard(final long did, final TLRPC.Message message) {
if (message == null) {
return;
}
try {
int mid = 0;
SQLiteCursor cursor = MessagesStorage.getInstance().getDatabase().queryFinalized(String.format(Locale.US, "SELECT mid FROM bot_keyboard WHERE uid = %d", did));
if (cursor.next()) {
mid = cursor.intValue(0);
}
cursor.dispose();
if (mid >= message.id) {
return;
}
SQLitePreparedStatement state = MessagesStorage.getInstance().getDatabase().executeFast("REPLACE INTO bot_keyboard VALUES(?, ?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
state.bindLong(1, did);
state.bindInteger(2, message.id);
state.bindByteBuffer(3, data);
state.step();
data.reuse();
state.dispose();
AndroidUtilities.runOnUIThread(new Runnable() {
@Override
public void run() {
TLRPC.Message old = botKeyboards.put(did, message);
if (old != null) {
botKeyboardsByMids.remove(old.id);
}
botKeyboardsByMids.put(message.id, did);
NotificationCenter.getInstance().postNotificationName(NotificationCenter.botKeyboardDidLoaded, message, did);
}
});
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
public static void putBotInfo(final TLRPC.BotInfo botInfo) {
if (botInfo == null) {
return;
}
botInfos.put(botInfo.user_id, botInfo);
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
@Override
public void run() {
try {
SQLitePreparedStatement state = MessagesStorage.getInstance().getDatabase().executeFast("REPLACE INTO bot_info(uid, info) VALUES(?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(botInfo.getObjectSize());
botInfo.serializeToStream(data);
state.bindInteger(1, botInfo.user_id);
state.bindByteBuffer(2, data);
state.step();
data.reuse();
state.dispose();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
});
}
}
@@ -37,7 +37,7 @@ import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesStorage;
import org.telegram.messenger.R;
import org.telegram.messenger.Utilities;
import org.telegram.messenger.query.BotQuery;
//import org.telegram.messenger.query.BotQuery; -- EDIT BY MR
import org.telegram.tgnet.NativeByteBuffer;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.ActionBar;
@@ -2721,6 +2721,7 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate
if (messageIdChanged) {
botButtonsByData.clear();
}
if (messageObject.messageOwner.reply_markup instanceof TLRPC.TL_replyInlineMarkup) {
int rows = messageObject.messageOwner.reply_markup.rows.size();
substractBackgroundHeight = keyboardHeight = dp(44 + 4) * rows + dp(1);
@@ -72,7 +72,7 @@ import org.telegram.messenger.UserObject;
import org.telegram.messenger.Utilities;
import org.telegram.messenger.VideoEditedInfo;
import org.telegram.messenger.browser.Browser;
import org.telegram.messenger.query.BotQuery;
//import org.telegram.messenger.query.BotQuery; -- EDIT BY MR
import org.telegram.messenger.query.DraftQuery;
import org.telegram.messenger.query.MessagesSearchQuery;
import org.telegram.messenger.query.MessagesQuery;
@@ -562,9 +562,11 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not
super.onFragmentCreate();
/* EDIT BY MR
if (currentEncryptedChat == null && !isBroadcast) {
BotQuery.loadBotKeyboard(dialog_id);
}
*/
/* EDIT BY MR
loading = true;
@@ -5718,11 +5720,13 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not
for (int a = 0; a < info.participants.participants.size(); a++) {
TLRPC.ChatParticipant participant = info.participants.participants.get(a);
TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
/* EDIT BY MR
if (user != null && user.bot) {
URLSpanBotCommand.enabled = true;
botsCount++;
BotQuery.loadBotInfo(user.id, true, classGuid);
}
*/
}
if (chatListView != null) {
chatListView.invalidateViews();
@@ -6110,6 +6114,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not
updateBotButtons();
}
} else if (id == NotificationCenter.botKeyboardDidLoaded) {
/* EDIT BY MR
if (dialog_id == (Long) args[1]) {
TLRPC.Message message = (TLRPC.Message) args[0];
if (message != null && !userBlocked) {
@@ -6141,6 +6146,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not
}
}
}
*/
} else if (id == NotificationCenter.chatSearchResultsAvailable) {
if (classGuid == (Integer) args[0]) {
int messageId = (Integer) args[1];
@@ -49,7 +49,7 @@ import org.telegram.messenger.MessagesStorage;
import org.telegram.messenger.SecretChatHelper;
import org.telegram.messenger.SendMessagesHelper;
import org.telegram.messenger.UserObject;
import org.telegram.messenger.query.BotQuery;
//import org.telegram.messenger.query.BotQuery; -- EDIT BY MR
import org.telegram.messenger.query.SharedMediaQuery;
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.support.widget.LinearLayoutManager;
@@ -241,9 +241,11 @@ public class ProfileActivity extends BaseFragment implements NotificationCenter.
NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
}
userBlocked = MessagesController.getInstance().blockedUsers.contains(user_id);
/* EDIT BY MR
if (user.bot) {
BotQuery.loadBotInfo(user.id, true, classGuid);
}
*/
MessagesController.getInstance().loadFullUser(MessagesController.getInstance().getUser(user_id), classGuid, true);
participantsMap = null;
} else if (chat_id != 0) {