use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method localSearch.
public void localSearch(int dialogsType, String query, ArrayList<Object> resultArray, ArrayList<CharSequence> resultArrayNames, ArrayList<TLRPC.User> encUsers, int folderId) {
long selfUserId = UserConfig.getInstance(currentAccount).getClientUserId();
try {
String search1 = query.trim().toLowerCase();
if (TextUtils.isEmpty(search1)) {
return;
}
String savedMessages = LocaleController.getString("SavedMessages", R.string.SavedMessages).toLowerCase();
String savedMessages2 = "saved messages";
String search2 = LocaleController.getInstance().getTranslitString(search1);
if (search1.equals(search2) || search2.length() == 0) {
search2 = null;
}
String[] search = new String[1 + (search2 != null ? 1 : 0)];
search[0] = search1;
if (search2 != null) {
search[1] = search2;
}
ArrayList<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> chatsToLoad = new ArrayList<>();
ArrayList<Integer> encryptedToLoad = new ArrayList<>();
int resultCount = 0;
LongSparseArray<DialogsSearchAdapter.DialogSearchResult> dialogsResult = new LongSparseArray<>();
SQLiteCursor cursor;
if (folderId >= 0) {
cursor = getDatabase().queryFinalized("SELECT did, date FROM dialogs WHERE folder_id = ? ORDER BY date DESC LIMIT 600", folderId);
} else {
cursor = getDatabase().queryFinalized("SELECT did, date FROM dialogs ORDER BY date DESC LIMIT 600");
}
while (cursor.next()) {
long id = cursor.longValue(0);
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = new DialogsSearchAdapter.DialogSearchResult();
dialogSearchResult.date = cursor.intValue(1);
dialogsResult.put(id, dialogSearchResult);
if (!DialogObject.isEncryptedDialog(id)) {
if (DialogObject.isUserDialog(id)) {
if (dialogsType == 4 && id == selfUserId) {
continue;
}
if (dialogsType != 2 && !usersToLoad.contains(id)) {
usersToLoad.add(id);
}
} else {
if (dialogsType == 4) {
continue;
}
if (!chatsToLoad.contains(-id)) {
chatsToLoad.add(-id);
}
}
} else if (dialogsType == 0 || dialogsType == 3) {
int encryptedChatId = DialogObject.getEncryptedChatId(id);
if (!encryptedToLoad.contains(encryptedChatId)) {
encryptedToLoad.add(encryptedChatId);
}
}
}
cursor.dispose();
if (dialogsType != 4 && (savedMessages).startsWith(search1) || savedMessages2.startsWith(search1)) {
TLRPC.User user = UserConfig.getInstance(currentAccount).getCurrentUser();
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = new DialogsSearchAdapter.DialogSearchResult();
dialogSearchResult.date = Integer.MAX_VALUE;
dialogSearchResult.name = savedMessages;
dialogSearchResult.object = user;
dialogsResult.put(user.id, dialogSearchResult);
resultCount++;
}
if (!usersToLoad.isEmpty()) {
cursor = getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, status, name FROM users WHERE uid IN(%s)", TextUtils.join(",", usersToLoad)));
while (cursor.next()) {
String name = cursor.stringValue(2);
String tName = LocaleController.getInstance().getTranslitString(name);
if (name.equals(tName)) {
tName = null;
}
String username = null;
int usernamePos = name.lastIndexOf(";;;");
if (usernamePos != -1) {
username = name.substring(usernamePos + 3);
}
int found = 0;
for (String q : search) {
if (name.startsWith(q) || name.contains(" " + q) || tName != null && (tName.startsWith(q) || tName.contains(" " + q))) {
found = 1;
} else if (username != null && username.startsWith(q)) {
found = 2;
}
if (found != 0) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.User user = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = dialogsResult.get(user.id);
if (user.status != null) {
user.status.expires = cursor.intValue(1);
}
if (found == 1) {
dialogSearchResult.name = AndroidUtilities.generateSearchName(user.first_name, user.last_name, q);
} else {
dialogSearchResult.name = AndroidUtilities.generateSearchName("@" + user.username, null, "@" + q);
}
dialogSearchResult.object = user;
resultCount++;
}
break;
}
}
}
cursor.dispose();
}
if (!chatsToLoad.isEmpty()) {
cursor = getDatabase().queryFinalized(String.format(Locale.US, "SELECT data, name FROM chats WHERE uid IN(%s)", TextUtils.join(",", chatsToLoad)));
while (cursor.next()) {
String name = cursor.stringValue(1);
String tName = LocaleController.getInstance().getTranslitString(name);
if (name.equals(tName)) {
tName = null;
}
for (String q : search) {
if (name.startsWith(q) || name.contains(" " + q) || tName != null && (tName.startsWith(q) || tName.contains(" " + q))) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Chat chat = TLRPC.Chat.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
if (!(chat == null || chat.deactivated || ChatObject.isChannel(chat) && ChatObject.isNotInChat(chat))) {
long dialog_id = -chat.id;
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = dialogsResult.get(dialog_id);
dialogSearchResult.name = AndroidUtilities.generateSearchName(chat.title, null, q);
dialogSearchResult.object = chat;
resultCount++;
}
}
break;
}
}
}
cursor.dispose();
}
if (!encryptedToLoad.isEmpty()) {
cursor = getDatabase().queryFinalized(String.format(Locale.US, "SELECT q.data, u.name, q.user, q.g, q.authkey, q.ttl, u.data, u.status, q.layer, q.seq_in, q.seq_out, q.use_count, q.exchange_id, q.key_date, q.fprint, q.fauthkey, q.khash, q.in_seq_no, q.admin_id, q.mtproto_seq FROM enc_chats as q INNER JOIN users as u ON q.user = u.uid WHERE q.uid IN(%s)", TextUtils.join(",", encryptedToLoad)));
while (cursor.next()) {
String name = cursor.stringValue(1);
String tName = LocaleController.getInstance().getTranslitString(name);
if (name.equals(tName)) {
tName = null;
}
String username = null;
int usernamePos = name.lastIndexOf(";;;");
if (usernamePos != -1) {
username = name.substring(usernamePos + 2);
}
int found = 0;
for (int a = 0; a < search.length; a++) {
String q = search[a];
if (name.startsWith(q) || name.contains(" " + q) || tName != null && (tName.startsWith(q) || tName.contains(" " + q))) {
found = 1;
} else if (username != null && username.startsWith(q)) {
found = 2;
}
if (found != 0) {
TLRPC.EncryptedChat chat = null;
TLRPC.User user = null;
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
chat = TLRPC.EncryptedChat.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
data = cursor.byteBufferValue(6);
if (data != null) {
user = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
if (chat != null && user != null) {
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = dialogsResult.get(DialogObject.makeEncryptedDialogId(chat.id));
chat.user_id = cursor.longValue(2);
chat.a_or_b = cursor.byteArrayValue(3);
chat.auth_key = cursor.byteArrayValue(4);
chat.ttl = cursor.intValue(5);
chat.layer = cursor.intValue(8);
chat.seq_in = cursor.intValue(9);
chat.seq_out = cursor.intValue(10);
int use_count = cursor.intValue(11);
chat.key_use_count_in = (short) (use_count >> 16);
chat.key_use_count_out = (short) (use_count);
chat.exchange_id = cursor.longValue(12);
chat.key_create_date = cursor.intValue(13);
chat.future_key_fingerprint = cursor.longValue(14);
chat.future_auth_key = cursor.byteArrayValue(15);
chat.key_hash = cursor.byteArrayValue(16);
chat.in_seq_no = cursor.intValue(17);
long admin_id = cursor.longValue(18);
if (admin_id != 0) {
chat.admin_id = admin_id;
}
chat.mtproto_seq = cursor.intValue(19);
if (user.status != null) {
user.status.expires = cursor.intValue(7);
}
if (found == 1) {
dialogSearchResult.name = new SpannableStringBuilder(ContactsController.formatName(user.first_name, user.last_name));
((SpannableStringBuilder) dialogSearchResult.name).setSpan(new ForegroundColorSpan(Theme.getColor(Theme.key_chats_secretName)), 0, dialogSearchResult.name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
dialogSearchResult.name = AndroidUtilities.generateSearchName("@" + user.username, null, "@" + q);
}
dialogSearchResult.object = chat;
encUsers.add(user);
resultCount++;
}
break;
}
}
}
cursor.dispose();
}
ArrayList<DialogsSearchAdapter.DialogSearchResult> searchResults = new ArrayList<>(resultCount);
for (int a = 0; a < dialogsResult.size(); a++) {
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = dialogsResult.valueAt(a);
if (dialogSearchResult.object != null && dialogSearchResult.name != null) {
searchResults.add(dialogSearchResult);
}
}
Collections.sort(searchResults, (lhs, rhs) -> {
if (lhs.date < rhs.date) {
return 1;
} else if (lhs.date > rhs.date) {
return -1;
}
return 0;
});
for (int a = 0; a < searchResults.size(); a++) {
DialogsSearchAdapter.DialogSearchResult dialogSearchResult = searchResults.get(a);
resultArray.add(dialogSearchResult.object);
resultArrayNames.add(dialogSearchResult.name);
}
if (dialogsType != 2) {
cursor = getDatabase().queryFinalized("SELECT u.data, u.status, u.name, u.uid FROM users as u INNER JOIN contacts as c ON u.uid = c.uid");
while (cursor.next()) {
long uid = cursor.longValue(3);
if (dialogsResult.indexOfKey(uid) >= 0) {
continue;
}
String name = cursor.stringValue(2);
String tName = LocaleController.getInstance().getTranslitString(name);
if (name.equals(tName)) {
tName = null;
}
String username = null;
int usernamePos = name.lastIndexOf(";;;");
if (usernamePos != -1) {
username = name.substring(usernamePos + 3);
}
int found = 0;
for (String q : search) {
if (name.startsWith(q) || name.contains(" " + q) || tName != null && (tName.startsWith(q) || tName.contains(" " + q))) {
found = 1;
} else if (username != null && username.startsWith(q)) {
found = 2;
}
if (found != 0) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.User user = TLRPC.User.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
if (user.status != null) {
user.status.expires = cursor.intValue(1);
}
if (found == 1) {
resultArrayNames.add(AndroidUtilities.generateSearchName(user.first_name, user.last_name, q));
} else {
resultArrayNames.add(AndroidUtilities.generateSearchName("@" + user.username, null, "@" + q));
}
resultArray.add(user);
}
break;
}
}
}
cursor.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 updateEncryptedChat.
public void updateEncryptedChat(TLRPC.EncryptedChat chat) {
if (chat == null) {
return;
}
storageQueue.postRunnable(() -> {
SQLitePreparedStatement state = null;
try {
if ((chat.key_hash == null || chat.key_hash.length < 16) && chat.auth_key != null) {
chat.key_hash = AndroidUtilities.calcAuthKeyHash(chat.auth_key);
}
state = database.executeFast("UPDATE enc_chats SET data = ?, g = ?, authkey = ?, ttl = ?, layer = ?, seq_in = ?, seq_out = ?, use_count = ?, exchange_id = ?, key_date = ?, fprint = ?, fauthkey = ?, khash = ?, in_seq_no = ?, admin_id = ?, mtproto_seq = ? WHERE uid = ?");
NativeByteBuffer data = new NativeByteBuffer(chat.getObjectSize());
NativeByteBuffer data2 = new NativeByteBuffer(chat.a_or_b != null ? chat.a_or_b.length : 1);
NativeByteBuffer data3 = new NativeByteBuffer(chat.auth_key != null ? chat.auth_key.length : 1);
NativeByteBuffer data4 = new NativeByteBuffer(chat.future_auth_key != null ? chat.future_auth_key.length : 1);
NativeByteBuffer data5 = new NativeByteBuffer(chat.key_hash != null ? chat.key_hash.length : 1);
chat.serializeToStream(data);
state.bindByteBuffer(1, data);
if (chat.a_or_b != null) {
data2.writeBytes(chat.a_or_b);
}
if (chat.auth_key != null) {
data3.writeBytes(chat.auth_key);
}
if (chat.future_auth_key != null) {
data4.writeBytes(chat.future_auth_key);
}
if (chat.key_hash != null) {
data5.writeBytes(chat.key_hash);
}
state.bindByteBuffer(2, data2);
state.bindByteBuffer(3, data3);
state.bindInteger(4, chat.ttl);
state.bindInteger(5, chat.layer);
state.bindInteger(6, chat.seq_in);
state.bindInteger(7, chat.seq_out);
state.bindInteger(8, (int) chat.key_use_count_in << 16 | chat.key_use_count_out);
state.bindLong(9, chat.exchange_id);
state.bindInteger(10, chat.key_create_date);
state.bindLong(11, chat.future_key_fingerprint);
state.bindByteBuffer(12, data4);
state.bindByteBuffer(13, data5);
state.bindInteger(14, chat.in_seq_no);
state.bindLong(15, chat.admin_id);
state.bindInteger(16, chat.mtproto_seq);
state.bindInteger(17, chat.id);
state.step();
data.reuse();
data2.reuse();
data3.reuse();
data4.reuse();
data5.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 putChatsInternal.
private void putChatsInternal(ArrayList<TLRPC.Chat> chats) throws Exception {
if (chats == null || chats.isEmpty()) {
return;
}
SQLitePreparedStatement state = database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)");
for (int a = 0; a < chats.size(); a++) {
TLRPC.Chat chat = chats.get(a);
if (chat.min) {
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid = %d", chat.id));
if (cursor.next()) {
try {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
TLRPC.Chat oldChat = TLRPC.Chat.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
if (oldChat != null) {
oldChat.title = chat.title;
oldChat.photo = chat.photo;
oldChat.broadcast = chat.broadcast;
oldChat.verified = chat.verified;
oldChat.megagroup = chat.megagroup;
oldChat.call_not_empty = chat.call_not_empty;
oldChat.call_active = chat.call_active;
if (chat.default_banned_rights != null) {
oldChat.default_banned_rights = chat.default_banned_rights;
oldChat.flags |= 262144;
}
if (chat.admin_rights != null) {
oldChat.admin_rights = chat.admin_rights;
oldChat.flags |= 16384;
}
if (chat.banned_rights != null) {
oldChat.banned_rights = chat.banned_rights;
oldChat.flags |= 32768;
}
if (chat.username != null) {
oldChat.username = chat.username;
oldChat.flags |= 64;
} else {
oldChat.username = null;
oldChat.flags = oldChat.flags & ~64;
}
chat = oldChat;
}
}
} catch (Exception e) {
FileLog.e(e);
}
}
cursor.dispose();
}
state.requery();
chat.flags |= 131072;
NativeByteBuffer data = new NativeByteBuffer(chat.getObjectSize());
chat.serializeToStream(data);
state.bindLong(1, chat.id);
if (chat.title != null) {
String name = chat.title.toLowerCase();
state.bindString(2, name);
} else {
state.bindString(2, "");
}
state.bindByteBuffer(3, data);
state.step();
data.reuse();
}
state.dispose();
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method updatePinnedMessages.
public void updatePinnedMessages(long dialogId, ArrayList<Integer> ids, boolean pin, int totalCount, int maxId, boolean end, HashMap<Integer, MessageObject> messages) {
storageQueue.postRunnable(() -> {
try {
if (pin) {
database.beginTransaction();
int alreadyAdded = 0;
boolean endReached;
if (messages != null) {
if (maxId == 0) {
database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + dialogId).stepThis().dispose();
}
} else {
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM chat_pinned_v2 WHERE uid = %d AND mid IN (%s)", dialogId, TextUtils.join(",", ids)));
alreadyAdded = cursor.next() ? cursor.intValue(0) : 0;
cursor.dispose();
}
SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_pinned_v2 VALUES(?, ?, ?)");
for (int a = 0, N = ids.size(); a < N; a++) {
Integer id = ids.get(a);
state.requery();
state.bindLong(1, dialogId);
state.bindInteger(2, id);
MessageObject message = null;
if (messages != null) {
message = messages.get(id);
}
NativeByteBuffer data = null;
if (message != null) {
data = new NativeByteBuffer(message.messageOwner.getObjectSize());
message.messageOwner.serializeToStream(data);
state.bindByteBuffer(3, data);
} else {
state.bindNull(3);
}
state.step();
if (data != null) {
data.reuse();
}
}
state.dispose();
database.commitTransaction();
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM chat_pinned_v2 WHERE uid = %d", dialogId));
int newCount1 = cursor.next() ? cursor.intValue(0) : 0;
cursor.dispose();
int newCount;
if (messages != null) {
newCount = Math.max(totalCount, newCount1);
endReached = end;
} else {
SQLiteCursor cursor2 = database.queryFinalized(String.format(Locale.US, "SELECT count, end FROM chat_pinned_count WHERE uid = %d", dialogId));
int newCount2;
if (cursor2.next()) {
newCount2 = cursor2.intValue(0);
endReached = cursor2.intValue(1) != 0;
} else {
newCount2 = 0;
endReached = false;
}
cursor2.dispose();
newCount = Math.max(newCount2 + (ids.size() - alreadyAdded), newCount1);
}
state = database.executeFast("REPLACE INTO chat_pinned_count VALUES(?, ?, ?)");
state.requery();
state.bindLong(1, dialogId);
state.bindInteger(2, newCount);
state.bindInteger(3, endReached ? 1 : 0);
state.step();
state.dispose();
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didLoadPinnedMessages, dialogId, ids, true, null, messages, maxId, newCount, endReached));
} else {
int newCount;
boolean endReached;
if (ids == null) {
database.executeFast("DELETE FROM chat_pinned_v2 WHERE uid = " + dialogId).stepThis().dispose();
if (DialogObject.isChatDialog(dialogId)) {
database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d", -dialogId)).stepThis().dispose();
} else {
database.executeFast(String.format(Locale.US, "UPDATE user_settings SET pinned = 0 WHERE uid = %d", dialogId)).stepThis().dispose();
}
newCount = 0;
endReached = true;
} else {
String idsStr = TextUtils.join(",", ids);
if (DialogObject.isChatDialog(dialogId)) {
database.executeFast(String.format(Locale.US, "UPDATE chat_settings_v2 SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", -dialogId, idsStr)).stepThis().dispose();
} else {
database.executeFast(String.format(Locale.US, "UPDATE user_settings SET pinned = 0 WHERE uid = %d AND pinned IN (%s)", dialogId, idsStr)).stepThis().dispose();
}
database.executeFast(String.format(Locale.US, "DELETE FROM chat_pinned_v2 WHERE uid = %d AND mid IN(%s)", dialogId, idsStr)).stepThis().dispose();
SQLiteCursor cursor = database.queryFinalized("SELECT changes()");
int updatedCount = cursor.next() ? cursor.intValue(0) : 0;
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT COUNT(mid) FROM chat_pinned_v2 WHERE uid = %d", dialogId));
int newCount1 = cursor.next() ? cursor.intValue(0) : 0;
cursor.dispose();
cursor = database.queryFinalized(String.format(Locale.US, "SELECT count, end FROM chat_pinned_count WHERE uid = %d", dialogId));
int newCount2;
if (cursor.next()) {
newCount2 = Math.max(0, cursor.intValue(0) - updatedCount);
endReached = cursor.intValue(1) != 0;
} else {
newCount2 = 0;
endReached = false;
}
cursor.dispose();
newCount = Math.max(newCount1, newCount2);
}
SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_pinned_count VALUES(?, ?, ?)");
state.requery();
state.bindLong(1, dialogId);
state.bindInteger(2, newCount);
state.bindInteger(3, endReached ? 1 : 0);
state.step();
state.dispose();
AndroidUtilities.runOnUIThread(() -> getNotificationCenter().postNotificationName(NotificationCenter.didLoadPinnedMessages, dialogId, ids, false, null, messages, maxId, newCount, endReached));
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesController method reloadMentionsCountForChannel.
public void reloadMentionsCountForChannel(TLRPC.InputPeer peer, long taskId) {
long newTaskId;
if (taskId == 0) {
NativeByteBuffer data = null;
try {
data = new NativeByteBuffer(4 + peer.getObjectSize());
data.writeInt32(22);
peer.serializeToStream(data);
} catch (Exception e) {
FileLog.e(e);
}
newTaskId = getMessagesStorage().createPendingTask(data);
} else {
newTaskId = taskId;
}
TLRPC.TL_messages_getUnreadMentions req = new TLRPC.TL_messages_getUnreadMentions();
req.peer = peer;
req.limit = 1;
getConnectionsManager().sendRequest(req, (response, error) -> {
TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
if (res != null) {
int newCount;
if (res.count != 0) {
newCount = res.count;
} else {
newCount = res.messages.size();
}
getMessagesStorage().resetMentionsCount(-peer.channel_id, newCount);
}
if (newTaskId != 0) {
getMessagesStorage().removePendingTask(newTaskId);
}
});
}
Aggregations