diff --git a/Telegram/SourceFiles/api/api_views.cpp b/Telegram/SourceFiles/api/api_views.cpp index b930e15971..426b2d02a2 100644 --- a/Telegram/SourceFiles/api/api_views.cpp +++ b/Telegram/SourceFiles/api/api_views.cpp @@ -106,7 +106,8 @@ void ViewsManager::done( item->setForwardsCount(forwards->v); } if (const auto replies = data.vreplies()) { - item->setReplies(*replies); + item->setReplies( + HistoryMessageRepliesData(replies)); } }); } diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 0da6cd1117..f4a617fef0 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -517,7 +517,7 @@ void HistoryItem::applySentMessage(const MTPDmessage &data) { updateForwardedInfo(data.vfwd_from()); setViewsCount(data.vviews().value_or(-1)); if (const auto replies = data.vreplies()) { - setReplies(*replies); + setReplies(HistoryMessageRepliesData(replies)); } else { clearReplies(); } diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 4a0c129d22..695bf9bfee 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -336,7 +336,7 @@ public: } virtual void setForwardsCount(int count) { } - virtual void setReplies(const MTPMessageReplies &data) { + virtual void setReplies(HistoryMessageRepliesData &&data) { } virtual void clearReplies() { } diff --git a/Telegram/SourceFiles/history/history_item_reply_markup.cpp b/Telegram/SourceFiles/history/history_item_reply_markup.cpp index 04787f9f25..0a52fb42b8 100644 --- a/Telegram/SourceFiles/history/history_item_reply_markup.cpp +++ b/Telegram/SourceFiles/history/history_item_reply_markup.cpp @@ -195,3 +195,22 @@ bool HistoryMessageMarkupData::isTrivial() const { && placeholder.isEmpty() && !(flags & ~ReplyMarkupFlag::IsNull); } + +HistoryMessageRepliesData::HistoryMessageRepliesData( + const MTPMessageReplies *data) { + if (!data) { + return; + } + const auto &fields = data->c_messageReplies(); + if (const auto list = fields.vrecent_repliers()) { + recentRepliers.reserve(list->v.size()); + for (const auto &id : list->v) { + recentRepliers.push_back(peerFromMTP(id)); + } + } + repliesCount = fields.vreplies().v; + channelId = ChannelId(fields.vchannel_id().value_or_empty()); + readMaxId = fields.vread_max_id().value_or_empty(); + maxId = fields.vmax_id().value_or_empty(); + isNull = false; +} diff --git a/Telegram/SourceFiles/history/history_item_reply_markup.h b/Telegram/SourceFiles/history/history_item_reply_markup.h index 692dcc5c41..db06327804 100644 --- a/Telegram/SourceFiles/history/history_item_reply_markup.h +++ b/Telegram/SourceFiles/history/history_item_reply_markup.h @@ -81,3 +81,15 @@ private: void fillRows(const QVector &v); }; + +struct HistoryMessageRepliesData { + HistoryMessageRepliesData() = default; + explicit HistoryMessageRepliesData(const MTPMessageReplies *data); + + std::vector recentRepliers; + ChannelId channelId = 0; + MsgId readMaxId = 0; + MsgId maxId = 0; + int repliesCount = 0; + bool isNull = true; +}; diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 8804665952..e316b77528 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -421,13 +421,11 @@ struct HistoryMessage::CreateConfig { QString authorOriginal; TimeId originalDate = 0; TimeId editDate = 0; - bool imported = false; HistoryMessageMarkupData markup; + HistoryMessageRepliesData replies; + bool imported = false; bool sponsored = false; - // For messages created from MTP structs. - const MTPMessageReplies *mtpReplies = nullptr; - // For messages created from existing messages (forwarded). const HistoryMessageReplyMarkup *inlineMarkup = nullptr; }; @@ -484,7 +482,9 @@ HistoryMessage::HistoryMessage( } config.viaBotId = data.vvia_bot_id().value_or_empty(); config.viewsCount = data.vviews().value_or(-1); - config.mtpReplies = isScheduled() ? nullptr : data.vreplies(); + config.replies = isScheduled() + ? HistoryMessageRepliesData() + : HistoryMessageRepliesData(data.vreplies()); config.markup = HistoryMessageMarkupData(data.vreply_markup()); config.editDate = data.vedit_date().value_or_empty(); config.author = qs(data.vpost_author().value_or_empty()); @@ -1062,7 +1062,7 @@ void HistoryMessage::createComponents(CreateConfig &&config) { if (config.viaBotId) { mask |= HistoryMessageVia::Bit(); } - if (config.viewsCount >= 0 || config.mtpReplies) { + if (config.viewsCount >= 0 || !config.replies.isNull) { mask |= HistoryMessageViews::Bit(); } if (!config.author.isEmpty()) { @@ -1115,23 +1115,17 @@ void HistoryMessage::createComponents(CreateConfig &&config) { } if (const auto views = Get()) { setViewsCount(config.viewsCount); - if (config.mtpReplies) { - setReplies(*config.mtpReplies); - } else if (isSending() && config.markup.isNull()) { + if (config.replies.isNull + && isSending() + && config.markup.isNull()) { if (const auto broadcast = history()->peer->asBroadcast()) { if (const auto linked = broadcast->linkedChat()) { - setReplies(MTP_messageReplies( - MTP_flags(MTPDmessageReplies::Flag::f_comments - | MTPDmessageReplies::Flag::f_comments), - MTP_int(0), - MTP_int(0), - MTPVector(), // recent_repliers - MTP_long(peerToChannel(linked->id).bare), - MTP_int(0), // max_id - MTP_int(0))); // read_max_id + config.replies.isNull = false; + config.replies.channelId = peerToChannel(linked->id); } } } + setReplies(std::move(config.replies)); } if (const auto edited = Get()) { edited->date = config.editDate; @@ -1425,7 +1419,7 @@ void HistoryMessage::applyEdition(const MTPDmessage &message) { setText(_media ? textWithEntities : EnsureNonEmpty(textWithEntities)); if (const auto replies = message.vreplies()) { if (checkRepliesPts(*replies)) { - setReplies(*replies); + setReplies(HistoryMessageRepliesData(replies)); } } else { clearReplies(); @@ -1757,57 +1751,46 @@ void HistoryMessage::setPostAuthor(const QString &author) { history()->owner().requestItemResize(this); } -void HistoryMessage::setReplies(const MTPMessageReplies &data) { - data.match([&](const MTPDmessageReplies &data) { - auto views = Get(); - if (!views) { - AddComponents(HistoryMessageViews::Bit()); - views = Get(); - } - const auto repliers = [&] { - auto result = std::vector(); - if (const auto list = data.vrecent_repliers()) { - result.reserve(list->v.size()); - for (const auto &id : list->v) { - result.push_back(peerFromMTP(id)); - } - } - return result; - }(); - const auto count = data.vreplies().v; - const auto channelId = ChannelId( - data.vchannel_id().value_or_empty()); - const auto readTillId = data.vread_max_id() - ? std::max({ - views->repliesInboxReadTillId.bare, - int64(data.vread_max_id()->v), - int64(1), - }) - : views->repliesInboxReadTillId; - const auto maxId = data.vmax_id() - ? data.vmax_id()->v - : views->repliesMaxId; - const auto countsChanged = (views->replies.count != count) - || (views->repliesInboxReadTillId != readTillId) - || (views->repliesMaxId != maxId); - const auto megagroupChanged = (views->commentsMegagroupId != channelId); - const auto recentChanged = (views->recentRepliers != repliers); - if (!countsChanged && !megagroupChanged && !recentChanged) { - return; - } - views->replies.count = count; - if (recentChanged) { - views->recentRepliers = repliers; - } - views->commentsMegagroupId = channelId; - const auto wasUnread = channelId && areRepliesUnread(); - views->repliesInboxReadTillId = readTillId; - views->repliesMaxId = maxId; - if (channelId && wasUnread != areRepliesUnread()) { - history()->owner().requestItemRepaint(this); - } - refreshRepliesText(views, megagroupChanged); - }); +void HistoryMessage::setReplies(HistoryMessageRepliesData &&data) { + if (data.isNull) { + return; + } + auto views = Get(); + if (!views) { + AddComponents(HistoryMessageViews::Bit()); + views = Get(); + } + const auto &repliers = data.recentRepliers; + const auto count = data.repliesCount; + const auto channelId = data.channelId; + const auto readTillId = data.readMaxId + ? std::max({ + views->repliesInboxReadTillId.bare, + data.readMaxId.bare, + int64(1), + }) + : views->repliesInboxReadTillId; + const auto maxId = data.maxId ? data.maxId : views->repliesMaxId; + const auto countsChanged = (views->replies.count != count) + || (views->repliesInboxReadTillId != readTillId) + || (views->repliesMaxId != maxId); + const auto megagroupChanged = (views->commentsMegagroupId != channelId); + const auto recentChanged = (views->recentRepliers != repliers); + if (!countsChanged && !megagroupChanged && !recentChanged) { + return; + } + views->replies.count = count; + if (recentChanged) { + views->recentRepliers = repliers; + } + views->commentsMegagroupId = channelId; + const auto wasUnread = channelId && areRepliesUnread(); + views->repliesInboxReadTillId = readTillId; + views->repliesMaxId = maxId; + if (channelId && wasUnread != areRepliesUnread()) { + history()->owner().requestItemRepaint(this); + } + refreshRepliesText(views, megagroupChanged); } void HistoryMessage::clearReplies() { diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 047811f101..4f16a8d6d3 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -134,7 +134,7 @@ public: void setViewsCount(int count) override; void setForwardsCount(int count) override; - void setReplies(const MTPMessageReplies &data) override; + void setReplies(HistoryMessageRepliesData &&data) override; void clearReplies() override; void changeRepliesCount( int delta,