Search in sources :

Example 91 with NativeByteBuffer

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

the class MessagesStorage method getEncryptedChatsInternal.

public void getEncryptedChatsInternal(String chatsToLoad, ArrayList<TLRPC.EncryptedChat> result, ArrayList<Long> usersToLoad) throws Exception {
    if (chatsToLoad == null || chatsToLoad.length() == 0 || result == null) {
        return;
    }
    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out, use_count, exchange_id, key_date, fprint, fauthkey, khash, in_seq_no, admin_id, mtproto_seq FROM enc_chats WHERE uid IN(%s)", chatsToLoad));
    while (cursor.next()) {
        try {
            NativeByteBuffer data = cursor.byteBufferValue(0);
            if (data != null) {
                TLRPC.EncryptedChat chat = TLRPC.EncryptedChat.TLdeserialize(data, data.readInt32(false), false);
                data.reuse();
                if (chat != null) {
                    chat.user_id = cursor.longValue(1);
                    if (usersToLoad != null && !usersToLoad.contains(chat.user_id)) {
                        usersToLoad.add(chat.user_id);
                    }
                    chat.a_or_b = cursor.byteArrayValue(2);
                    chat.auth_key = cursor.byteArrayValue(3);
                    chat.ttl = cursor.intValue(4);
                    chat.layer = cursor.intValue(5);
                    chat.seq_in = cursor.intValue(6);
                    chat.seq_out = cursor.intValue(7);
                    int use_count = cursor.intValue(8);
                    chat.key_use_count_in = (short) (use_count >> 16);
                    chat.key_use_count_out = (short) (use_count);
                    chat.exchange_id = cursor.longValue(9);
                    chat.key_create_date = cursor.intValue(10);
                    chat.future_key_fingerprint = cursor.longValue(11);
                    chat.future_auth_key = cursor.byteArrayValue(12);
                    chat.key_hash = cursor.byteArrayValue(13);
                    chat.in_seq_no = cursor.intValue(14);
                    long admin_id = cursor.longValue(15);
                    if (admin_id != 0) {
                        chat.admin_id = admin_id;
                    }
                    chat.mtproto_seq = cursor.intValue(16);
                    result.add(chat);
                }
            }
        } 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 92 with NativeByteBuffer

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

the class MessagesStorage method putDialogsInternal.

private void putDialogsInternal(TLRPC.messages_Dialogs dialogs, int check) {
    try {
        database.beginTransaction();
        LongSparseArray<TLRPC.Message> new_dialogMessage = new LongSparseArray<>(dialogs.messages.size());
        for (int a = 0; a < dialogs.messages.size(); a++) {
            TLRPC.Message message = dialogs.messages.get(a);
            new_dialogMessage.put(MessageObject.getDialogId(message), message);
        }
        if (!dialogs.dialogs.isEmpty()) {
            SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, 0)");
            SQLitePreparedStatement state_dialogs = database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            SQLitePreparedStatement state_media = database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
            SQLitePreparedStatement state_settings = database.executeFast("REPLACE INTO dialog_settings VALUES(?, ?)");
            SQLitePreparedStatement state_holes = database.executeFast("REPLACE INTO messages_holes VALUES(?, ?, ?)");
            SQLitePreparedStatement state_media_holes = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
            SQLitePreparedStatement state_polls = null;
            SQLitePreparedStatement state_tasks = null;
            int minDeleteTime = Integer.MAX_VALUE;
            for (int a = 0; a < dialogs.dialogs.size(); a++) {
                TLRPC.Dialog dialog = dialogs.dialogs.get(a);
                boolean exists = false;
                DialogObject.initDialog(dialog);
                unknownDialogsIds.remove(dialog.id);
                if (check == 1) {
                    SQLiteCursor cursor = database.queryFinalized("SELECT did FROM dialogs WHERE did = " + dialog.id);
                    exists = cursor.next();
                    cursor.dispose();
                    if (exists) {
                        continue;
                    }
                } else if (check == 2) {
                    SQLiteCursor cursor = database.queryFinalized("SELECT pinned FROM dialogs WHERE did = " + dialog.id);
                    if (cursor.next()) {
                        exists = true;
                        if (dialog.pinned) {
                            dialog.pinnedNum = cursor.intValue(0);
                        }
                    }
                    cursor.dispose();
                } else if (check == 3) {
                    int mid = 0;
                    SQLiteCursor cursor = database.queryFinalized("SELECT last_mid FROM dialogs WHERE did = " + dialog.id);
                    if (cursor.next()) {
                        mid = cursor.intValue(0);
                    }
                    cursor.dispose();
                    if (mid < 0) {
                        continue;
                    }
                }
                int messageDate = 0;
                TLRPC.Message message = new_dialogMessage.get(dialog.id);
                if (message != null) {
                    messageDate = Math.max(message.date, messageDate);
                    if (isValidKeyboardToSave(message)) {
                        getMediaDataController().putBotKeyboard(dialog.id, message);
                    }
                    fixUnsupportedMedia(message);
                    NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
                    message.serializeToStream(data);
                    state_messages.requery();
                    state_messages.bindInteger(1, message.id);
                    state_messages.bindLong(2, dialog.id);
                    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, 0);
                    state_messages.bindInteger(9, (message.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0 ? message.views : 0);
                    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, dialog.id);
                        state_media.bindInteger(3, message.date);
                        state_media.bindInteger(4, MediaDataController.getMediaType(message));
                        state_media.bindByteBuffer(5, data);
                        state_media.step();
                    }
                    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();
                    }
                    if (exists) {
                        closeHolesInTable("messages_holes", dialog.id, message.id, message.id);
                        closeHolesInMedia(dialog.id, message.id, message.id, -1);
                    } else {
                        createFirstHoles(dialog.id, state_holes, state_media_holes, message.id);
                    }
                }
                state_dialogs.requery();
                state_dialogs.bindLong(1, dialog.id);
                state_dialogs.bindInteger(2, messageDate);
                state_dialogs.bindInteger(3, dialog.unread_count);
                state_dialogs.bindInteger(4, dialog.top_message);
                state_dialogs.bindInteger(5, dialog.read_inbox_max_id);
                state_dialogs.bindInteger(6, dialog.read_outbox_max_id);
                state_dialogs.bindLong(7, 0);
                state_dialogs.bindInteger(8, dialog.unread_mentions_count);
                state_dialogs.bindInteger(9, dialog.pts);
                state_dialogs.bindInteger(10, 0);
                state_dialogs.bindInteger(11, dialog.pinnedNum);
                int flags = 0;
                if (dialog.unread_mark) {
                    flags |= 1;
                }
                state_dialogs.bindInteger(12, flags);
                state_dialogs.bindInteger(13, dialog.folder_id);
                NativeByteBuffer data;
                if (dialog instanceof TLRPC.TL_dialogFolder) {
                    TLRPC.TL_dialogFolder dialogFolder = (TLRPC.TL_dialogFolder) dialog;
                    data = new NativeByteBuffer(dialogFolder.folder.getObjectSize());
                    dialogFolder.folder.serializeToStream(data);
                    state_dialogs.bindByteBuffer(14, data);
                } else {
                    data = null;
                    state_dialogs.bindNull(14);
                }
                state_dialogs.bindInteger(15, dialog.unread_reactions_count);
                state_dialogs.step();
                if (data != null) {
                    data.reuse();
                }
                if (dialog.notify_settings != null) {
                    state_settings.requery();
                    state_settings.bindLong(1, dialog.id);
                    state_settings.bindInteger(2, dialog.notify_settings.mute_until != 0 ? 1 : 0);
                    state_settings.step();
                }
            }
            state_messages.dispose();
            state_dialogs.dispose();
            state_media.dispose();
            state_settings.dispose();
            state_holes.dispose();
            state_media_holes.dispose();
            if (state_tasks != null) {
                state_tasks.dispose();
                getMessagesController().didAddedNewTask(minDeleteTime, 0, null);
            }
            if (state_polls != null) {
                state_polls.dispose();
            }
        }
        putUsersInternal(dialogs.users);
        putChatsInternal(dialogs.chats);
        database.commitTransaction();
        resetAllUnreadCounters(false);
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) TLRPC(org.telegram.tgnet.TLRPC) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 93 with NativeByteBuffer

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

