Search in sources :

Example 86 with SQLiteCursor

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

the class MessagesStorage method updateDialogsWithReadMessagesInternal.

private void updateDialogsWithReadMessagesInternal(ArrayList<Integer> messages, LongSparseIntArray inbox, LongSparseIntArray outbox, LongSparseArray<ArrayList<Integer>> mentions) {
    try {
        LongSparseIntArray dialogsToUpdate = new LongSparseIntArray();
        LongSparseIntArray dialogsToUpdateMentions = new LongSparseIntArray();
        ArrayList<Long> channelMentionsToReload = new ArrayList<>();
        if (!isEmpty(messages)) {
            String ids = TextUtils.join(",", messages);
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, read_state, out FROM messages_v2 WHERE mid IN(%s) AND is_channel = 0", ids));
            while (cursor.next()) {
                int out = cursor.intValue(2);
                if (out != 0) {
                    continue;
                }
                int read_state = cursor.intValue(1);
                if (read_state != 0) {
                    continue;
                }
                long uid = cursor.longValue(0);
                int currentCount = dialogsToUpdate.get(uid);
                if (currentCount == 0) {
                    dialogsToUpdate.put(uid, 1);
                } else {
                    dialogsToUpdate.put(uid, currentCount + 1);
                }
            }
            cursor.dispose();
        } else {
            if (!isEmpty(inbox)) {
                for (int b = 0; b < inbox.size(); b++) {
                    long key = inbox.keyAt(b);
                    int messageId = inbox.get(key);
                    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM messages_v2 WHERE uid = %d AND mid > %d AND read_state IN(0,2) AND out = 0", key, messageId));
                    if (cursor.next()) {
                        dialogsToUpdate.put(key, cursor.intValue(0));
                    }
                    cursor.dispose();
                    SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET inbox_max = max((SELECT inbox_max FROM dialogs WHERE did = ?), ?) WHERE did = ?");
                    state.requery();
                    state.bindLong(1, key);
                    state.bindInteger(2, messageId);
                    state.bindLong(3, key);
                    state.step();
                    state.dispose();
                }
            }
            if (!isEmpty(mentions)) {
                for (int b = 0, N = mentions.size(); b < N; b++) {
                    ArrayList<Integer> arrayList = mentions.valueAt(b);
                    ArrayList<Integer> notFoundMentions = new ArrayList<>(arrayList);
                    String ids = TextUtils.join(",", arrayList);
                    long channelId = 0;
                    SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, read_state, out, mention, mid, is_channel FROM messages_v2 WHERE mid IN(%s)", ids));
                    while (cursor.next()) {
                        long did = cursor.longValue(0);
                        notFoundMentions.remove((Integer) cursor.intValue(4));
                        if (cursor.intValue(1) < 2 && cursor.intValue(2) == 0 && cursor.intValue(3) == 1) {
                            int unread_count = dialogsToUpdateMentions.get(did, -1);
                            if (unread_count < 0) {
                                SQLiteCursor cursor2 = database.queryFinalized("SELECT unread_count_i FROM dialogs WHERE did = " + did);
                                int old_mentions_count = 0;
                                if (cursor2.next()) {
                                    old_mentions_count = cursor2.intValue(0);
                                }
                                cursor2.dispose();
                                dialogsToUpdateMentions.put(did, Math.max(0, old_mentions_count - 1));
                            } else {
                                dialogsToUpdateMentions.put(did, Math.max(0, unread_count - 1));
                            }
                        }
                        channelId = cursor.longValue(5);
                    }
                    cursor.dispose();
                    if (!notFoundMentions.isEmpty() && channelId != 0) {
                        if (!channelMentionsToReload.contains(channelId)) {
                            channelMentionsToReload.add(channelId);
                        }
                    }
                }
            }
            if (!isEmpty(outbox)) {
                for (int b = 0; b < outbox.size(); b++) {
                    long key = outbox.keyAt(b);
                    int messageId = outbox.get(key);
                    SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET outbox_max = max((SELECT outbox_max FROM dialogs WHERE did = ?), ?) WHERE did = ?");
                    state.requery();
                    state.bindLong(1, key);
                    state.bindInteger(2, messageId);
                    state.bindLong(3, key);
                    state.step();
                    state.dispose();
                }
            }
        }
        if (dialogsToUpdate.size() > 0 || dialogsToUpdateMentions.size() > 0) {
            database.beginTransaction();
            if (dialogsToUpdate.size() > 0) {
                ArrayList<Long> dids = new ArrayList<>();
                SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET unread_count = ? WHERE did = ?");
                for (int a = 0; a < dialogsToUpdate.size(); a++) {
                    long did = dialogsToUpdate.keyAt(a);
                    int prevUnreadCount = 0;
                    int newCount = dialogsToUpdate.valueAt(a);
                    SQLiteCursor cursor = database.queryFinalized("SELECT unread_count FROM dialogs WHERE did = " + did);
                    if (cursor.next()) {
                        prevUnreadCount = cursor.intValue(0);
                    }
                    cursor.dispose();
                    if (prevUnreadCount == newCount) {
                        dialogsToUpdate.removeAt(a);
                        a--;
                        continue;
                    }
                    state.requery();
                    state.bindInteger(1, newCount);
                    state.bindLong(2, did);
                    state.step();
                    dids.add(did);
                }
                state.dispose();
                updateWidgets(dids);
            }
            if (dialogsToUpdateMentions.size() > 0) {
                SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET unread_count_i = ? WHERE did = ?");
                for (int a = 0; a < dialogsToUpdateMentions.size(); a++) {
                    state.requery();
                    state.bindInteger(1, dialogsToUpdateMentions.valueAt(a));
                    state.bindLong(2, dialogsToUpdateMentions.keyAt(a));
                    state.step();
                }
                state.dispose();
            }
            database.commitTransaction();
        }
        updateFiltersReadCounter(dialogsToUpdate, dialogsToUpdateMentions, true);
        getMessagesController().processDialogsUpdateRead(dialogsToUpdate, dialogsToUpdateMentions);
        if (!channelMentionsToReload.isEmpty()) {
            getMessagesController().reloadMentionsCountForChannels(channelMentionsToReload);
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) LongSparseIntArray(org.telegram.messenger.support.LongSparseIntArray) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 87 with SQLiteCursor

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

the class MessagesStorage method getSentFile.

public Object[] getSentFile(String path, int type) {
    if (path == null || path.toLowerCase().endsWith("attheme")) {
        return null;
    }
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Object[] result = new Object[2];
    storageQueue.postRunnable(() -> {
        try {
            String id = Utilities.MD5(path);
            if (id != null) {
                SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, parent FROM sent_files_v2 WHERE uid = '%s' AND type = %d", id, type));
                if (cursor.next()) {
                    NativeByteBuffer data = cursor.byteBufferValue(0);
                    if (data != null) {
                        TLObject file = TLRPC.MessageMedia.TLdeserialize(data, data.readInt32(false), false);
                        data.reuse();
                        if (file instanceof TLRPC.TL_messageMediaDocument) {
                            result[0] = ((TLRPC.TL_messageMediaDocument) file).document;
                        } else if (file instanceof TLRPC.TL_messageMediaPhoto) {
                            result[0] = ((TLRPC.TL_messageMediaPhoto) file).photo;
                        }
                        if (result[0] != null) {
                            result[1] = cursor.stringValue(1);
                        }
                    }
                }
                cursor.dispose();
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (Exception e) {
        FileLog.e(e);
    }
    return result[0] != null ? result : null;
}
Also used : TLObject(org.telegram.tgnet.TLObject) TLObject(org.telegram.tgnet.TLObject) 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 88 with SQLiteCursor

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

the class MessagesStorage method markMessageReactionsAsReadInternal.

public void markMessageReactionsAsReadInternal(long dialogId, int messageId) {
    try {
        SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("UPDATE reaction_mentions SET state = 0 WHERE message_id = ? AND dialog_id = ?");
        state.bindInteger(1, messageId);
        state.bindLong(2, dialogId);
        state.step();
        state.dispose();
        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM messages_v2 WHERE uid = %d AND mid = %d", dialogId, messageId));
        TLRPC.Message message = null;
        if (cursor.next()) {
            NativeByteBuffer data = cursor.byteBufferValue(0);
            if (data != null) {
                message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
                message.readAttachPath(data, getUserConfig().clientUserId);
                data.reuse();
                if (message.reactions != null && message.reactions.recent_reactions != null) {
                    for (int i = 0; i < message.reactions.recent_reactions.size(); i++) {
                        message.reactions.recent_reactions.get(i).unread = false;
                    }
                }
            }
        }
        cursor.dispose();
        if (message != null) {
            state = getMessagesStorage().getDatabase().executeFast(String.format(Locale.US, "UPDATE messages_v2 SET data = ? WHERE uid = %d AND mid = %d", dialogId, messageId));
            try {
                NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
                message.serializeToStream(data);
                state.bindByteBuffer(1, data);
                state.step();
                state.dispose();
                data.reuse();
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    } catch (SQLiteException e) {
        FileLog.e(e);
    }
}
Also used : NativeByteBuffer(org.telegram.tgnet.NativeByteBuffer) SQLiteException(org.telegram.SQLite.SQLiteException) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 89 with SQLiteCursor

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

the class MessagesStorage method getDialogReadMax.

public int getDialogReadMax(boolean outbox, long dialog_id) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Integer[] max = new Integer[] { 0 };
    storageQueue.postRunnable(() -> {
        SQLiteCursor cursor = null;
        try {
            if (outbox) {
                cursor = database.queryFinalized("SELECT outbox_max FROM dialogs WHERE did = " + dialog_id);
            } else {
                cursor = database.queryFinalized("SELECT inbox_max FROM dialogs WHERE did = " + dialog_id);
            }
            if (cursor.next()) {
                max[0] = cursor.intValue(0);
            }
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            if (cursor != null) {
                cursor.dispose();
            }
        }
        countDownLatch.countDown();
    });
    try {
        countDownLatch.await();
    } catch (Exception e) {
        FileLog.e(e);
    }
    return max[0];
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException)

Example 90 with SQLiteCursor

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

the class MessagesStorage method markMessagesAsDeletedByRandoms.

public void markMessagesAsDeletedByRandoms(ArrayList<Long> messages) {
    if (messages.isEmpty()) {
        return;
    }
    storageQueue.postRunnable(() -> {
        try {
            String ids = TextUtils.join(",", messages);
            SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid, uid FROM randoms_v2 WHERE random_id IN(%s)", ids));
            LongSparseArray<ArrayList<Integer>> dialogs = new LongSparseArray<>();
            while (cursor.next()) {
                long dialogId = cursor.longValue(1);
                ArrayList<Integer> mids = dialogs.get(dialogId);
                if (mids == null) {
                    mids = new ArrayList<>();
                    dialogs.put(dialogId, mids);
                }
                mids.add(cursor.intValue(0));
            }
            cursor.dispose();
            if (!dialogs.isEmpty()) {
                for (int a = 0, N = dialogs.size(); a < N; a++) {
                    long dialogId = dialogs.keyAt(a);
                    ArrayList<Integer> mids = dialogs.valueAt(a);
                    AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.messagesDeleted, mids, 0L, false));
                    updateDialogsWithReadMessagesInternal(mids, null, null, null);
                    markMessagesAsDeletedInternal(dialogId, mids, true, false);
                    updateDialogsWithDeletedMessagesInternal(dialogId, 0, mids, null);
                }
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) 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