use of org.telegram.SQLite.SQLitePreparedStatement 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);
});
}
});
}
use of org.telegram.SQLite.SQLitePreparedStatement in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method updateBotInfo.
public void updateBotInfo(long dialogId, TLRPC.TL_updateBotCommands update) {
TLRPC.BotInfo botInfo = botInfos.get(update.bot_id + "_" + dialogId);
if (botInfo != null) {
botInfo.commands = update.commands;
getNotificationCenter().postNotificationName(NotificationCenter.botInfoDidLoad, botInfo, 0);
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
TLRPC.BotInfo info = loadBotInfoInternal(update.bot_id, dialogId);
if (info != null) {
info.commands = update.commands;
}
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO bot_info_v2 VALUES(?, ?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(info.getObjectSize());
info.serializeToStream(data);
state.bindLong(1, info.user_id);
state.bindLong(2, dialogId);
state.bindByteBuffer(3, data);
state.step();
data.reuse();
state.dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLitePreparedStatement in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method putDiceStickersToCache.
private void putDiceStickersToCache(String emoji, TLRPC.TL_messages_stickerSet stickers, int date) {
if (TextUtils.isEmpty(emoji)) {
return;
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
if (stickers != null) {
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO stickers_dice VALUES(?, ?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(stickers.getObjectSize());
stickers.serializeToStream(data);
state.bindString(1, emoji);
state.bindByteBuffer(2, data);
state.bindInteger(3, date);
state.step();
data.reuse();
state.dispose();
} else {
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("UPDATE stickers_dice SET date = ?");
state.requery();
state.bindInteger(1, date);
state.step();
state.dispose();
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLitePreparedStatement in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method putBotInfo.
public void putBotInfo(long dialogId, TLRPC.BotInfo botInfo) {
if (botInfo == null) {
return;
}
botInfos.put(botInfo.user_id + "_" + dialogId, botInfo);
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO bot_info_v2 VALUES(?, ?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(botInfo.getObjectSize());
botInfo.serializeToStream(data);
state.bindLong(1, botInfo.user_id);
state.bindLong(2, dialogId);
state.bindByteBuffer(3, data);
state.step();
data.reuse();
state.dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.SQLite.SQLitePreparedStatement in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadMediaDatabase.
private void loadMediaDatabase(long uid, int count, int max_id, int min_id, int type, int classGuid, boolean isChannel, int fromCache, int requestIndex) {
Runnable runnable = new Runnable() {
@Override
public void run() {
boolean topReached = false;
TLRPC.TL_messages_messages res = new TLRPC.TL_messages_messages();
try {
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
int countToLoad = count + 1;
SQLiteCursor cursor;
SQLiteDatabase database = getMessagesStorage().getDatabase();
boolean isEnd = false;
boolean reverseMessages = false;
if (!DialogObject.isEncryptedDialog(uid)) {
if (min_id == 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start FROM media_holes_v2 WHERE uid = %d AND type = %d AND start IN (0, 1)", uid, type));
if (cursor.next()) {
isEnd = cursor.intValue(0) == 1;
} else {
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT min(mid) FROM media_v4 WHERE uid = %d AND type = %d AND mid > 0", uid, type));
if (cursor.next()) {
int mid = cursor.intValue(0);
if (mid != 0) {
SQLitePreparedStatement state = database.executeFast("REPLACE INTO media_holes_v2 VALUES(?, ?, ?, ?)");
state.requery();
state.bindLong(1, uid);
state.bindInteger(2, type);
state.bindInteger(3, 0);
state.bindInteger(4, mid);
state.step();
state.dispose();
}
}
}
cursor.dispose();
}
int holeMessageId = 0;
if (max_id != 0) {
int startHole = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start, end FROM media_holes_v2 WHERE uid = %d AND type = %d AND start <= %d ORDER BY end DESC LIMIT 1", uid, type, max_id));
if (cursor.next()) {
startHole = cursor.intValue(0);
holeMessageId = cursor.intValue(1);
}
cursor.dispose();
if (holeMessageId > 1) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > 0 AND mid < %d AND mid >= %d AND type = %d ORDER BY date DESC, mid DESC LIMIT %d", uid, max_id, holeMessageId, type, countToLoad));
isEnd = false;
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > 0 AND mid < %d AND type = %d ORDER BY date DESC, mid DESC LIMIT %d", uid, max_id, type, countToLoad));
}
} else if (min_id != 0) {
int startHole = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT start, end FROM media_holes_v2 WHERE uid = %d AND type = %d AND end >= %d ORDER BY end ASC LIMIT 1", uid, type, min_id));
if (cursor.next()) {
startHole = cursor.intValue(0);
holeMessageId = cursor.intValue(1);
}
cursor.dispose();
reverseMessages = true;
if (startHole > 1) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > 0 AND mid >= %d AND mid <= %d AND type = %d ORDER BY date ASC, mid ASC LIMIT %d", uid, min_id, startHole, type, countToLoad));
} else {
isEnd = true;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > 0 AND mid >= %d AND type = %d ORDER BY date ASC, mid ASC LIMIT %d", uid, min_id, type, countToLoad));
}
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT max(end) FROM media_holes_v2 WHERE uid = %d AND type = %d", uid, type));
if (cursor.next()) {
holeMessageId = cursor.intValue(0);
}
cursor.dispose();
if (holeMessageId > 1) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid >= %d AND type = %d ORDER BY date DESC, mid DESC LIMIT %d", uid, holeMessageId, type, countToLoad));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, mid FROM media_v4 WHERE uid = %d AND mid > 0 AND type = %d ORDER BY date DESC, mid DESC LIMIT %d", uid, type, countToLoad));
}
}
} else {
isEnd = true;
if (max_id != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT m.data, m.mid, r.random_id FROM media_v4 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid WHERE m.uid = %d AND m.mid > %d AND type = %d ORDER BY m.mid ASC LIMIT %d", uid, max_id, type, countToLoad));
} else if (min_id != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT m.data, m.mid, r.random_id FROM media_v4 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid WHERE m.uid = %d AND m.mid < %d AND type = %d ORDER BY m.mid DESC LIMIT %d", uid, min_id, type, countToLoad));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT m.data, m.mid, r.random_id FROM media_v4 as m LEFT JOIN randoms_v2 as r ON r.mid = m.mid WHERE m.uid = %d AND type = %d ORDER BY m.mid ASC LIMIT %d", uid, type, countToLoad));
}
}
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
message.readAttachPath(data, getUserConfig().clientUserId);
data.reuse();
message.id = cursor.intValue(1);
message.dialog_id = uid;
if (DialogObject.isEncryptedDialog(uid)) {
message.random_id = cursor.longValue(2);
}
if (reverseMessages) {
res.messages.add(0, message);
} else {
res.messages.add(message);
}
MessagesStorage.addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad);
}
}
cursor.dispose();
if (!usersToLoad.isEmpty()) {
getMessagesStorage().getUsersInternal(TextUtils.join(",", usersToLoad), res.users);
}
if (!chatsToLoad.isEmpty()) {
getMessagesStorage().getChatsInternal(TextUtils.join(",", chatsToLoad), res.chats);
}
if (res.messages.size() > count && min_id == 0) {
res.messages.remove(res.messages.size() - 1);
} else {
if (min_id != 0) {
topReached = false;
} else {
topReached = isEnd;
}
}
} catch (Exception e) {
res.messages.clear();
res.chats.clear();
res.users.clear();
FileLog.e(e);
} finally {
Runnable task = this;
AndroidUtilities.runOnUIThread(() -> getMessagesStorage().completeTaskForGuid(task, classGuid));
processLoadedMedia(res, uid, count, max_id, min_id, type, fromCache, classGuid, isChannel, topReached, requestIndex);
}
}
};
MessagesStorage messagesStorage = getMessagesStorage();
messagesStorage.getStorageQueue().postRunnable(runnable);
messagesStorage.bindTaskToGuid(runnable, classGuid);
}
Aggregations