the class MessagesStorage method getBotCache.

public void getBotCache(String key, RequestDelegate requestDelegate) {
    if (key == null || requestDelegate == null) {
        return;
    }
    int currentDate = getConnectionsManager().getCurrentTime();
    storageQueue.postRunnable(() -> {
        TLObject result = null;
        try {
            database.executeFast("DELETE FROM botcache WHERE date < " + currentDate).stepThis().dispose();
            SQLiteCursor cursor = database.queryFinalized("SELECT data FROM botcache WHERE id = ?", key);
            if (cursor.next()) {
                try {
                    NativeByteBuffer data = cursor.byteBufferValue(0);
                    if (data != null) {
                        int constructor = data.readInt32(false);
                        if (constructor == TLRPC.TL_messages_botCallbackAnswer.constructor) {
                            result = TLRPC.TL_messages_botCallbackAnswer.TLdeserialize(data, constructor, false);
                        } else {
                            result = TLRPC.messages_BotResults.TLdeserialize(data, constructor, false);
                        }
                        data.reuse();
                    }
                } catch (Exception e) {
                    FileLog.e(e);
                }
            }
            cursor.dispose();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            requestDelegate.run(result, null);
        }
    });
}
Also used : TLObject(org.telegram.tgnet.TLObject) NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 94 with NativeByteBuffer

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

