Search in sources :

Example 81 with SQLiteCursor

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

the class MessagesStorage method getChannelPtsSync.

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

Example 82 with SQLiteCursor

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

the class MessagesStorage method updateRepliesMaxReadIdInternal.

private void updateRepliesMaxReadIdInternal(long chatId, int mid, int readMaxId) {
    try {
        long dialogId = -chatId;
        SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
        TLRPC.MessageReplies currentReplies = null;
        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", mid, dialogId));
        if (cursor.next()) {
            NativeByteBuffer data = cursor.byteBufferValue(0);
            if (data != null) {
                currentReplies = TLRPC.MessageReplies.TLdeserialize(data, data.readInt32(false), false);
                data.reuse();
            }
        }
        cursor.dispose();
        if (currentReplies != null) {
            currentReplies.read_max_id = readMaxId;
            state.requery();
            NativeByteBuffer data = new NativeByteBuffer(currentReplies.getObjectSize());
            currentReplies.serializeToStream(data);
            state.bindByteBuffer(1, data);
            state.bindInteger(2, mid);
            state.bindLong(3, dialogId);
            state.step();
            data.reuse();
        }
        state.dispose();
    } catch (Exception e) {
        FileLog.e(e);
    }
}
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 83 with SQLiteCursor

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

the class MessagesStorage method processPendingRead.

