Search in sources :

Example 51 with SQLiteCursor

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

the class MessagesStorage method isMigratedChat.

public boolean isMigratedChat(long chatId) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    boolean[] result = new boolean[1];
    storageQueue.postRunnable(() -> {
        try {
            SQLiteCursor cursor = database.queryFinalized("SELECT info FROM chat_settings_v2 WHERE uid = " + chatId);
            TLRPC.ChatFull info = null;
            ArrayList<TLRPC.User> loadedUsers = new ArrayList<>();
            if (cursor.next()) {
                NativeByteBuffer data = cursor.byteBufferValue(0);
                if (data != null) {
                    info = TLRPC.ChatFull.TLdeserialize(data, data.readInt32(false), false);
                    data.reuse();
                }
            }
            cursor.dispose();
            result[0] = info instanceof TLRPC.TL_channelFull && info.migrated_from_chat_id != 0;
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (Exception e) {
        FileLog.e(e);
    }
    return result[0];
}
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)

Example 52 with SQLiteCursor

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

the class MessagesStorage method updateChatInfo.

public void updateChatInfo(long chatId, long userId, int what, long invited_id, int version) {
    storageQueue.postRunnable(() -> {
        try {
            SQLiteCursor cursor = database.queryFinalized("SELECT info, pinned, online, inviter FROM chat_settings_v2 WHERE uid = " + chatId);
            TLRPC.ChatFull info = null;
            ArrayList<TLRPC.User> loadedUsers = new ArrayList<>();
            if (cursor.next()) {
                NativeByteBuffer data = cursor.byteBufferValue(0);
                if (data != null) {
                    info = TLRPC.ChatFull.TLdeserialize(data, data.readInt32(false), false);
                    data.reuse();
                    info.pinned_msg_id = cursor.intValue(1);
                    info.online_count = cursor.intValue(2);
                    info.inviterId = cursor.longValue(3);
                }
            }
            cursor.dispose();
            if (info instanceof TLRPC.TL_chatFull) {
                if (what == 1) {
                    for (int a = 0; a < info.participants.participants.size(); a++) {
                        TLRPC.ChatParticipant participant = info.participants.participants.get(a);
                        if (participant.user_id == userId) {
                            info.participants.participants.remove(a);
                            break;
                        }
                    }
                } else if (what == 0) {
                    for (TLRPC.ChatParticipant part : info.participants.participants) {
                        if (part.user_id == userId) {
                            return;
                        }
                    }
                    TLRPC.TL_chatParticipant participant = new TLRPC.TL_chatParticipant();
                    participant.user_id = userId;
                    participant.inviter_id = invited_id;
                    participant.date = getConnectionsManager().getCurrentTime();
                    info.participants.participants.add(participant);
                } else if (what == 2) {
                    for (int a = 0; a < info.participants.participants.size(); a++) {
                        TLRPC.ChatParticipant participant = info.participants.participants.get(a);
                        if (participant.user_id == userId) {
                            TLRPC.ChatParticipant newParticipant;
                            if (invited_id == 1) {
                                newParticipant = new TLRPC.TL_chatParticipantAdmin();
                            } else {
                                newParticipant = new TLRPC.TL_chatParticipant();
                            }
                            newParticipant.user_id = participant.user_id;
                            newParticipant.date = participant.date;
                            newParticipant.inviter_id = participant.inviter_id;
                            info.participants.participants.set(a, newParticipant);
                            break;
                        }
                    }
                }
                info.participants.version = version;
                TLRPC.ChatFull finalInfo = info;
                AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.chatInfoDidLoad, finalInfo, 0, false, false));
                SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_settings_v2 VALUES(?, ?, ?, ?, ?, ?)");
                NativeByteBuffer data = new NativeByteBuffer(info.getObjectSize());
                info.serializeToStream(data);
                state.bindLong(1, chatId);
                state.bindByteBuffer(2, data);
                state.bindInteger(3, info.pinned_msg_id);
                state.bindInteger(4, info.online_count);
                state.bindLong(5, info.inviterId);
                state.bindInteger(6, info.invitesCount);
                state.step();
                state.dispose();
                data.reuse();
            }
        } 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 53 with SQLiteCursor

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

the class MessagesStorage method getWallpapers.

public void getWallpapers() {
    storageQueue.postRunnable(() -> {
        SQLiteCursor cursor = null;
        try {
            cursor = database.queryFinalized("SELECT data FROM wallpapers2 WHERE 1 ORDER BY num ASC");
            ArrayList<TLRPC.WallPaper> wallPapers = new ArrayList<>();
            while (cursor.next()) {
                NativeByteBuffer data = cursor.byteBufferValue(0);
                if (data != null) {
                    TLRPC.WallPaper wallPaper = TLRPC.WallPaper.TLdeserialize(data, data.readInt32(false), false);
                    data.reuse();
                    if (wallPaper != null) {
                        wallPapers.add(wallPaper);
                    }
                }
            }
            AndroidUtilities.runOnUIThread(() -> NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.wallpapersDidLoad, wallPapers));
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (cursor != null) {
                cursor.dispose();
            }
        }
    });
}
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)

Example 54 with SQLiteCursor

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

the class MessagesStorage method deleteUserChatHistory.

