Search in sources :

Example 26 with NativeByteBuffer

use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method updateMessagePollResults.

public void updateMessagePollResults(long pollId, TLRPC.Poll poll, TLRPC.PollResults results) {
    storageQueue.postRunnable(() -> {
        try {
            LongSparseArray<ArrayList<Integer>> dialogs = null;
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, mid FROM polls_v2 WHERE id = %d", pollId));
            while (cursor.next()) {
                long dialogId = cursor.longValue(0);
                if (dialogs == null) {
                    dialogs = new LongSparseArray<>();
                }
                ArrayList<Integer> mids = dialogs.get(dialogId);
                if (mids == null) {
                    mids = new ArrayList<>();
                    dialogs.put(dialogId, mids);
                }
                mids.add(cursor.intValue(1));
            }
            cursor.dispose();
            if (dialogs != null) {
                database.beginTransaction();
                SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET data = ? WHERE mid = ? AND uid = ?");
                for (int b = 0, N2 = dialogs.size(); b < N2; b++) {
                    long dialogId = dialogs.keyAt(b);
                    ArrayList<Integer> mids = dialogs.valueAt(b);
                    for (int a = 0, N = mids.size(); a < N; a++) {
                        Integer mid = mids.get(a);
                        cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE mid = %d AND uid = %d", mid, dialogId));
                        if (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 (message.media instanceof TLRPC.TL_messageMediaPoll) {
                                    TLRPC.TL_messageMediaPoll media = (TLRPC.TL_messageMediaPoll) message.media;
                                    if (poll != null) {
                                        media.poll = poll;
                                    }
                                    if (results != null) {
                                        MessageObject.updatePollResults(media, results);
                                    }
                                    data = new NativeByteBuffer(message.getObjectSize());
                                    message.serializeToStream(data);
                                    state.requery();
                                    state.bindByteBuffer(1, data);
                                    state.bindInteger(2, mid);
                                    state.bindLong(3, dialogId);
                                    state.step();
                                    data.reuse();
                                }
                            }
                        } else {
                            database.executeFast(String.format(Locale.US, "DELETE FROM polls_v2 WHERE mid = %d AND uid = %d", mid, dialogId)).stepThis().dispose();
                        }
                        cursor.dispose();
                    }
                }
                state.dispose();
                database.commitTransaction();
            }
        } 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)

Example 27 with NativeByteBuffer

use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method getUsersInternal.

public void getUsersInternal(String usersToLoad, ArrayList<TLRPC.User> result) throws Exception {
    if (usersToLoad == null || usersToLoad.length() == 0 || result == null) {
        return;
    }
    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad));
    while (cursor.next()) {
        try {
            NativeByteBuffer data = cursor.byteBufferValue(0);
            if (data != null) {
                TLRPC.User user = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
                data.reuse();
                if (user != null) {
                    if (user.status != null) {
                        user.status.expires = cursor.intValue(1);
                    }
                    result.add(user);
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }
    cursor.dispose();
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 28 with NativeByteBuffer

use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method deleteDialog.

public void deleteDialog(long did, int messagesOnly) {
    storageQueue.postRunnable(() -> {
        try {
            if (messagesOnly == 3) {
                int lastMid = -1;
                SQLiteCursor cursor = database.queryFinalized("SELECT last_mid FROM dialogs WHERE did = " + did);
                if (cursor.next()) {
                    lastMid = cursor.intValue(0);
                }
                cursor.dispose();
                if (lastMid != 0) {
                    return;
                }
            }
            if (DialogObject.isEncryptedDialog(did) || messagesOnly == 2) {
                SQLiteCursor cursor = database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + did);
                ArrayList<File> filesToDelete = new ArrayList<>();
                ArrayList<String> namesToDelete = new ArrayList<>();
                ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
                try {
                    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();
                            addFilesToDelete(message, filesToDelete, idsToDelete, namesToDelete, false);
                        }
                    }
                } catch (Exception e) {
                    FileLog.e(e);
                }
                cursor.dispose();
                deleteFromDownloadQueue(idsToDelete, true);
                AndroidUtilities.runOnUIThread(() -> getFileLoader().cancelLoadFiles(namesToDelete));
                getFileLoader().deleteFiles(filesToDelete, messagesOnly);
            }
            if (messagesOnly == 0 || messagesOnly == 3) {
                database.executeFast("DELETE FROM dialogs WHERE did = " + did).stepThis().dispose();
                database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + did).stepThis().dispose();
                database.executeFast("DELETE FROM chat_pinned_count WHERE uid = " + did).stepThis().dispose();
                database.executeFast("DELETE FROM channel_users_v2 WHERE did = " + did).stepThis().dispose();
                database.executeFast("DELETE FROM search_recent WHERE did = " + did).stepThis().dispose();
                if (!DialogObject.isEncryptedDialog(did)) {
                    if (DialogObject.isChatDialog(did)) {
                        database.executeFast("DELETE FROM chat_settings_v2 WHERE uid = " + (-did)).stepThis().dispose();
                    }
                } else {
                    database.executeFast("DELETE FROM enc_chats WHERE uid = " + DialogObject.getEncryptedChatId(did)).stepThis().dispose();
                }
            } else if (messagesOnly == 2) {
                SQLiteCursor cursor = database.queryFinalized("SELECT last_mid_i, last_mid FROM dialogs WHERE did = " + did);
                int messageId = -1;
                if (cursor.next()) {
                    long last_mid_i = cursor.longValue(0);
                    long last_mid = cursor.longValue(1);
                    SQLiteCursor cursor2 = database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + did + " AND mid IN (" + last_mid_i + "," + last_mid + ")");
                    try {
                        while (cursor2.next()) {
                            NativeByteBuffer data = cursor2.byteBufferValue(0);
                            if (data != null) {
                                TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
                                if (message != null) {
                                    message.readAttachPath(data, getUserConfig().clientUserId);
                                }
                                data.reuse();
                                if (message != null) {
                                    messageId = message.id;
                                }
                            }
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                    cursor2.dispose();
                    database.executeFast("DELETE FROM messages_v2 WHERE uid = " + did + " AND mid != " + last_mid_i + " AND mid != " + last_mid).stepThis().dispose();
                    database.executeFast("DELETE FROM messages_holes WHERE uid = " + did).stepThis().dispose();
                    database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + did).stepThis().dispose();
                    database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + did).stepThis().dispose();
                    database.executeFast("DELETE FROM media_v4 WHERE uid = " + did).stepThis().dispose();
                    database.executeFast("DELETE FROM media_holes_v2 WHERE uid = " + did).stepThis().dispose();
                    getMediaDataController().clearBotKeyboard(did, null);
                    SQLitePreparedStatement state5 = database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
                    SQLitePreparedStatement state6 = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
                    if (messageId != -1) {
                        createFirstHoles(did, state5, state6, messageId);
                    }
                    state5.dispose();
                    state6.dispose();
                    updateWidgets(did);
                }
                cursor.dispose();
                return;
            }
            database.executeFast("UPDATE dialogs SET unread_count = 0, unread_count_i = 0 WHERE did = " + did).stepThis().dispose();
            database.executeFast("DELETE FROM messages_v2 WHERE uid = " + did).stepThis().dispose();
            database.executeFast("DELETE FROM bot_keyboard WHERE uid = " + did).stepThis().dispose();
            database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + did).stepThis().dispose();
            database.executeFast("DELETE FROM media_v4 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();
            getMediaDataController().clearBotKeyboard(did, null);
            AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.needReloadRecentDialogsSearch));
            resetAllUnreadCounters(false);
            updateWidgets(did);
        } 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) File(java.io.File) Pair(android.util.Pair)

