use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method isDialogHasTopMessage.
public void isDialogHasTopMessage(long did, Runnable onDontExist) {
storageQueue.postRunnable(() -> {
boolean exists = false;
try {
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT last_mid FROM dialogs WHERE did = %d", did));
if (cursor.next()) {
exists = cursor.intValue(0) != 0;
}
cursor.dispose();
} catch (Exception e) {
FileLog.e(e);
}
if (!exists) {
AndroidUtilities.runOnUIThread(onDontExist);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method replaceMessageIfExists.
public void replaceMessageIfExists(TLRPC.Message message, ArrayList<TLRPC.User> users, ArrayList<TLRPC.Chat> chats, boolean broadcast) {
if (message == null) {
return;
}
storageQueue.postRunnable(() -> {
try {
SQLiteCursor cursor = null;
int readState = 0;
try {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, read_state FROM messages_v2 WHERE mid = %d AND uid = %d LIMIT 1", message.id, MessageObject.getDialogId(message)));
if (!cursor.next()) {
return;
}
readState = cursor.intValue(1);
} catch (Exception e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
database.beginTransaction();
SQLitePreparedStatement state = database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, 0)");
SQLitePreparedStatement state2 = database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
if (message.dialog_id == 0) {
MessageObject.getDialogId(message);
}
fixUnsupportedMedia(message);
state.requery();
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
state.bindInteger(1, message.id);
state.bindLong(2, message.dialog_id);
state.bindInteger(3, readState);
state.bindInteger(4, message.send_state);
state.bindInteger(5, message.date);
state.bindByteBuffer(6, data);
state.bindInteger(7, (MessageObject.isOut(message) || message.from_scheduled ? 1 : 0));
state.bindInteger(8, message.ttl);
if ((message.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
state.bindInteger(9, message.views);
} else {
state.bindInteger(9, getMessageMediaType(message));
}
int flags = 0;
if (message.stickerVerified == 0) {
flags |= 1;
} else if (message.stickerVerified == 2) {
flags |= 2;
}
state.bindInteger(10, flags);
state.bindInteger(11, message.mentioned ? 1 : 0);
state.bindInteger(12, message.forwards);
NativeByteBuffer repliesData = null;
if (message.replies != null) {
repliesData = new NativeByteBuffer(message.replies.getObjectSize());
message.replies.serializeToStream(repliesData);
state.bindByteBuffer(13, repliesData);
} else {
state.bindNull(13);
}
if (message.reply_to != null) {
state.bindInteger(14, message.reply_to.reply_to_top_id != 0 ? message.reply_to.reply_to_top_id : message.reply_to.reply_to_msg_id);
} else {
state.bindInteger(14, 0);
}
state.bindLong(15, MessageObject.getChannelId(message));
state.step();
if (MediaDataController.canAddMessageToMedia(message)) {
state2.requery();
state2.bindInteger(1, message.id);
state2.bindLong(2, message.dialog_id);
state2.bindInteger(3, message.date);
state2.bindInteger(4, MediaDataController.getMediaType(message));
state2.bindByteBuffer(5, data);
state2.step();
}
if (repliesData != null) {
repliesData.reuse();
}
data.reuse();
state.dispose();
state2.dispose();
database.commitTransaction();
if (broadcast) {
HashMap<Long, TLRPC.User> userHashMap = new HashMap<>();
HashMap<Long, TLRPC.Chat> chatHashMap = new HashMap<>();
for (int a = 0; a < users.size(); a++) {
TLRPC.User user = users.get(a);
userHashMap.put(user.id, user);
}
for (int a = 0; a < chats.size(); a++) {
TLRPC.Chat chat = chats.get(a);
chatHashMap.put(chat.id, chat);
}
MessageObject messageObject = new MessageObject(currentAccount, message, userHashMap, chatHashMap, true, true);
ArrayList<MessageObject> arrayList = new ArrayList<>();
arrayList.add(messageObject);
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.replaceMessagesObjects, messageObject.getDialogId(), arrayList));
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method readAllDialogs.
public void readAllDialogs(int folderId) {
storageQueue.postRunnable(() -> {
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
ArrayList<Integer> encryptedChatIds = new ArrayList<>();
LongSparseArray<ReadDialog> dialogs = new LongSparseArray<>();
SQLiteCursor cursor;
if (folderId >= 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT did, last_mid, unread_count, date FROM dialogs WHERE unread_count > 0 AND folder_id = %1$d", folderId));
} else {
cursor = database.queryFinalized("SELECT did, last_mid, unread_count, date FROM dialogs WHERE unread_count > 0");
}
while (cursor.next()) {
long did = cursor.longValue(0);
if (DialogObject.isFolderDialogId(did)) {
continue;
}
ReadDialog dialog = new ReadDialog();
dialog.lastMid = cursor.intValue(1);
dialog.unreadCount = cursor.intValue(2);
dialog.date = cursor.intValue(3);
dialogs.put(did, dialog);
if (!DialogObject.isEncryptedDialog(did)) {
if (DialogObject.isChatDialog(did)) {
if (!chatsToLoad.contains(-did)) {
chatsToLoad.add(-did);
}
} else {
if (!usersToLoad.contains(did)) {
usersToLoad.add(did);
}
}
} else {
int encryptedChatId = DialogObject.getEncryptedChatId(did);
if (!encryptedChatIds.contains(encryptedChatId)) {
encryptedChatIds.add(encryptedChatId);
}
}
}
cursor.dispose();
ArrayList<TLRPC.User> users = new ArrayList<>();
ArrayList<TLRPC.Chat> chats = new ArrayList<>();
ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
if (!encryptedChatIds.isEmpty()) {
getEncryptedChatsInternal(TextUtils.join(",", encryptedChatIds), encryptedChats, usersToLoad);
}
if (!usersToLoad.isEmpty()) {
getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
if (!chatsToLoad.isEmpty()) {
getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
}
AndroidUtilities.runOnUIThread(() -> {
getMessagesController().putUsers(users, true);
getMessagesController().putChats(chats, true);
getMessagesController().putEncryptedChats(encryptedChats, true);
for (int a = 0; a < dialogs.size(); a++) {
long did = dialogs.keyAt(a);
ReadDialog dialog = dialogs.valueAt(a);
getMessagesController().markDialogAsRead(did, dialog.lastMid, dialog.lastMid, dialog.date, false, 0, dialog.unreadCount, true, 0);
}
});
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method resetDialogs.
public void resetDialogs(TLRPC.messages_Dialogs dialogsRes, int messagesCount, int seq, int newPts, int date, int qts, LongSparseArray<TLRPC.Dialog> new_dialogs_dict, LongSparseArray<MessageObject> new_dialogMessage, TLRPC.Message lastMessage, int dialogsCount) {
storageQueue.postRunnable(() -> {
try {
int maxPinnedNum = 0;
ArrayList<Long> dids = new ArrayList<>();
int totalPinnedCount = dialogsRes.dialogs.size() - dialogsCount;
LongSparseIntArray oldPinnedDialogNums = new LongSparseIntArray();
ArrayList<Long> oldPinnedOrder = new ArrayList<>();
ArrayList<Long> orderArrayList = new ArrayList<>();
for (int a = dialogsCount; a < dialogsRes.dialogs.size(); a++) {
TLRPC.Dialog dialog = dialogsRes.dialogs.get(a);
orderArrayList.add(dialog.id);
}
SQLiteCursor cursor = database.queryFinalized("SELECT did, pinned FROM dialogs WHERE 1");
while (cursor.next()) {
long did = cursor.longValue(0);
int pinnedNum = cursor.intValue(1);
if (!DialogObject.isEncryptedDialog(did)) {
dids.add(did);
if (pinnedNum > 0) {
maxPinnedNum = Math.max(pinnedNum, maxPinnedNum);
oldPinnedDialogNums.put(did, pinnedNum);
oldPinnedOrder.add(did);
}
}
}
Collections.sort(oldPinnedOrder, (o1, o2) -> {
int val1 = oldPinnedDialogNums.get(o1);
int val2 = oldPinnedDialogNums.get(o2);
if (val1 < val2) {
return 1;
} else if (val1 > val2) {
return -1;
}
return 0;
});
while (oldPinnedOrder.size() < totalPinnedCount) {
oldPinnedOrder.add(0, 0L);
}
cursor.dispose();
String ids = "(" + TextUtils.join(",", dids) + ")";
database.beginTransaction();
database.executeFast("DELETE FROM chat_pinned_count WHERE uid IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM dialogs WHERE did IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM messages_v2 WHERE uid IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM polls_v2 WHERE 1").stepThis().dispose();
database.executeFast("DELETE FROM bot_keyboard WHERE uid IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM media_v4 WHERE uid IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM messages_holes WHERE uid IN " + ids).stepThis().dispose();
database.executeFast("DELETE FROM media_holes_v2 WHERE uid IN " + ids).stepThis().dispose();
database.commitTransaction();
for (int a = 0; a < totalPinnedCount; a++) {
TLRPC.Dialog dialog = dialogsRes.dialogs.get(dialogsCount + a);
if (dialog instanceof TLRPC.TL_dialog && !dialog.pinned) {
continue;
}
int oldIdx = oldPinnedOrder.indexOf(dialog.id);
int newIdx = orderArrayList.indexOf(dialog.id);
if (oldIdx != -1 && newIdx != -1) {
if (oldIdx == newIdx) {
int oldNum = oldPinnedDialogNums.get(dialog.id, -1);
if (oldNum != -1) {
dialog.pinnedNum = oldNum;
}
} else {
long oldDid = oldPinnedOrder.get(newIdx);
int oldNum = oldPinnedDialogNums.get(oldDid, -1);
if (oldNum != -1) {
dialog.pinnedNum = oldNum;
}
}
}
if (dialog.pinnedNum == 0) {
dialog.pinnedNum = (totalPinnedCount - a) + maxPinnedNum;
}
}
putDialogsInternal(dialogsRes, 0);
saveDiffParamsInternal(seq, newPts, date, qts);
int totalDialogsLoadCount = getUserConfig().getTotalDialogsCount(0);
int dialogsLoadOffsetId;
int dialogsLoadOffsetDate;
long dialogsLoadOffsetChannelId = 0;
long dialogsLoadOffsetChatId = 0;
long dialogsLoadOffsetUserId = 0;
long dialogsLoadOffsetAccess = 0;
totalDialogsLoadCount += dialogsRes.dialogs.size();
dialogsLoadOffsetId = lastMessage.id;
dialogsLoadOffsetDate = lastMessage.date;
if (lastMessage.peer_id.channel_id != 0) {
dialogsLoadOffsetChannelId = lastMessage.peer_id.channel_id;
dialogsLoadOffsetChatId = 0;
dialogsLoadOffsetUserId = 0;
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat chat = dialogsRes.chats.get(a);
if (chat.id == dialogsLoadOffsetChannelId) {
dialogsLoadOffsetAccess = chat.access_hash;
break;
}
}
} else if (lastMessage.peer_id.chat_id != 0) {
dialogsLoadOffsetChatId = lastMessage.peer_id.chat_id;
dialogsLoadOffsetChannelId = 0;
dialogsLoadOffsetUserId = 0;
for (int a = 0; a < dialogsRes.chats.size(); a++) {
TLRPC.Chat chat = dialogsRes.chats.get(a);
if (chat.id == dialogsLoadOffsetChatId) {
dialogsLoadOffsetAccess = chat.access_hash;
break;
}
}
} else if (lastMessage.peer_id.user_id != 0) {
dialogsLoadOffsetUserId = lastMessage.peer_id.user_id;
dialogsLoadOffsetChatId = 0;
dialogsLoadOffsetChannelId = 0;
for (int a = 0; a < dialogsRes.users.size(); a++) {
TLRPC.User user = dialogsRes.users.get(a);
if (user.id == dialogsLoadOffsetUserId) {
dialogsLoadOffsetAccess = user.access_hash;
break;
}
}
}
for (int a = 0; a < 2; a++) {
getUserConfig().setDialogsLoadOffset(a, dialogsLoadOffsetId, dialogsLoadOffsetDate, dialogsLoadOffsetUserId, dialogsLoadOffsetChatId, dialogsLoadOffsetChannelId, dialogsLoadOffsetAccess);
getUserConfig().setTotalDialogsCount(a, totalDialogsLoadCount);
}
getUserConfig().draftsLoaded = false;
getUserConfig().saveConfig(false);
getMessagesController().completeDialogsReset(dialogsRes, messagesCount, seq, newPts, date, qts, new_dialogs_dict, new_dialogMessage, lastMessage);
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method updateRepliesCount.
public void updateRepliesCount(long chatId, int mid, ArrayList<TLRPC.Peer> repliers, int maxId, int count) {
storageQueue.postRunnable(() -> {
try {
SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
TLRPC.MessageReplies currentReplies = null;
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.ENGLISH, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", mid, -chatId));
if (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
currentReplies = TLRPC.MessageReplies.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
}
cursor.dispose();
if (currentReplies != null) {
currentReplies.replies += count;
if (currentReplies.replies < 0) {
currentReplies.replies = 0;
}
if (repliers != null) {
currentReplies.recent_repliers = repliers;
currentReplies.flags |= 2;
}
if (maxId != 0) {
currentReplies.max_id = maxId;
}
state.requery();
NativeByteBuffer data = new NativeByteBuffer(currentReplies.getObjectSize());
currentReplies.serializeToStream(data);
state.bindByteBuffer(1, data);
state.bindInteger(2, mid);
state.bindLong(3, -chatId);
state.step();
data.reuse();
}
state.dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
Aggregations