use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method getDownloadQueue.
public void getDownloadQueue(int type) {
storageQueue.postRunnable(() -> {
try {
ArrayList<DownloadObject> objects = new ArrayList<>();
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, type, data, parent FROM download_queue WHERE type = %d ORDER BY date DESC LIMIT 3", type));
while (cursor.next()) {
DownloadObject downloadObject = new DownloadObject();
downloadObject.type = cursor.intValue(1);
downloadObject.id = cursor.longValue(0);
downloadObject.parent = cursor.stringValue(3);
NativeByteBuffer data = cursor.byteBufferValue(2);
if (data != null) {
TLRPC.MessageMedia messageMedia = TLRPC.MessageMedia.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
if (messageMedia.document != null) {
downloadObject.object = messageMedia.document;
downloadObject.secret = MessageObject.isVideoDocument(messageMedia.document) && messageMedia.ttl_seconds > 0 && messageMedia.ttl_seconds <= 60;
} else if (messageMedia.photo != null) {
downloadObject.object = messageMedia.photo;
downloadObject.secret = messageMedia.ttl_seconds > 0 && messageMedia.ttl_seconds <= 60;
}
downloadObject.forceCache = (messageMedia.flags & 0x80000000) != 0;
}
objects.add(downloadObject);
}
cursor.dispose();
AndroidUtilities.runOnUIThread(() -> getDownloadController().processDownloadObjects(type, objects));
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method getDialogs.
public void getDialogs(int folderId, int offset, int count, boolean loadDraftsPeersAndFolders) {
long[] draftsDialogIds;
if (loadDraftsPeersAndFolders) {
LongSparseArray<SparseArray<TLRPC.DraftMessage>> drafts = getMediaDataController().getDrafts();
int draftsCount = drafts.size();
if (draftsCount > 0) {
draftsDialogIds = new long[draftsCount];
for (int i = 0; i < draftsCount; i++) {
SparseArray<TLRPC.DraftMessage> threads = drafts.valueAt(i);
if (threads.get(0) == null) {
continue;
}
draftsDialogIds[i] = drafts.keyAt(i);
}
} else {
draftsDialogIds = null;
}
} else {
draftsDialogIds = null;
}
storageQueue.postRunnable(() -> {
TLRPC.messages_Dialogs dialogs = new TLRPC.TL_messages_dialogs();
ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
usersToLoad.add(getUserConfig().getClientUserId());
ArrayList<Long> chatsToLoad = new ArrayList<>();
ArrayList<Integer> encryptedToLoad = new ArrayList<>();
ArrayList<Long> loadedDialogs = new ArrayList<>();
LongSparseArray<SparseArray<ArrayList<TLRPC.Message>>> replyMessageOwners = new LongSparseArray<>();
LongSparseArray<ArrayList<Integer>> dialogReplyMessagesIds = new LongSparseArray<>();
ArrayList<Integer> foldersToLoad = new ArrayList<>(2);
foldersToLoad.add(folderId);
for (int a = 0; a < foldersToLoad.size(); a++) {
int fid = foldersToLoad.get(a);
int off;
int cnt;
if (a == 0) {
off = offset;
cnt = count;
} else {
off = 0;
cnt = 100;
}
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, s.flags, m.date, d.pts, d.inbox_max, d.outbox_max, m.replydata, d.pinned, d.unread_count_i, d.flags, d.folder_id, d.data, d.unread_reactions FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid LEFT JOIN dialog_settings as s ON d.did = s.did WHERE d.folder_id = %d ORDER BY d.pinned DESC, d.date DESC LIMIT %d,%d", fid, off, cnt));
while (cursor.next()) {
long dialogId = cursor.longValue(0);
TLRPC.Dialog dialog;
if (DialogObject.isFolderDialogId(dialogId)) {
TLRPC.TL_dialogFolder dialogFolder = new TLRPC.TL_dialogFolder();
if (!cursor.isNull(18)) {
NativeByteBuffer data = cursor.byteBufferValue(18);
if (data != null) {
dialogFolder.folder = TLRPC.TL_folder.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
} else {
dialogFolder.folder = new TLRPC.TL_folder();
dialogFolder.folder.id = DialogObject.getFolderId(dialogId);
}
}
dialog = dialogFolder;
if (a == 0) {
foldersToLoad.add(dialogFolder.folder.id);
}
} else {
dialog = new TLRPC.TL_dialog();
}
dialog.id = dialogId;
dialog.top_message = cursor.intValue(1);
dialog.unread_count = cursor.intValue(2);
dialog.last_message_date = cursor.intValue(3);
dialog.pts = cursor.intValue(10);
dialog.flags = dialog.pts == 0 || DialogObject.isUserDialog(dialog.id) ? 0 : 1;
dialog.read_inbox_max_id = cursor.intValue(11);
dialog.read_outbox_max_id = cursor.intValue(12);
dialog.pinnedNum = cursor.intValue(14);
dialog.pinned = dialog.pinnedNum != 0;
dialog.unread_mentions_count = cursor.intValue(15);
int dialog_flags = cursor.intValue(16);
dialog.unread_mark = (dialog_flags & 1) != 0;
long flags = cursor.longValue(8);
int low_flags = (int) flags;
dialog.notify_settings = new TLRPC.TL_peerNotifySettings();
if ((low_flags & 1) != 0) {
dialog.notify_settings.mute_until = (int) (flags >> 32);
if (dialog.notify_settings.mute_until == 0) {
dialog.notify_settings.mute_until = Integer.MAX_VALUE;
}
}
dialog.folder_id = cursor.intValue(17);
dialog.unread_reactions_count = cursor.intValue(19);
dialogs.dialogs.add(dialog);
if (draftsDialogIds != null) {
loadedDialogs.add(dialogId);
}
NativeByteBuffer data = cursor.byteBufferValue(4);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
if (message != null) {
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
MessageObject.setUnreadFlags(message, cursor.intValue(5));
message.id = cursor.intValue(6);
int date = cursor.intValue(9);
if (date != 0) {
dialog.last_message_date = date;
}
message.send_state = cursor.intValue(7);
message.dialog_id = dialog.id;
dialogs.messages.add(message);
addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
try {
if (message.reply_to != null && message.reply_to.reply_to_msg_id != 0 && (message.action instanceof TLRPC.TL_messageActionPinMessage || message.action instanceof TLRPC.TL_messageActionPaymentSent || message.action instanceof TLRPC.TL_messageActionGameScore)) {
if (!cursor.isNull(13)) {
NativeByteBuffer data2 = cursor.byteBufferValue(13);
if (data2 != null) {
message.replyMessage = TLRPC.Message.TLdeserialize(data2, data2.readInt32(false), false);
message.replyMessage.readAttachPath(data2, getUserConfig().clientUserId);
data2.reuse();
if (message.replyMessage != null) {
addUsersAndChatsFromMessage(message.replyMessage, usersToLoad, chatsToLoad);
}
}
}
if (message.replyMessage == null) {
addReplyMessages(message, replyMessageOwners, dialogReplyMessagesIds);
}
}
} catch (Exception e) {
FileLog.e(e);
}
} else {
data.reuse();
}
}
if (DialogObject.isEncryptedDialog(dialogId)) {
int encryptedChatId = DialogObject.getEncryptedChatId(dialogId);
if (!encryptedToLoad.contains(encryptedChatId)) {
encryptedToLoad.add(encryptedChatId);
}
} else if (DialogObject.isUserDialog(dialogId)) {
if (!usersToLoad.contains(dialogId)) {
usersToLoad.add(dialogId);
}
} else if (DialogObject.isChatDialog(dialogId)) {
if (!chatsToLoad.contains(-dialogId)) {
chatsToLoad.add(-dialogId);
}
}
}
cursor.dispose();
}
loadReplyMessages(replyMessageOwners, dialogReplyMessagesIds, usersToLoad, chatsToLoad);
if (draftsDialogIds != null) {
ArrayList<Long> unloadedDialogs = new ArrayList<>();
for (int i = 0; i < draftsDialogIds.length; i++) {
long dialogId = draftsDialogIds[i];
if (DialogObject.isEncryptedDialog(dialogId)) {
continue;
}
if (dialogId > 0) {
if (!usersToLoad.contains(dialogId)) {
usersToLoad.add(dialogId);
}
} else {
if (!chatsToLoad.contains(-dialogId)) {
chatsToLoad.add(-dialogId);
}
}
if (!loadedDialogs.contains(draftsDialogIds[i])) {
unloadedDialogs.add(draftsDialogIds[i]);
}
}
LongSparseArray<Integer> folderIds;
if (!unloadedDialogs.isEmpty()) {
folderIds = new LongSparseArray<>(unloadedDialogs.size());
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT did, folder_id FROM dialogs WHERE did IN(%s)", TextUtils.join(",", unloadedDialogs)));
while (cursor.next()) {
folderIds.put(cursor.longValue(0), cursor.intValue(1));
}
cursor.dispose();
} else {
folderIds = null;
}
AndroidUtilities.runOnUIThread(() -> {
MediaDataController mediaDataController = getMediaDataController();
mediaDataController.clearDraftsFolderIds();
if (folderIds != null) {
for (int i = 0, size = folderIds.size(); i < size; i++) {
mediaDataController.setDraftFolderId(folderIds.keyAt(i), folderIds.valueAt(i));
}
}
});
}
if (!encryptedToLoad.isEmpty()) {
getEncryptedChatsInternal(TextUtils.join(",", encryptedToLoad), encryptedChats, usersToLoad);
}
if (!chatsToLoad.isEmpty()) {
getChatsInternal(TextUtils.join(",", chatsToLoad), dialogs.chats);
}
if (!usersToLoad.isEmpty()) {
getUsersInternal(TextUtils.join(",", usersToLoad), dialogs.users);
}
getMessagesController().processLoadedDialogs(dialogs, encryptedChats, folderId, offset, count, 1, false, false, true);
} catch (Exception e) {
dialogs.dialogs.clear();
dialogs.users.clear();
dialogs.chats.clear();
encryptedChats.clear();
FileLog.e(e);
getMessagesController().processLoadedDialogs(dialogs, encryptedChats, folderId, 0, 100, 1, true, false, true);
}
});
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method loadDialogFilters.
private void loadDialogFilters() {
storageQueue.postRunnable(() -> {
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
ArrayList<Integer> encryptedToLoad = new ArrayList<>();
ArrayList<Long> dialogsToLoad = new ArrayList<>();
SparseArray<MessagesController.DialogFilter> filtersById = new SparseArray<>();
usersToLoad.add(getUserConfig().getClientUserId());
SQLiteCursor filtersCursor = database.queryFinalized("SELECT id, ord, unread_count, flags, title FROM dialog_filter WHERE 1");
boolean updateCounters = false;
while (filtersCursor.next()) {
MessagesController.DialogFilter filter = new MessagesController.DialogFilter();
filter.id = filtersCursor.intValue(0);
filter.order = filtersCursor.intValue(1);
// filtersCursor.intValue(2);
filter.pendingUnreadCount = filter.unreadCount = -1;
filter.flags = filtersCursor.intValue(3);
filter.name = filtersCursor.stringValue(4);
dialogFilters.add(filter);
dialogFiltersMap.put(filter.id, filter);
filtersById.put(filter.id, filter);
if (filter.pendingUnreadCount < 0) {
updateCounters = true;
}
for (int a = 0; a < 2; a++) {
SQLiteCursor cursor2;
if (a == 0) {
cursor2 = database.queryFinalized("SELECT peer, pin FROM dialog_filter_pin_v2 WHERE id = " + filter.id);
} else {
cursor2 = database.queryFinalized("SELECT peer FROM dialog_filter_ep WHERE id = " + filter.id);
}
while (cursor2.next()) {
long did = cursor2.longValue(0);
if (a == 0) {
if (!DialogObject.isEncryptedDialog(did)) {
filter.alwaysShow.add(did);
}
int pin = cursor2.intValue(1);
if (pin != Integer.MIN_VALUE) {
filter.pinnedDialogs.put(did, pin);
if (!dialogsToLoad.contains(did)) {
dialogsToLoad.add(did);
}
}
} else {
if (!DialogObject.isEncryptedDialog(did)) {
filter.neverShow.add(did);
}
}
if (DialogObject.isChatDialog(did)) {
if (!chatsToLoad.contains(-did)) {
chatsToLoad.add(-did);
}
} else if (DialogObject.isUserDialog(did)) {
if (!usersToLoad.contains(did)) {
usersToLoad.add(did);
}
} else {
int encryptedChatId = DialogObject.getEncryptedChatId(did);
if (!encryptedToLoad.contains(encryptedChatId)) {
encryptedToLoad.add(encryptedChatId);
}
}
}
cursor2.dispose();
}
}
filtersCursor.dispose();
Collections.sort(dialogFilters, (o1, o2) -> {
if (o1.order > o2.order) {
return 1;
} else if (o1.order < o2.order) {
return -1;
}
return 0;
});
if (updateCounters) {
calcUnreadCounters(true);
}
TLRPC.messages_Dialogs dialogs;
if (!dialogsToLoad.isEmpty()) {
dialogs = loadDialogsByIds(TextUtils.join(",", dialogsToLoad), usersToLoad, chatsToLoad, encryptedToLoad);
} else {
dialogs = new TLRPC.TL_messages_dialogs();
}
ArrayList<TLRPC.User> users = new ArrayList<>();
ArrayList<TLRPC.Chat> chats = new ArrayList<>();
ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
if (!encryptedToLoad.isEmpty()) {
getEncryptedChatsInternal(TextUtils.join(",", encryptedToLoad), encryptedChats, usersToLoad);
}
if (!usersToLoad.isEmpty()) {
getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
if (!chatsToLoad.isEmpty()) {
getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
}
getMessagesController().processLoadedDialogFilters(new ArrayList<>(dialogFilters), dialogs, null, users, chats, encryptedChats, 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 checkMessageByRandomId.
public boolean checkMessageByRandomId(long random_id) {
boolean[] result = new boolean[1];
CountDownLatch countDownLatch = new CountDownLatch(1);
storageQueue.postRunnable(() -> {
SQLiteCursor cursor = null;
try {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT random_id FROM randoms_v2 WHERE random_id = %d", random_id));
if (cursor.next()) {
result[0] = true;
}
} catch (Exception e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
countDownLatch.countDown();
});
try {
countDownLatch.await();
} catch (Exception e) {
FileLog.e(e);
}
return result[0];
}
use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method markMentionMessageAsRead.
public void markMentionMessageAsRead(long dialogId, int messageId, long did) {
storageQueue.postRunnable(() -> {
try {
database.executeFast(String.format(Locale.US, "UPDATE messages_v2 SET read_state = read_state | 2 WHERE mid = %d AND uid = %d", messageId, dialogId)).stepThis().dispose();
SQLiteCursor cursor = database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + did);
int old_mentions_count = 0;
if (cursor.next()) {
old_mentions_count = Math.max(0, cursor.intValue(0) - 1);
}
cursor.dispose();
database.executeFast(String.format(Locale.US, "UPDATE dialogs SET unread_count_i = %d WHERE did = %d", old_mentions_count, did)).stepThis().dispose();
LongSparseIntArray sparseArray = new LongSparseIntArray(1);
sparseArray.put(did, old_mentions_count);
if (old_mentions_count == 0) {
updateFiltersReadCounter(null, sparseArray, true);
}
getMessagesController().processDialogsUpdateRead(null, sparseArray);
} catch (Exception e) {
FileLog.e(e);
}
});
}
Aggregations