Search in sources :

Example 1 with SQLiteException

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

the class MessagesController method getNextReactionMention.

public void getNextReactionMention(long dialogId, int count, Consumer<Integer> callback) {
    final MessagesStorage messagesStorage = getMessagesStorage();
    messagesStorage.getStorageQueue().postRunnable(() -> {
        boolean needRequest = true;
        try {
            SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT message_id FROM reaction_mentions WHERE state = 1 AND dialog_id = %d LIMIT 1", dialogId));
            int messageId = 0;
            if (cursor.next()) {
                messageId = cursor.intValue(0);
                needRequest = false;
            }
            cursor.dispose();
            if (messageId != 0) {
                getMessagesStorage().markMessageReactionsAsRead(dialogId, messageId, false);
                int finalMessageId = messageId;
                AndroidUtilities.runOnUIThread(() -> callback.accept(finalMessageId));
            }
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        if (needRequest) {
            TLRPC.TL_messages_getUnreadReactions req = new TLRPC.TL_messages_getUnreadReactions();
            req.peer = getMessagesController().getInputPeer(dialogId);
            req.limit = 1;
            req.add_offset = count - 1;
            getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
                TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
                int messageId = 0;
                if (error != null && res != null && res.messages != null && !res.messages.isEmpty()) {
                    messageId = res.messages.get(0).id;
                }
                int finalMessageId = messageId;
                AndroidUtilities.runOnUIThread(() -> callback.accept(finalMessageId));
            }));
        }
    });
}
Also used : SQLiteException(org.telegram.SQLite.SQLiteException) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC)

Example 2 with SQLiteException

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

the class MessagesController method checkUnreadReactions.

public void checkUnreadReactions(long dialogId, SparseBooleanArray unreadReactions) {
    getMessagesStorage().getStorageQueue().postRunnable(() -> {
        boolean needReload = false;
        boolean changed = false;
        ArrayList<Integer> newUnreadMessages = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < unreadReactions.size(); i++) {
            int messageId = unreadReactions.keyAt(i);
            if (stringBuilder.length() > 0) {
                stringBuilder.append(", ");
            }
            stringBuilder.append(messageId);
        }
        SparseBooleanArray reactionsMentionsMessageIds = new SparseBooleanArray();
        try {
            SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT message_id, state FROM reaction_mentions WHERE message_id IN (%s) AND dialog_id = %d", stringBuilder.toString(), dialogId));
            while (cursor.next()) {
                int messageId = cursor.intValue(0);
                boolean hasUnreadReactions = cursor.intValue(1) == 1;
                reactionsMentionsMessageIds.put(messageId, hasUnreadReactions);
            }
            cursor.dispose();
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        int newUnreadCount = 0;
        for (int i = 0; i < unreadReactions.size(); i++) {
            int messageId = unreadReactions.keyAt(i);
            boolean hasUnreadReaction = unreadReactions.valueAt(i);
            if (reactionsMentionsMessageIds.indexOfKey(messageId) >= 0) {
                if (reactionsMentionsMessageIds.get(messageId) != hasUnreadReaction) {
                    newUnreadCount += hasUnreadReaction ? 1 : -1;
                    changed = true;
                }
            } else {
                needReload = true;
            }
            if (hasUnreadReaction) {
                newUnreadMessages.add(messageId);
            }
            SQLitePreparedStatement state = null;
            try {
                state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO reaction_mentions VALUES(?, ?, ?)");
                state.requery();
                state.bindInteger(1, messageId);
                state.bindInteger(2, hasUnreadReaction ? 1 : 0);
                state.bindLong(3, dialogId);
                state.step();
                state.dispose();
            } catch (SQLiteException e) {
                e.printStackTrace();
            }
        }
        if (needReload) {
            TLRPC.TL_messages_getUnreadReactions req = new TLRPC.TL_messages_getUnreadReactions();
            req.limit = 1;
            req.peer = getInputPeer(dialogId);
            ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, error) -> {
                if (response != null) {
                    TLRPC.messages_Messages messages = (TLRPC.messages_Messages) response;
                    int count = Math.max(messages.count, messages.messages.size());
                    AndroidUtilities.runOnUIThread(() -> {
                        TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
                        if (dialog == null) {
                            getMessagesStorage().updateDialogUnreadReactions(dialogId, count, false);
                            return;
                        }
                        dialog.unread_reactions_count = count;
                        getMessagesStorage().updateUnreadReactionsCount(dialogId, count);
                        getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadReactionsCounterChanged, dialogId, count, newUnreadMessages);
                    });
                }
            });
        } else if (changed) {
            int finalNewUnreadCount = newUnreadCount;
            AndroidUtilities.runOnUIThread(() -> {
                TLRPC.Dialog dialog = dialogs_dict.get(dialogId);
                if (dialog == null) {
                    getMessagesStorage().updateDialogUnreadReactions(dialogId, finalNewUnreadCount, true);
                    return;
                }
                dialog.unread_reactions_count += finalNewUnreadCount;
                if (dialog.unread_reactions_count < 0) {
                    dialog.unread_reactions_count = 0;
                }
                getMessagesStorage().updateUnreadReactionsCount(dialogId, dialog.unread_reactions_count);
                getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadReactionsCounterChanged, dialogId, dialog.unread_reactions_count, newUnreadMessages);
            });
        }
    });
}
Also used : ArrayList(java.util.ArrayList) SQLiteException(org.telegram.SQLite.SQLiteException) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) TLRPC(org.telegram.tgnet.TLRPC) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement) AlertDialog(org.telegram.ui.ActionBar.AlertDialog) SparseBooleanArray(android.util.SparseBooleanArray)