public void processPendingRead(long dialogId, int maxPositiveId, int maxNegativeId, int scheduledCount) {
    int maxDate = lastSavedDate;
    storageQueue.postRunnable(() -> {
        try {
            int currentMaxId = 0;
            int unreadCount = 0;
            long last_mid = 0;
            int prevUnreadCount = 0;
            SQLiteCursor cursor = database.queryFinalized("SELECT unread_count, inbox_max, last_mid FROM dialogs WHERE did = " + dialogId);
            if (cursor.next()) {
                prevUnreadCount = unreadCount = cursor.intValue(0);
                currentMaxId = cursor.intValue(1);
                last_mid = cursor.longValue(2);
            }
            cursor.dispose();
            database.beginTransaction();
            SQLitePreparedStatement state;
            if (!DialogObject.isEncryptedDialog(dialogId)) {
                currentMaxId = Math.max(currentMaxId, maxPositiveId);
                state = database.executeFast("UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = ? AND mid <= ? AND read_state IN(0,2) AND out = 0");
                state.requery();
                state.bindLong(1, dialogId);
                state.bindInteger(2, currentMaxId);
                state.step();
                state.dispose();
                if (currentMaxId >= last_mid) {
                    unreadCount = 0;
                } else {
                    int updatedCount = 0;
                    cursor = database.queryFinalized("SELECT changes()");
                    if (cursor.next()) {
                        updatedCount = cursor.intValue(0) + scheduledCount;
                    }
                    cursor.dispose();
                    unreadCount = Math.max(0, unreadCount - updatedCount);
                }
                state = database.executeFast("DELETE FROM unread_push_messages WHERE uid = ? AND mid <= ?");
                state.requery();
                state.bindLong(1, dialogId);
                state.bindInteger(2, currentMaxId);
                state.step();
                state.dispose();
                state = database.executeFast("DELETE FROM unread_push_messages WHERE uid = ? AND date <= ?");
                state.requery();
                state.bindLong(1, dialogId);
                state.bindInteger(2, maxDate);
                state.step();
                state.dispose();
            } else {
                currentMaxId = maxNegativeId;
                state = database.executeFast("UPDATE messages_v2 SET read_state = read_state | 1 WHERE uid = ? AND mid >= ? AND read_state IN(0,2) AND out = 0");
                state.requery();
                state.bindLong(1, dialogId);
                state.bindInteger(2, currentMaxId);
                state.step();
                state.dispose();
                if (currentMaxId <= last_mid) {
                    unreadCount = 0;
                } else {
                    int updatedCount = 0;
                    cursor = database.queryFinalized("SELECT changes()");
                    if (cursor.next()) {
                        updatedCount = cursor.intValue(0) + scheduledCount;
                    }
                    cursor.dispose();
                    unreadCount = Math.max(0, unreadCount - updatedCount);
                }
            }
            state = database.executeFast("UPDATE dialogs SET unread_count = ?, inbox_max = ? WHERE did = ?");
            state.requery();
            state.bindInteger(1, unreadCount);
            state.bindInteger(2, currentMaxId);
            state.bindLong(3, dialogId);
            state.step();
            state.dispose();
            database.commitTransaction();
            if (prevUnreadCount != 0 && unreadCount == 0) {
                LongSparseIntArray dialogsToUpdate = new LongSparseIntArray();
                dialogsToUpdate.put(dialogId, unreadCount);
                updateFiltersReadCounter(dialogsToUpdate, null, true);
            }
            updateWidgets(dialogId);
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : LongSparseIntArray(org.telegram.messenger.support.LongSparseIntArray) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 84 with SQLiteCursor

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

the class MessagesStorage method updateChatInfo.

public void updateChatInfo(TLRPC.ChatFull info, boolean ifExist) {
    storageQueue.postRunnable(() -> {
        try {
            int currentOnline = -1;
            int inviter = 0;
            int links = 0;
            SQLiteCursor cursor = database.queryFinalized("SELECT online, inviter, links FROM chat_settings_v2 WHERE uid = " + info.id);
            if (cursor.next()) {
                currentOnline = cursor.intValue(0);
                info.inviterId = cursor.longValue(1);
                links = cursor.intValue(2);
            }
            cursor.dispose();
            if (ifExist && currentOnline == -1) {
                return;
            }
            if (currentOnline >= 0 && (info.flags & 8192) == 0) {
                info.online_count = currentOnline;
            }
            if (links >= 0) {
                info.invitesCount = links;
            }
            SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_settings_v2 VALUES(?, ?, ?, ?, ?, ?)");
            NativeByteBuffer data = new NativeByteBuffer(info.getObjectSize());
            info.serializeToStream(data);
            state.bindLong(1, info.id);
            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();
            if (info instanceof TLRPC.TL_channelFull) {
                cursor = database.queryFinalized("SELECT inbox_max, outbox_max FROM dialogs WHERE did = " + (-info.id));
                if (cursor.next()) {
                    int inbox_max = cursor.intValue(0);
                    if (inbox_max < info.read_inbox_max_id) {
                        int outbox_max = cursor.intValue(1);
                        state = database.executeFast("UPDATE dialogs SET unread_count = ?, inbox_max = ?, outbox_max = ? WHERE did = ?");
                        state.bindInteger(1, info.unread_count);
                        state.bindInteger(2, info.read_inbox_max_id);
                        state.bindInteger(3, Math.max(outbox_max, info.read_outbox_max_id));
                        state.bindLong(4, -info.id);
                        state.step();
                        state.dispose();
                    }
                }
                cursor.dispose();
            }
            if ((info.flags & 2048) != 0) {
                state = database.executeFast("UPDATE dialogs SET folder_id = ? WHERE did = ?");
                state.bindInteger(1, info.folder_id);
                state.bindLong(2, -info.id);
                state.step();
                state.dispose();
                unknownDialogsIds.remove(-info.id);
            }
        } 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 85 with SQLiteCursor

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

the class MessagesStorage method putChannelViews.

public void putChannelViews(LongSparseArray<SparseIntArray> channelViews, LongSparseArray<SparseIntArray> channelForwards, LongSparseArray<SparseArray<TLRPC.MessageReplies>> channelReplies, boolean addReply) {
    if (isEmpty(channelViews) && isEmpty(channelForwards) && isEmpty(channelReplies)) {
        return;
    }
    storageQueue.postRunnable(() -> {
        try {
            database.beginTransaction();
            if (!isEmpty(channelViews)) {
                SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET media = max((SELECT media FROM messages_v2 WHERE mid = ? AND uid = ?), ?) WHERE mid = ? AND uid = ?");
                for (int a = 0; a < channelViews.size(); a++) {
                    long peer = channelViews.keyAt(a);
                    SparseIntArray messages = channelViews.valueAt(a);
                    for (int b = 0, N = messages.size(); b < N; b++) {
                        int views = messages.valueAt(b);
                        int messageId = messages.keyAt(b);
                        state.requery();
                        state.bindInteger(1, messageId);
                        state.bindLong(2, peer);
                        state.bindInteger(3, views);
                        state.bindInteger(4, messageId);
                        state.bindLong(5, peer);
                        state.step();
                    }
                }
                state.dispose();
            }
            if (!isEmpty(channelForwards)) {
                SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET forwards = max((SELECT forwards FROM messages_v2 WHERE mid = ? AND uid = ?), ?) WHERE mid = ? AND uid = ?");
                for (int a = 0; a < channelForwards.size(); a++) {
                    long peer = channelForwards.keyAt(a);
                    SparseIntArray messages = channelForwards.valueAt(a);
                    for (int b = 0, N = messages.size(); b < N; b++) {
                        int forwards = messages.valueAt(b);
                        int messageId = messages.keyAt(b);
                        state.requery();
                        state.bindInteger(1, messageId);
                        state.bindLong(2, peer);
                        state.bindInteger(3, forwards);
                        state.bindInteger(4, messageId);
                        state.bindLong(5, peer);
                        state.step();
                    }
                }
                state.dispose();
            }
            if (!isEmpty(channelReplies)) {
                SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
                for (int a = 0; a < channelReplies.size(); a++) {
                    long peer = channelReplies.keyAt(a);
                    SparseArray<TLRPC.MessageReplies> messages = channelReplies.valueAt(a);
                    for (int b = 0, N3 = messages.size(); b < N3; b++) {
                        int messageId = messages.keyAt(b);
                        boolean messageExists;
                        TLRPC.MessageReplies currentReplies = null;
                        SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", messageId, peer));
                        if (messageExists = cursor.next()) {
                            NativeByteBuffer data = cursor.byteBufferValue(0);
                            if (data != null) {
                                currentReplies = TLRPC.MessageReplies.TLdeserialize(data, data.readInt32(false), false);
                                data.reuse();
                            }
                        }
                        cursor.dispose();
                        if (!messageExists) {
                            continue;
                        }
                        TLRPC.MessageReplies replies = messages.get(messages.keyAt(b));
                        if (!addReply && currentReplies != null && currentReplies.replies_pts != 0 && replies.replies_pts <= currentReplies.replies_pts && replies.read_max_id <= currentReplies.read_max_id && replies.max_id <= currentReplies.max_id) {
                            continue;
                        }
                        if (addReply) {
                            if (currentReplies == null) {
                                currentReplies = new TLRPC.TL_messageReplies();
                                currentReplies.flags |= 2;
                            }
                            currentReplies.replies += replies.replies;
                            for (int c = 0, N = replies.recent_repliers.size(); c < N; c++) {
                                long id = MessageObject.getPeerId(replies.recent_repliers.get(c));
                                for (int d = 0, N2 = currentReplies.recent_repliers.size(); d < N2; d++) {
                                    long id2 = MessageObject.getPeerId(currentReplies.recent_repliers.get(d));
                                    if (id == id2) {
                                        currentReplies.recent_repliers.remove(d);
                                        d--;
                                        N2--;
                                    }
                                }
                            }
                            currentReplies.recent_repliers.addAll(0, replies.recent_repliers);
                            while (currentReplies.recent_repliers.size() > 3) {
                                currentReplies.recent_repliers.remove(0);
                            }
                            replies = currentReplies;
                        }
                        if (currentReplies != null && currentReplies.read_max_id > replies.read_max_id) {
                            replies.read_max_id = currentReplies.read_max_id;
                        }
                        state.requery();
                        NativeByteBuffer data = new NativeByteBuffer(replies.getObjectSize());
                        replies.serializeToStream(data);
                        state.bindByteBuffer(1, data);
                        state.bindInteger(2, messageId);
                        state.bindLong(3, peer);
                        state.step();
                        data.reuse();
                    }
                }
                state.dispose();
            }
            database.commitTransaction();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
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) SparseIntArray(android.util.SparseIntArray) LongSparseIntArray(org.telegram.messenger.support.LongSparseIntArray)

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