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) { void ChannelData::setAllowedReactions(std::vector<QString> list) {
if (_allowedReactions != list) { if (_allowedReactions != list) {
const auto toggled = (_allowedReactions.empty() != list.empty());
_allowedReactions = std::move(list); _allowedReactions = std::move(list);
if (toggled) {
owner().reactions().updateAllInHistory(
this,
!_allowedReactions.empty());
}
session().changes().peerUpdated(this, UpdateFlag::Reactions); session().changes().peerUpdated(this, UpdateFlag::Reactions);
} }
} }

View file

@ -289,7 +289,13 @@ void ChatData::setPendingRequestsCount(
void ChatData::setAllowedReactions(std::vector<QString> list) { void ChatData::setAllowedReactions(std::vector<QString> list) {
if (_allowedReactions != list) { if (_allowedReactions != list) {
const auto toggled = (_allowedReactions.empty() != list.empty());
_allowedReactions = std::move(list); _allowedReactions = std::move(list);
if (toggled) {
owner().reactions().updateAllInHistory(
this,
!_allowedReactions.empty());
}
session().changes().peerUpdated(this, UpdateFlag::Reactions); 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() { void Reactions::repaintCollected() {
const auto now = crl::now(); const auto now = crl::now();
auto closest = 0; auto closest = 0;
@ -407,7 +413,6 @@ void MessageReactions::remove() {
void MessageReactions::set( void MessageReactions::set(
const QVector<MTPReactionCount> &list, const QVector<MTPReactionCount> &list,
bool ignoreChosen) { bool ignoreChosen) {
_lastRefreshTime = crl::now();
if (_item->history()->owner().reactions().sending(_item)) { if (_item->history()->owner().reactions().sending(_item)) {
// We'll apply non-stale data from the request response. // We'll apply non-stale data from the request response.
return; return;
@ -458,10 +463,6 @@ bool MessageReactions::empty() const {
return _list.empty(); return _list.empty();
} }
crl::time MessageReactions::lastRefreshTime() const {
return _lastRefreshTime;
}
QString MessageReactions::chosen() const { QString MessageReactions::chosen() const {
return _chosen; return _chosen;
} }

View file

@ -63,6 +63,8 @@ public:
void poll(not_null<HistoryItem*> item, crl::time now); void poll(not_null<HistoryItem*> item, crl::time now);
void updateAllInHistory(not_null<PeerData*> peer, bool enabled);
private: private:
struct ImageSet { struct ImageSet {
QImage bottomInfo; QImage bottomInfo;
@ -118,14 +120,12 @@ public:
[[nodiscard]] const base::flat_map<QString, int> &list() const; [[nodiscard]] const base::flat_map<QString, int> &list() const;
[[nodiscard]] QString chosen() const; [[nodiscard]] QString chosen() const;
[[nodiscard]] bool empty() const; [[nodiscard]] bool empty() const;
[[nodiscard]] crl::time lastRefreshTime() const;
private: private:
const not_null<HistoryItem*> _item; const not_null<HistoryItem*> _item;
QString _chosen; QString _chosen;
base::flat_map<QString, int> _list; 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(); session->saveSettingsDelayed();
} }
}, _lifetime); }, _lifetime);
} }
void Session::clear() { 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 { bool History::isEmpty() const {
return blocks.empty(); return blocks.empty();
} }

View file

@ -89,6 +89,8 @@ public:
void checkLocalMessages(); void checkLocalMessages();
void removeJoinedMessage(); void removeJoinedMessage();
void reactionsEnabledChanged(bool enabled);
bool isEmpty() const; bool isEmpty() const;
bool isDisplayedEmpty() const; bool isDisplayedEmpty() const;
Element *findFirstNonEmpty() 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 { bool HistoryItem::definesReplyKeyboard() const {
if (const auto markup = Get<HistoryMessageReplyMarkup>()) { if (const auto markup = Get<HistoryMessageReplyMarkup>()) {
if (markup->data.flags & ReplyMarkupFlag::Inline) { if (markup->data.flags & ReplyMarkupFlag::Inline) {
@ -774,6 +795,9 @@ void HistoryItem::toggleReaction(const QString &reaction) {
} }
void HistoryItem::updateReactions(const MTPMessageReactions *reactions) { void HistoryItem::updateReactions(const MTPMessageReactions *reactions) {
if (reactions || _reactionsLastRefreshed) {
_reactionsLastRefreshed = crl::now();
}
if (!reactions) { if (!reactions) {
_flags &= ~MessageFlag::CanViewReactions; _flags &= ~MessageFlag::CanViewReactions;
if (_reactions) { 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 { const base::flat_map<QString, int> &HistoryItem::reactions() const {
static const auto kEmpty = base::flat_map<QString, int>(); static const auto kEmpty = base::flat_map<QString, int>();
return _reactions ? _reactions->list() : kEmpty; return _reactions ? _reactions->list() : kEmpty;
@ -817,7 +845,7 @@ QString HistoryItem::chosenReaction() const {
} }
crl::time HistoryItem::lastReactionsRefreshTime() const { crl::time HistoryItem::lastReactionsRefreshTime() const {
return _reactions ? _reactions->lastRefreshTime() : 0; return _reactionsLastRefreshed;
} }
bool HistoryItem::hasDirectLink() const { bool HistoryItem::hasDirectLink() const {

View file

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

View file

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