Example 3 with SQLiteException

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

the class MessagesStorage method updateUnreadReactionsCount.

public void updateUnreadReactionsCount(long dialogId, int count) {
    storageQueue.postRunnable(() -> {
        try {
            SQLitePreparedStatement state = database.executeFast("UPDATE dialogs SET unread_reactions = ? WHERE did = ?");
            state.bindInteger(1, count);
            state.bindLong(2, dialogId);
            state.step();
            state.dispose();
            if (count == 0) {
                state = database.executeFast("UPDATE reaction_mentions SET state = 0 WHERE dialog_id ?");
                state.bindLong(1, dialogId);
                state.step();
                state.dispose();
            }
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
    });
}
Also used : SQLiteException(org.telegram.SQLite.SQLiteException) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Example 4 with SQLiteException

use of org.telegram.SQLite.SQLiteException 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 5 with SQLiteException

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

the class MessagesStorage method updateDialogUnreadReactions.

public void updateDialogUnreadReactions(long dialogId, int newUnreadCount, boolean increment) {
    storageQueue.postRunnable(() -> {
        try {
            int oldUnreadRactions = 0;
            if (increment) {
                SQLiteCursor cursor = database.queryFinalized("SELECT unread_reactions FROM dialogs WHERE did = " + dialogId);
                if (cursor.next()) {
                    oldUnreadRactions = Math.max(0, cursor.intValue(0));
                }
                cursor.dispose();
            }
            oldUnreadRactions += newUnreadCount;
            SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("UPDATE dialogs SET unread_reactions = ? WHERE did = ?");
            state.bindInteger(1, oldUnreadRactions);
            state.bindLong(2, dialogId);
            state.step();
            state.dispose();
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
    });
}
Also used : SQLiteException(org.telegram.SQLite.SQLiteException) SQLiteCursor(org.telegram.SQLite.SQLiteCursor) SQLitePreparedStatement(org.telegram.SQLite.SQLitePreparedStatement)

Aggregations

SQLiteException (org.telegram.SQLite.SQLiteException)5 SQLiteCursor (org.telegram.SQLite.SQLiteCursor)4 SQLitePreparedStatement (org.telegram.SQLite.SQLitePreparedStatement)4 TLRPC (org.telegram.tgnet.TLRPC)3 SparseBooleanArray (android.util.SparseBooleanArray)1 ArrayList (java.util.ArrayList)1 NativeByteBuffer (org.telegram.tgnet.NativeByteBuffer)1 AlertDialog (org.telegram.ui.ActionBar.AlertDialog)1