Fix missing messages when forwarding to open chat

This commit is contained in:
Eric Kotato 2021-04-06 06:56:17 +03:00
parent 8d0b9ce1b5
commit 74cd006cac
6 changed files with 156 additions and 90 deletions

View file

@ -122,20 +122,18 @@ void SendExistingMedia(
session->data().registerMessageRandomId(randomId, newId); session->data().registerMessageRandomId(randomId, newId);
if (!forwarding) { history->addNewLocalMessage(
history->addNewLocalMessage( newId.msg,
newId.msg, flags,
flags, clientFlags,
clientFlags, 0,
0, replyTo,
replyTo, HistoryItem::NewMessageDate(message.action.options.scheduled),
HistoryItem::NewMessageDate(message.action.options.scheduled), messageFromId,
messageFromId, messagePostAuthor,
messagePostAuthor, media,
media, caption,
caption, MTPReplyMarkup());
MTPReplyMarkup());
}
auto performRequest = [=](const auto &repeatRequest) -> void { auto performRequest = [=](const auto &repeatRequest) -> void {
auto &histories = history->owner().histories(); auto &histories = history->owner().histories();
@ -301,36 +299,34 @@ bool SendDice(
session->data().registerMessageRandomId(randomId, newId); session->data().registerMessageRandomId(randomId, newId);
if (!forwarding) { const auto views = 1;
const auto views = 1; const auto forwards = 0;
const auto forwards = 0; history->addNewMessage(
history->addNewMessage( MTP_message(
MTP_message( MTP_flags(flags),
MTP_flags(flags), MTP_int(newId.msg),
MTP_int(newId.msg), peerToMTP(messageFromId),
peerToMTP(messageFromId), peerToMTP(history->peer->id),
peerToMTP(history->peer->id), MTPMessageFwdHeader(),
MTPMessageFwdHeader(), MTPint(), // via_bot_id
MTPint(), // via_bot_id replyHeader,
replyHeader, MTP_int(HistoryItem::NewMessageDate(
MTP_int(HistoryItem::NewMessageDate( message.action.options.scheduled)),
message.action.options.scheduled)), MTP_string(),
MTP_string(), MTP_messageMediaDice(MTP_int(0), MTP_string(emoji)),
MTP_messageMediaDice(MTP_int(0), MTP_string(emoji)), MTPReplyMarkup(),
MTPReplyMarkup(), MTP_vector<MTPMessageEntity>(),
MTP_vector<MTPMessageEntity>(), MTP_int(views),
MTP_int(views), MTP_int(forwards),
MTP_int(forwards), MTPMessageReplies(),
MTPMessageReplies(), MTPint(), // edit_date
MTPint(), // edit_date MTP_string(messagePostAuthor),
MTP_string(messagePostAuthor), MTPlong(),
MTPlong(), //MTPMessageReactions(),
//MTPMessageReactions(), MTPVector<MTPRestrictionReason>(),
MTPVector<MTPRestrictionReason>(), MTPint()), // ttl_period
MTPint()), // ttl_period clientFlags,
clientFlags, NewMessageType::Unread);
NewMessageType::Unread);
}
const auto requestType = Data::Histories::RequestType::Send; const auto requestType = Data::Histories::RequestType::Send;
histories.sendRequest(history, requestType, [=](Fn<void()> finish) { histories.sendRequest(history, requestType, [=](Fn<void()> finish) {

View file

@ -3795,10 +3795,12 @@ void ApiWrap::forwardMessagesUnquoted(
auto currentGroupId = items.front()->groupId(); auto currentGroupId = items.front()->groupId();
auto lastGroup = LastGroupType::None; auto lastGroup = LastGroupType::None;
auto ids = QVector<MTPint>(); auto ids = QVector<MTPint>();
auto randomIds = QVector<MTPlong>(); auto randomIds = QVector<uint64>();
auto fromIter = items.begin(); auto fromIter = items.begin();
auto toIter = items.begin(); auto toIter = items.begin();
auto messageGroupCount = 0; auto messageGroupCount = 0;
auto messageFromId = anonymousPost ? 0 : _session->userPeerId();
auto messagePostAuthor = peer->isBroadcast() ? _session->user()->name : QString();
const auto needNextGroup = [&] (not_null<HistoryItem *> item) { const auto needNextGroup = [&] (not_null<HistoryItem *> item) {
auto lastGroupCheck = false; auto lastGroupCheck = false;
@ -3845,7 +3847,7 @@ void ApiWrap::forwardMessagesUnquoted(
auto currentIds = QVector<MTPint>(); auto currentIds = QVector<MTPint>();
currentIds.push_back(MTP_int(item->id)); currentIds.push_back(MTP_int(item->id));
auto currentRandomId = randomIds.takeFirst(); auto currentRandomId = MTP_long(randomIds.takeFirst());
auto currentRandomIds = QVector<MTPlong>(); auto currentRandomIds = QVector<MTPlong>();
currentRandomIds.push_back(currentRandomId); currentRandomIds.push_back(currentRandomId);
@ -3885,12 +3887,27 @@ void ApiWrap::forwardMessagesUnquoted(
mediaInputs->reserve(ids.size()); mediaInputs->reserve(ids.size());
mediaRefs->reserve(ids.size()); mediaRefs->reserve(ids.size());
const auto views = 1;
const auto forwards = 0;
const auto newGroupId = openssl::RandomValue<uint64>();
auto msgFlags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
auto clientFlags = NewMessageClientFlags();
FillMessagePostFlags(action, peer, msgFlags);
if (action.options.scheduled) {
msgFlags |= MTPDmessage::Flag::f_from_scheduled;
} else {
clientFlags |= MTPDmessage_ClientFlag::f_local_history_entry;
}
for (auto i = fromIter, e = toIter; i != e; i++) { for (auto i = fromIter, e = toIter; i != e; i++) {
const auto item = *i; const auto item = *i;
const auto media = item->media(); const auto media = item->media();
medias->push_back(media); medias->push_back(media);
auto inputMedia = media->photo() const auto inputMedia = media->photo()
? MTP_inputMediaPhoto(MTP_flags(0), media->photo()->mtpInput(), MTPint()) ? MTP_inputMediaPhoto(MTP_flags(0), media->photo()->mtpInput(), MTPint())
: MTP_inputMediaDocument(MTP_flags(0), media->document()->mtpInput(), MTPint(), MTPstring()); : MTP_inputMediaDocument(MTP_flags(0), media->document()->mtpInput(), MTPint(), MTPstring());
auto caption = cForwardCaptioned() auto caption = cForwardCaptioned()
@ -3905,14 +3922,49 @@ void ApiWrap::forwardMessagesUnquoted(
? MTPDinputSingleMedia::Flag::f_entities ? MTPDinputSingleMedia::Flag::f_entities
: MTPDinputSingleMedia::Flag(0); : MTPDinputSingleMedia::Flag(0);
const auto newId = FullMsgId(
peerToChannel(peer->id),
_session->data().nextLocalMessageId());
auto randomId = randomIds.takeFirst(); auto randomId = randomIds.takeFirst();
mediaInputs->push_back(MTP_inputSingleMedia( mediaInputs->push_back(MTP_inputSingleMedia(
MTP_flags(flags), MTP_flags(flags),
inputMedia, inputMedia,
randomId, MTP_long(randomId),
MTP_string(caption.text), MTP_string(caption.text),
sentEntities)); sentEntities));
_session->data().registerMessageRandomId(randomId, newId);
if (const auto photo = media->photo()) {
history->addNewLocalMessage(
newId.msg,
msgFlags,
clientFlags,
0, // viaBotId
0, // replyTo
HistoryItem::NewMessageDate(action.options.scheduled),
messageFromId,
messagePostAuthor,
photo,
caption,
MTPReplyMarkup(),
newGroupId);
} else if (const auto document = media->document()) {
history->addNewLocalMessage(
newId.msg,
msgFlags,
clientFlags,
0, // viaBotId
0, // replyTo
HistoryItem::NewMessageDate(action.options.scheduled),
messageFromId,
messagePostAuthor,
document,
caption,
MTPReplyMarkup(),
newGroupId);
}
} }
const auto finalFlags = MTPmessages_SendMultiMedia::Flags(0) const auto finalFlags = MTPmessages_SendMultiMedia::Flags(0)
@ -4149,7 +4201,7 @@ void ApiWrap::forwardMessagesUnquoted(
fromIter = i; fromIter = i;
} }
ids.push_back(MTP_int(item->id)); ids.push_back(MTP_int(item->id));
randomIds.push_back(MTP_long(randomId)); randomIds.push_back(randomId);
if (item->media() && item->media()->canBeGrouped()) { if (item->media() && item->media()->canBeGrouped()) {
lastGroup = ((item->media()->photo() lastGroup = ((item->media()->photo()
|| (item->media()->document() || (item->media()->document()
@ -4516,36 +4568,34 @@ void ApiWrap::sendMessage(
} else { } else {
clientFlags |= MTPDmessage_ClientFlag::f_local_history_entry; clientFlags |= MTPDmessage_ClientFlag::f_local_history_entry;
} }
if (!forwarding) { const auto views = 1;
const auto views = 1; const auto forwards = 0;
const auto forwards = 0; lastMessage = history->addNewMessage(
lastMessage = history->addNewMessage( MTP_message(
MTP_message( MTP_flags(flags),
MTP_flags(flags), MTP_int(newId.msg),
MTP_int(newId.msg), peerToMTP(messageFromId),
peerToMTP(messageFromId), peerToMTP(peer->id),
peerToMTP(peer->id), MTPMessageFwdHeader(),
MTPMessageFwdHeader(), MTPint(), // via_bot_id
MTPint(), // via_bot_id replyHeader,
replyHeader, MTP_int(
MTP_int( HistoryItem::NewMessageDate(action.options.scheduled)),
HistoryItem::NewMessageDate(action.options.scheduled)), msgText,
msgText, media,
media, MTPReplyMarkup(),
MTPReplyMarkup(), localEntities,
localEntities, MTP_int(views),
MTP_int(views), MTP_int(forwards),
MTP_int(forwards), MTPMessageReplies(),
MTPMessageReplies(), MTPint(), // edit_date
MTPint(), // edit_date MTP_string(messagePostAuthor),
MTP_string(messagePostAuthor), MTPlong(),
MTPlong(), //MTPMessageReactions(),
//MTPMessageReactions(), MTPVector<MTPRestrictionReason>(),
MTPVector<MTPRestrictionReason>(), MTPint()), // ttl_period
MTPint()), // ttl_period clientFlags,
clientFlags, NewMessageType::Unread);
NewMessageType::Unread);
}
histories.sendRequest(history, requestType, [=](Fn<void()> finish) { histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
history->sendRequestId = request(MTPmessages_SendMessage( history->sendRequestId = request(MTPmessages_SendMessage(
MTP_flags(sendFlags), MTP_flags(sendFlags),

View file

@ -542,7 +542,8 @@ not_null<HistoryItem*> History::addNewLocalMessage(
const QString &postAuthor, const QString &postAuthor,
not_null<DocumentData*> document, not_null<DocumentData*> document,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup) { const MTPReplyMarkup &markup,
uint64 newGroupId) {
return addNewItem( return addNewItem(
makeMessage( makeMessage(
id, id,
@ -555,7 +556,8 @@ not_null<HistoryItem*> History::addNewLocalMessage(
postAuthor, postAuthor,
document, document,
caption, caption,
markup), markup,
newGroupId),
true); true);
} }
@ -570,7 +572,8 @@ not_null<HistoryItem*> History::addNewLocalMessage(
const QString &postAuthor, const QString &postAuthor,
not_null<PhotoData*> photo, not_null<PhotoData*> photo,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup) { const MTPReplyMarkup &markup,
uint64 newGroupId) {
return addNewItem( return addNewItem(
makeMessage( makeMessage(
id, id,
@ -583,7 +586,8 @@ not_null<HistoryItem*> History::addNewLocalMessage(
postAuthor, postAuthor,
photo, photo,
caption, caption,
markup), markup,
newGroupId),
true); true);
} }

View file

@ -142,7 +142,8 @@ public:
const QString &postAuthor, const QString &postAuthor,
not_null<DocumentData*> document, not_null<DocumentData*> document,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup); const MTPReplyMarkup &markup,
uint64 newGroupId = 0);
not_null<HistoryItem*> addNewLocalMessage( not_null<HistoryItem*> addNewLocalMessage(
MsgId id, MsgId id,
MTPDmessage::Flags flags, MTPDmessage::Flags flags,
@ -154,7 +155,8 @@ public:
const QString &postAuthor, const QString &postAuthor,
not_null<PhotoData*> photo, not_null<PhotoData*> photo,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup); const MTPReplyMarkup &markup,
uint64 newGroupId = 0);
not_null<HistoryItem*> addNewLocalMessage( not_null<HistoryItem*> addNewLocalMessage(
MsgId id, MsgId id,
MTPDmessage::Flags flags, MTPDmessage::Flags flags,

View file

@ -697,7 +697,8 @@ HistoryMessage::HistoryMessage(
const QString &postAuthor, const QString &postAuthor,
not_null<DocumentData*> document, not_null<DocumentData*> document,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup) const MTPReplyMarkup &markup,
uint64 newGroupId)
: HistoryItem( : HistoryItem(
history, history,
id, id,
@ -709,6 +710,11 @@ HistoryMessage::HistoryMessage(
_media = std::make_unique<Data::MediaFile>(this, document); _media = std::make_unique<Data::MediaFile>(this, document);
setText(caption); setText(caption);
if (newGroupId) {
setGroupId(
MessageGroupId::FromRaw(history->peer->id, newGroupId));
}
} }
HistoryMessage::HistoryMessage( HistoryMessage::HistoryMessage(
@ -723,7 +729,8 @@ HistoryMessage::HistoryMessage(
const QString &postAuthor, const QString &postAuthor,
not_null<PhotoData*> photo, not_null<PhotoData*> photo,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup) const MTPReplyMarkup &markup,
uint64 newGroupId)
: HistoryItem( : HistoryItem(
history, history,
id, id,
@ -735,6 +742,11 @@ HistoryMessage::HistoryMessage(
_media = std::make_unique<Data::MediaPhoto>(this, photo); _media = std::make_unique<Data::MediaPhoto>(this, photo);
setText(caption); setText(caption);
if (newGroupId) {
setGroupId(
MessageGroupId::FromRaw(history->peer->id, newGroupId));
}
} }
HistoryMessage::HistoryMessage( HistoryMessage::HistoryMessage(

View file

@ -87,7 +87,8 @@ public:
const QString &postAuthor, const QString &postAuthor,
not_null<DocumentData*> document, not_null<DocumentData*> document,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup); // local document const MTPReplyMarkup &markup,
uint64 newGroupId = 0); // local document
HistoryMessage( HistoryMessage(
not_null<History*> history, not_null<History*> history,
MsgId id, MsgId id,
@ -100,7 +101,8 @@ public:
const QString &postAuthor, const QString &postAuthor,
not_null<PhotoData*> photo, not_null<PhotoData*> photo,
const TextWithEntities &caption, const TextWithEntities &caption,
const MTPReplyMarkup &markup); // local photo const MTPReplyMarkup &markup,
uint64 newGroupId = 0); // local photo
HistoryMessage( HistoryMessage(
not_null<History*> history, not_null<History*> history,
MsgId id, MsgId id,