Don't use MTP* for replies data.

This commit is contained in:
John Preston 2021-10-07 22:52:27 +04:00
parent bef35b9bc3
commit d532b65d1c
7 changed files with 89 additions and 74 deletions

View file

@ -106,7 +106,8 @@ void ViewsManager::done(
item->setForwardsCount(forwards->v); item->setForwardsCount(forwards->v);
} }
if (const auto replies = data.vreplies()) { if (const auto replies = data.vreplies()) {
item->setReplies(*replies); item->setReplies(
HistoryMessageRepliesData(replies));
} }
}); });
} }

View file

@ -517,7 +517,7 @@ void HistoryItem::applySentMessage(const MTPDmessage &data) {
updateForwardedInfo(data.vfwd_from()); updateForwardedInfo(data.vfwd_from());
setViewsCount(data.vviews().value_or(-1)); setViewsCount(data.vviews().value_or(-1));
if (const auto replies = data.vreplies()) { if (const auto replies = data.vreplies()) {
setReplies(*replies); setReplies(HistoryMessageRepliesData(replies));
} else { } else {
clearReplies(); clearReplies();
} }

View file

@ -336,7 +336,7 @@ public:
} }
virtual void setForwardsCount(int count) { virtual void setForwardsCount(int count) {
} }
virtual void setReplies(const MTPMessageReplies &data) { virtual void setReplies(HistoryMessageRepliesData &&data) {
} }
virtual void clearReplies() { virtual void clearReplies() {
} }

View file

@ -195,3 +195,22 @@ bool HistoryMessageMarkupData::isTrivial() const {
&& placeholder.isEmpty() && placeholder.isEmpty()
&& !(flags & ~ReplyMarkupFlag::IsNull); && !(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;
}

View file

@ -81,3 +81,15 @@ private:
void fillRows(const QVector<MTPKeyboardButtonRow> &v); void fillRows(const QVector<MTPKeyboardButtonRow> &v);
}; };
struct HistoryMessageRepliesData {
HistoryMessageRepliesData() = default;
explicit HistoryMessageRepliesData(const MTPMessageReplies *data);
std::vector<PeerId> recentRepliers;
ChannelId channelId = 0;
MsgId readMaxId = 0;
MsgId maxId = 0;
int repliesCount = 0;
bool isNull = true;
};

View file