Example 29 with NativeByteBuffer

use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method getWidgetDialogs.

public void getWidgetDialogs(int widgetId, int type, ArrayList<Long> dids, LongSparseArray<TLRPC.Dialog> dialogs, LongSparseArray<TLRPC.Message> messages, ArrayList<TLRPC.User> users, ArrayList<TLRPC.Chat> chats) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    storageQueue.postRunnable(() -> {
        try {
            boolean add = false;
            ArrayList<Long> usersToLoad = new ArrayList<>();
            ArrayList<Long> chatsToLoad = new ArrayList<>();
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT did FROM shortcut_widget WHERE id = %d ORDER BY ord ASC", widgetId));
            while (cursor.next()) {
                long id = cursor.longValue(0);
                if (id == -1) {
                    continue;
                }
                dids.add(id);
                if (DialogObject.isUserDialog(id)) {
                    usersToLoad.add(id);
                } else {
                    chatsToLoad.add(-id);
                }
            }
            cursor.dispose();
            if (dids.isEmpty() && type == EditWidgetActivity.TYPE_CONTACTS) {
                cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT did FROM chat_hints WHERE type = 0 ORDER BY rating DESC LIMIT 4");
                while (cursor.next()) {
                    long dialogId = cursor.longValue(0);
                    dids.add(dialogId);
                    if (DialogObject.isUserDialog(dialogId)) {
                        usersToLoad.add(dialogId);
                    } else {
                        chatsToLoad.add(-dialogId);
                    }
                }
                cursor.dispose();
            }
            if (dids.isEmpty()) {
                add = true;
                cursor = database.queryFinalized("SELECT d.did, d.last_mid, d.unread_count, d.date, m.data, m.read_state, m.mid, m.send_state, m.date FROM dialogs as d LEFT JOIN messages_v2 as m ON d.last_mid = m.mid AND d.did = m.uid WHERE d.folder_id = 0 ORDER BY d.pinned DESC, d.date DESC LIMIT 0,10");
            } else {
                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 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)", TextUtils.join(",", dids)));
            }
            while (cursor.next()) {
                long dialogId = cursor.longValue(0);
                if (DialogObject.isFolderDialogId(dialogId)) {
                    continue;
                }
                if (add) {
                    dids.add(dialogId);
                }
                TLRPC.Dialog 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);
                dialogs.put(dialog.id, 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;
                    messages.put(dialog.id, message);
                    addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
                }
            }
            cursor.dispose();
            if (!add) {
                if (dids.size() > dialogs.size()) {
                    for (int a = 0, N = dids.size(); a < N; a++) {
                        long did = dids.get(a);
                        if (dialogs.get(dids.get(a)) == null) {
                            TLRPC.TL_dialog dialog = new TLRPC.TL_dialog();
                            dialog.id = did;
                            dialogs.put(dialog.id, dialog);
                            if (DialogObject.isChatDialog(did)) {
                                if (chatsToLoad.contains(-did)) {
                                    chatsToLoad.add(-did);
                                }
                            } else {
                                if (usersToLoad.contains(did)) {
                                    usersToLoad.add(did);
                                }
                            }
                        }
                    }
                }
            }
            if (!chatsToLoad.isEmpty()) {
                getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
            }
            if (!usersToLoad.isEmpty()) {
                getUsersInternal(TextUtils.join(",", usersToLoad), users);
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : ArrayList(java.util.ArrayList) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) CountDownLatch(java.util.concurrent.CountDownLatch) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 30 with NativeByteBuffer

use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.

the class MessagesStorage method putMessages.

public void putMessages(TLRPC.messages_Messages messages, long dialogId, int load_type, int max_id, boolean createDialog, boolean scheduled) {
    storageQueue.postRunnable(() -> {
        try {
            if (scheduled) {
                database.executeFast(String.format(Locale.US, "DELETE FROM scheduled_messages_v2 WHERE uid = %d AND mid > 0", dialogId)).stepThis().dispose();
                SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO scheduled_messages_v2 VALUES(?, ?, ?, ?, ?, ?, NULL, 0)");
                int count = messages.messages.size();
                for (int a = 0; a < count; a++) {
                    TLRPC.Message message = messages.messages.get(a);
                    if (message instanceof TLRPC.TL_messageEmpty) {
                        continue;
                    }
                    fixUnsupportedMedia(message);
                    state_messages.requery();
                    NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
                    message.serializeToStream(data);
                    state_messages.bindInteger(1, message.id);
                    state_messages.bindLong(2, dialogId);
                    state_messages.bindInteger(3, message.send_state);
                    state_messages.bindInteger(4, message.date);
                    state_messages.bindByteBuffer(5, data);
                    state_messages.bindInteger(6, message.ttl);
                    state_messages.step();
                    data.reuse();
                }
                state_messages.dispose();
                putUsersInternal(messages.users);
                putChatsInternal(messages.chats);
                database.commitTransaction();
                broadcastScheduledMessagesChange(dialogId);
            } else {
                int mentionCountUpdate = Integer.MAX_VALUE;
                if (messages.messages.isEmpty()) {
                    if (load_type == 0) {
                        doneHolesInTable("messages_holes", dialogId, max_id);
                        doneHolesInMedia(dialogId, max_id, -1);
                    }
                    return;
                }
                database.beginTransaction();
                if (load_type == 0) {
                    int minId = messages.messages.get(messages.messages.size() - 1).id;
                    closeHolesInTable("messages_holes", dialogId, minId, max_id);
                    closeHolesInMedia(dialogId, minId, max_id, -1);
                } else if (load_type == 1) {
                    int maxId = messages.messages.get(0).id;
                    closeHolesInTable("messages_holes", dialogId, max_id, maxId);
                    closeHolesInMedia(dialogId, max_id, maxId, -1);
                } else if (load_type == 3 || load_type == 2 || load_type == 4) {
                    int maxId = max_id == 0 && load_type != 4 ? Integer.MAX_VALUE : messages.messages.get(0).id;
                    int minId = messages.messages.get(messages.messages.size() - 1).id;
                    closeHolesInTable("messages_holes", dialogId, minId, maxId);
                    closeHolesInMedia(dialogId, minId, maxId, -1);
                }
                int count = messages.messages.size();
                // load_type == 0 ? backward loading
                // load_type == 1 ? forward loading
                // load_type == 2 ? load from first unread
                // load_type == 3 ? load around message
                // load_type == 4 ? load around date
                ArrayList<File> filesToDelete = new ArrayList<>();
                ArrayList<String> namesToDelete = new ArrayList<>();
                ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
                SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, 0)");
                SQLitePreparedStatement state_media = database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
                SQLitePreparedStatement state_polls = null;
                SQLitePreparedStatement state_webpage = null;
                SQLitePreparedStatement state_tasks = null;
                int minDeleteTime = Integer.MAX_VALUE;
                TLRPC.Message botKeyboard = null;
                long channelId = 0;
                for (int a = 0; a < count; a++) {
                    TLRPC.Message message = messages.messages.get(a);
                    if (channelId == 0) {
                        channelId = message.peer_id.channel_id;
                    }
                    if (load_type == -2) {
                        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid, data, ttl, mention, read_state, send_state FROM messages_v2 WHERE mid = %d AND uid = %d", message.id, MessageObject.getDialogId(message)));
                        boolean exist;
                        if (exist = cursor.next()) {
                            NativeByteBuffer data = cursor.byteBufferValue(1);
                            if (data != null) {
                                TLRPC.Message oldMessage = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
                                oldMessage.readAttachPath(data, getUserConfig().clientUserId);
                                data.reuse();
                                int send_state = cursor.intValue(5);
                                if (send_state != 3) {
                                    if (MessageObject.getFileName(oldMessage).equals(MessageObject.getFileName(message))) {
                                        message.attachPath = oldMessage.attachPath;
                                    }
                                    message.ttl = cursor.intValue(2);
                                }
                                boolean sameMedia = false;
                                if (oldMessage.media instanceof TLRPC.TL_messageMediaPhoto && message.media instanceof TLRPC.TL_messageMediaPhoto && oldMessage.media.photo != null && message.media.photo != null) {
                                    sameMedia = oldMessage.media.photo.id == message.media.photo.id;
                                } else if (oldMessage.media instanceof TLRPC.TL_messageMediaDocument && message.media instanceof TLRPC.TL_messageMediaDocument && oldMessage.media.document != null && message.media.document != null) {
                                    sameMedia = oldMessage.media.document.id == message.media.document.id;
                                }
                                if (!sameMedia) {
                                    addFilesToDelete(oldMessage, filesToDelete, idsToDelete, namesToDelete, false);
                                }
                            }
                            boolean oldMention = cursor.intValue(3) != 0;
                            int readState = cursor.intValue(4);
                            if (oldMention != message.mentioned) {
                                if (mentionCountUpdate == Integer.MAX_VALUE) {
                                    SQLiteCursor cursor2 = database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + dialogId);
                                    if (cursor2.next()) {
                                        mentionCountUpdate = cursor2.intValue(0);
                                    }
                                    cursor2.dispose();
                                }
                                if (oldMention) {
                                    if (readState <= 1) {
                                        mentionCountUpdate--;
                                    }
                                } else {
                                    if (message.media_unread) {
                                        mentionCountUpdate++;
                                    }
                                }
                            }
                        }
                        cursor.dispose();
                        if (!exist) {
                            continue;
                        }
                    }
                    if (a == 0 && createDialog) {
                        int pinned = 0;
                        int mentions = 0;
                        int flags = 0;
                        SQLiteCursor cursor = database.queryFinalized("SELECT pinned, unread_count_i, flags FROM dialogs WHERE did = " + dialogId);
                        boolean exist;
                        if (exist = cursor.next()) {
                            pinned = cursor.intValue(0);
                            mentions = cursor.intValue(1);
                            flags = cursor.intValue(2);
                        }
                        cursor.dispose();
                        SQLitePreparedStatement state3;
                        if (exist) {
                            state3 = database.executeFast("UPDATE dialogs SET date = ?, last_mid = ?, inbox_max = ?, last_mid_i = ?, pts = ?, date_i = ? WHERE did = ?");
                            state3.bindInteger(1, message.date);
                            state3.bindInteger(2, message.id);
                            state3.bindInteger(3, message.id);
                            state3.bindInteger(4, message.id);
                            state3.bindInteger(5, messages.pts);
                            state3.bindInteger(6, message.date);
                            state3.bindLong(7, dialogId);
                        } else {
                            state3 = database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                            state3.bindLong(1, dialogId);
                            state3.bindInteger(2, message.date);
                            state3.bindInteger(3, 0);
                            state3.bindInteger(4, message.id);
                            state3.bindInteger(5, message.id);
                            state3.bindInteger(6, 0);
                            state3.bindInteger(7, message.id);
                            state3.bindInteger(8, mentions);
                            state3.bindInteger(9, messages.pts);
                            state3.bindInteger(10, message.date);
                            state3.bindInteger(11, pinned);
                            state3.bindInteger(12, flags);
                            state3.bindInteger(13, -1);
                            state3.bindNull(14);
                            state3.bindInteger(15, 0);
                            unknownDialogsIds.put(dialogId, true);
                        }
                        state3.step();
                        state3.dispose();
                    }
                    fixUnsupportedMedia(message);
                    state_messages.requery();
                    NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
                    message.serializeToStream(data);
                    state_messages.bindInteger(1, message.id);
                    state_messages.bindLong(2, dialogId);
                    state_messages.bindInteger(3, MessageObject.getUnreadFlags(message));
                    state_messages.bindInteger(4, message.send_state);
                    state_messages.bindInteger(5, message.date);
                    state_messages.bindByteBuffer(6, data);
                    state_messages.bindInteger(7, (MessageObject.isOut(message) || message.from_scheduled ? 1 : 0));
                    state_messages.bindInteger(8, message.ttl);
                    if ((message.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
                        state_messages.bindInteger(9, message.views);
                    } else {
                        state_messages.bindInteger(9, getMessageMediaType(message));
                    }
                    int flags = 0;
                    if (message.stickerVerified == 0) {
                        flags |= 1;
                    } else if (message.stickerVerified == 2) {
                        flags |= 2;
                    }
                    state_messages.bindInteger(10, flags);
                    state_messages.bindInteger(11, message.mentioned ? 1 : 0);
                    state_messages.bindInteger(12, message.forwards);
                    NativeByteBuffer repliesData = null;
                    if (message.replies != null) {
                        repliesData = new NativeByteBuffer(message.replies.getObjectSize());
                        message.replies.serializeToStream(repliesData);
                        state_messages.bindByteBuffer(13, repliesData);
                    } else {
                        state_messages.bindNull(13);
                    }
                    if (message.reply_to != null) {
                        state_messages.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_messages.bindInteger(14, 0);
                    }
                    state_messages.bindLong(15, MessageObject.getChannelId(message));
                    state_messages.step();
                    if (MediaDataController.canAddMessageToMedia(message)) {
                        state_media.requery();
                        state_media.bindInteger(1, message.id);
                        state_media.bindLong(2, dialogId);
                        state_media.bindInteger(3, message.date);
                        state_media.bindInteger(4, MediaDataController.getMediaType(message));
                        state_media.bindByteBuffer(5, data);
                        state_media.step();
                    } else if (message instanceof TLRPC.TL_messageService && message.action instanceof TLRPC.TL_messageActionHistoryClear) {
                        try {
                            database.executeFast(String.format(Locale.US, "DELETE FROM media_v4 WHERE mid = %d AND uid = %d", message.id, dialogId)).stepThis().dispose();
                            database.executeFast("DELETE FROM media_counts_v2 WHERE uid = " + dialogId).stepThis().dispose();
                        } catch (Exception e2) {
                            FileLog.e(e2);
                        }
                    }
                    if (repliesData != null) {
                        repliesData.reuse();
                    }
                    data.reuse();
                    if (message.ttl_period != 0 && message.id > 0) {
                        if (state_tasks == null) {
                            state_tasks = database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
                        }
                        state_tasks.requery();
                        state_tasks.bindInteger(1, message.id);
                        state_tasks.bindLong(2, message.dialog_id);
                        state_tasks.bindInteger(3, message.date + message.ttl_period);
                        state_tasks.bindInteger(4, 0);
                        state_tasks.step();
                        minDeleteTime = Math.min(minDeleteTime, message.date + message.ttl_period);
                    }
                    if (message.media instanceof TLRPC.TL_messageMediaPoll) {
                        if (state_polls == null) {
                            state_polls = database.executeFast("REPLACE INTO polls_v2 VALUES(?, ?, ?)");
                        }
                        TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) message.media;
                        state_polls.requery();
                        state_polls.bindInteger(1, message.id);
                        state_polls.bindLong(2, message.dialog_id);
                        state_polls.bindLong(3, mediaPoll.poll.id);
                        state_polls.step();
                    } else if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
                        if (state_webpage == null) {
                            state_webpage = database.executeFast("REPLACE INTO webpage_pending_v2 VALUES(?, ?, ?)");
                        }
                        state_webpage.requery();
                        state_webpage.bindLong(1, message.media.webpage.id);
                        state_webpage.bindInteger(2, message.id);
                        state_webpage.bindLong(3, message.dialog_id);
                        state_webpage.step();
                    }
                    if (load_type == 0 && isValidKeyboardToSave(message)) {
                        if (botKeyboard == null || botKeyboard.id < message.id) {
                            botKeyboard = message;
                        }
                    }
                }
                state_messages.dispose();
                state_media.dispose();
                if (state_webpage != null) {
                    state_webpage.dispose();
                }
                if (state_tasks != null) {
                    state_tasks.dispose();
                    getMessagesController().didAddedNewTask(minDeleteTime, 0, null);
                }
                if (state_polls != null) {
                    state_polls.dispose();
                }
                if (botKeyboard != null) {
                    getMediaDataController().putBotKeyboard(dialogId, botKeyboard);
                }
                deleteFromDownloadQueue(idsToDelete, false);
                AndroidUtilities.runOnUIThread(() -> getFileLoader().cancelLoadFiles(namesToDelete));
                getFileLoader().deleteFiles(filesToDelete, 0);
                putUsersInternal(messages.users);
                putChatsInternal(messages.chats);
                if (mentionCountUpdate != Integer.MAX_VALUE) {
                    database.executeFast(String.format(Locale.US, "UPDATE dialogs SET unread_count_i = %d WHERE did = %d", mentionCountUpdate, dialogId)).stepThis().dispose();
                    LongSparseIntArray sparseArray = new LongSparseIntArray(1);
                    sparseArray.put(dialogId, mentionCountUpdate);
                    getMessagesController().processDialogsUpdateRead(null, sparseArray);
                }
                database.commitTransaction();
                if (createDialog) {
                    updateDialogsWithDeletedMessages(dialogId, channelId, new ArrayList<>(), null, false);
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) LongSparseIntArray(org.telegram.messenger.support.LongSparseIntArray) TLRPC(org.telegram.tgnet.TLRPC) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement) Pair(android.util.Pair) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteException(org.telegram.SQLite.SQLiteException) File(java.io.File)

Aggregations

NativeByteBuffer (org.telegram.tgnet.NativeByteBuffer)108 TLRPC (org.telegram.tgnet.TLRPC)92 SQLiteException (org.telegram.SQLite.SQLiteException)91 SQLiteCursor (org.telegram.SQLite.SQLiteCursor)68 SQLitePreparedStatement (org.telegram.SQLite.SQLitePreparedStatement)54 ArrayList (java.util.ArrayList)47 Paint (android.graphics.Paint)19 LongSparseArray (androidx.collection.LongSparseArray)18 SparseArray (android.util.SparseArray)12 File (java.io.File)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 TLObject (org.telegram.tgnet.TLObject)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 SpannableStringBuilder (android.text.SpannableStringBuilder)7 SharedPreferences (android.content.SharedPreferences)6 Pair (android.util.Pair)6 HashMap (java.util.HashMap)6 SparseIntArray (android.util.SparseIntArray)5 SQLiteDatabase (org.telegram.SQLite.SQLiteDatabase)5 LongSparseIntArray (org.telegram.messenger.support.LongSparseIntArray)5