Search in sources :

Example 36 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method getNewTask.

public void getNewTask(LongSparseArray<ArrayList<Integer>> oldTask, LongSparseArray<ArrayList<Integer>> oldTaskMedia) {
    storageQueue.postRunnable(() -> {
        try {
            if (oldTask != null) {
                for (int a = 0, N = oldTask.size(); a < N; a++) {
                    database.executeFast(String.format(Locale.US, "DELETE FROM enc_tasks_v4 WHERE mid IN(%s) AND uid = %d AND media = 0", TextUtils.join(",", oldTask.valueAt(a)), oldTask.keyAt(a))).stepThis().dispose();
                }
            }
            if (oldTaskMedia != null) {
                for (int a = 0, N = oldTaskMedia.size(); a < N; a++) {
                    database.executeFast(String.format(Locale.US, "DELETE FROM enc_tasks_v4 WHERE mid IN(%s) AND uid = %d AND media = 1", TextUtils.join(",", oldTaskMedia.valueAt(a)), oldTaskMedia.keyAt(a))).stepThis().dispose();
                }
            }
            int date = 0;
            LongSparseArray<ArrayList<Integer>> newTask = null;
            LongSparseArray<ArrayList<Integer>> newTaskMedia = null;
            SQLiteCursor cursor = database.queryFinalized("SELECT mid, date, media, uid FROM enc_tasks_v4 WHERE date = (SELECT min(date) FROM enc_tasks_v4)");
            while (cursor.next()) {
                int mid = cursor.intValue(0);
                date = cursor.intValue(1);
                int isMedia = cursor.intValue(2);
                long uid = cursor.longValue(3);
                boolean media;
                if (isMedia == -1) {
                    media = mid > 0;
                } else {
                    media = isMedia != 0;
                }
                LongSparseArray<ArrayList<Integer>> task;
                if (media) {
                    if (newTaskMedia == null) {
                        newTaskMedia = new LongSparseArray<>();
                    }
                    task = newTaskMedia;
                } else {
                    if (newTask == null) {
                        newTask = new LongSparseArray<>();
                    }
                    task = newTask;
                }
                ArrayList<Integer> arr = task.get(uid);
                if (arr == null) {
                    arr = new ArrayList<>();
                    task.put(uid, arr);
                }
                arr.add(mid);
            }
            cursor.dispose();
            getMessagesController().processLoadedDeleteTask(date, newTask, newTaskMedia);
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 37 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method updateDialogsWithDeletedMessagesInternal.

private void updateDialogsWithDeletedMessagesInternal(long originalDialogId, long channelId, ArrayList<Integer> messages, ArrayList<Long> additionalDialogsToUpdate) {
    try {
        ArrayList<Long> dialogsToUpdate = new ArrayList<>();
        if (!messages.isEmpty()) {
            SQLitePreparedStatement state;
            if (channelId != 0) {
                dialogsToUpdate.add(-channelId);
                state = database.executeFast("UPDATE dialogs SET last_mid = (SELECT mid FROM messages_v2 WHERE uid = ? AND date = (SELECT MAX(date) FROM messages_v2 WHERE uid = ?)) WHERE did = ?");
            } else {
                if (originalDialogId == 0) {
                    String ids = TextUtils.join(",", messages);
                    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT did FROM dialogs WHERE last_mid IN(%s) AND flags = 0", ids));
                    while (cursor.next()) {
                        dialogsToUpdate.add(cursor.longValue(0));
                    }
                    cursor.dispose();
                } else {
                    dialogsToUpdate.add(originalDialogId);
                }
                state = database.executeFast("UPDATE dialogs SET last_mid = (SELECT mid FROM messages_v2 WHERE uid = ? AND date = (SELECT MAX(date) FROM messages_v2 WHERE uid = ? AND date != 0)) WHERE did = ?");
            }
            database.beginTransaction();
            for (int a = 0; a < dialogsToUpdate.size(); a++) {
                long did = dialogsToUpdate.get(a);
                state.requery();
                state.bindLong(1, did);
                state.bindLong(2, did);
                state.bindLong(3, did);
                state.step();
            }
            state.dispose();
            database.commitTransaction();
        } else {
            dialogsToUpdate.add(-channelId);
        }
        if (additionalDialogsToUpdate != null) {
            for (int a = 0; a < additionalDialogsToUpdate.size(); a++) {
                Long did = additionalDialogsToUpdate.get(a);
                if (!dialogsToUpdate.contains(did)) {
                    dialogsToUpdate.add(did);
                }
            }
        }
        String ids = TextUtils.join(",", dialogsToUpdate);
        TLRPC.messages_Dialogs dialogs = new TLRPC.TL_messages_dialogs();
        ArrayList<TLRPC.EncryptedChat> encryptedChats = new ArrayList<>();
        ArrayList<Long> usersToLoad = new ArrayList<>();
        ArrayList<Long> chatsToLoad = new ArrayList<>();
        ArrayList<Integer> encryptedToLoad = new ArrayList<>();
        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, m.date, d.pts, d.inbox_max, d.outbox_max, 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 WHERE d.did IN(%s)", ids));
        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(16)) {
                    NativeByteBuffer data = cursor.byteBufferValue(16);
                    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 = cursor.intValue(15);
                    }
                }
                dialog = dialogFolder;
            } else {
                dialog = new TLRPC.TL_dialog();
            }
            dialog.id = dialogId;
            dialog.top_message = cursor.intValue(1);
            dialog.read_inbox_max_id = cursor.intValue(10);
            dialog.read_outbox_max_id = cursor.intValue(11);
            dialog.unread_count = cursor.intValue(2);
            dialog.unread_mentions_count = cursor.intValue(13);
            dialog.last_message_date = cursor.intValue(3);
            dialog.pts = cursor.intValue(9);
            dialog.flags = channelId == 0 ? 0 : 1;
            dialog.pinnedNum = cursor.intValue(12);
            dialog.pinned = dialog.pinnedNum != 0;
            int dialog_flags = cursor.intValue(14);
            dialog.unread_mark = (dialog_flags & 1) != 0;
            dialog.folder_id = cursor.intValue(15);
            dialog.unread_reactions_count = cursor.intValue(17);
            dialogs.dialogs.add(dialog);
            NativeByteBuffer data = cursor.byteBufferValue(4);
            if (data != null) {
                TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
                message.readAttachPath(data, getUserConfig().clientUserId);
                data.reuse();
                MessageObject.setUnreadFlags(message, cursor.intValue(5));
                message.id = cursor.intValue(6);
                message.send_state = cursor.intValue(7);
                int date = cursor.intValue(8);
                if (date != 0) {
                    dialog.last_message_date = date;
                }
                message.dialog_id = dialog.id;
                dialogs.messages.add(message);
                addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
            }
            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 (!chatsToLoad.contains(-dialogId)) {
                    chatsToLoad.add(-dialogId);
                }
            }
        }
        cursor.dispose();
        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);
        }
        if (!dialogs.dialogs.isEmpty() || !encryptedChats.isEmpty()) {
            getMessagesController().processDialogsUpdate(dialogs, encryptedChats, true);
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 38 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method loadReplyMessages.

protected void loadReplyMessages(LongSparseArray<SparseArray<ArrayList<TLRPC.Message>>> replyMessageOwners, LongSparseArray<ArrayList<Integer>> dialogReplyMessagesIds, ArrayList<Long> usersToLoad, ArrayList<Long> chatsToLoad) throws SQLiteException {
    if (replyMessageOwners.isEmpty()) {
        return;
    }
    for (int b = 0, N2 = replyMessageOwners.size(); b < N2; b++) {
        long dialogId = replyMessageOwners.keyAt(b);
        SparseArray<ArrayList<TLRPC.Message>> owners = replyMessageOwners.valueAt(b);
        ArrayList<Integer> ids = dialogReplyMessagesIds.get(dialogId);
        if (ids == null) {
            continue;
        }
        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid, date, uid FROM messages_v2 WHERE mid IN(%s) AND uid = %d", TextUtils.join(",", ids), dialogId));
        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();
                message.id = cursor.intValue(1);
                message.date = cursor.intValue(2);
                message.dialog_id = cursor.longValue(3);
                addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
                ArrayList<TLRPC.Message> arrayList = owners.get(message.id);
                if (arrayList != null) {
                    for (int a = 0, N = arrayList.size(); a < N; a++) {
                        TLRPC.Message m = arrayList.get(a);
                        m.replyMessage = message;
                        MessageObject.getDialogId(message);
                    }
                }
            }
        }
        cursor.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) TLRPC(org.telegram.tgnet.TLRPC) SQLiteCursor(org.telegram.SQLite.SQLiteCursor)

