use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method putPushMessage.
public void putPushMessage(MessageObject message) {
storageQueue.postRunnable(() -> {
try {
NativeByteBuffer data = new NativeByteBuffer(message.messageOwner.getObjectSize());
message.messageOwner.serializeToStream(data);
int flags = 0;
if (message.localType == 2) {
flags |= 1;
}
if (message.localChannel) {
flags |= 2;
}
SQLitePreparedStatement state = database.executeFast("REPLACE INTO unread_push_messages VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)");
state.requery();
state.bindLong(1, message.getDialogId());
state.bindInteger(2, message.getId());
state.bindLong(3, message.messageOwner.random_id);
state.bindInteger(4, message.messageOwner.date);
state.bindByteBuffer(5, data);
if (message.messageText == null) {
state.bindNull(6);
} else {
state.bindString(6, message.messageText.toString());
}
if (message.localName == null) {
state.bindNull(7);
} else {
state.bindString(7, message.localName);
}
if (message.localUserName == null) {
state.bindNull(8);
} else {
state.bindString(8, message.localUserName);
}
state.bindInteger(9, flags);
state.step();
data.reuse();
state.dispose();
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method putSentFile.
public void putSentFile(String path, TLObject file, int type, String parent) {
if (path == null || file == null || parent == null) {
return;
}
storageQueue.postRunnable(() -> {
SQLitePreparedStatement state = null;
try {
String id = Utilities.MD5(path);
if (id != null) {
TLRPC.MessageMedia messageMedia = null;
if (file instanceof TLRPC.Photo) {
messageMedia = new TLRPC.TL_messageMediaPhoto();
messageMedia.photo = (TLRPC.Photo) file;
messageMedia.flags |= 1;
} else if (file instanceof TLRPC.Document) {
messageMedia = new TLRPC.TL_messageMediaDocument();
messageMedia.document = (TLRPC.Document) file;
messageMedia.flags |= 1;
}
if (messageMedia == null) {
return;
}
state = database.executeFast("REPLACE INTO sent_files_v2 VALUES(?, ?, ?, ?)");
state.requery();
NativeByteBuffer data = new NativeByteBuffer(messageMedia.getObjectSize());
messageMedia.serializeToStream(data);
state.bindString(1, id);
state.bindInteger(2, type);
state.bindByteBuffer(3, data);
state.bindString(4, parent);
state.step();
data.reuse();
}
} catch (Exception e) {
FileLog.e(e);
} finally {
if (state != null) {
state.dispose();
}
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method putMessagesInternal.
private void putMessagesInternal(ArrayList<TLRPC.Message> messages, boolean withTransaction, boolean doNotUpdateDialogDate, int downloadMask, boolean ifNoLastMessage, boolean scheduled) {
try {
if (scheduled) {
if (withTransaction) {
database.beginTransaction();
}
SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO scheduled_messages_v2 VALUES(?, ?, ?, ?, ?, ?, NULL, 0)");
SQLitePreparedStatement state_randoms = database.executeFast("REPLACE INTO randoms_v2 VALUES(?, ?, ?)");
ArrayList<Long> dialogsToUpdate = new ArrayList<>();
for (int a = 0; a < messages.size(); a++) {
TLRPC.Message message = messages.get(a);
if (message instanceof TLRPC.TL_messageEmpty) {
continue;
}
fixUnsupportedMedia(message);
state_messages.requery();
int messageId = message.id;
if (message.local_id != 0) {
messageId = message.local_id;
}
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
long did = MessageObject.getDialogId(message);
state_messages.bindInteger(1, messageId);
state_messages.bindLong(2, did);
state_messages.bindInteger(3, message.send_state);
state_messages.bindInteger(4, message.date);
state_messages.bindByteBuffer(5, data);
state_messages.bindInteger(6, message.ttl);
state_messages.step();
if (message.random_id != 0) {
state_randoms.requery();
state_randoms.bindLong(1, message.random_id);
state_randoms.bindInteger(2, messageId);
state_randoms.bindLong(3, message.dialog_id);
state_randoms.step();
}
data.reuse();
if (!dialogsToUpdate.contains(did)) {
dialogsToUpdate.add(did);
}
}
state_messages.dispose();
state_randoms.dispose();
if (withTransaction) {
database.commitTransaction();
}
for (int a = 0, N = dialogsToUpdate.size(); a < N; a++) {
broadcastScheduledMessagesChange(dialogsToUpdate.get(a));
}
} else {
if (ifNoLastMessage) {
TLRPC.Message lastMessage = messages.get(0);
if (lastMessage.dialog_id == 0) {
MessageObject.getDialogId(lastMessage);
}
int lastMid = -1;
SQLiteCursor cursor = database.queryFinalized("SELECT last_mid FROM dialogs WHERE did = " + lastMessage.dialog_id);
if (cursor.next()) {
lastMid = cursor.intValue(0);
}
cursor.dispose();
if (lastMid != 0) {
return;
}
}
if (withTransaction) {
database.beginTransaction();
}
LongSparseArray<TLRPC.Message> messagesMap = new LongSparseArray<>();
LongSparseIntArray messagesCounts = new LongSparseIntArray();
LongSparseIntArray newMessagesCounts = new LongSparseIntArray();
LongSparseIntArray newMentionsCounts = new LongSparseIntArray();
LongSparseIntArray mentionCounts = new LongSparseIntArray();
SparseArray<LongSparseIntArray> mediaCounts = null;
LongSparseArray<TLRPC.Message> botKeyboards = new LongSparseArray<>();
LongSparseArray<ArrayList<Integer>> dialogMessagesMediaIdsMap = null;
LongSparseArray<SparseIntArray> dialogsMediaTypesChange = null;
LongSparseArray<StringBuilder> mediaIdsMap = null;
LongSparseArray<SparseIntArray> dialogMediaTypes = null;
LongSparseArray<StringBuilder> messageIdsMap = new LongSparseArray<>();
LongSparseIntArray dialogsReadMax = new LongSparseIntArray();
LongSparseArray<ArrayList<Integer>> dialogMessagesIdsMap = new LongSparseArray<>();
LongSparseArray<ArrayList<Integer>> dialogMentionsIdsMap = new LongSparseArray<>();
SQLitePreparedStatement state_messages = database.executeFast("REPLACE INTO messages_v2 VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, 0)");
SQLitePreparedStatement state_media = null;
SQLitePreparedStatement state_randoms = database.executeFast("REPLACE INTO randoms_v2 VALUES(?, ?, ?)");
SQLitePreparedStatement state_download = database.executeFast("REPLACE INTO download_queue VALUES(?, ?, ?, ?, ?)");
SQLitePreparedStatement state_webpage = database.executeFast("REPLACE INTO webpage_pending_v2 VALUES(?, ?, ?)");
SQLitePreparedStatement state_polls = null;
SQLitePreparedStatement state_tasks = null;
int minDeleteTime = Integer.MAX_VALUE;
for (int a = 0; a < messages.size(); a++) {
TLRPC.Message message = messages.get(a);
int messageId = message.id;
MessageObject.getDialogId(message);
if (message.mentioned && message.media_unread) {
ArrayList<Integer> ids = dialogMentionsIdsMap.get(message.dialog_id);
if (ids == null) {
ids = new ArrayList<>();
dialogMentionsIdsMap.put(message.dialog_id, ids);
}
ids.add(messageId);
}
if (!(message.action instanceof TLRPC.TL_messageActionHistoryClear) && (!MessageObject.isOut(message) || message.from_scheduled) && (message.id > 0 || MessageObject.isUnread(message))) {
int currentMaxId = dialogsReadMax.get(message.dialog_id, -1);
if (currentMaxId == -1) {
SQLiteCursor cursor = database.queryFinalized("SELECT inbox_max FROM dialogs WHERE did = " + message.dialog_id);
if (cursor.next()) {
currentMaxId = cursor.intValue(0);
} else {
currentMaxId = 0;
}
cursor.dispose();
dialogsReadMax.put(message.dialog_id, currentMaxId);
}
if (message.id < 0 || currentMaxId < message.id) {
StringBuilder messageIds = messageIdsMap.get(message.dialog_id);
if (messageIds == null) {
messageIds = new StringBuilder();
messageIdsMap.put(message.dialog_id, messageIds);
}
if (messageIds.length() > 0) {
messageIds.append(",");
}
messageIds.append(messageId);
ArrayList<Integer> ids = dialogMessagesIdsMap.get(message.dialog_id);
if (ids == null) {
ids = new ArrayList<>();
dialogMessagesIdsMap.put(message.dialog_id, ids);
}
ids.add(messageId);
}
}
if (MediaDataController.canAddMessageToMedia(message)) {
if (mediaIdsMap == null) {
mediaIdsMap = new LongSparseArray<>();
dialogMessagesMediaIdsMap = new LongSparseArray<>();
dialogMediaTypes = new LongSparseArray<>();
}
StringBuilder messageMediaIds = mediaIdsMap.get(message.dialog_id);
if (messageMediaIds == null) {
messageMediaIds = new StringBuilder();
mediaIdsMap.put(message.dialog_id, messageMediaIds);
}
if (messageMediaIds.length() > 0) {
messageMediaIds.append(",");
}
messageMediaIds.append(messageId);
ArrayList<Integer> ids = dialogMessagesMediaIdsMap.get(message.dialog_id);
if (ids == null) {
ids = new ArrayList<>();
dialogMessagesMediaIdsMap.put(message.dialog_id, ids);
}
ids.add(messageId);
SparseIntArray mediaTypes = dialogMediaTypes.get(message.dialog_id);
if (mediaTypes == null) {
mediaTypes = new SparseIntArray();
dialogMediaTypes.put(message.dialog_id, mediaTypes);
}
mediaTypes.put(messageId, MediaDataController.getMediaType(message));
}
if (isValidKeyboardToSave(message)) {
TLRPC.Message oldMessage = botKeyboards.get(message.dialog_id);
if (oldMessage == null || oldMessage.id < message.id) {
botKeyboards.put(message.dialog_id, message);
}
}
}
for (int a = 0; a < botKeyboards.size(); a++) {
getMediaDataController().putBotKeyboard(botKeyboards.keyAt(a), botKeyboards.valueAt(a));
}
if (mediaIdsMap != null) {
for (int b = 0, N2 = mediaIdsMap.size(); b < N2; b++) {
long dialogId = mediaIdsMap.keyAt(b);
StringBuilder messageMediaIds = mediaIdsMap.valueAt(b);
SparseIntArray mediaTypes = dialogMediaTypes.get(dialogId);
ArrayList<Integer> messagesMediaIdsMap = dialogMessagesMediaIdsMap.get(dialogId);
SparseIntArray mediaTypesChange = null;
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid, type FROM media_v4 WHERE mid IN(%s) AND uid = %d", messageMediaIds.toString(), dialogId));
while (cursor.next()) {
int mid = cursor.intValue(0);
int type = cursor.intValue(1);
if (type == mediaTypes.get(mid)) {
messagesMediaIdsMap.remove((Integer) mid);
} else {
if (mediaTypesChange == null) {
if (dialogsMediaTypesChange == null) {
dialogsMediaTypesChange = new LongSparseArray<>();
}
mediaTypesChange = dialogsMediaTypesChange.get(dialogId);
if (mediaTypesChange == null) {
mediaTypesChange = new SparseIntArray();
dialogsMediaTypesChange.put(dialogId, mediaTypesChange);
}
}
mediaTypesChange.put(mid, type);
}
}
cursor.dispose();
if (mediaCounts == null) {
mediaCounts = new SparseArray<>();
}
for (int a = 0, N = messagesMediaIdsMap.size(); a < N; a++) {
int key = messagesMediaIdsMap.get(a);
int type = mediaTypes.get(key);
LongSparseIntArray counts = mediaCounts.get(type);
int count;
if (counts == null) {
counts = new LongSparseIntArray();
mediaCounts.put(type, counts);
count = 0;
} else {
count = counts.get(dialogId, Integer.MIN_VALUE);
}
if (count == Integer.MIN_VALUE) {
count = 0;
}
count++;
counts.put(dialogId, count);
if (mediaTypesChange != null) {
int previousType = mediaTypesChange.get(key, -1);
if (previousType >= 0) {
counts = mediaCounts.get(previousType);
if (counts == null) {
counts = new LongSparseIntArray();
count = 0;
mediaCounts.put(previousType, counts);
} else {
count = counts.get(dialogId, Integer.MIN_VALUE);
}
if (count == Integer.MIN_VALUE) {
count = 0;
}
count--;
counts.put(dialogId, count);
}
}
}
}
}
if (!messageIdsMap.isEmpty()) {
for (int b = 0, N2 = messageIdsMap.size(); b < N2; b++) {
long dialogId = messageIdsMap.keyAt(b);
StringBuilder messageIds = messageIdsMap.valueAt(b);
ArrayList<Integer> messagesIdsMap = dialogMessagesIdsMap.get(dialogId);
ArrayList<Integer> mentionsIdsMap = dialogMentionsIdsMap.get(dialogId);
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid FROM messages_v2 WHERE mid IN(%s) AND uid = %d", messageIds.toString(), dialogId));
while (cursor.next()) {
Integer mid = cursor.intValue(0);
if (messagesIdsMap != null) {
messagesIdsMap.remove(mid);
}
if (mentionsIdsMap != null) {
mentionsIdsMap.remove(mid);
}
}
cursor.dispose();
if (messagesCounts != null) {
int count = messagesCounts.get(dialogId, -1);
if (count < 0) {
count = 0;
}
count += messagesIdsMap.size();
messagesCounts.put(dialogId, count);
}
if (mentionsIdsMap != null) {
int count = mentionCounts.get(dialogId, -1);
if (count < 0) {
count = 0;
}
count += mentionsIdsMap.size();
mentionCounts.put(dialogId, count);
}
}
}
int downloadMediaMask = 0;
for (int a = 0; a < messages.size(); a++) {
TLRPC.Message message = messages.get(a);
fixUnsupportedMedia(message);
state_messages.requery();
int messageId = message.id;
if (message.local_id != 0) {
messageId = message.local_id;
}
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
boolean updateDialog = true;
if (message.action instanceof TLRPC.TL_messageEncryptedAction && !(message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL || message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionScreenshotMessages)) {
updateDialog = false;
} else if (message.out) {
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT mid FROM messages_v2 WHERE mid = %d AND uid = %d", messageId, message.dialog_id));
if (cursor.next()) {
updateDialog = false;
}
cursor.dispose();
}
if (updateDialog) {
TLRPC.Message lastMessage = messagesMap.get(message.dialog_id);
if (lastMessage == null || message.date > lastMessage.date || lastMessage.id > 0 && message.id > lastMessage.id || lastMessage.id < 0 && message.id < lastMessage.id) {
messagesMap.put(message.dialog_id, message);
}
}
state_messages.bindInteger(1, messageId);
state_messages.bindLong(2, message.dialog_id);
state_messages.bindInteger(3, MessageObject.getUnreadFlags(message));
state_messages.bindInteger(4, message.send_state);
state_messages.bindInteger(5, message.date);
state_messages.bindByteBuffer(6, data);
state_messages.bindInteger(7, (MessageObject.isOut(message) || message.from_scheduled ? 1 : 0));
state_messages.bindInteger(8, message.ttl);
if ((message.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
state_messages.bindInteger(9, message.views);
} else {
state_messages.bindInteger(9, getMessageMediaType(message));
}
int flags = 0;
if (message.stickerVerified == 0) {
flags |= 1;
} else if (message.stickerVerified == 2) {
flags |= 2;
}
state_messages.bindInteger(10, flags);
state_messages.bindInteger(11, message.mentioned ? 1 : 0);
state_messages.bindInteger(12, message.forwards);
NativeByteBuffer repliesData = null;
if (message.replies != null) {
repliesData = new NativeByteBuffer(message.replies.getObjectSize());
message.replies.serializeToStream(repliesData);
state_messages.bindByteBuffer(13, repliesData);
} else {
state_messages.bindNull(13);
}
if (message.reply_to != null) {
state_messages.bindInteger(14, message.reply_to.reply_to_top_id != 0 ? message.reply_to.reply_to_top_id : message.reply_to.reply_to_msg_id);
} else {
state_messages.bindInteger(14, 0);
}
state_messages.bindLong(15, MessageObject.getChannelId(message));
state_messages.step();
if (message.random_id != 0) {
state_randoms.requery();
state_randoms.bindLong(1, message.random_id);
state_randoms.bindInteger(2, messageId);
state_randoms.bindLong(3, message.dialog_id);
state_randoms.step();
}
if (MediaDataController.canAddMessageToMedia(message)) {
if (state_media == null) {
state_media = database.executeFast("REPLACE INTO media_v4 VALUES(?, ?, ?, ?, ?)");
}
state_media.requery();
state_media.bindInteger(1, messageId);
state_media.bindLong(2, message.dialog_id);
state_media.bindInteger(3, message.date);
state_media.bindInteger(4, MediaDataController.getMediaType(message));
state_media.bindByteBuffer(5, data);
state_media.step();
}
if (message.ttl_period != 0 && message.id > 0) {
if (state_tasks == null) {
state_tasks = database.executeFast("REPLACE INTO enc_tasks_v4 VALUES(?, ?, ?, ?)");
}
state_tasks.requery();
state_tasks.bindInteger(1, messageId);
state_tasks.bindLong(2, message.dialog_id);
state_tasks.bindInteger(3, message.date + message.ttl_period);
state_tasks.bindInteger(4, 0);
state_tasks.step();
minDeleteTime = Math.min(minDeleteTime, message.date + message.ttl_period);
}
if (message.media instanceof TLRPC.TL_messageMediaPoll) {
if (state_polls == null) {
state_polls = database.executeFast("REPLACE INTO polls_v2 VALUES(?, ?, ?)");
}
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) message.media;
state_polls.requery();
state_polls.bindInteger(1, messageId);
state_polls.bindLong(2, message.dialog_id);
state_polls.bindLong(3, mediaPoll.poll.id);
state_polls.step();
} else if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
state_webpage.requery();
state_webpage.bindLong(1, message.media.webpage.id);
state_webpage.bindInteger(2, messageId);
state_webpage.bindLong(3, message.dialog_id);
state_webpage.step();
}
if (repliesData != null) {
repliesData.reuse();
}
data.reuse();
if (downloadMask != 0 && (message.peer_id.channel_id == 0 || message.post) && message.date >= getConnectionsManager().getCurrentTime() - 60 * 60 && getDownloadController().canDownloadMedia(message) == 1) {
if (message.media instanceof TLRPC.TL_messageMediaPhoto || message.media instanceof TLRPC.TL_messageMediaDocument || message.media instanceof TLRPC.TL_messageMediaWebPage) {
int type = 0;
long id = 0;
TLRPC.MessageMedia object = null;
TLRPC.Document document = MessageObject.getDocument(message);
TLRPC.Photo photo = MessageObject.getPhoto(message);
if (MessageObject.isVoiceMessage(message)) {
id = document.id;
type = DownloadController.AUTODOWNLOAD_TYPE_AUDIO;
object = new TLRPC.TL_messageMediaDocument();
object.document = document;
object.flags |= 1;
} else if (MessageObject.isStickerMessage(message) || MessageObject.isAnimatedStickerMessage(message)) {
id = document.id;
type = DownloadController.AUTODOWNLOAD_TYPE_PHOTO;
object = new TLRPC.TL_messageMediaDocument();
object.document = document;
object.flags |= 1;
} else if (MessageObject.isVideoMessage(message) || MessageObject.isRoundVideoMessage(message) || MessageObject.isGifMessage(message)) {
id = document.id;
type = DownloadController.AUTODOWNLOAD_TYPE_VIDEO;
object = new TLRPC.TL_messageMediaDocument();
object.document = document;
object.flags |= 1;
} else if (document != null) {
id = document.id;
type = DownloadController.AUTODOWNLOAD_TYPE_DOCUMENT;
object = new TLRPC.TL_messageMediaDocument();
object.document = document;
object.flags |= 1;
} else if (photo != null) {
TLRPC.PhotoSize photoSize = FileLoader.getClosestPhotoSizeWithSize(photo.sizes, AndroidUtilities.getPhotoSize());
if (photoSize != null) {
id = photo.id;
type = DownloadController.AUTODOWNLOAD_TYPE_PHOTO;
object = new TLRPC.TL_messageMediaPhoto();
object.photo = photo;
object.flags |= 1;
if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
object.flags |= 0x80000000;
}
}
}
if (object != null) {
if (message.media.ttl_seconds != 0) {
object.ttl_seconds = message.media.ttl_seconds;
object.flags |= 4;
}
downloadMediaMask |= type;
state_download.requery();
data = new NativeByteBuffer(object.getObjectSize());
object.serializeToStream(data);
state_download.bindLong(1, id);
state_download.bindInteger(2, type);
state_download.bindInteger(3, message.date);
state_download.bindByteBuffer(4, data);
state_download.bindString(5, "sent_" + (message.peer_id != null ? message.peer_id.channel_id : 0) + "_" + message.id);
state_download.step();
data.reuse();
}
}
}
}
state_messages.dispose();
if (state_media != null) {
state_media.dispose();
}
if (state_tasks != null) {
state_tasks.dispose();
getMessagesController().didAddedNewTask(minDeleteTime, 0, null);
}
if (state_polls != null) {
state_polls.dispose();
}
state_randoms.dispose();
state_download.dispose();
state_webpage.dispose();
SQLitePreparedStatement state_dialogs_replace = database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
SQLitePreparedStatement state_dialogs_update = database.executeFast("UPDATE dialogs SET date = ?, unread_count = ?, last_mid = ?, unread_count_i = ? WHERE did = ?");
ArrayList<Long> dids = new ArrayList<>();
for (int a = 0; a < messagesMap.size(); a++) {
long key = messagesMap.keyAt(a);
if (key == 0) {
continue;
}
TLRPC.Message message = messagesMap.valueAt(a);
long channelId = MessageObject.getChannelId(message);
SQLiteCursor cursor = database.queryFinalized("SELECT date, unread_count, last_mid, unread_count_i FROM dialogs WHERE did = " + key);
int dialog_date = 0;
int last_mid = 0;
int old_unread_count = 0;
int old_mentions_count = 0;
boolean exists;
if (exists = cursor.next()) {
dialog_date = cursor.intValue(0);
old_unread_count = Math.max(0, cursor.intValue(1));
last_mid = cursor.intValue(2);
old_mentions_count = Math.max(0, cursor.intValue(3));
} else if (channelId != 0) {
getMessagesController().checkChatInviter(channelId, true);
}
cursor.dispose();
int mentions_count = mentionCounts.get(key, -1);
int unread_count = messagesCounts.get(key, -1);
if (unread_count == -1) {
unread_count = 0;
} else {
messagesCounts.put(key, unread_count + old_unread_count);
}
if (mentions_count == -1) {
mentions_count = 0;
} else {
mentionCounts.put(key, mentions_count + old_mentions_count);
}
int messageId = message != null ? message.id : last_mid;
if (message != null) {
if (message.local_id != 0) {
messageId = message.local_id;
}
}
if (old_unread_count == 0 && unread_count != 0) {
newMessagesCounts.put(key, unread_count);
}
if (old_mentions_count == 0 && mentions_count != 0) {
newMentionsCounts.put(key, mentions_count);
}
dids.add(key);
if (exists) {
state_dialogs_update.requery();
state_dialogs_update.bindInteger(1, message != null && (!doNotUpdateDialogDate || dialog_date == 0) ? message.date : dialog_date);
state_dialogs_update.bindInteger(2, old_unread_count + unread_count);
state_dialogs_update.bindInteger(3, messageId);
state_dialogs_update.bindInteger(4, old_mentions_count + mentions_count);
state_dialogs_update.bindLong(5, key);
state_dialogs_update.step();
} else {
state_dialogs_replace.requery();
state_dialogs_replace.bindLong(1, key);
state_dialogs_replace.bindInteger(2, message != null && (!doNotUpdateDialogDate || dialog_date == 0) ? message.date : dialog_date);
state_dialogs_replace.bindInteger(3, old_unread_count + unread_count);
state_dialogs_replace.bindInteger(4, messageId);
state_dialogs_replace.bindInteger(5, 0);
state_dialogs_replace.bindInteger(6, 0);
state_dialogs_replace.bindLong(7, 0);
state_dialogs_replace.bindInteger(8, old_mentions_count + mentions_count);
state_dialogs_replace.bindInteger(9, channelId != 0 ? 1 : 0);
state_dialogs_replace.bindInteger(10, 0);
state_dialogs_replace.bindInteger(11, 0);
state_dialogs_replace.bindInteger(12, 0);
state_dialogs_replace.bindInteger(13, 0);
state_dialogs_replace.bindNull(14);
state_dialogs_replace.bindInteger(15, 0);
state_dialogs_replace.step();
unknownDialogsIds.put(key, true);
}
}
state_dialogs_update.dispose();
state_dialogs_replace.dispose();
if (mediaCounts != null) {
state_randoms = database.executeFast("REPLACE INTO media_counts_v2 VALUES(?, ?, ?, ?)");
for (int a = 0, N = mediaCounts.size(); a < N; a++) {
int type = mediaCounts.keyAt(a);
LongSparseIntArray value = mediaCounts.valueAt(a);
for (int b = 0, N2 = value.size(); b < N2; b++) {
long uid = value.keyAt(b);
int count = -1;
int old = 0;
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT count, old FROM media_counts_v2 WHERE uid = %d AND type = %d LIMIT 1", uid, type));
if (cursor.next()) {
count = cursor.intValue(0);
old = cursor.intValue(1);
}
cursor.dispose();
if (count != -1) {
state_randoms.requery();
count += value.valueAt(b);
state_randoms.bindLong(1, uid);
state_randoms.bindInteger(2, type);
state_randoms.bindInteger(3, Math.max(0, count));
state_randoms.bindInteger(4, old);
state_randoms.step();
}
}
}
state_randoms.dispose();
}
if (withTransaction) {
database.commitTransaction();
}
updateFiltersReadCounter(newMessagesCounts, newMentionsCounts, false);
getMessagesController().processDialogsUpdateRead(messagesCounts, mentionCounts);
if (downloadMediaMask != 0) {
int downloadMediaMaskFinal = downloadMediaMask;
AndroidUtilities.runOnUIThread(() -> getDownloadController().newDownloadObjectsAvailable(downloadMediaMaskFinal));
}
updateWidgets(dids);
}
} catch (Exception e) {
FileLog.e(e);
}
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method loadChatInfoInternal.
private TLRPC.ChatFull loadChatInfoInternal(long chatId, boolean isChannel, boolean force, boolean byChannelUsers, int fromMessageId) {
TLRPC.ChatFull info = null;
ArrayList<TLRPC.User> loadedUsers = new ArrayList<>();
HashMap<Integer, MessageObject> pinnedMessagesMap = new HashMap<>();
ArrayList<Integer> pinnedMessages = new ArrayList<>();
int totalPinnedCount = 0;
boolean pinnedEndReached = false;
try {
SQLiteCursor cursor = database.queryFinalized("SELECT info, pinned, online, inviter, links FROM chat_settings_v2 WHERE uid = " + chatId);
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);
info.invitesCount = cursor.intValue(4);
}
}
cursor.dispose();
if (info instanceof TLRPC.TL_chatFull) {
StringBuilder usersToLoad = new StringBuilder();
for (int a = 0; a < info.participants.participants.size(); a++) {
TLRPC.ChatParticipant c = info.participants.participants.get(a);
if (usersToLoad.length() != 0) {
usersToLoad.append(",");
}
usersToLoad.append(c.user_id);
}
if (usersToLoad.length() != 0) {
getUsersInternal(usersToLoad.toString(), loadedUsers);
}
} else if (info instanceof TLRPC.TL_channelFull) {
cursor = database.queryFinalized("SELECT us.data, us.status, cu.data, cu.date FROM channel_users_v2 as cu LEFT JOIN users as us ON us.uid = cu.uid WHERE cu.did = " + (-chatId) + " ORDER BY cu.date DESC");
info.participants = new TLRPC.TL_chatParticipants();
while (cursor.next()) {
try {
TLRPC.User user = null;
TLRPC.ChannelParticipant participant = null;
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
user = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
data = cursor.byteBufferValue(2);
if (data != null) {
participant = TLRPC.ChannelParticipant.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
if (participant != null && participant.user_id == getUserConfig().clientUserId) {
user = getUserConfig().getCurrentUser();
}
if (user != null && participant != null) {
if (user.status != null) {
user.status.expires = cursor.intValue(1);
}
loadedUsers.add(user);
participant.date = cursor.intValue(3);
TLRPC.TL_chatChannelParticipant chatChannelParticipant = new TLRPC.TL_chatChannelParticipant();
chatChannelParticipant.user_id = MessageObject.getPeerId(participant.peer);
chatChannelParticipant.date = participant.date;
chatChannelParticipant.inviter_id = participant.inviter_id;
chatChannelParticipant.channelParticipant = participant;
info.participants.participants.add(chatChannelParticipant);
}
} catch (Exception e) {
FileLog.e(e);
}
}
cursor.dispose();
StringBuilder usersToLoad = new StringBuilder();
for (int a = 0; a < info.bot_info.size(); a++) {
TLRPC.BotInfo botInfo = info.bot_info.get(a);
if (usersToLoad.length() != 0) {
usersToLoad.append(",");
}
usersToLoad.append(botInfo.user_id);
}
if (usersToLoad.length() != 0) {
getUsersInternal(usersToLoad.toString(), loadedUsers);
}
}
if (info != null && info.inviterId != 0) {
getUsersInternal("" + info.inviterId, loadedUsers);
}
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT mid FROM chat_pinned_v2 WHERE uid = %d ORDER BY mid DESC", -chatId));
while (cursor.next()) {
int id = cursor.intValue(0);
pinnedMessages.add(id);
pinnedMessagesMap.put(id, null);
}
cursor.dispose();
cursor = database.queryFinalized("SELECT count, end FROM chat_pinned_count WHERE uid = " + (-chatId));
if (cursor.next()) {
totalPinnedCount = cursor.intValue(0);
pinnedEndReached = cursor.intValue(1) != 0;
}
cursor.dispose();
if (info != null && info.pinned_msg_id != 0) {
if (pinnedMessages.isEmpty() || info.pinned_msg_id > pinnedMessages.get(0)) {
pinnedMessages.clear();
pinnedMessages.add(info.pinned_msg_id);
pinnedMessagesMap.put(info.pinned_msg_id, null);
}
}
if (!pinnedMessages.isEmpty()) {
ArrayList<MessageObject> messageObjects = getMediaDataController().loadPinnedMessages(-chatId, isChannel ? chatId : 0, pinnedMessages, false);
if (messageObjects != null) {
for (int a = 0, N = messageObjects.size(); a < N; a++) {
MessageObject messageObject = messageObjects.get(a);
pinnedMessagesMap.put(messageObject.getId(), messageObject);
}
}
}
} catch (Exception e) {
FileLog.e(e);
} finally {
getMessagesController().processChatInfo(chatId, info, loadedUsers, true, force, byChannelUsers, pinnedMessages, pinnedMessagesMap, totalPinnedCount, pinnedEndReached);
}
return info;
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method markMessagesAsDeletedInternal.
private ArrayList<Long> markMessagesAsDeletedInternal(long dialogId, ArrayList<Integer> messages, boolean deleteFiles, boolean scheduled) {
try {
ArrayList<Long> dialogsIds = new ArrayList<>();
if (scheduled) {
String ids = TextUtils.join(",", messages);
ArrayList<Long> dialogsToUpdate = new ArrayList<>();
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid FROM scheduled_messages_v2 WHERE mid IN(%s) AND uid = %d", ids, dialogId));
try {
while (cursor.next()) {
long did = cursor.longValue(0);
if (!dialogsToUpdate.contains(did)) {
dialogsToUpdate.add(did);
}
}
} catch (Exception e) {
FileLog.e(e);
}
cursor.dispose();
database.executeFast(String.format(Locale.US, "DELETE FROM scheduled_messages_v2 WHERE mid IN(%s) AND uid = %d", ids, dialogId)).stepThis().dispose();
for (int a = 0, N = dialogsToUpdate.size(); a < N; a++) {
broadcastScheduledMessagesChange(dialogsToUpdate.get(a));
}
} else {
ArrayList<Integer> temp = new ArrayList<>(messages);
LongSparseArray<Integer[]> dialogsToUpdate = new LongSparseArray<>();
LongSparseArray<ArrayList<Integer>> messagesByDialogs = new LongSparseArray<>();
String ids = TextUtils.join(",", messages);
ArrayList<File> filesToDelete = new ArrayList<>();
ArrayList<String> namesToDelete = new ArrayList<>();
ArrayList<Pair<Long, Integer>> idsToDelete = new ArrayList<>();
long currentUser = getUserConfig().getClientUserId();
SQLiteCursor cursor;
if (dialogId != 0) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, out, mention, mid FROM messages_v2 WHERE mid IN(%s) AND uid = %d", ids, dialogId));
} else {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, data, read_state, out, mention, mid FROM messages_v2 WHERE mid IN(%s) AND is_channel = 0", ids));
}
try {
while (cursor.next()) {
long did = cursor.longValue(0);
int mid = cursor.intValue(5);
temp.remove((Integer) mid);
ArrayList<Integer> mids = messagesByDialogs.get(did);
if (mids == null) {
mids = new ArrayList<>();
messagesByDialogs.put(did, mids);
}
mids.add(mid);
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();
getMessagesStorage().getDatabase().beginTransaction();
SQLitePreparedStatement state;
for (int i = 0; i < 2; i++) {
if (i == 0) {
if (dialogId != 0) {
state = getMessagesStorage().getDatabase().executeFast("UPDATE messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?) AND uid = ?");
} else {
state = getMessagesStorage().getDatabase().executeFast("UPDATE messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?) AND is_channel = 0");
}
} else {
if (dialogId != 0) {
state = getMessagesStorage().getDatabase().executeFast("UPDATE scheduled_messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?) AND uid = ?");
} else {
state = getMessagesStorage().getDatabase().executeFast("UPDATE scheduled_messages_v2 SET replydata = ? WHERE reply_to_message_id IN(?)");
}
}
TLRPC.TL_messageEmpty emptyMessage = new TLRPC.TL_messageEmpty();
NativeByteBuffer data = new NativeByteBuffer(emptyMessage.getObjectSize());
emptyMessage.serializeToStream(data);
state.requery();
state.bindByteBuffer(1, data);
state.bindString(2, ids);
if (dialogId != 0) {
state.bindLong(3, dialogId);
}
state.step();
state.dispose();
getMessagesStorage().getDatabase().commitTransaction();
}
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);
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();
}
for (int a = 0, N = messagesByDialogs.size(); a < N; a++) {
long did = messagesByDialogs.keyAt(a);
ArrayList<Integer> mids = messagesByDialogs.valueAt(a);
String idsStr = TextUtils.join(",", mids);
if (!DialogObject.isEncryptedDialog(did)) {
if (DialogObject.isChatDialog(did)) {
database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", -did, idsStr)).stepThis().dispose();
} else {
database.executeFast(String.format(Locale.US, "UPDATE user_settings SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", did, idsStr)).stepThis().dispose();
}
}
database.executeFast(String.format(Locale.US, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid IN(%s)", did, idsStr)).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", did));
if (cursor.next()) {
int count = cursor.intValue(0);
state = database.executeFast("UPDATE chat_pinned_count SET count = ? WHERE uid = ?");
state.requery();
state.bindInteger(1, Math.max(0, count - updatedCount));
state.bindLong(2, did);
state.step();
state.dispose();
}
cursor.dispose();
}
database.executeFast(String.format(Locale.US, "DELETE FROM messages_v2 WHERE mid IN(%s) AND uid = %d", ids, did)).stepThis().dispose();
database.executeFast(String.format(Locale.US, "DELETE FROM polls_v2 WHERE mid IN(%s) AND uid = %d", ids, did)).stepThis().dispose();
database.executeFast(String.format(Locale.US, "DELETE FROM bot_keyboard WHERE mid IN(%s) AND uid = %d", ids, did)).stepThis().dispose();
if (temp.isEmpty()) {
cursor = database.queryFinalized(String.format(Locale.US, "SELECT uid, type FROM media_v4 WHERE mid IN(%s) AND uid = %d", ids, did));
SparseArray<LongSparseArray<Integer>> mediaCounts = null;
while (cursor.next()) {
long uid = cursor.longValue(0);
int type = cursor.intValue(1);
if (mediaCounts == null) {
mediaCounts = new SparseArray<>();
}
LongSparseArray<Integer> counts = mediaCounts.get(type);
Integer count;
if (counts == null) {
counts = new LongSparseArray<>();
count = 0;
mediaCounts.put(type, counts);
} else {
count = counts.get(uid);
}
if (count == null) {
count = 0;
}
count++;
counts.put(uid, count);
}
cursor.dispose();
if (mediaCounts != null) {
state = database.executeFast("REPLACE INTO media_counts_v2 VALUES(?, ?, ?, ?)");
for (int c = 0, N3 = mediaCounts.size(); c < N3; c++) {
int type = mediaCounts.keyAt(c);
LongSparseArray<Integer> value = mediaCounts.valueAt(c);
for (int b = 0, N2 = value.size(); b < N2; b++) {
long uid = value.keyAt(b);
int count = -1;
int old = 0;
cursor = database.queryFinalized(String.format(Locale.US, "SELECT count, old FROM media_counts_v2 WHERE uid = %d AND type = %d LIMIT 1", uid, type));
if (cursor.next()) {
count = cursor.intValue(0);
old = cursor.intValue(1);
}
cursor.dispose();
if (count != -1) {
state.requery();
count = Math.max(0, count - value.valueAt(b));
state.bindLong(1, uid);
state.bindInteger(2, type);
state.bindInteger(3, count);
state.bindInteger(4, old);
state.step();
}
}
}
state.dispose();
}
}
long time = System.currentTimeMillis();
database.executeFast(String.format(Locale.US, "DELETE FROM media_v4 WHERE mid IN(%s) AND uid = %d", ids, did)).stepThis().dispose();
}
database.executeFast(String.format(Locale.US, "DELETE FROM messages_seq WHERE mid IN(%s)", ids)).stepThis().dispose();
if (!temp.isEmpty()) {
if (dialogId == 0) {
database.executeFast("UPDATE media_counts_v2 SET old = 1 WHERE 1").stepThis().dispose();
} else {
database.executeFast(String.format(Locale.US, "UPDATE media_counts_v2 SET old = 1 WHERE uid = %d", dialogId)).stepThis().dispose();
}
}
getMediaDataController().clearBotKeyboard(0, messages);
if (dialogsToUpdate.size() != 0) {
resetAllUnreadCounters(false);
}
updateWidgets(dialogsIds);
}
return dialogsIds;
} catch (Exception e) {
FileLog.e(e);
}
return null;
}
Aggregations