tdesktop/Telegram/SourceFiles/chat_helpers/emoji_interactions.h

148 lines
3.8 KiB
C
Raw Normal View History

/*
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
#include "base/timer.h"
2021-09-16 11:20:25 +00:00
class PeerData;
class HistoryItem;
class DocumentData;
namespace Data {
class DocumentMedia;
} // namespace Data
namespace Main {
class Session;
} // namespace Main
namespace HistoryView {
class Element;
} // namespace HistoryView
namespace ChatHelpers {
2021-09-15 14:49:06 +00:00
struct EmojiInteractionPlayRequest {
QString emoticon;
2021-09-15 14:49:06 +00:00
not_null<HistoryItem*> item;
std::shared_ptr<Data::DocumentMedia> media;
2021-09-15 15:30:29 +00:00
crl::time shouldHaveStartedAt = 0;
2021-09-16 16:30:41 +00:00
bool incoming = false;
2021-09-15 14:49:06 +00:00
};
2021-09-16 11:20:25 +00:00
struct EmojiInteractionsBunch {
struct Single {
int index = 0;
double time = 0;
};
std::vector<Single> interactions;
};
2021-09-16 21:14:53 +00:00
struct EmojiInteractionSeen {
not_null<PeerData*> peer;
QString emoticon;
2021-09-16 21:14:53 +00:00
};
class EmojiInteractions final {
public:
explicit EmojiInteractions(not_null<Main::Session*> session);
~EmojiInteractions();
2021-09-15 14:49:06 +00:00
using PlayRequest = EmojiInteractionPlayRequest;
2021-09-16 11:20:25 +00:00
void startOutgoing(not_null<const HistoryView::Element*> view);
void startIncoming(
not_null<PeerData*> peer,
MsgId messageId,
const QString &emoticon,
EmojiInteractionsBunch &&bunch);
2021-09-16 21:14:53 +00:00
void seenOutgoing(not_null<PeerData*> peer, const QString &emoticon);
[[nodiscard]] rpl::producer<EmojiInteractionSeen> seen() const {
return _seen.events();
}
2021-09-15 14:49:06 +00:00
[[nodiscard]] rpl::producer<PlayRequest> playRequests() const {
return _playRequests.events();
}
2021-09-16 16:30:41 +00:00
void playStarted(not_null<PeerData*> peer, QString emoji);
2021-09-16 11:20:25 +00:00
[[nodiscard]] static EmojiInteractionsBunch Parse(const QByteArray &json);
[[nodiscard]] static QByteArray ToJson(
const EmojiInteractionsBunch &bunch);
private:
struct Animation {
QString emoticon;
2021-09-16 21:14:53 +00:00
not_null<EmojiPtr> emoji;
not_null<DocumentData*> document;
std::shared_ptr<Data::DocumentMedia> media;
crl::time scheduledAt = 0;
crl::time startedAt = 0;
2021-09-16 16:30:41 +00:00
bool incoming = false;
int index = 0;
};
2021-09-16 21:14:53 +00:00
struct PlaySent {
mtpRequestId lastRequestId = 0;
crl::time lastDoneReceivedAt = 0;
};
struct CheckResult {
crl::time nextCheckAt = 0;
bool waitingForDownload = false;
};
[[nodiscard]] static CheckResult Combine(CheckResult a, CheckResult b);
2021-09-16 21:31:53 +00:00
[[nodiscard]] EmojiPtr chooseInteractionEmoji(
not_null<HistoryItem*> item) const;
[[nodiscard]] EmojiPtr chooseInteractionEmoji(
const QString &emoticon) const;
void check(crl::time now = 0);
[[nodiscard]] CheckResult checkAnimations(crl::time now);
2021-09-16 11:20:25 +00:00
[[nodiscard]] CheckResult checkAnimations(
crl::time now,
base::flat_map<not_null<HistoryItem*>, std::vector<Animation>> &map);
[[nodiscard]] CheckResult checkAccumulated(crl::time now);
2021-09-16 11:20:25 +00:00
void sendAccumulatedOutgoing(
crl::time now,
not_null<HistoryItem*> item,
std::vector<Animation> &animations);
2021-09-16 11:20:25 +00:00
void clearAccumulatedIncoming(
crl::time now,
std::vector<Animation> &animations);
void setWaitingForDownload(bool waiting);
2021-09-16 16:30:41 +00:00
void checkSeenRequests(crl::time now);
2021-09-16 21:14:53 +00:00
void checkSentRequests(crl::time now);
2021-09-16 11:20:25 +00:00
void checkEdition(
not_null<HistoryItem*> item,
base::flat_map<not_null<HistoryItem*>, std::vector<Animation>> &map);
const not_null<Main::Session*> _session;
2021-09-16 11:20:25 +00:00
base::flat_map<not_null<HistoryItem*>, std::vector<Animation>> _outgoing;
base::flat_map<not_null<HistoryItem*>, std::vector<Animation>> _incoming;
base::Timer _checkTimer;
2021-09-15 14:49:06 +00:00
rpl::event_stream<PlayRequest> _playRequests;
2021-09-16 16:30:41 +00:00
base::flat_map<
not_null<PeerData*>,
base::flat_map<QString, crl::time>> _playStarted;
2021-09-16 21:14:53 +00:00
base::flat_map<
not_null<PeerData*>,
base::flat_map<not_null<EmojiPtr>, PlaySent>> _playsSent;
rpl::event_stream<EmojiInteractionSeen> _seen;
bool _waitingForDownload = false;
rpl::lifetime _downloadCheckLifetime;
rpl::lifetime _lifetime;
};
} // namespace ChatHelpers