the class MediaDataController method putMediaDatabase.

private void putMediaDatabase(long uid, int type, ArrayList<TLRPC.Message> messages, int max_id, int min_id, boolean topReached) {
    getMessagesStorage().getStorageQueue().postRunnable(() -> {
        try {
            if (min_id == 0 && (messages.isEmpty() || topReached)) {
                getMessagesStorage().doneHolesInMedia(uid, max_id, type);
                if (messages.isEmpty()) {
                    return;
                }
            }
            getMessagesStorage().getDatabase().beginTransaction();
            SQLitePreparedStatement state2 = getMessagesStorage().getDatabase().executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
            for (TLRPC.Message message : messages) {
                if (canAddMessageToMedia(message)) {
                    state2.requery();
                    NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
                    message.serializeToStream(data);
                    state2.bindInteger(1, message.id);
                    state2.bindLong(2, uid);
                    state2.bindInteger(3, message.date);
                    state2.bindInteger(4, type);
                    state2.bindByteBuffer(5, data);
                    state2.step();
                    data.reuse();
                }
            }
            state2.dispose();
            if (!topReached || max_id != 0 || min_id != 0) {
                int minId = (topReached && min_id == 0) ? 1 : messages.get(messages.size() - 1).id;
                if (min_id != 0) {
                    getMessagesStorage().closeHolesInMedia(uid, minId, messages.get(0).id, type);
                } else if (max_id != 0) {
                    getMessagesStorage().closeHolesInMedia(uid, minId, max_id, type);
                } else {
                    getMessagesStorage().closeHolesInMedia(uid, minId, Integer.MAX_VALUE, type);
                }
            }
            getMessagesStorage().getDatabase().commitTransaction();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) TLRPC(org.telegram.tgnet.TLRPC) Paint(android.graphics.Paint) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 95 with NativeByteBuffer

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

the class MediaDataController method loadReactions.

public void loadReactions(boolean cache, boolean force) {
    isLoadingReactions = true;
    if (cache) {
        getMessagesStorage().getStorageQueue().postRunnable(() -> {
            SQLiteCursor c = null;
            int hash = 0;
            int date = 0;
            List<TLRPC.TL_availableReaction> reactions = null;
            try {
                c = getMessagesStorage().getDatabase().queryFinalized("SELECT data, hash, date FROM reactions");
                if (c.next()) {
                    NativeByteBuffer data = c.byteBufferValue(0);
                    if (data != null) {
                        int count = data.readInt32(false);
                        reactions = new ArrayList<>(count);
                        for (int i = 0; i < count; i++) {
                            TLRPC.TL_availableReaction react = TLRPC.TL_availableReaction.TLdeserialize(data, data.readInt32(false), true);
                            reactions.add(react);
                        }
                        data.reuse();
                    }
                    hash = c.intValue(1);
                    date = c.intValue(2);
                }
            } catch (Exception e) {
                FileLog.e(e, false);
            } finally {
                if (c != null) {
                    c.dispose();
                }
            }
            processLoadedReactions(reactions, hash, date, true);
        });
    } else {
        TLRPC.TL_messages_getAvailableReactions req = new TLRPC.TL_messages_getAvailableReactions();
        req.hash = force ? 0 : reactionsUpdateHash;
        getConnectionsManager().sendRequest(req, (response, error) -> {
            int date = (int) (System.currentTimeMillis() / 1000);
            if (response instanceof TLRPC.TL_messages_availableReactionsNotModified) {
                processLoadedReactions(null, 0, date, false);
            } else if (response instanceof TLRPC.TL_messages_availableReactions) {
                TLRPC.TL_messages_availableReactions r = (TLRPC.TL_messages_availableReactions) response;
                processLoadedReactions(r.reactions, r.hash, date, false);
            }
        });
    }
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) Paint(android.graphics.Paint) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException)

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