use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method putBotKeyboard.
public void putBotKeyboard(long dialogId, TLRPC.Message message) {
if (message == null) {
return;
}
try {
int mid = 0;
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT mid FROM bot_keyboard WHERE uid = %d", dialogId));
if (cursor.next()) {
mid = cursor.intValue(0);
}
cursor.dispose();
if (mid >= message.id) {
return;
}
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO bot_keyboard VALUES(?, ?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
state.bindLong(1, dialogId);
state.bindInteger(2, message.id);
state.bindByteBuffer(3, data);
state.step();
data.reuse();
state.dispose();
AndroidUtilities.runOnUIThread(() -> {
TLRPC.Message old = botKeyboards.get(dialogId);
botKeyboards.put(dialogId, message);
long channelId = MessageObject.getChannelId(message);
if (channelId == 0) {
if (old != null) {
botKeyboardsByMids.delete(old.id);
}
botKeyboardsByMids.put(message.id, dialogId);
}
getNotificationCenter().postNotificationName(NotificationCenter.botKeyboardDidLoad, message, dialogId);
});
} catch (Exception e) {
FileLog.e(e);
}
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadBotKeyboard.
public void loadBotKeyboard(long dialogId) {
TLRPC.Message keyboard = botKeyboards.get(dialogId);
if (keyboard != null) {
getNotificationCenter().postNotificationName(NotificationCenter.botKeyboardDidLoad, keyboard, dialogId);
return;
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
TLRPC.Message botKeyboard = null;
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT info FROM bot_keyboard WHERE uid = %d", dialogId));
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) {
TLRPC.Message botKeyboardFinal = botKeyboard;
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.botKeyboardDidLoad, botKeyboardFinal, dialogId));
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadMusic.
public void loadMusic(long dialogId, long maxId, long minId) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
ArrayList<MessageObject> arrayListBegin = new ArrayList<>();
ArrayList<MessageObject> arrayListEnd = new ArrayList<>();
try {
for (int a = 0; a < 2; a++) {
ArrayList<MessageObject> arrayList = a == 0 ? arrayListBegin : arrayListEnd;
SQLiteCursor cursor;
if (a == 0) {
if (!DialogObject.isEncryptedDialog(dialogId)) {
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid < %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, maxId, MEDIA_MUSIC));
} else {
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, maxId, MEDIA_MUSIC));
}
} else {
if (!DialogObject.isEncryptedDialog(dialogId)) {
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, minId, MEDIA_MUSIC));
} else {
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid < %d AND type = %d ORDER BY date DESC, mid DESC LIMIT 1000", dialogId, minId, MEDIA_MUSIC));
}
}
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
if (MessageObject.isMusicMessage(message)) {
message.id = cursor.intValue(1);
message.dialog_id = dialogId;
arrayList.add(0, new MessageObject(currentAccount, message, false, true));
}
}
}
cursor.dispose();
}
} catch (Exception e) {
FileLog.e(e);
}
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.musicDidLoad, dialogId, arrayListBegin, arrayListEnd));
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method loadUserInfo.
public void loadUserInfo(TLRPC.User user, boolean force, int classGuid, int fromMessageId) {
if (user == null) {
return;
}
storageQueue.postRunnable(() -> {
HashMap<Integer, MessageObject> pinnedMessagesMap = new HashMap<>();
ArrayList<Integer> pinnedMessages = new ArrayList<>();
int totalPinnedCount = 0;
boolean pinnedEndReached = false;
TLRPC.UserFull info = null;
try {
SQLiteCursor cursor = database.queryFinalized("SELECT info, pinned FROM user_settings WHERE uid = " + user.id);
if (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
info = TLRPC.UserFull.TLdeserialize(data, data.readInt32(false), false);
info.pinned_msg_id = cursor.intValue(1);
data.reuse();
}
}
cursor.dispose();
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", user.id));
while (cursor.next()) {
int id = cursor.intValue(0);
pinnedMessages.add(id);
pinnedMessagesMap.put(id, null);
}
cursor.dispose();
cursor = database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + user.id);
if (cursor.next()) {
totalPinnedCount = cursor.intValue(0);
pinnedEndReached = cursor.intValue(1) != 0;
}
cursor.dispose();
if (info != null && info.pinned_msg_id != 0) {
if (pinnedMessages.isEmpty() || info.pinned_msg_id > pinnedMessages.get(0)) {
pinnedMessages.clear();
pinnedMessages.add(info.pinned_msg_id);
pinnedMessagesMap.put(info.pinned_msg_id, null);
}
}
if (!pinnedMessages.isEmpty()) {
ArrayList<MessageObject> messageObjects = getMediaDataController().loadPinnedMessages(user.id, 0, pinnedMessages, false);
if (messageObjects != null) {
for (int a = 0, N = messageObjects.size(); a < N; a++) {
MessageObject messageObject = messageObjects.get(a);
pinnedMessagesMap.put(messageObject.getId(), messageObject);
}
}
}
} catch (Exception e) {
FileLog.e(e);
} finally {
getMessagesController().processUserInfo(user, info, true, force, classGuid, pinnedMessages, pinnedMessagesMap, totalPinnedCount, pinnedEndReached);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method getUnsentMessages.
public void getUnsentMessages(int count) {
storageQueue.postRunnable(() -> {
try {
SparseArray<TLRPC.Message> messageHashMap = new SparseArray<>();
ArrayList<TLRPC.Message> messages = new ArrayList<>();
ArrayList<TLRPC.Message> scheduledMessages = new ArrayList<>();
ArrayList<TLRPC.User> users = new ArrayList<>();
ArrayList<TLRPC.Chat> chats = new ArrayList<>();
ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
ArrayList<Integer> encryptedChatIds = new ArrayList<>();
SQLiteCursor cursor = database.queryFinalized("SELECT m.read_state, m.data, m.send_state, m.mid, m.date, r.random_id, m.uid, s.seq_in, s.seq_out, m.ttl FROM messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid LEFT JOIN messages_seq as s ON m.mid = s.mid WHERE (m.mid < 0 AND m.send_state = 1) OR (m.mid > 0 AND m.send_state = 3) ORDER BY m.mid DESC LIMIT " + count);
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(1);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.send_state = cursor.intValue(2);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
if (messageHashMap.indexOfKey(message.id) < 0) {
MessageObject.setUnreadFlags(message, cursor.intValue(0));
message.id = cursor.intValue(3);
message.date = cursor.intValue(4);
if (!cursor.isNull(5)) {
message.random_id = cursor.longValue(5);
}
message.dialog_id = cursor.longValue(6);
message.seq_in = cursor.intValue(7);
message.seq_out = cursor.intValue(8);
message.ttl = cursor.intValue(9);
messages.add(message);
messageHashMap.put(message.id, message);
if (DialogObject.isEncryptedDialog(message.dialog_id)) {
int encryptedChatId = DialogObject.getEncryptedChatId(message.dialog_id);
if (!encryptedChatIds.contains(encryptedChatId)) {
encryptedChatIds.add(encryptedChatId);
}
} else if (DialogObject.isUserDialog(message.dialog_id)) {
if (!usersToLoad.contains(message.dialog_id)) {
usersToLoad.add(message.dialog_id);
}
} else {
if (!chatsToLoad.contains(-message.dialog_id)) {
chatsToLoad.add(-message.dialog_id);
}
}
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
if (message.send_state != 3 && (message.peer_id.channel_id == 0 && !MessageObject.isUnread(message) && !DialogObject.isEncryptedDialog(message.dialog_id) || message.id > 0)) {
message.send_state = 0;
}
}
}
}
cursor.dispose();
cursor = database.queryFinalized("SELECT m.data, m.send_state, m.mid, m.date, r.random_id, m.uid, m.ttl FROM scheduled_messages_v2 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid AND r.uid = m.uid WHERE (m.mid < 0 AND m.send_state = 1) OR (m.mid > 0 AND m.send_state = 3) ORDER BY date ASC");
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.send_state = cursor.intValue(1);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
if (messageHashMap.indexOfKey(message.id) < 0) {
message.id = cursor.intValue(2);
message.date = cursor.intValue(3);
if (!cursor.isNull(4)) {
message.random_id = cursor.longValue(4);
}
message.dialog_id = cursor.longValue(5);
message.ttl = cursor.intValue(6);
scheduledMessages.add(message);
messageHashMap.put(message.id, message);
if (DialogObject.isEncryptedDialog(message.dialog_id)) {
int encryptedChatId = DialogObject.getEncryptedChatId(message.dialog_id);
if (!encryptedChatIds.contains(encryptedChatId)) {
encryptedChatIds.add(encryptedChatId);
}
} else if (DialogObject.isUserDialog(message.dialog_id)) {
if (!usersToLoad.contains(message.dialog_id)) {
usersToLoad.add(message.dialog_id);
}
} else {
if (!chatsToLoad.contains(-message.dialog_id)) {
chatsToLoad.add(-message.dialog_id);
}
}
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
if (message.send_state != 3 && (message.peer_id.channel_id == 0 && !MessageObject.isUnread(message) && !DialogObject.isEncryptedDialog(message.dialog_id) || message.id > 0)) {
message.send_state = 0;
}
}
}
}
cursor.dispose();
if (!encryptedChatIds.isEmpty()) {
getEncryptedChatsInternal(TextUtils.join(",", encryptedChatIds), encryptedChats, usersToLoad);
}
if (!usersToLoad.isEmpty()) {
getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
if (!chatsToLoad.isEmpty()) {
StringBuilder stringToLoad = new StringBuilder();
for (int a = 0; a < chatsToLoad.size(); a++) {
Long cid = chatsToLoad.get(a);
if (stringToLoad.length() != 0) {
stringToLoad.append(",");
}
stringToLoad.append(cid);
}
getChatsInternal(stringToLoad.toString(), chats);
}
getSendMessagesHelper().processUnsentMessages(messages, scheduledMessages, users, chats, encryptedChats);
} catch (Exception e) {
FileLog.e(e);
}
});
}
Aggregations