Search in sources :

Example 96 with SQLiteCursor

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

the class MessagesStorage method getDialogFolderId.

public void getDialogFolderId(long dialogId, IntCallback callback) {
    storageQueue.postRunnable(() -> {
        try {
            int folderId;
            if (unknownDialogsIds.get(dialogId) != null) {
                folderId = -1;
            } else {
                SQLiteCursor cursor = database.queryFinalized("SELECT folder_id FROM dialogs WHERE did = ?", dialogId);
                if (cursor.next()) {
                    folderId = cursor.intValue(0);
                } else {
                    folderId = -1;
                }
                cursor.dispose();
            }
            AndroidUtilities.runOnUIThread(() -> callback.run(folderId));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 97 with SQLiteCursor

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

the class MessagesStorage method putChatsInternal.

private void putChatsInternal(ArrayList<TLRPC.Chat> chats) throws Exception {
    if (chats == null || chats.isEmpty()) {
        return;
    }
    SQLitePreparedStatement state = database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)");
    for (int a = 0; a < chats.size(); a++) {
        TLRPC.Chat chat = chats.get(a);
        if (chat.min) {
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid = %d", chat.id));
            if (cursor.next()) {
                try {
                    NativeByteBuffer data = cursor.byteBufferValue(0);
                    if (data != null) {
                        TLRPC.Chat oldChat = TLRPC.Chat.TLdeserialize(data, data.readInt32(false), false);
                        data.reuse();
                        if (oldChat != null) {
                            oldChat.title = chat.title;
                            oldChat.photo = chat.photo;
                            oldChat.broadcast = chat.broadcast;
                            oldChat.verified = chat.verified;
                            oldChat.megagroup = chat.megagroup;
                            oldChat.call_not_empty = chat.call_not_empty;
                            oldChat.call_active = chat.call_active;
                            if (chat.default_banned_rights != null) {
                                oldChat.default_banned_rights = chat.default_banned_rights;
                                oldChat.flags |= 262144;
                            }
                            if (chat.admin_rights != null) {
                                oldChat.admin_rights = chat.admin_rights;
                                oldChat.flags |= 16384;
                            }
                            if (chat.banned_rights != null) {
                                oldChat.banned_rights = chat.banned_rights;
                                oldChat.flags |= 32768;
                            }
                            if (chat.username != null) {
                                oldChat.username = chat.username;
                                oldChat.flags |= 64;
                            } else {
                                oldChat.username = null;
                                oldChat.flags = oldChat.flags & ~64;
                            }
                            chat = oldChat;
                        }
                    }
                } catch (Exception e) {
                    FileLog.e(e);
                }
            }
            cursor.dispose();
        }
        state.requery();
        chat.flags |= 131072;
        NativeByteBuffer data = new NativeByteBuffer(chat.getObjectSize());
        chat.serializeToStream(data);
        state.bindLong(1, chat.id);
        if (chat.title != null) {
            String name = chat.title.toLowerCase();
            state.bindString(2, name);
        } else {
            state.bindString(2, "");
        }
        state.bindByteBuffer(3, 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 98 with SQLiteCursor

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

the class MessagesStorage method updatePinnedMessages.

public void updatePinnedMessages(long dialogId, ArrayList<Integer> ids, boolean pin, int totalCount, int maxId, boolean end, HashMap<Integer, MessageObject> messages) {
    storageQueue.postRunnable(() -> {
        try {
            if (pin) {
                database.beginTransaction();
                int alreadyAdded = 0;
                boolean endReached;
                if (messages != null) {
                    if (maxId == 0) {
                        database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + dialogId).stepThis().dispose();
                    }
                } else {
                    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM chat_pinned_v2 WHERE uid = %d AND mid IN (%s)", dialogId, TextUtils.join(",", ids)));
                    alreadyAdded = cursor.next() ? cursor.intValue(0) : 0;
                    cursor.dispose();
                }
                SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_pinned_v2 VALUES(?, ?, ?)");
                for (int a = 0, N = ids.size(); a < N; a++) {
                    Integer id = ids.get(a);
                    state.requery();
                    state.bindLong(1, dialogId);
                    state.bindInteger(2, id);
                    MessageObject message = null;
                    if (messages != null) {
                        message = messages.get(id);
                    }
                    NativeByteBuffer data = null;
                    if (message != null) {
                        data = new NativeByteBuffer(message.messageOwner.getObjectSize());
                        message.messageOwner.serializeToStream(data);
                        state.bindByteBuffer(3, data);
                    } else {
                        state.bindNull(3);
                    }
                    state.step();
                    if (data != null) {
                        data.reuse();
                    }
                }
                state.dispose();
                database.commitTransaction();
                SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM chat_pinned_v2 WHERE uid = %d", dialogId));
                int newCount1 = cursor.next() ? cursor.intValue(0) : 0;
                cursor.dispose();
                int newCount;
                if (messages != null) {
                    newCount = Math.max(totalCount, newCount1);
                    endReached = end;
                } else {
                    SQLiteCursor cursor2 = database.queryFinalized(String.format(Locale.US, "SELECT count, end FROM chat_pinned_count WHERE uid = %d", dialogId));
                    int newCount2;
                    if (cursor2.next()) {
                        newCount2 = cursor2.intValue(0);
                        endReached = cursor2.intValue(1) != 0;
                    } else {
                        newCount2 = 0;
                        endReached = false;
                    }
                    cursor2.dispose();
                    newCount = Math.max(newCount2 + (ids.size() - alreadyAdded), newCount1);
                }
                state = database.executeFast("REPLACE INTO chat_pinned_count VALUES(?, ?, ?)");
                state.requery();
                state.bindLong(1, dialogId);
                state.bindInteger(2, newCount);
                state.bindInteger(3, endReached ? 1 : 0);
                state.step();
                state.dispose();
                AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didLoadPinnedMessages, dialogId, ids, true, null, messages, maxId, newCount, endReached));
            } else {
                int newCount;
                boolean endReached;
                if (ids == null) {
                    database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + dialogId).stepThis().dispose();
                    if (DialogObject.isChatDialog(dialogId)) {
                        database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d", -dialogId)).stepThis().dispose();
                    } else {
                        database.executeFast(String.format(Locale.US, "UPDATE user_settings SET pinned = 0 WHERE uid = %d", dialogId)).stepThis().dispose();
                    }
                    newCount = 0;
                    endReached = true;
                } else {
                    String idsStr = TextUtils.join(",", ids);
                    if (DialogObject.isChatDialog(dialogId)) {
                        database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", -dialogId, idsStr)).stepThis().dispose();
                    } else {
                        database.executeFast(String.format(Locale.US, "UPDATE user_settings SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", dialogId, idsStr)).stepThis().dispose();
                    }
                    database.executeFast(String.format(Locale.US, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid IN(%s)", dialogId, idsStr)).stepThis().dispose();
                    SQLiteCursor cursor = database.queryFinalized("SELECT changes()");
                    int updatedCount = cursor.next() ? cursor.intValue(0) : 0;
                    cursor.dispose();
                    cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM chat_pinned_v2 WHERE uid = %d", dialogId));
                    int newCount1 = cursor.next() ? cursor.intValue(0) : 0;
                    cursor.dispose();
                    cursor = database.queryFinalized(String.format(Locale.US, "SELECT count, end FROM chat_pinned_count WHERE uid = %d", dialogId));
                    int newCount2;
                    if (cursor.next()) {
                        newCount2 = Math.max(0, cursor.intValue(0) - updatedCount);
                        endReached = cursor.intValue(1) != 0;
                    } else {
                        newCount2 = 0;
                        endReached = false;
                    }
                    cursor.dispose();
                    newCount = Math.max(newCount1, newCount2);
                }
                SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_pinned_count VALUES(?, ?, ?)");
                state.requery();
                state.bindLong(1, dialogId);
                state.bindInteger(2, newCount);
                state.bindInteger(3, endReached ? 1 : 0);
                state.step();
                state.dispose();
                AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didLoadPinnedMessages, dialogId, ids, false, null, messages, maxId, newCount, endReached));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 99 with SQLiteCursor

use of org.telegram.SQLite.SQLiteCursor 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 100 with SQLiteCursor

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

the class MessagesStorage method getCachedMessagesInRange.

public ArrayList<Integer> getCachedMessagesInRange(long dialogId, int minDate, int maxDate) {
    ArrayList<Integer> messageIds = new ArrayList<>();
    try {
        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid FROM messages_v2 WHERE uid = %d AND date >= %d AND date <= %d", dialogId, minDate, maxDate));
        try {
            while (cursor.next()) {
                int mid = cursor.intValue(0);
                messageIds.add(mid);
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        cursor.dispose();
    } catch (Exception e) {
        FileLog.e(e);
    }
    return messageIds;
}
Also used : ArrayList(java.util.ArrayList) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

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