public void deleteUserChatHistory(long dialogId, long fromId) {
    storageQueue.postRunnable(() -> {
        try {
            ArrayList<Integer> mids = new ArrayList<>();
            SQLiteCursor cursor = database.queryFinalized("SELECT data FROM messages_v2 WHERE uid = " + dialogId);
            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);
                        if (message != null) {
                            message.readAttachPath(data, getUserConfig().clientUserId);
                            if (UserObject.isReplyUser(dialogId) && MessageObject.getPeerId(message.fwd_from.from_id) == fromId || MessageObject.getFromChatId(message) == fromId && message.id != 1) {
                                mids.add(message.id);
                                addFilesToDelete(message, filesToDelete, idsToDelete, namesToDelete, false);
                            }
                        }
                        data.reuse();
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            cursor.dispose();
            deleteFromDownloadQueue(idsToDelete, true);
            AndroidUtilities.runOnUIThread(() -> {
                getFileLoader().cancelLoadFiles(namesToDelete);
                getMessagesController().markDialogMessageAsDeleted(dialogId, mids);
            });
            markMessagesAsDeletedInternal(dialogId, mids, false, false);
            updateDialogsWithDeletedMessagesInternal(dialogId, DialogObject.isChatDialog(dialogId) ? -dialogId : 0, mids, null);
            getFileLoader().deleteFiles(filesToDelete, 0);
            if (!mids.isEmpty()) {
                AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.messagesDeleted, mids, DialogObject.isChatDialog(dialogId) ? -dialogId : 0, false));
            }
        } 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) File(java.io.File) Pair(android.util.Pair)

Example 55 with SQLiteCursor

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

the class MessagesStorage method markMessagesAsDeletedInternal.

private ArrayList<Long> markMessagesAsDeletedInternal(long channelId, int mid, boolean deleteFiles) {
    try {
        String ids;
        ArrayList<Long> dialogsIds = new ArrayList<>();
        LongSparseArray<Integer[]> dialogsToUpdate = new LongSparseArray<>();
        ArrayList<File> filesToDelete = new ArrayList<>();
        ArrayList<String> namesToDelete = new ArrayList<>();
        ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
        long currentUser = getUserConfig().getClientUserId();
        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, out, mention FROM messages_v2 WHERE uid = %d AND mid <= %d", -channelId, mid));
        try {
            while (cursor.next()) {
                long did = cursor.longValue(0);
                if (did != currentUser) {
                    int read_state = cursor.intValue(2);
                    if (cursor.intValue(3) == 0) {
                        Integer[] unread_count = dialogsToUpdate.get(did);
                        if (unread_count == null) {
                            unread_count = new Integer[] { 0, 0 };
                            dialogsToUpdate.put(did, unread_count);
                        }
                        if (read_state < 2) {
                            unread_count[1]++;
                        }
                        if (read_state == 0 || read_state == 2) {
                            unread_count[0]++;
                        }
                    }
                }
                if (!DialogObject.isEncryptedDialog(did) && !deleteFiles) {
                    continue;
                }
                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();
                    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, 0);
        for (int a = 0; a < dialogsToUpdate.size(); a++) {
            long did = dialogsToUpdate.keyAt(a);
            Integer[] counts = dialogsToUpdate.valueAt(a);
            cursor = database.queryFinalized("SELECT unread_count, unread_count_i FROM dialogs WHERE did = " + did);
            int old_unread_count = 0;
            int old_mentions_count = 0;
            if (cursor.next()) {
                old_unread_count = cursor.intValue(0);
                old_mentions_count = cursor.intValue(1);
            }
            cursor.dispose();
            dialogsIds.add(did);
            SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET unread_count = ?, unread_count_i = ? WHERE did = ?");
            state.requery();
            state.bindInteger(1, Math.max(0, old_unread_count - counts[0]));
            state.bindInteger(2, Math.max(0, old_mentions_count - counts[1]));
            state.bindLong(3, did);
            state.step();
            state.dispose();
        }
        database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned <= %d", channelId, mid)).stepThis().dispose();
        database.executeFast(String.format(Locale.US, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid <= %d", channelId, mid)).stepThis().dispose();
        int updatedCount = 0;
        cursor = database.queryFinalized("SELECT changes()");
        if (cursor.next()) {
            updatedCount = cursor.intValue(0);
        }
        cursor.dispose();
        if (updatedCount > 0) {
            cursor = database.queryFinalized(String.format(Locale.US, "SELECT count FROM chat_pinned_count WHERE uid = %d", -channelId));
            if (cursor.next()) {
                int count = cursor.intValue(0);
                SQLitePreparedStatement state = database.executeFast("UPDATE chat_pinned_count SET count = ? WHERE uid = ?");
                state.requery();
                state.bindInteger(1, Math.max(0, count - updatedCount));
                state.bindLong(2, -channelId);
                state.step();
                state.dispose();
            }
            cursor.dispose();
        }
        database.executeFast(String.format(Locale.US, "DELETE FROM messages_v2 WHERE uid = %d AND mid <= %d", -channelId, mid)).stepThis().dispose();
        database.executeFast(String.format(Locale.US, "DELETE FROM media_v4 WHERE uid = %d AND mid <= %d", -channelId, mid)).stepThis().dispose();
        database.executeFast(String.format(Locale.US, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", -channelId)).stepThis().dispose();
        updateWidgets(dialogsIds);
        return dialogsIds;
    } catch (Exception e) {
        FileLog.e(e);
    }
    return null;
}
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) AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) Pair(android.util.Pair)

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