diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.cpp b/Telegram/SourceFiles/data/data_scheduled_messages.cpp index 042bbcb443..e0ee4c8355 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.cpp +++ b/Telegram/SourceFiles/data/data_scheduled_messages.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "history/history.h" #include "history/history_item_components.h" +#include "history/history_message.h" #include "apiwrap.h" namespace Data { @@ -116,6 +117,56 @@ int ScheduledMessages::count(not_null history) const { return (i != end(_data)) ? i->second.items.size() : 0; } +void ScheduledMessages::sendNowSimpleMessage( + const MTPDupdateShortSentMessage &update, + not_null local) { + Expects(local->isSending()); + Expects(local->isScheduled()); + Expects(local->date() == kScheduledUntilOnlineTimestamp); + + // When the user sends a text message scheduled until online + // while the recipient is already online, the server sends + // updateShortSentMessage to the client and the client calls this method. + // Since such messages can only be sent to recipients, + // we know for sure that a message can't have fields such as the author, + // views count, etc. + + const auto &history = local->history(); + auto flags = NewMessageFlags(history->peer) + | MTPDmessage::Flag::f_entities + | MTPDmessage::Flag::f_from_id + | (local->replyToId() + ? MTPDmessage::Flag::f_reply_to_msg_id + : MTPDmessage::Flag(0)); + auto clientFlags = NewMessageClientFlags() + | MTPDmessage_ClientFlag::f_local_history_entry; + + history->addNewMessage( + MTP_message( + MTP_flags(flags), + update.vid(), + MTP_int(_session->userId()), + peerToMTP(history->peer->id), + MTPMessageFwdHeader(), + MTPint(), + MTP_int(local->replyToId()), + update.vdate(), + MTP_string(local->originalText().text), + MTP_messageMediaEmpty(), + MTPReplyMarkup(), + Api::EntitiesToMTP(local->originalText().entities), + MTP_int(1), + MTPint(), + MTP_string(), + MTPlong(), + //MTPMessageReactions(), + MTPVector()), + clientFlags, + NewMessageType::Unread); + + local->destroy(); +} + void ScheduledMessages::apply(const MTPDupdateNewScheduledMessage &update) { const auto &message = update.vmessage(); const auto peer = PeerFromMessage(message); diff --git a/Telegram/SourceFiles/data/data_scheduled_messages.h b/Telegram/SourceFiles/data/data_scheduled_messages.h index 09a2c3eb4b..73e683d84c 100644 --- a/Telegram/SourceFiles/data/data_scheduled_messages.h +++ b/Telegram/SourceFiles/data/data_scheduled_messages.h @@ -41,6 +41,10 @@ public: void appendSending(not_null item); void removeSending(not_null item); + void sendNowSimpleMessage( + const MTPDupdateShortSentMessage &update, + not_null local); + [[nodiscard]] rpl::producer<> updates(not_null history); [[nodiscard]] Data::MessagesSlice list(not_null history); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 8d9ab64909..4dfd751993 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -3799,14 +3799,19 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) { if (!IsServerMsgId(d.vid().v)) { LOG(("API Error: Bad msgId got from server: %1").arg(d.vid().v)); } else if (randomId) { - const auto sent = session().data().messageSentData(randomId); + auto &owner = session().data(); + const auto sent = owner.messageSentData(randomId); const auto lookupMessage = [&] { return sent.peerId - ? session().data().message( - peerToChannel(sent.peerId), - d.vid().v) + ? owner.message(peerToChannel(sent.peerId), d.vid().v) : nullptr; }; + if (const auto id = owner.messageIdByRandomId(randomId)) { + if (const auto local = owner.message(id); + local->isScheduled()) { + owner.scheduledMessages().sendNowSimpleMessage(d, local); + } + } const auto wasAlready = (lookupMessage() != nullptr); feedUpdate(MTP_updateMessageID(d.vid(), MTP_long(randomId))); // ignore real date if (const auto item = lookupMessage()) {