Search in sources :

Example 86 with NativeByteBuffer

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

the class MessagesStorage method updateMessageReactions.

public void updateMessageReactions(long dialogId, int msgId, TLRPC.TL_messageReactions reactions) {
    storageQueue.postRunnable(() -> {
        try {
            database.beginTransaction();
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE mid = %d AND uid = %d", msgId, dialogId));
            if (cursor.next()) {
                NativeByteBuffer data = cursor.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();
                        MessageObject.updateReactions(message, reactions);
                        SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET data = ? WHERE mid = ? AND uid = ?");
                        NativeByteBuffer data2 = new NativeByteBuffer(message.getObjectSize());
                        message.serializeToStream(data2);
                        state.requery();
                        state.bindByteBuffer(1, data2);
                        state.bindInteger(2, msgId);
                        state.bindLong(3, dialogId);
                        state.step();
                        data2.reuse();
                        state.dispose();
                    } else {
                        data.reuse();
                    }
                }
            }
            cursor.dispose();
            database.commitTransaction();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : 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 87 with NativeByteBuffer

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

the class MessagesStorage method saveSecretParams.

public void saveSecretParams(int lsv, int sg, byte[] pbytes) {
    storageQueue.postRunnable(() -> {
        try {
            SQLitePreparedStatement state = database.executeFast("UPDATE params SET lsv = ?, sg = ?, pbytes = ? WHERE id = 1");
            state.bindInteger(1, lsv);
            state.bindInteger(2, sg);
            NativeByteBuffer data = new NativeByteBuffer(pbytes != null ? pbytes.length : 1);
            if (pbytes != null) {
                data.writeBytes(pbytes);
            }
            state.bindByteBuffer(3, data);
            state.step();
            state.dispose();
            data.reuse();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 88 with NativeByteBuffer

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

the class MessagesStorage method getChatsInternal.

public void getChatsInternal(String chatsToLoad, ArrayList<TLRPC.Chat> result) throws Exception {
    if (chatsToLoad == null || chatsToLoad.length() == 0 || result == null) {
        return;
    }
    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", chatsToLoad));
    while (cursor.next()) {
        try {
            NativeByteBuffer data = cursor.byteBufferValue(0);
            if (data != null) {
                TLRPC.Chat chat = TLRPC.Chat.TLdeserialize(data, data.readInt32(false), false);
                data.reuse();
                if (chat != null) {
                    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 89 with NativeByteBuffer

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

the class MessagesStorage method putUsersInternal.

private void putUsersInternal(ArrayList<TLRPC.User> users) throws Exception {
    if (users == null || users.isEmpty()) {
        return;
    }
    SQLitePreparedStatement state = database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)");
    for (int a = 0; a < users.size(); a++) {
        TLRPC.User user = users.get(a);
        if (user.min) {
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM users WHERE uid = %d", user.id));
            if (cursor.next()) {
                try {
                    NativeByteBuffer data = cursor.byteBufferValue(0);
                    if (data != null) {
                        TLRPC.User oldUser = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
                        data.reuse();
                        if (oldUser != null) {
                            if (user.username != null) {
                                oldUser.username = user.username;
                                oldUser.flags |= 8;
                            } else {
                                oldUser.username = null;
                                oldUser.flags = oldUser.flags & ~8;
                            }
                            if (user.apply_min_photo) {
                                if (user.photo != null) {
                                    oldUser.photo = user.photo;
                                    oldUser.flags |= 32;
                                } else {
                                    oldUser.photo = null;
                                    oldUser.flags = oldUser.flags & ~32;
                                }
                            }
                            user = oldUser;
                        }
                    }
                } catch (Exception e) {
                    FileLog.e(e);
                }
            }
            cursor.dispose();
        }
        state.requery();
        NativeByteBuffer data = new NativeByteBuffer(user.getObjectSize());
        user.serializeToStream(data);
        state.bindLong(1, user.id);
        state.bindString(2, formatUserSearchName(user));
        if (user.status != null) {
            if (user.status instanceof TLRPC.TL_userStatusRecently) {
                user.status.expires = -100;
            } else if (user.status instanceof TLRPC.TL_userStatusLastWeek) {
                user.status.expires = -101;
            } else if (user.status instanceof TLRPC.TL_userStatusLastMonth) {
                user.status.expires = -102;
            }
            state.bindInteger(3, user.status.expires);
        } else {
            state.bindInteger(3, 0);
        }
        state.bindByteBuffer(4, data);
        state.step();
        data.reuse();
    }
    state.dispose();
}
Also used : 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 90 with NativeByteBuffer

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

the class MessagesStorage method saveBotCache.

public void saveBotCache(String key, TLObject result) {
    if (result == null || TextUtils.isEmpty(key)) {
        return;
    }
    storageQueue.postRunnable(() -> {
        try {
            int currentDate = getConnectionsManager().getCurrentTime();
            if (result instanceof TLRPC.TL_messages_botCallbackAnswer) {
                currentDate += ((TLRPC.TL_messages_botCallbackAnswer) result).cache_time;
            } else if (result instanceof TLRPC.TL_messages_botResults) {
                currentDate += ((TLRPC.TL_messages_botResults) result).cache_time;
            }
            SQLitePreparedStatement state = database.executeFast("REPLACE INTO botcache VALUES(?, ?, ?)");
            NativeByteBuffer data = new NativeByteBuffer(result.getObjectSize());
            result.serializeToStream(data);
            state.bindString(1, key);
            state.bindInteger(2, currentDate);
            state.bindByteBuffer(3, data);
            state.step();
            state.dispose();
            data.reuse();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

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