Update all messages in case of chat reactions toggle.

This commit is contained in:
John Preston 2021-12-29 01:06:01 +03:00
parent ec16ca7df7
commit a8bc7baa70
10 changed files with 70 additions and 25 deletions

View file

@ -763,7 +763,13 @@ PeerId ChannelData::groupCallDefaultJoinAs() const {
void ChannelData::setAllowedReactions(std::vector<QString> list) {
if (_allowedReactions != list) {
const auto toggled = (_allowedReactions.empty() != list.empty());
_allowedReactions = std::move(list);
if (toggled) {
owner().reactions().updateAllInHistory(
this,
!_allowedReactions.empty());
}
session().changes().peerUpdated(this, UpdateFlag::Reactions);
}
}

View file

@ -289,7 +289,13 @@ void ChatData::setPendingRequestsCount(
void ChatData::setAllowedReactions(std::vector<QString> list) {
if (_allowedReactions != list) {
const auto toggled = (_allowedReactions.empty() != list.empty());
_allowedReactions = std::move(list);
if (toggled) {
owner().reactions().updateAllInHistory(
this,
!_allowedReactions.empty());
}
session().changes().peerUpdated(this, UpdateFlag::Reactions);
}
}

View file

@ -317,6 +317,12 @@ void Reactions::poll(not_null<HistoryItem*> item, crl::time now) {
}
}
void Reactions::updateAllInHistory(not_null<PeerData*> peer, bool enabled) {
if (const auto history = _owner->historyLoaded(peer)) {
history->reactionsEnabledChanged(enabled);
}
}
void Reactions::repaintCollected() {
const auto now = crl::now();
auto closest = 0;
@ -407,7 +413,6 @@ void MessageReactions::remove() {
void MessageReactions::set(
const QVector<MTPReactionCount> &list,
bool ignoreChosen) {
_lastRefreshTime = crl::now();
if (_item->history()->owner().reactions().sending(_item)) {
// We'll apply non-stale data from the request response.
return;
@ -458,10 +463,6 @@ bool MessageReactions::empty() const {
return _list.empty();
}
crl::time MessageReactions::lastRefreshTime() const {
return _lastRefreshTime;
}
QString MessageReactions::chosen() const {
return _chosen;
}

View file

@ -63,6 +63,8 @@ public:
void poll(not_null<HistoryItem*> item, crl::time now);
void updateAllInHistory(not_null<PeerData*> peer, bool enabled);
private:
struct ImageSet {
QImage bottomInfo;
@ -118,14 +120,12 @@ public:
[[nodiscard]] const base::flat_map<QString, int> &list() const;
[[nodiscard]] QString chosen() const;
[[nodiscard]] bool empty() const;
[[nodiscard]] crl::time lastRefreshTime() const;
private:
const not_null<HistoryItem*> _item;
QString _chosen;
base::flat_map<QString, int> _list;
crl::time _lastRefreshTime = 0;
};

View file

@ -273,7 +273,6 @@ Session::Session(not_null<Main::Session*> session)
session->saveSettingsDelayed();
}
}, _lifetime);
}
void Session::clear() {

View file

@ -2965,6 +2965,16 @@ void History::removeJoinedMessage() {
}
}
void History::reactionsEnabledChanged(bool enabled) {
if (!enabled) {
for (const auto &item : _messages) {
item->updateReactions(nullptr);
}
} else {
}
}
bool History::isEmpty() const {
return blocks.empty();
}

View file

@ -89,6 +89,8 @@ public:
void checkLocalMessages();
void removeJoinedMessage();
void reactionsEnabledChanged(bool enabled);
bool isEmpty() const;
bool isDisplayedEmpty() const;
Element *findFirstNonEmpty() const;

View file

@ -381,6 +381,27 @@ void HistoryItem::setIsPinned(bool pinned) {
}
}
void HistoryItem::returnSavedMedia() {
}
void HistoryItem::savePreviousMedia() {
Expects(_media != nullptr);
using Data = SavedMediaData;
_savedLocalEditMediaData = std::make_unique<Data>(Data{
.text = originalText(),
.media = _media->clone(this),
});
}
bool HistoryItem::isEditingMedia() const {
return _savedLocalEditMediaData != nullptr;
}
void HistoryItem::clearSavedMedia() {
_savedLocalEditMediaData = nullptr;
}
bool HistoryItem::definesReplyKeyboard() const {
if (const auto markup = Get<HistoryMessageReplyMarkup>()) {
if (markup->data.flags & ReplyMarkupFlag::Inline) {
@ -774,6 +795,9 @@ void HistoryItem::toggleReaction(const QString &reaction) {
}
void HistoryItem::updateReactions(const MTPMessageReactions *reactions) {
if (reactions || _reactionsLastRefreshed) {
_reactionsLastRefreshed = crl::now();
}
if (!reactions) {
_flags &= ~MessageFlag::CanViewReactions;
if (_reactions) {
@ -801,6 +825,10 @@ void HistoryItem::updateReactions(const MTPMessageReactions *reactions) {
});
}
void HistoryItem::updateReactionsUnknown() {
_reactionsLastRefreshed = 1;
}
const base::flat_map<QString, int> &HistoryItem::reactions() const {
static const auto kEmpty = base::flat_map<QString, int>();
return _reactions ? _reactions->list() : kEmpty;
@ -817,7 +845,7 @@ QString HistoryItem::chosenReaction() const {
}
crl::time HistoryItem::lastReactionsRefreshTime() const {
return _reactions ? _reactions->lastRefreshTime() : 0;
return _reactionsLastRefreshed;
}
bool HistoryItem::hasDirectLink() const {

View file

@ -146,19 +146,10 @@ public:
void setIsPinned(bool isPinned);
// For edit media in history_message.
virtual void returnSavedMedia() {};
void savePreviousMedia() {
_savedLocalEditMediaData = {
originalText(),
_media->clone(this),
};
}
[[nodiscard]] bool isEditingMedia() const {
return _savedLocalEditMediaData.media != nullptr;
}
void clearSavedMedia() {
_savedLocalEditMediaData = {};
}
virtual void returnSavedMedia();
void savePreviousMedia();
[[nodiscard]] bool isEditingMedia() const;
void clearSavedMedia();
// Zero result means this message is not self-destructing right now.
virtual crl::time getSelfDestructIn(crl::time now) {
@ -364,6 +355,7 @@ public:
void addReaction(const QString &reaction);
void toggleReaction(const QString &reaction);
void updateReactions(const MTPMessageReactions *reactions);
void updateReactionsUnknown();
[[nodiscard]] const base::flat_map<QString, int> &reactions() const;
[[nodiscard]] bool canViewReactions() const;
[[nodiscard]] QString chosenReaction() const;
@ -467,9 +459,10 @@ protected:
std::unique_ptr<Data::Media> media;
};
SavedMediaData _savedLocalEditMediaData;
std::unique_ptr<SavedMediaData> _savedLocalEditMediaData;
std::unique_ptr<Data::Media> _media;
std::unique_ptr<Data::MessageReactions> _reactions;
crl::time _reactionsLastRefreshed = 0;
private:

View file

@ -1222,8 +1222,8 @@ void HistoryMessage::returnSavedMedia() {
return;
}
const auto wasGrouped = history()->owner().groups().isGrouped(this);
_media = std::move(_savedLocalEditMediaData.media);
setText(_savedLocalEditMediaData.text);
_media = std::move(_savedLocalEditMediaData->media);
setText(_savedLocalEditMediaData->text);
clearSavedMedia();
if (wasGrouped) {
history()->owner().groups().refreshMessage(this, true);