use of org.telegram.messenger.support.SparseLongArray in project Telegram-FOSS by Telegram-FOSS-Team.
the class SendMessagesHelper method sendMessage.
public int sendMessage(ArrayList<MessageObject> messages, final long peer, boolean forwardFromMyName, boolean hideCaption, boolean notify, int scheduleDate) {
if (messages == null || messages.isEmpty()) {
return 0;
}
int sendResult = 0;
long myId = getUserConfig().getClientUserId();
boolean isChannel = false;
if (!DialogObject.isEncryptedDialog(peer)) {
final TLRPC.Peer peer_id = getMessagesController().getPeer(peer);
boolean isSignature = false;
boolean canSendStickers = true;
boolean canSendMedia = true;
boolean canSendPolls = true;
boolean canSendPreview = true;
String rank = null;
long linkedToGroup = 0;
TLRPC.Chat chat;
if (DialogObject.isUserDialog(peer)) {
TLRPC.User sendToUser = getMessagesController().getUser(peer);
if (sendToUser == null) {
return 0;
}
chat = null;
} else {
chat = getMessagesController().getChat(-peer);
if (ChatObject.isChannel(chat)) {
isSignature = chat.signatures;
isChannel = !chat.megagroup;
if (isChannel && chat.has_link) {
TLRPC.ChatFull chatFull = getMessagesController().getChatFull(chat.id);
if (chatFull != null) {
linkedToGroup = chatFull.linked_chat_id;
}
}
}
if (chat != null) {
rank = getMessagesController().getAdminRank(chat.id, myId);
}
canSendStickers = ChatObject.canSendStickers(chat);
canSendMedia = ChatObject.canSendMedia(chat);
canSendPreview = ChatObject.canSendEmbed(chat);
canSendPolls = ChatObject.canSendPolls(chat);
}
LongSparseArray<Long> groupsMap = new LongSparseArray<>();
ArrayList<MessageObject> objArr = new ArrayList<>();
ArrayList<TLRPC.Message> arr = new ArrayList<>();
ArrayList<Long> randomIds = new ArrayList<>();
ArrayList<Integer> ids = new ArrayList<>();
LongSparseArray<TLRPC.Message> messagesByRandomIds = new LongSparseArray<>();
TLRPC.InputPeer inputPeer = getMessagesController().getInputPeer(peer);
long lastDialogId = 0;
final boolean toMyself = peer == myId;
long lastGroupedId;
for (int a = 0; a < messages.size(); a++) {
MessageObject msgObj = messages.get(a);
if (msgObj.getId() <= 0 || msgObj.needDrawBluredPreview()) {
if (msgObj.type == 0 && !TextUtils.isEmpty(msgObj.messageText)) {
TLRPC.WebPage webPage = msgObj.messageOwner.media != null ? msgObj.messageOwner.media.webpage : null;
sendMessage(msgObj.messageText.toString(), peer, null, null, webPage, webPage != null, msgObj.messageOwner.entities, null, null, notify, scheduleDate, null);
}
continue;
}
if (!canSendStickers && (msgObj.isSticker() || msgObj.isAnimatedSticker() || msgObj.isGif() || msgObj.isGame())) {
if (sendResult == 0) {
sendResult = ChatObject.isActionBannedByDefault(chat, ChatObject.ACTION_SEND_STICKERS) ? 4 : 1;
}
continue;
} else if (!canSendMedia && (msgObj.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto || msgObj.messageOwner.media instanceof TLRPC.TL_messageMediaDocument)) {
if (sendResult == 0) {
sendResult = ChatObject.isActionBannedByDefault(chat, ChatObject.ACTION_SEND_MEDIA) ? 5 : 2;
}
continue;
} else if (!canSendPolls && msgObj.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
if (sendResult == 0) {
sendResult = ChatObject.isActionBannedByDefault(chat, ChatObject.ACTION_SEND_POLLS) ? 6 : 3;
}
continue;
}
final TLRPC.Message newMsg = new TLRPC.TL_message();
if (!forwardFromMyName) {
boolean forwardFromSaved = msgObj.getDialogId() == myId && msgObj.isFromUser() && msgObj.messageOwner.from_id.user_id == myId;
if (msgObj.isForwarded()) {
newMsg.fwd_from = new TLRPC.TL_messageFwdHeader();
if ((msgObj.messageOwner.fwd_from.flags & 1) != 0) {
newMsg.fwd_from.flags |= 1;
newMsg.fwd_from.from_id = msgObj.messageOwner.fwd_from.from_id;
}
if ((msgObj.messageOwner.fwd_from.flags & 32) != 0) {
newMsg.fwd_from.flags |= 32;
newMsg.fwd_from.from_name = msgObj.messageOwner.fwd_from.from_name;
}
if ((msgObj.messageOwner.fwd_from.flags & 4) != 0) {
newMsg.fwd_from.flags |= 4;
newMsg.fwd_from.channel_post = msgObj.messageOwner.fwd_from.channel_post;
}
if ((msgObj.messageOwner.fwd_from.flags & 8) != 0) {
newMsg.fwd_from.flags |= 8;
newMsg.fwd_from.post_author = msgObj.messageOwner.fwd_from.post_author;
}
if ((peer == myId || isChannel) && (msgObj.messageOwner.fwd_from.flags & 16) != 0 && !UserObject.isReplyUser(msgObj.getDialogId())) {
newMsg.fwd_from.flags |= 16;
newMsg.fwd_from.saved_from_peer = msgObj.messageOwner.fwd_from.saved_from_peer;
newMsg.fwd_from.saved_from_msg_id = msgObj.messageOwner.fwd_from.saved_from_msg_id;
}
newMsg.fwd_from.date = msgObj.messageOwner.fwd_from.date;
newMsg.flags = TLRPC.MESSAGE_FLAG_FWD;
} else if (!forwardFromSaved) {
// if (!toMyself || !msgObj.isOutOwner())
long fromId = msgObj.getFromChatId();
newMsg.fwd_from = new TLRPC.TL_messageFwdHeader();
newMsg.fwd_from.channel_post = msgObj.getId();
newMsg.fwd_from.flags |= 4;
if (msgObj.isFromUser()) {
newMsg.fwd_from.from_id = msgObj.messageOwner.from_id;
newMsg.fwd_from.flags |= 1;
} else {
newMsg.fwd_from.from_id = new TLRPC.TL_peerChannel();
newMsg.fwd_from.from_id.channel_id = msgObj.messageOwner.peer_id.channel_id;
newMsg.fwd_from.flags |= 1;
if (msgObj.messageOwner.post && fromId > 0) {
newMsg.fwd_from.from_id = msgObj.messageOwner.from_id != null ? msgObj.messageOwner.from_id : msgObj.messageOwner.peer_id;
}
}
if (msgObj.messageOwner.post_author != null) {
/*newMsg.fwd_from.post_author = msgObj.messageOwner.post_author;
newMsg.fwd_from.flags |= 8;*/
} else if (!msgObj.isOutOwner() && fromId > 0 && msgObj.messageOwner.post) {
TLRPC.User signUser = getMessagesController().getUser(fromId);
if (signUser != null) {
newMsg.fwd_from.post_author = ContactsController.formatName(signUser.first_name, signUser.last_name);
newMsg.fwd_from.flags |= 8;
}
}
newMsg.date = msgObj.messageOwner.date;
newMsg.flags = TLRPC.MESSAGE_FLAG_FWD;
}
if (peer == myId && newMsg.fwd_from != null) {
newMsg.fwd_from.flags |= 16;
newMsg.fwd_from.saved_from_msg_id = msgObj.getId();
newMsg.fwd_from.saved_from_peer = msgObj.messageOwner.peer_id;
if (newMsg.fwd_from.saved_from_peer.user_id == myId) {
newMsg.fwd_from.saved_from_peer.user_id = msgObj.getDialogId();
}
}
}
newMsg.params = new HashMap<>();
newMsg.params.put("fwd_id", "" + msgObj.getId());
newMsg.params.put("fwd_peer", "" + msgObj.getDialogId());
if (!msgObj.messageOwner.restriction_reason.isEmpty()) {
newMsg.restriction_reason = msgObj.messageOwner.restriction_reason;
newMsg.flags |= 4194304;
}
if (!canSendPreview && msgObj.messageOwner.media instanceof TLRPC.TL_messageMediaWebPage) {
newMsg.media = new TLRPC.TL_messageMediaEmpty();
} else {
newMsg.media = msgObj.messageOwner.media;
}
if (newMsg.media != null) {
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_MEDIA;
}
if (msgObj.messageOwner.via_bot_id != 0) {
newMsg.via_bot_id = msgObj.messageOwner.via_bot_id;
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_BOT_ID;
}
if (linkedToGroup != 0) {
newMsg.replies = new TLRPC.TL_messageReplies();
newMsg.replies.comments = true;
newMsg.replies.channel_id = linkedToGroup;
newMsg.replies.flags |= 1;
newMsg.flags |= 8388608;
}
if (!hideCaption || newMsg.media == null) {
newMsg.message = msgObj.messageOwner.message;
}
if (newMsg.message == null) {
newMsg.message = "";
}
newMsg.fwd_msg_id = msgObj.getId();
newMsg.attachPath = msgObj.messageOwner.attachPath;
newMsg.entities = msgObj.messageOwner.entities;
if (msgObj.messageOwner.reply_markup instanceof TLRPC.TL_replyInlineMarkup) {
newMsg.reply_markup = new TLRPC.TL_replyInlineMarkup();
boolean dropMarkup = false;
for (int b = 0, N = msgObj.messageOwner.reply_markup.rows.size(); b < N; b++) {
TLRPC.TL_keyboardButtonRow oldRow = msgObj.messageOwner.reply_markup.rows.get(b);
TLRPC.TL_keyboardButtonRow newRow = null;
for (int c = 0, N2 = oldRow.buttons.size(); c < N2; c++) {
TLRPC.KeyboardButton button = oldRow.buttons.get(c);
if (button instanceof TLRPC.TL_keyboardButtonUrlAuth || button instanceof TLRPC.TL_keyboardButtonUrl || button instanceof TLRPC.TL_keyboardButtonSwitchInline || button instanceof TLRPC.TL_keyboardButtonBuy) {
if (button instanceof TLRPC.TL_keyboardButtonUrlAuth) {
TLRPC.TL_keyboardButtonUrlAuth auth = new TLRPC.TL_keyboardButtonUrlAuth();
auth.flags = button.flags;
if (button.fwd_text != null) {
auth.text = auth.fwd_text = button.fwd_text;
} else {
auth.text = button.text;
}
auth.url = button.url;
auth.button_id = button.button_id;
button = auth;
}
if (newRow == null) {
newRow = new TLRPC.TL_keyboardButtonRow();
newMsg.reply_markup.rows.add(newRow);
}
newRow.buttons.add(button);
} else {
dropMarkup = true;
break;
}
}
if (dropMarkup) {
break;
}
}
if (!dropMarkup) {
newMsg.flags |= 64;
} else {
msgObj.messageOwner.reply_markup = null;
newMsg.flags &= ~64;
}
}
if (!newMsg.entities.isEmpty()) {
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_ENTITIES;
}
if (newMsg.attachPath == null) {
newMsg.attachPath = "";
}
newMsg.local_id = newMsg.id = getUserConfig().getNewMessageId();
newMsg.out = true;
if ((lastGroupedId = msgObj.messageOwner.grouped_id) != 0) {
Long gId = groupsMap.get(msgObj.messageOwner.grouped_id);
if (gId == null) {
gId = Utilities.random.nextLong();
groupsMap.put(msgObj.messageOwner.grouped_id, gId);
}
newMsg.grouped_id = gId;
newMsg.flags |= 131072;
}
if (peer_id.channel_id != 0 && isChannel) {
if (isSignature) {
newMsg.from_id = new TLRPC.TL_peerUser();
newMsg.from_id.user_id = myId;
} else {
newMsg.from_id = peer_id;
}
newMsg.post = true;
} else {
long fromPeerId = ChatObject.getSendAsPeerId(chat, getMessagesController().getChatFull(-peer), true);
if (fromPeerId == myId) {
newMsg.from_id = new TLRPC.TL_peerUser();
newMsg.from_id.user_id = myId;
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_FROM_ID;
} else {
newMsg.from_id = getMessagesController().getPeer(fromPeerId);
if (rank != null) {
newMsg.post_author = rank;
newMsg.flags |= 65536;
}
}
}
if (newMsg.random_id == 0) {
newMsg.random_id = getNextRandomId();
}
randomIds.add(newMsg.random_id);
messagesByRandomIds.put(newMsg.random_id, newMsg);
ids.add(newMsg.fwd_msg_id);
newMsg.date = scheduleDate != 0 ? scheduleDate : getConnectionsManager().getCurrentTime();
if (inputPeer instanceof TLRPC.TL_inputPeerChannel && isChannel) {
if (scheduleDate == 0) {
newMsg.views = 1;
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_VIEWS;
}
} else {
if ((msgObj.messageOwner.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
if (scheduleDate == 0) {
newMsg.views = msgObj.messageOwner.views;
newMsg.flags |= TLRPC.MESSAGE_FLAG_HAS_VIEWS;
}
}
newMsg.unread = true;
}
newMsg.dialog_id = peer;
newMsg.peer_id = peer_id;
if (MessageObject.isVoiceMessage(newMsg) || MessageObject.isRoundVideoMessage(newMsg)) {
if (inputPeer instanceof TLRPC.TL_inputPeerChannel && msgObj.getChannelId() != 0) {
newMsg.media_unread = msgObj.isContentUnread();
} else {
newMsg.media_unread = true;
}
}
MessageObject newMsgObj = new MessageObject(currentAccount, newMsg, true, true);
newMsgObj.scheduled = scheduleDate != 0;
newMsgObj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENDING;
newMsgObj.wasJustSent = true;
objArr.add(newMsgObj);
arr.add(newMsg);
putToSendingMessages(newMsg, scheduleDate != 0);
boolean differentDialog = false;
if (BuildVars.LOGS_ENABLED) {
FileLog.d("forward message user_id = " + inputPeer.user_id + " chat_id = " + inputPeer.chat_id + " channel_id = " + inputPeer.channel_id + " access_hash = " + inputPeer.access_hash);
}
if (arr.size() == 100 || a == messages.size() - 1 || a != messages.size() - 1 && messages.get(a + 1).getDialogId() != msgObj.getDialogId()) {
getMessagesStorage().putMessages(new ArrayList<>(arr), false, true, false, 0, scheduleDate != 0);
getMessagesController().updateInterfaceWithMessages(peer, objArr, scheduleDate != 0);
getNotificationCenter().postNotificationName(NotificationCenter.dialogsNeedReload);
getUserConfig().saveConfig(false);
final TLRPC.TL_messages_forwardMessages req = new TLRPC.TL_messages_forwardMessages();
req.to_peer = inputPeer;
req.silent = !notify || MessagesController.getNotificationsSettings(currentAccount).getBoolean("silent_" + peer, false);
if (scheduleDate != 0) {
req.schedule_date = scheduleDate;
req.flags |= 1024;
}
if (msgObj.messageOwner.peer_id instanceof TLRPC.TL_peerChannel) {
TLRPC.Chat channel = getMessagesController().getChat(msgObj.messageOwner.peer_id.channel_id);
req.from_peer = new TLRPC.TL_inputPeerChannel();
req.from_peer.channel_id = msgObj.messageOwner.peer_id.channel_id;
if (channel != null) {
req.from_peer.access_hash = channel.access_hash;
}
} else {
req.from_peer = new TLRPC.TL_inputPeerEmpty();
}
req.random_id = randomIds;
req.id = ids;
req.drop_author = forwardFromMyName;
req.drop_media_captions = hideCaption;
req.with_my_score = messages.size() == 1 && messages.get(0).messageOwner.with_my_score;
final ArrayList<TLRPC.Message> newMsgObjArr = arr;
final ArrayList<MessageObject> newMsgArr = new ArrayList<>(objArr);
final LongSparseArray<TLRPC.Message> messagesByRandomIdsFinal = messagesByRandomIds;
boolean scheduledOnline = scheduleDate == 0x7FFFFFFE;
getConnectionsManager().sendRequest(req, (response, error) -> {
if (error == null) {
SparseLongArray newMessagesByIds = new SparseLongArray();
TLRPC.Updates updates = (TLRPC.Updates) response;
for (int a1 = 0; a1 < updates.updates.size(); a1++) {
TLRPC.Update update = updates.updates.get(a1);
if (update instanceof TLRPC.TL_updateMessageID) {
TLRPC.TL_updateMessageID updateMessageID = (TLRPC.TL_updateMessageID) update;
newMessagesByIds.put(updateMessageID.id, updateMessageID.random_id);
updates.updates.remove(a1);
a1--;
}
}
Integer value = getMessagesController().dialogs_read_outbox_max.get(peer);
if (value == null) {
value = getMessagesStorage().getDialogReadMax(true, peer);
getMessagesController().dialogs_read_outbox_max.put(peer, value);
}
int sentCount = 0;
for (int a1 = 0; a1 < updates.updates.size(); a1++) {
TLRPC.Update update = updates.updates.get(a1);
if (update instanceof TLRPC.TL_updateNewMessage || update instanceof TLRPC.TL_updateNewChannelMessage || update instanceof TLRPC.TL_updateNewScheduledMessage) {
boolean currentSchedule = scheduleDate != 0;
updates.updates.remove(a1);
a1--;
final TLRPC.Message message;
if (update instanceof TLRPC.TL_updateNewMessage) {
TLRPC.TL_updateNewMessage updateNewMessage = (TLRPC.TL_updateNewMessage) update;
message = updateNewMessage.message;
getMessagesController().processNewDifferenceParams(-1, updateNewMessage.pts, -1, updateNewMessage.pts_count);
} else if (update instanceof TLRPC.TL_updateNewScheduledMessage) {
TLRPC.TL_updateNewScheduledMessage updateNewMessage = (TLRPC.TL_updateNewScheduledMessage) update;
message = updateNewMessage.message;
} else {
TLRPC.TL_updateNewChannelMessage updateNewChannelMessage = (TLRPC.TL_updateNewChannelMessage) update;
message = updateNewChannelMessage.message;
getMessagesController().processNewChannelDifferenceParams(updateNewChannelMessage.pts, updateNewChannelMessage.pts_count, message.peer_id.channel_id);
}
if (scheduledOnline && message.date != 0x7FFFFFFE) {
currentSchedule = false;
}
ImageLoader.saveMessageThumbs(message);
if (!currentSchedule) {
message.unread = value < message.id;
}
if (toMyself) {
message.out = true;
message.unread = false;
message.media_unread = false;
}
long random_id = newMessagesByIds.get(message.id);
if (random_id != 0) {
final TLRPC.Message newMsgObj1 = messagesByRandomIdsFinal.get(random_id);
if (newMsgObj1 == null) {
continue;
}
int index = newMsgObjArr.indexOf(newMsgObj1);
if (index == -1) {
continue;
}
MessageObject msgObj1 = newMsgArr.get(index);
newMsgObjArr.remove(index);
newMsgArr.remove(index);
final int oldId = newMsgObj1.id;
final ArrayList<TLRPC.Message> sentMessages = new ArrayList<>();
sentMessages.add(message);
msgObj1.messageOwner.post_author = message.post_author;
if ((message.flags & 33554432) != 0) {
msgObj1.messageOwner.ttl_period = message.ttl_period;
msgObj1.messageOwner.flags |= 33554432;
}
updateMediaPaths(msgObj1, message, message.id, null, true);
int existFlags = msgObj1.getMediaExistanceFlags();
newMsgObj1.id = message.id;
sentCount++;
if (scheduleDate != 0 && !currentSchedule) {
AndroidUtilities.runOnUIThread(() -> {
ArrayList<Integer> messageIds = new ArrayList<>();
messageIds.add(oldId);
getMessagesController().deleteMessages(messageIds, null, null, newMsgObj1.dialog_id, false, true);
getMessagesStorage().getStorageQueue().postRunnable(() -> {
getMessagesStorage().putMessages(sentMessages, true, false, false, 0, false);
AndroidUtilities.runOnUIThread(() -> {
ArrayList<MessageObject> messageObjects = new ArrayList<>();
messageObjects.add(new MessageObject(msgObj.currentAccount, msgObj.messageOwner, true, true));
getMessagesController().updateInterfaceWithMessages(newMsgObj1.dialog_id, messageObjects, false);
getMediaDataController().increasePeerRaiting(newMsgObj1.dialog_id);
processSentMessage(oldId);
removeFromSendingMessages(oldId, scheduleDate != 0);
});
});
});
} else {
getMessagesStorage().getStorageQueue().postRunnable(() -> {
getMessagesStorage().updateMessageStateAndId(newMsgObj1.random_id, MessageObject.getPeerId(peer_id), oldId, newMsgObj1.id, 0, false, scheduleDate != 0 ? 1 : 0);
getMessagesStorage().putMessages(sentMessages, true, false, false, 0, scheduleDate != 0);
AndroidUtilities.runOnUIThread(() -> {
newMsgObj1.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
getMediaDataController().increasePeerRaiting(peer);
getNotificationCenter().postNotificationName(NotificationCenter.messageReceivedByServer, oldId, message.id, message, peer, 0L, existFlags, scheduleDate != 0);
processSentMessage(oldId);
removeFromSendingMessages(oldId, scheduleDate != 0);
});
});
}
}
}
}
if (!updates.updates.isEmpty()) {
getMessagesController().processUpdates(updates, false);
}
getStatsController().incrementSentItemsCount(ApplicationLoader.getCurrentNetworkType(), StatsController.TYPE_MESSAGES, sentCount);
} else {
AndroidUtilities.runOnUIThread(() -> AlertsCreator.processError(currentAccount, error, null, req));
}
for (int a1 = 0; a1 < newMsgObjArr.size(); a1++) {
final TLRPC.Message newMsgObj1 = newMsgObjArr.get(a1);
getMessagesStorage().markMessageAsSendError(newMsgObj1, scheduleDate != 0);
AndroidUtilities.runOnUIThread(() -> {
newMsgObj1.send_state = MessageObject.MESSAGE_SEND_STATE_SEND_ERROR;
getNotificationCenter().postNotificationName(NotificationCenter.messageSendError, newMsgObj1.id);
processSentMessage(newMsgObj1.id);
removeFromSendingMessages(newMsgObj1.id, scheduleDate != 0);
});
}
}, ConnectionsManager.RequestFlagCanCompress | ConnectionsManager.RequestFlagInvokeAfter);
if (a != messages.size() - 1) {
objArr = new ArrayList<>();
arr = new ArrayList<>();
randomIds = new ArrayList<>();
ids = new ArrayList<>();
messagesByRandomIds = new LongSparseArray<>();
}
}
}
} else {
for (int a = 0; a < messages.size(); a++) {
processForwardFromMyName(messages.get(a), peer);
}
}
return sendResult;
}
Aggregations