Example 39 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method putWebPages.

public void putWebPages(LongSparseArray<TLRPC.WebPage> webPages) {
    if (isEmpty(webPages)) {
        return;
    }
    storageQueue.postRunnable(() -> {
        try {
            ArrayList<TLRPC.Message> messages = new ArrayList<>();
            for (int a = 0, N = webPages.size(); a < N; a++) {
                SQLiteCursor cursor = database.queryFinalized("SELECT mid, uid FROM webpage_pending_v2 WHERE id = " + webPages.keyAt(a));
                LongSparseArray<ArrayList<Integer>> dialogs = new LongSparseArray<>();
                while (cursor.next()) {
                    long dialogId = cursor.longValue(1);
                    ArrayList<Integer> mids = dialogs.get(dialogId);
                    if (mids == null) {
                        mids = new ArrayList<>();
                        dialogs.put(dialogId, mids);
                    }
                    mids.add(cursor.intValue(0));
                }
                cursor.dispose();
                if (dialogs.isEmpty()) {
                    continue;
                }
                for (int b = 0, N2 = dialogs.size(); b < N2; b++) {
                    long dialogId = dialogs.keyAt(b);
                    ArrayList<Integer> mids = dialogs.valueAt(b);
                    cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid, data FROM messages_v2 WHERE mid IN (%s) AND uid = %d", TextUtils.join(",", mids), dialogId));
                    while (cursor.next()) {
                        int mid = cursor.intValue(0);
                        NativeByteBuffer data = cursor.byteBufferValue(1);
                        if (data != null) {
                            TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
                            message.readAttachPath(data, getUserConfig().clientUserId);
                            data.reuse();
                            if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
                                message.id = mid;
                                message.media.webpage = webPages.valueAt(a);
                                messages.add(message);
                            }
                        }
                    }
                    cursor.dispose();
                }
            }
            if (messages.isEmpty()) {
                return;
            }
            database.beginTransaction();
            SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET data = ? WHERE mid = ? AND uid = ?");
            SQLitePreparedStatement state2 = database.executeFast("UPDATE media_v4 SET data = ? WHERE mid = ? AND uid = ?");
            for (int a = 0; a < messages.size(); a++) {
                TLRPC.Message message = messages.get(a);
                NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
                message.serializeToStream(data);
                state.requery();
                state.bindByteBuffer(1, data);
                state.bindInteger(2, message.id);
                state.bindLong(3, MessageObject.getDialogId(message));
                state.step();
                state2.requery();
                state2.bindByteBuffer(1, data);
                state2.bindInteger(2, message.id);
                state2.bindLong(3, MessageObject.getDialogId(message));
                state2.step();
                data.reuse();
            }
            state.dispose();
            state2.dispose();
            database.commitTransaction();
            AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didReceivedWebpages, messages));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 40 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method updateMessageStateAndIdInternal.

private long[] updateMessageStateAndIdInternal(long randomId, long dialogId, Integer _oldId, int newId, int date, int scheduled) {
    SQLiteCursor cursor = null;
    int oldMessageId;
    if (_oldId == null) {
        try {
            cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid, uid FROM randoms_v2 WHERE random_id = %d LIMIT 1", randomId));
            if (cursor.next()) {
                _oldId = cursor.intValue(0);
                dialogId = cursor.longValue(1);
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (cursor != null) {
                cursor.dispose();
            }
        }
        if (_oldId == null) {
            return null;
        }
    }
    oldMessageId = _oldId;
    if (_oldId < 0 && scheduled == 1) {
        SQLitePreparedStatement state = null;
        try {
            state = database.executeFast("UPDATE randoms_v2 SET mid = ? WHERE random_id = ? AND mid = ?");
            state.bindInteger(1, newId);
            state.bindLong(2, randomId);
            state.bindInteger(3, oldMessageId);
            state.step();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (state != null) {
                state.dispose();
            }
        }
    } else if (_oldId > 0) {
        TLRPC.TL_updateDeleteScheduledMessages update = new TLRPC.TL_updateDeleteScheduledMessages();
        update.messages.add(oldMessageId);
        if (DialogObject.isChatDialog(dialogId)) {
            update.peer = new TLRPC.TL_peerChannel();
            update.peer.channel_id = -dialogId;
        } else {
            update.peer = new TLRPC.TL_peerUser();
            update.peer.user_id = dialogId;
        }
        TLRPC.TL_updates updates = new TLRPC.TL_updates();
        updates.updates.add(update);
        Utilities.stageQueue.postRunnable(() -> getMessagesController().processUpdates(updates, false));
        try {
            database.executeFast(String.format(Locale.US, "DELETE FROM randoms_v2 WHERE random_id = %d AND mid = %d AND uid = %d", randomId, _oldId, dialogId)).stepThis().dispose();
        } catch (Exception e) {
            FileLog.e(e);
        }
        return null;
    }
    long did = 0;
    if (scheduled == -1 || scheduled == 0) {
        try {
            cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid FROM messages_v2 WHERE mid = %d LIMIT 1", oldMessageId));
            if (cursor.next()) {
                did = cursor.longValue(0);
                scheduled = 0;
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (cursor != null) {
                cursor.dispose();
            }
        }
    }
    if (scheduled == -1 || scheduled == 1) {
        try {
            cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid FROM scheduled_messages_v2 WHERE mid = %d LIMIT 1", oldMessageId));
            if (cursor.next()) {
                did = cursor.longValue(0);
                scheduled = 1;
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (cursor != null) {
                cursor.dispose();
            }
        }
    }
    if (did == 0) {
        return null;
    }
    SQLitePreparedStatement state = null;
    if (oldMessageId == newId && date != 0) {
        try {
            if (scheduled == 0) {
                state = database.executeFast("UPDATE messages_v2 SET send_state = 0, date = ? WHERE mid = ? AND uid = ?");
            } else {
                state = database.executeFast("UPDATE scheduled_messages_v2 SET send_state = 0, date = ? WHERE mid = ? AND uid = ?");
            }
            state.bindInteger(1, date);
            state.bindInteger(2, newId);
            state.bindLong(3, did);
            state.step();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (state != null) {
                state.dispose();
            }
        }
        return new long[] { did, newId };
    } else {
        if (scheduled == 0) {
            try {
                state = database.executeFast("UPDATE messages_v2 SET mid = ?, send_state = 0 WHERE mid = ? AND uid = ?");
                state.bindInteger(1, newId);
                state.bindInteger(2, oldMessageId);
                state.bindLong(3, did);
                state.step();
            } catch (Exception e) {
                try {
                    database.executeFast(String.format(Locale.US, "DELETE FROM messages_v2 WHERE mid = %d AND uid = %d", oldMessageId, did)).stepThis().dispose();
                    database.executeFast(String.format(Locale.US, "DELETE FROM messages_seq WHERE mid = %d", oldMessageId)).stepThis().dispose();
                } catch (Exception e2) {
                    FileLog.e(e2);
                }
            } finally {
                if (state != null) {
                    state.dispose();
                    state = null;
                }
            }
            try {
                state = database.executeFast("UPDATE media_v4 SET mid = ? WHERE mid = ? AND uid = ?");
                state.bindInteger(1, newId);
                state.bindInteger(2, oldMessageId);
                state.bindLong(3, did);
                state.step();
            } catch (Exception e) {
                try {
                    database.executeFast(String.format(Locale.US, "DELETE FROM media_v4 WHERE mid = %d AND uid = %d", oldMessageId, did)).stepThis().dispose();
                } catch (Exception e2) {
                    FileLog.e(e2);
                }
            } finally {
                if (state != null) {
                    state.dispose();
                    state = null;
                }
            }
            try {
                state = database.executeFast("UPDATE dialogs SET last_mid = ? WHERE last_mid = ?");
                state.bindInteger(1, newId);
                state.bindInteger(2, oldMessageId);
                state.step();
            } catch (Exception e) {
                FileLog.e(e);
            } finally {
                if (state != null) {
                    state.dispose();
                }
            }
        } else {
            try {
                state = database.executeFast("UPDATE scheduled_messages_v2 SET mid = ?, send_state = 0 WHERE mid = ? AND uid = ?");
                state.bindInteger(1, newId);
                state.bindInteger(2, oldMessageId);
                state.bindLong(3, did);
                state.step();
            } catch (Exception e) {
                try {
                    database.executeFast(String.format(Locale.US, "DELETE FROM scheduled_messages_v2 WHERE mid = %d AND uid = %d", oldMessageId, did)).stepThis().dispose();
                } catch (Exception e2) {
                    FileLog.e(e2);
                }
            } finally {
                if (state != null) {
                    state.dispose();
                }
            }
        }
        return new long[] { did, _oldId };
    }
}
Also used : SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) TLRPC(org.telegram.tgnet.TLRPC) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Aggregations

SQLiteCursor (org.telegram.SQLite.SQLiteCursor)118 SQLiteException (org.telegram.SQLite.SQLiteException)105 TLRPC (org.telegram.tgnet.TLRPC)77 NativeByteBuffer (org.telegram.tgnet.NativeByteBuffer)66 ArrayList (java.util.ArrayList)58 SQLitePreparedStatement (org.telegram.SQLite.SQLitePreparedStatement)42 LongSparseArray (androidx.collection.LongSparseArray)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 Paint (android.graphics.Paint)18 SparseArray (android.util.SparseArray)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 SpannableStringBuilder (android.text.SpannableStringBuilder)10 LongSparseIntArray (org.telegram.messenger.support.LongSparseIntArray)10 File (java.io.File)8 HashMap (java.util.HashMap)7 Pair (android.util.Pair)6 TLObject (org.telegram.tgnet.TLObject)6 LinkedHashMap (java.util.LinkedHashMap)4 SharedPreferences (android.content.SharedPreferences)3 SpannedString (android.text.SpannedString)3