tdesktop/Telegram/SourceFiles/data/data_message_reactions.h

208 lines
5.2 KiB
C
Raw Normal View History

2019-09-11 09:26:13 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2021-12-28 18:20:32 +00:00
#include "base/timer.h"
namespace Lottie {
class Icon;
} // namespace Lottie
2019-09-11 09:26:13 +00:00
namespace Data {
class DocumentMedia;
class Session;
2022-08-16 11:28:17 +00:00
struct ReactionId {
std::variant<QString, DocumentId> data;
[[nodiscard]] bool empty() const {
const auto emoji = std::get_if<QString>(&data);
return emoji && emoji->isEmpty();
}
[[nodiscard]] QString emoji() const {
const auto emoji = std::get_if<QString>(&data);
return emoji ? *emoji : QString();
}
[[nodiscard]] DocumentId custom() const {
const auto custom = std::get_if<DocumentId>(&data);
return custom ? *custom : DocumentId();
}
};
Q_DECLARE_METATYPE(ReactionId);
inline bool operator<(const ReactionId &a, const ReactionId &b) {
return a.data < b.data;
}
inline bool operator==(const ReactionId &a, const ReactionId &b) {
return a.data == b.data;
}
[[nodiscard]] ReactionId ReactionFromMTP(const MTPReaction &reaction);
[[nodiscard]] MTPReaction ReactionToMTP(ReactionId id);
struct Reaction {
2022-08-16 11:28:17 +00:00
ReactionId id;
QString title;
not_null<DocumentData*> staticIcon;
2021-12-22 11:56:02 +00:00
not_null<DocumentData*> appearAnimation;
not_null<DocumentData*> selectAnimation;
//not_null<DocumentData*> activateAnimation;
//not_null<DocumentData*> activateEffects;
2022-01-10 09:43:26 +00:00
DocumentData *centerIcon = nullptr;
2022-01-05 10:37:35 +00:00
DocumentData *aroundAnimation = nullptr;
2021-12-22 11:56:02 +00:00
bool active = false;
bool premium = false;
};
class Reactions final {
public:
explicit Reactions(not_null<Session*> owner);
~Reactions();
void refresh();
2021-12-22 11:56:02 +00:00
enum class Type {
Active,
All,
};
[[nodiscard]] const std::vector<Reaction> &list(Type type) const;
2022-08-16 11:28:17 +00:00
[[nodiscard]] ReactionId favorite() const;
void setFavorite(const ReactionId &emoji);
2022-01-10 14:22:43 +00:00
[[nodiscard]] static base::flat_set<QString> ParseAllowed(
const MTPVector<MTPstring> *list);
[[nodiscard]] rpl::producer<> updates() const;
enum class ImageSize {
BottomInfo,
InlineList,
};
2022-08-16 11:28:17 +00:00
void preloadImageFor(const ReactionId &emoji);
void preloadAnimationsFor(const ReactionId &emoji);
[[nodiscard]] QImage resolveImageFor(
2022-08-16 11:28:17 +00:00
const ReactionId &emoji,
ImageSize size);
2022-08-16 11:28:17 +00:00
void send(not_null<HistoryItem*> item, const ReactionId &chosen);
2021-12-27 21:37:00 +00:00
[[nodiscard]] bool sending(not_null<HistoryItem*> item) const;
2021-12-28 18:20:32 +00:00
void poll(not_null<HistoryItem*> item, crl::time now);
void updateAllInHistory(not_null<PeerData*> peer, bool enabled);
[[nodiscard]] static bool HasUnread(const MTPMessageReactions &data);
static void CheckUnknownForUnread(
not_null<Session*> owner,
const MTPMessage &message);
private:
struct ImageSet {
QImage bottomInfo;
QImage inlineList;
std::shared_ptr<DocumentMedia> media;
std::unique_ptr<Lottie::Icon> icon;
2022-01-10 09:43:26 +00:00
bool fromAppearAnimation = false;
};
void request();
void updateFromData(const MTPDmessages_availableReactions &data);
[[nodiscard]] std::optional<Reaction> parse(
const MTPAvailableReaction &entry);
2022-01-10 09:43:26 +00:00
void loadImage(
ImageSet &set,
not_null<DocumentData*> document,
bool fromAppearAnimation);
void setLottie(ImageSet &set);
void resolveImages();
void downloadTaskFinished();
2021-12-28 18:20:32 +00:00
void repaintCollected();
void pollCollected();
const not_null<Session*> _owner;
2021-12-22 11:56:02 +00:00
std::vector<Reaction> _active;
std::vector<Reaction> _available;
2022-08-16 11:28:17 +00:00
ReactionId _favorite;
2022-01-07 13:19:43 +00:00
base::flat_map<
not_null<DocumentData*>,
std::shared_ptr<Data::DocumentMedia>> _iconsCache;
rpl::event_stream<> _updated;
mtpRequestId _requestId = 0;
int32 _hash = 0;
2022-08-16 11:28:17 +00:00
base::flat_map<ReactionId, ImageSet> _images;
rpl::lifetime _imagesLoadLifetime;
bool _waitingForList = false;
2021-12-27 21:37:00 +00:00
base::flat_map<FullMsgId, mtpRequestId> _sentRequests;
2021-12-28 18:20:32 +00:00
base::flat_map<not_null<HistoryItem*>, crl::time> _repaintItems;
base::Timer _repaintTimer;
base::flat_set<not_null<HistoryItem*>> _pollItems;
base::flat_set<not_null<HistoryItem*>> _pollingItems;
mtpRequestId _pollRequestId = 0;
mtpRequestId _saveFaveRequestId = 0;
rpl::lifetime _lifetime;
};
struct RecentReaction {
not_null<PeerData*> peer;
bool unread = false;
bool big = false;
inline friend constexpr bool operator==(
const RecentReaction &a,
const RecentReaction &b) noexcept {
return (a.peer.get() == b.peer.get())
&& (a.unread == b.unread)
&& (a.big == b.big);
}
};
2019-09-11 09:26:13 +00:00
class MessageReactions final {
public:
explicit MessageReactions(not_null<HistoryItem*> item);
2022-08-16 11:28:17 +00:00
void add(const ReactionId &reaction);
void remove();
2022-01-31 08:19:21 +00:00
bool change(
const QVector<MTPReactionCount> &list,
2022-01-21 12:48:14 +00:00
const QVector<MTPMessagePeerReaction> &recent,
bool ignoreChosen);
2022-01-28 07:44:12 +00:00
[[nodiscard]] bool checkIfChanged(
const QVector<MTPReactionCount> &list,
const QVector<MTPMessagePeerReaction> &recent) const;
2022-08-16 11:28:17 +00:00
[[nodiscard]] const base::flat_map<ReactionId, int> &list() const;
[[nodiscard]] auto recent() const
2022-08-16 11:28:17 +00:00
-> const base::flat_map<ReactionId, std::vector<RecentReaction>> &;
[[nodiscard]] ReactionId chosen() const;
[[nodiscard]] bool empty() const;
2019-09-11 09:26:13 +00:00
[[nodiscard]] bool hasUnread() const;
2022-01-27 14:38:59 +00:00
void markRead();
2019-09-11 09:26:13 +00:00
private:
const not_null<HistoryItem*> _item;
2022-08-16 11:28:17 +00:00
ReactionId _chosen;
base::flat_map<ReactionId, int> _list;
base::flat_map<ReactionId, std::vector<RecentReaction>> _recent;
2019-09-11 09:26:13 +00:00
};
} // namespace Data