use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadStickersByEmojiOrName.
public void loadStickersByEmojiOrName(String name, boolean isEmoji, boolean cache) {
if (loadingDiceStickerSets.contains(name) || isEmoji && diceStickerSetsByEmoji.get(name) != null) {
return;
}
loadingDiceStickerSets.add(name);
if (cache) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
TLRPC.TL_messages_stickerSet stickerSet = null;
int date = 0;
SQLiteCursor cursor = null;
try {
cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT data, date FROM stickers_dice WHERE emoji = ?", name);
if (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
stickerSet = TLRPC.TL_messages_stickerSet.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
date = cursor.intValue(1);
}
} catch (Throwable e) {
FileLog.e(e);
} finally {
if (cursor != null) {
cursor.dispose();
}
}
processLoadedDiceStickers(name, isEmoji, stickerSet, true, date);
});
} else {
TLRPC.TL_messages_getStickerSet req = new TLRPC.TL_messages_getStickerSet();
if (isEmoji) {
TLRPC.TL_inputStickerSetDice inputStickerSetDice = new TLRPC.TL_inputStickerSetDice();
inputStickerSetDice.emoticon = name;
req.stickerset = inputStickerSetDice;
} else {
TLRPC.TL_inputStickerSetShortName inputStickerSetShortName = new TLRPC.TL_inputStickerSetShortName();
inputStickerSetShortName.short_name = name;
req.stickerset = inputStickerSetShortName;
}
getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
if (BuildConfig.DEBUG && error != null) {
// supress test backend warning
return;
}
if (response instanceof TLRPC.TL_messages_stickerSet) {
processLoadedDiceStickers(name, isEmoji, (TLRPC.TL_messages_stickerSet) response, false, (int) (System.currentTimeMillis() / 1000));
} else {
processLoadedDiceStickers(name, isEmoji, null, false, (int) (System.currentTimeMillis() / 1000));
}
}));
}
}
use of org.telegram.tgnet.NativeByteBuffer 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);
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadRecents.
public void loadRecents(int type, boolean gif, boolean cache, boolean force) {
if (gif) {
if (loadingRecentGifs) {
return;
}
loadingRecentGifs = true;
if (recentGifsLoaded) {
cache = false;
}
} else {
if (loadingRecentStickers[type]) {
return;
}
loadingRecentStickers[type] = true;
if (recentStickersLoaded[type]) {
cache = false;
}
}
if (cache) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
int cacheType;
if (gif) {
cacheType = 2;
} else if (type == TYPE_IMAGE) {
cacheType = 3;
} else if (type == TYPE_MASK) {
cacheType = 4;
} else if (type == TYPE_GREETINGS) {
cacheType = 6;
} else {
cacheType = 5;
}
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized("SELECT document FROM web_recent_v3 WHERE type = " + cacheType + " ORDER BY date DESC");
ArrayList<TLRPC.Document> arrayList = new ArrayList<>();
while (cursor.next()) {
if (!cursor.isNull(0)) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Document document = TLRPC.Document.TLdeserialize(data, data.readInt32(false), false);
if (document != null) {
arrayList.add(document);
}
data.reuse();
}
}
}
cursor.dispose();
AndroidUtilities.runOnUIThread(() -> {
if (gif) {
recentGifs = arrayList;
loadingRecentGifs = false;
recentGifsLoaded = true;
} else {
recentStickers[type] = arrayList;
loadingRecentStickers[type] = false;
recentStickersLoaded[type] = true;
}
if (type == TYPE_GREETINGS) {
preloadNextGreetingsSticker();
}
getNotificationCenter().postNotificationName(NotificationCenter.recentDocumentsDidLoad, gif, type);
loadRecents(type, gif, false, false);
});
} catch (Throwable e) {
FileLog.e(e);
}
});
} else {
SharedPreferences preferences = MessagesController.getEmojiSettings(currentAccount);
if (!force) {
long lastLoadTime;
if (gif) {
lastLoadTime = preferences.getLong("lastGifLoadTime", 0);
} else if (type == TYPE_IMAGE) {
lastLoadTime = preferences.getLong("lastStickersLoadTime", 0);
} else if (type == TYPE_MASK) {
lastLoadTime = preferences.getLong("lastStickersLoadTimeMask", 0);
} else if (type == TYPE_GREETINGS) {
lastLoadTime = preferences.getLong("lastStickersLoadTimeGreet", 0);
} else {
lastLoadTime = preferences.getLong("lastStickersLoadTimeFavs", 0);
}
if (Math.abs(System.currentTimeMillis() - lastLoadTime) < 60 * 60 * 1000) {
if (gif) {
loadingRecentGifs = false;
} else {
loadingRecentStickers[type] = false;
}
return;
}
}
if (gif) {
TLRPC.TL_messages_getSavedGifs req = new TLRPC.TL_messages_getSavedGifs();
req.hash = calcDocumentsHash(recentGifs);
getConnectionsManager().sendRequest(req, (response, error) -> {
ArrayList<TLRPC.Document> arrayList = null;
if (response instanceof TLRPC.TL_messages_savedGifs) {
TLRPC.TL_messages_savedGifs res = (TLRPC.TL_messages_savedGifs) response;
arrayList = res.gifs;
}
processLoadedRecentDocuments(type, arrayList, true, 0, true);
});
} else {
TLObject request;
if (type == TYPE_FAVE) {
TLRPC.TL_messages_getFavedStickers req = new TLRPC.TL_messages_getFavedStickers();
req.hash = calcDocumentsHash(recentStickers[type]);
request = req;
} else if (type == TYPE_GREETINGS) {
TLRPC.TL_messages_getStickers req = new TLRPC.TL_messages_getStickers();
req.emoticon = "\uD83D\uDC4B" + Emoji.fixEmoji("⭐");
req.hash = calcDocumentsHash(recentStickers[type]);
request = req;
} else {
TLRPC.TL_messages_getRecentStickers req = new TLRPC.TL_messages_getRecentStickers();
req.hash = calcDocumentsHash(recentStickers[type]);
req.attached = type == TYPE_MASK;
request = req;
}
getConnectionsManager().sendRequest(request, (response, error) -> {
ArrayList<TLRPC.Document> arrayList = null;
if (type == TYPE_GREETINGS) {
if (response instanceof TLRPC.TL_messages_stickers) {
TLRPC.TL_messages_stickers res = (TLRPC.TL_messages_stickers) response;
arrayList = res.stickers;
}
} else if (type == TYPE_FAVE) {
if (response instanceof TLRPC.TL_messages_favedStickers) {
TLRPC.TL_messages_favedStickers res = (TLRPC.TL_messages_favedStickers) response;
arrayList = res.stickers;
}
} else {
if (response instanceof TLRPC.TL_messages_recentStickers) {
TLRPC.TL_messages_recentStickers res = (TLRPC.TL_messages_recentStickers) response;
arrayList = res.stickers;
}
}
processLoadedRecentDocuments(type, arrayList, false, 0, true);
});
}
}
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method loadPinnedMessageInternal.
private ArrayList<MessageObject> loadPinnedMessageInternal(long dialogId, long channelId, ArrayList<Integer> mids, boolean returnValue) {
try {
ArrayList<Integer> midsCopy = new ArrayList<>(mids);
CharSequence longIds;
if (channelId != 0) {
StringBuilder builder = new StringBuilder();
for (int a = 0, N = mids.size(); a < N; a++) {
Integer messageId = mids.get(a);
if (builder.length() != 0) {
builder.append(",");
}
builder.append(messageId);
}
longIds = builder;
} else {
longIds = TextUtils.join(",", mids);
}
ArrayList<TLRPC.Message> results = new ArrayList<>();
ArrayList<TLRPC.User> users = new ArrayList<>();
ArrayList<TLRPC.Chat> chats = new ArrayList<>();
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
long selfUserId = getUserConfig().clientUserId;
SQLiteCursor cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, mid, date FROM messages_v2 WHERE mid IN (%s) AND uid = %d", longIds, dialogId));
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message result = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
if (!(result.action instanceof TLRPC.TL_messageActionHistoryClear)) {
result.readAttachPath(data, selfUserId);
result.id = cursor.intValue(1);
result.date = cursor.intValue(2);
result.dialog_id = dialogId;
MessagesStorage.addUsersAndChatsFromMessage(result, usersToLoad, chatsToLoad);
results.add(result);
midsCopy.remove((Integer) result.id);
}
data.reuse();
}
}
cursor.dispose();
if (!midsCopy.isEmpty()) {
cursor = getMessagesStorage().getDatabase().queryFinalized(String.format(Locale.US, "SELECT data FROM chat_pinned_v2 WHERE uid = %d AND mid IN (%s)", dialogId, TextUtils.join(",", midsCopy)));
while (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Message result = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false);
if (!(result.action instanceof TLRPC.TL_messageActionHistoryClear)) {
result.readAttachPath(data, selfUserId);
result.dialog_id = dialogId;
MessagesStorage.addUsersAndChatsFromMessage(result, usersToLoad, chatsToLoad);
results.add(result);
midsCopy.remove((Integer) result.id);
}
data.reuse();
}
}
cursor.dispose();
}
if (!midsCopy.isEmpty()) {
if (channelId != 0) {
TLRPC.TL_channels_getMessages req = new TLRPC.TL_channels_getMessages();
req.channel = getMessagesController().getInputChannel(channelId);
req.id = midsCopy;
getConnectionsManager().sendRequest(req, (response, error) -> {
boolean ok = false;
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
removeEmptyMessages(messagesRes.messages);
if (!messagesRes.messages.isEmpty()) {
TLRPC.Chat chat = getMessagesController().getChat(channelId);
ImageLoader.saveMessagesThumbs(messagesRes.messages);
broadcastPinnedMessage(messagesRes.messages, messagesRes.users, messagesRes.chats, false, false);
getMessagesStorage().putUsersAndChats(messagesRes.users, messagesRes.chats, true, true);
savePinnedMessages(dialogId, messagesRes.messages);
ok = true;
}
}
if (!ok) {
getMessagesStorage().updatePinnedMessages(dialogId, req.id, false, -1, 0, false, null);
}
});
} else {
TLRPC.TL_messages_getMessages req = new TLRPC.TL_messages_getMessages();
req.id = midsCopy;
getConnectionsManager().sendRequest(req, (response, error) -> {
boolean ok = false;
if (error == null) {
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
removeEmptyMessages(messagesRes.messages);
if (!messagesRes.messages.isEmpty()) {
ImageLoader.saveMessagesThumbs(messagesRes.messages);
broadcastPinnedMessage(messagesRes.messages, messagesRes.users, messagesRes.chats, false, false);
getMessagesStorage().putUsersAndChats(messagesRes.users, messagesRes.chats, true, true);
savePinnedMessages(dialogId, messagesRes.messages);
ok = true;
}
}
if (!ok) {
getMessagesStorage().updatePinnedMessages(dialogId, req.id, false, -1, 0, false, null);
}
});
}
}
if (!results.isEmpty()) {
if (!usersToLoad.isEmpty()) {
getMessagesStorage().getUsersInternal(TextUtils.join(",", usersToLoad), users);
}
if (!chatsToLoad.isEmpty()) {
getMessagesStorage().getChatsInternal(TextUtils.join(",", chatsToLoad), chats);
}
if (returnValue) {
return broadcastPinnedMessage(results, users, chats, true, true);
} else {
broadcastPinnedMessage(results, users, chats, true, false);
}
}
} catch (Exception e) {
FileLog.e(e);
}
return null;
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MediaDataController method saveReplyMessages.
private void saveReplyMessages(LongSparseArray<SparseArray<ArrayList<MessageObject>>> replyMessageOwners, ArrayList<TLRPC.Message> result, boolean scheduled) {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
try {
getMessagesStorage().getDatabase().beginTransaction();
SQLitePreparedStatement state;
if (scheduled) {
state = getMessagesStorage().getDatabase().executeFast("UPDATE scheduled_messages_v2 SET replydata = ?, reply_to_message_id = ? WHERE mid = ? AND uid = ?");
} else {
state = getMessagesStorage().getDatabase().executeFast("UPDATE messages_v2 SET replydata = ?, reply_to_message_id = ? WHERE mid = ? AND uid = ?");
}
for (int a = 0; a < result.size(); a++) {
TLRPC.Message message = result.get(a);
long dialogId = MessageObject.getDialogId(message);
SparseArray<ArrayList<MessageObject>> sparseArray = replyMessageOwners.get(dialogId);
if (sparseArray == null) {
continue;
}
ArrayList<MessageObject> messageObjects = sparseArray.get(message.id);
if (messageObjects != null) {
NativeByteBuffer data = new NativeByteBuffer(message.getObjectSize());
message.serializeToStream(data);
for (int b = 0; b < messageObjects.size(); b++) {
MessageObject messageObject = messageObjects.get(b);
state.requery();
state.bindByteBuffer(1, data);
state.bindInteger(2, message.id);
state.bindInteger(3, messageObject.getId());
state.bindLong(4, messageObject.getDialogId());
state.step();
}
data.reuse();
}
}
state.dispose();
getMessagesStorage().getDatabase().commitTransaction();
} catch (Exception e) {
FileLog.e(e);
}
});
}
Aggregations