use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method updateRepliesMaxReadIdInternal.
private void updateRepliesMaxReadIdInternal(long chatId, int mid, int readMaxId) {
try {
long dialogId = -chatId;
SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
TLRPC.MessageReplies currentReplies = null;
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", mid, dialogId));
if (cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
currentReplies = TLRPC.MessageReplies.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
}
cursor.dispose();
if (currentReplies != null) {
currentReplies.read_max_id = readMaxId;
state.requery();
NativeByteBuffer data = new NativeByteBuffer(currentReplies.getObjectSize());
currentReplies.serializeToStream(data);
state.bindByteBuffer(1, data);
state.bindInteger(2, mid);
state.bindLong(3, dialogId);
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 putEncryptedChat.
public void putEncryptedChat(TLRPC.EncryptedChat chat, TLRPC.User user, TLRPC.Dialog dialog) {
if (chat == null) {
return;
}
storageQueue.postRunnable(() -> {
try {
if ((chat.key_hash == null || chat.key_hash.length < 16) && chat.auth_key != null) {
chat.key_hash = AndroidUtilities.calcAuthKeyHash(chat.auth_key);
}
SQLitePreparedStatement state = database.executeFast("REPLACE INTO enc_chats VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
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.bindInteger(1, chat.id);
state.bindLong(2, user.id);
state.bindString(3, formatUserSearchName(user));
state.bindByteBuffer(4, 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(5, data2);
state.bindByteBuffer(6, data3);
state.bindInteger(7, chat.ttl);
state.bindInteger(8, chat.layer);
state.bindInteger(9, chat.seq_in);
state.bindInteger(10, chat.seq_out);
state.bindInteger(11, (int) chat.key_use_count_in << 16 | chat.key_use_count_out);
state.bindLong(12, chat.exchange_id);
state.bindInteger(13, chat.key_create_date);
state.bindLong(14, chat.future_key_fingerprint);
state.bindByteBuffer(15, data4);
state.bindByteBuffer(16, data5);
state.bindInteger(17, chat.in_seq_no);
state.bindLong(18, chat.admin_id);
state.bindInteger(19, chat.mtproto_seq);
state.step();
state.dispose();
data.reuse();
data2.reuse();
data3.reuse();
data4.reuse();
data5.reuse();
if (dialog != null) {
state = database.executeFast("REPLACE INTO dialogs VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
state.bindLong(1, dialog.id);
state.bindInteger(2, dialog.last_message_date);
state.bindInteger(3, dialog.unread_count);
state.bindInteger(4, dialog.top_message);
state.bindInteger(5, dialog.read_inbox_max_id);
state.bindInteger(6, dialog.read_outbox_max_id);
state.bindInteger(7, 0);
state.bindInteger(8, dialog.unread_mentions_count);
state.bindInteger(9, dialog.pts);
state.bindInteger(10, 0);
state.bindInteger(11, dialog.pinnedNum);
state.bindInteger(12, dialog.flags);
state.bindInteger(13, dialog.folder_id);
state.bindNull(14);
state.bindInteger(15, dialog.unread_reactions_count);
state.step();
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 putChannelAdmins.
public void putChannelAdmins(long chatId, LongSparseArray<TLRPC.ChannelParticipant> ids) {
storageQueue.postRunnable(() -> {
try {
database.executeFast("DELETE FROM channel_admins_v3 WHERE did = " + chatId).stepThis().dispose();
database.beginTransaction();
SQLitePreparedStatement state = database.executeFast("REPLACE INTO channel_admins_v3 VALUES(?, ?, ?)");
int date = (int) (System.currentTimeMillis() / 1000);
NativeByteBuffer data;
for (int a = 0; a < ids.size(); a++) {
state.requery();
state.bindLong(1, chatId);
state.bindLong(2, ids.keyAt(a));
TLRPC.ChannelParticipant participant = ids.valueAt(a);
data = new NativeByteBuffer(participant.getObjectSize());
participant.serializeToStream(data);
state.bindByteBuffer(3, data);
state.step();
data.reuse();
}
state.dispose();
database.commitTransaction();
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method updateChatInfo.
public void updateChatInfo(TLRPC.ChatFull info, boolean ifExist) {
storageQueue.postRunnable(() -> {
try {
int currentOnline = -1;
int inviter = 0;
int links = 0;
SQLiteCursor cursor = database.queryFinalized("SELECT online, inviter, links FROM chat_settings_v2 WHERE uid = " + info.id);
if (cursor.next()) {
currentOnline = cursor.intValue(0);
info.inviterId = cursor.longValue(1);
links = cursor.intValue(2);
}
cursor.dispose();
if (ifExist && currentOnline == -1) {
return;
}
if (currentOnline >= 0 && (info.flags & 8192) == 0) {
info.online_count = currentOnline;
}
if (links >= 0) {
info.invitesCount = links;
}
SQLitePreparedStatement state = database.executeFast("REPLACE INTO chat_settings_v2 VALUES(?, ?, ?, ?, ?, ?)");
NativeByteBuffer data = new NativeByteBuffer(info.getObjectSize());
info.serializeToStream(data);
state.bindLong(1, info.id);
state.bindByteBuffer(2, data);
state.bindInteger(3, info.pinned_msg_id);
state.bindInteger(4, info.online_count);
state.bindLong(5, info.inviterId);
state.bindInteger(6, info.invitesCount);
state.step();
state.dispose();
data.reuse();
if (info instanceof TLRPC.TL_channelFull) {
cursor = database.queryFinalized("SELECT inbox_max, outbox_max FROM dialogs WHERE did = " + (-info.id));
if (cursor.next()) {
int inbox_max = cursor.intValue(0);
if (inbox_max < info.read_inbox_max_id) {
int outbox_max = cursor.intValue(1);
state = database.executeFast("UPDATE dialogs SET unread_count = ?, inbox_max = ?, outbox_max = ? WHERE did = ?");
state.bindInteger(1, info.unread_count);
state.bindInteger(2, info.read_inbox_max_id);
state.bindInteger(3, Math.max(outbox_max, info.read_outbox_max_id));
state.bindLong(4, -info.id);
state.step();
state.dispose();
}
}
cursor.dispose();
}
if ((info.flags & 2048) != 0) {
state = database.executeFast("UPDATE dialogs SET folder_id = ? WHERE did = ?");
state.bindInteger(1, info.folder_id);
state.bindLong(2, -info.id);
state.step();
state.dispose();
unknownDialogsIds.remove(-info.id);
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
use of org.telegram.tgnet.NativeByteBuffer in project Telegram-FOSS by Telegram-FOSS-Team.
the class MessagesStorage method putChannelViews.
public void putChannelViews(LongSparseArray<SparseIntArray> channelViews, LongSparseArray<SparseIntArray> channelForwards, LongSparseArray<SparseArray<TLRPC.MessageReplies>> channelReplies, boolean addReply) {
if (isEmpty(channelViews) && isEmpty(channelForwards) && isEmpty(channelReplies)) {
return;
}
storageQueue.postRunnable(() -> {
try {
database.beginTransaction();
if (!isEmpty(channelViews)) {
SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET media = max((SELECT media FROM messages_v2 WHERE mid = ? AND uid = ?), ?) WHERE mid = ? AND uid = ?");
for (int a = 0; a < channelViews.size(); a++) {
long peer = channelViews.keyAt(a);
SparseIntArray messages = channelViews.valueAt(a);
for (int b = 0, N = messages.size(); b < N; b++) {
int views = messages.valueAt(b);
int messageId = messages.keyAt(b);
state.requery();
state.bindInteger(1, messageId);
state.bindLong(2, peer);
state.bindInteger(3, views);
state.bindInteger(4, messageId);
state.bindLong(5, peer);
state.step();
}
}
state.dispose();
}
if (!isEmpty(channelForwards)) {
SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET forwards = max((SELECT forwards FROM messages_v2 WHERE mid = ? AND uid = ?), ?) WHERE mid = ? AND uid = ?");
for (int a = 0; a < channelForwards.size(); a++) {
long peer = channelForwards.keyAt(a);
SparseIntArray messages = channelForwards.valueAt(a);
for (int b = 0, N = messages.size(); b < N; b++) {
int forwards = messages.valueAt(b);
int messageId = messages.keyAt(b);
state.requery();
state.bindInteger(1, messageId);
state.bindLong(2, peer);
state.bindInteger(3, forwards);
state.bindInteger(4, messageId);
state.bindLong(5, peer);
state.step();
}
}
state.dispose();
}
if (!isEmpty(channelReplies)) {
SQLitePreparedStatement state = database.executeFast("UPDATE messages_v2 SET replies_data = ? WHERE mid = ? AND uid = ?");
for (int a = 0; a < channelReplies.size(); a++) {
long peer = channelReplies.keyAt(a);
SparseArray<TLRPC.MessageReplies> messages = channelReplies.valueAt(a);
for (int b = 0, N3 = messages.size(); b < N3; b++) {
int messageId = messages.keyAt(b);
boolean messageExists;
TLRPC.MessageReplies currentReplies = null;
SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT replies_data FROM messages_v2 WHERE mid = %d AND uid = %d", messageId, peer));
if (messageExists = cursor.next()) {
NativeByteBuffer data = cursor.byteBufferValue(0);
if (data != null) {
currentReplies = TLRPC.MessageReplies.TLdeserialize(data, data.readInt32(false), false);
data.reuse();
}
}
cursor.dispose();
if (!messageExists) {
continue;
}
TLRPC.MessageReplies replies = messages.get(messages.keyAt(b));
if (!addReply && currentReplies != null && currentReplies.replies_pts != 0 && replies.replies_pts <= currentReplies.replies_pts && replies.read_max_id <= currentReplies.read_max_id && replies.max_id <= currentReplies.max_id) {
continue;
}
if (addReply) {
if (currentReplies == null) {
currentReplies = new TLRPC.TL_messageReplies();
currentReplies.flags |= 2;
}
currentReplies.replies += replies.replies;
for (int c = 0, N = replies.recent_repliers.size(); c < N; c++) {
long id = MessageObject.getPeerId(replies.recent_repliers.get(c));
for (int d = 0, N2 = currentReplies.recent_repliers.size(); d < N2; d++) {
long id2 = MessageObject.getPeerId(currentReplies.recent_repliers.get(d));
if (id == id2) {
currentReplies.recent_repliers.remove(d);
d--;
N2--;
}
}
}
currentReplies.recent_repliers.addAll(0, replies.recent_repliers);
while (currentReplies.recent_repliers.size() > 3) {
currentReplies.recent_repliers.remove(0);
}
replies = currentReplies;
}
if (currentReplies != null && currentReplies.read_max_id > replies.read_max_id) {
replies.read_max_id = currentReplies.read_max_id;
}
state.requery();
NativeByteBuffer data = new NativeByteBuffer(replies.getObjectSize());
replies.serializeToStream(data);
state.bindByteBuffer(1, data);
state.bindInteger(2, messageId);
state.bindLong(3, peer);
state.step();
data.reuse();
}
}
state.dispose();
}
database.commitTransaction();
} catch (Exception e) {
FileLog.e(e);
}
});
}
Aggregations