@ -421,13 +421,11 @@ struct HistoryMessage::CreateConfig {
QString authorOriginal; QString authorOriginal;
TimeId originalDate = 0; TimeId originalDate = 0;
TimeId editDate = 0; TimeId editDate = 0;
bool imported = false;
HistoryMessageMarkupData markup; HistoryMessageMarkupData markup;
HistoryMessageRepliesData replies;
bool imported = false;
bool sponsored = false; bool sponsored = false;
// For messages created from MTP structs.
const MTPMessageReplies *mtpReplies = nullptr;
// For messages created from existing messages (forwarded). // For messages created from existing messages (forwarded).
const HistoryMessageReplyMarkup *inlineMarkup = nullptr; const HistoryMessageReplyMarkup *inlineMarkup = nullptr;
}; };
@ -484,7 +482,9 @@ HistoryMessage::HistoryMessage(
} }
config.viaBotId = data.vvia_bot_id().value_or_empty(); config.viaBotId = data.vvia_bot_id().value_or_empty();
config.viewsCount = data.vviews().value_or(-1); 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.markup = HistoryMessageMarkupData(data.vreply_markup());
config.editDate = data.vedit_date().value_or_empty(); config.editDate = data.vedit_date().value_or_empty();
config.author = qs(data.vpost_author().value_or_empty()); config.author = qs(data.vpost_author().value_or_empty());
@ -1062,7 +1062,7 @@ void HistoryMessage::createComponents(CreateConfig &&config) {
if (config.viaBotId) { if (config.viaBotId) {
mask |= HistoryMessageVia::Bit(); mask |= HistoryMessageVia::Bit();
} }
if (config.viewsCount >= 0 || config.mtpReplies) { if (config.viewsCount >= 0 || !config.replies.isNull) {
mask |= HistoryMessageViews::Bit(); mask |= HistoryMessageViews::Bit();
} }
if (!config.author.isEmpty()) { if (!config.author.isEmpty()) {
@ -1115,23 +1115,17 @@ void HistoryMessage::createComponents(CreateConfig &&config) {
} }
if (const auto views = Get<HistoryMessageViews>()) { if (const auto views = Get<HistoryMessageViews>()) {
setViewsCount(config.viewsCount); setViewsCount(config.viewsCount);
if (config.mtpReplies) { if (config.replies.isNull
setReplies(*config.mtpReplies); && isSending()
} else if (isSending() && config.markup.isNull()) { && config.markup.isNull()) {
if (const auto broadcast = history()->peer->asBroadcast()) { if (const auto broadcast = history()->peer->asBroadcast()) {
if (const auto linked = broadcast->linkedChat()) { if (const auto linked = broadcast->linkedChat()) {
setReplies(MTP_messageReplies( config.replies.isNull = false;
MTP_flags(MTPDmessageReplies::Flag::f_comments config.replies.channelId = peerToChannel(linked->id);
| MTPDmessageReplies::Flag::f_comments),
MTP_int(0),
MTP_int(0),
MTPVector<MTPPeer>(), // recent_repliers
MTP_long(peerToChannel(linked->id).bare),
MTP_int(0), // max_id
MTP_int(0))); // read_max_id
} }
} }
} }
setReplies(std::move(config.replies));
} }
if (const auto edited = Get<HistoryMessageEdited>()) { if (const auto edited = Get<HistoryMessageEdited>()) {
edited->date = config.editDate; edited->date = config.editDate;
@ -1425,7 +1419,7 @@ void HistoryMessage::applyEdition(const MTPDmessage &message) {
setText(_media ? textWithEntities : EnsureNonEmpty(textWithEntities)); setText(_media ? textWithEntities : EnsureNonEmpty(textWithEntities));
if (const auto replies = message.vreplies()) { if (const auto replies = message.vreplies()) {
if (checkRepliesPts(*replies)) { if (checkRepliesPts(*replies)) {
setReplies(*replies); setReplies(HistoryMessageRepliesData(replies));
} }
} else { } else {
clearReplies(); clearReplies();
@ -1757,57 +1751,46 @@ void HistoryMessage::setPostAuthor(const QString &author) {
history()->owner().requestItemResize(this); history()->owner().requestItemResize(this);
} }
void HistoryMessage::setReplies(const MTPMessageReplies &data) { void HistoryMessage::setReplies(HistoryMessageRepliesData &&data) {
data.match([&](const MTPDmessageReplies &data) { if (data.isNull) {
auto views = Get<HistoryMessageViews>(); return;
if (!views) { }
AddComponents(HistoryMessageViews::Bit()); auto views = Get<HistoryMessageViews>();
views = Get<HistoryMessageViews>(); if (!views) {
} AddComponents(HistoryMessageViews::Bit());
const auto repliers = [&] { views = Get<HistoryMessageViews>();
auto result = std::vector<PeerId>(); }
if (const auto list = data.vrecent_repliers()) { const auto &repliers = data.recentRepliers;
result.reserve(list->v.size()); const auto count = data.repliesCount;
for (const auto &id : list->v) { const auto channelId = data.channelId;
result.push_back(peerFromMTP(id)); const auto readTillId = data.readMaxId
} ? std::max({
} views->repliesInboxReadTillId.bare,
return result; data.readMaxId.bare,
}(); int64(1),
const auto count = data.vreplies().v; })
const auto channelId = ChannelId( : views->repliesInboxReadTillId;
data.vchannel_id().value_or_empty()); const auto maxId = data.maxId ? data.maxId : views->repliesMaxId;
const auto readTillId = data.vread_max_id() const auto countsChanged = (views->replies.count != count)
? std::max({ || (views->repliesInboxReadTillId != readTillId)
views->repliesInboxReadTillId.bare, || (views->repliesMaxId != maxId);
int64(data.vread_max_id()->v), const auto megagroupChanged = (views->commentsMegagroupId != channelId);
int64(1), const auto recentChanged = (views->recentRepliers != repliers);
}) if (!countsChanged && !megagroupChanged && !recentChanged) {
: views->repliesInboxReadTillId; return;
const auto maxId = data.vmax_id() }
? data.vmax_id()->v views->replies.count = count;
: views->repliesMaxId; if (recentChanged) {
const auto countsChanged = (views->replies.count != count) views->recentRepliers = repliers;
|| (views->repliesInboxReadTillId != readTillId) }
|| (views->repliesMaxId != maxId); views->commentsMegagroupId = channelId;
const auto megagroupChanged = (views->commentsMegagroupId != channelId); const auto wasUnread = channelId && areRepliesUnread();
const auto recentChanged = (views->recentRepliers != repliers); views->repliesInboxReadTillId = readTillId;
if (!countsChanged && !megagroupChanged && !recentChanged) { views->repliesMaxId = maxId;
return; if (channelId && wasUnread != areRepliesUnread()) {
} history()->owner().requestItemRepaint(this);
views->replies.count = count; }
if (recentChanged) { refreshRepliesText(views, megagroupChanged);
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() { void HistoryMessage::clearReplies() {

View file

@ -134,7 +134,7 @@ public:
void setViewsCount(int count) override; void setViewsCount(int count) override;
void setForwardsCount(int count) override; void setForwardsCount(int count) override;
void setReplies(const MTPMessageReplies &data) override; void setReplies(HistoryMessageRepliesData &&data) override;
void clearReplies() override; void clearReplies() override;
void changeRepliesCount( void changeRepliesCount(
int delta, int delta,