2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Send emoji interaction seen requests.

This commit is contained in:
John Preston
2021-09-16 19:30:41 +03:00
parent cfb43081c7
commit 4b7f594b0e
5 changed files with 89 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ namespace {
constexpr auto kMinDelay = crl::time(200);
constexpr auto kAccumulateDelay = crl::time(1000);
constexpr auto kAccumulateSeenRequests = kAccumulateDelay;
constexpr auto kMaxDelay = 2 * crl::time(1000);
constexpr auto kTimeNever = std::numeric_limits<crl::time>::max();
constexpr auto kJsonVersion = 1;
@@ -75,7 +76,8 @@ void EmojiInteractions::checkEdition(
}
}
void EmojiInteractions::startOutgoing(not_null<const HistoryView::Element*> view) {
void EmojiInteractions::startOutgoing(
not_null<const HistoryView::Element*> view) {
const auto item = view->data();
if (!IsServerMsgId(item->id) || !item->history()->peer->isUser()) {
return;
@@ -163,6 +165,7 @@ void EmojiInteractions::startIncoming(
.document = document,
.media = media,
.scheduledAt = at,
.incoming = true,
.index = index,
});
}
@@ -206,9 +209,11 @@ auto EmojiInteractions::checkAnimations(
} else if (!lastStartedAt || lastStartedAt + kMinDelay <= now) {
animation.startedAt = now;
_playRequests.fire({
animation.emoji->text(),
item,
animation.media,
animation.scheduledAt,
animation.incoming,
});
break;
} else {
@@ -309,16 +314,36 @@ void EmojiInteractions::check(crl::time now) {
if (!now) {
now = crl::now();
}
checkSeenRequests(now);
const auto result1 = checkAnimations(now);
const auto result2 = checkAccumulated(now);
const auto result = Combine(result1, result2);
if (result.nextCheckAt < kTimeNever) {
Assert(result.nextCheckAt > now);
_checkTimer.callOnce(result.nextCheckAt - now);
} else if (!_playStarted.empty()) {
_checkTimer.callOnce(kAccumulateSeenRequests);
}
setWaitingForDownload(result.waitingForDownload);
}
void EmojiInteractions::checkSeenRequests(crl::time now) {
for (auto i = begin(_playStarted); i != end(_playStarted);) {
for (auto j = begin(i->second); j != end(i->second);) {
if (j->second + kAccumulateSeenRequests <= now) {
j = i->second.erase(j);
} else {
++j;
}
}
if (i->second.empty()) {
i = _playStarted.erase(i);
} else {
++i;
}
}
}
void EmojiInteractions::setWaitingForDownload(bool waiting) {
if (_waitingForDownload == waiting) {
return;
@@ -335,6 +360,25 @@ void EmojiInteractions::setWaitingForDownload(bool waiting) {
}
}
void EmojiInteractions::playStarted(not_null<PeerData*> peer, QString emoji) {
auto &map = _playStarted[peer];
const auto i = map.find(emoji);
const auto now = crl::now();
if (i != end(map) && now - i->second < kAccumulateSeenRequests) {
return;
}
_session->api().request(MTPmessages_SetTyping(
MTP_flags(0),
peer->input,
MTPint(), // top_msg_id
MTP_sendMessageEmojiInteractionSeen(MTP_string(emoji))
)).send();
map[emoji] = now;
if (!_checkTimer.isActive()) {
_checkTimer.callOnce(kAccumulateSeenRequests);
}
}
EmojiInteractionsBunch EmojiInteractions::Parse(const QByteArray &json) {
auto error = QJsonParseError{ 0, QJsonParseError::NoError };
const auto document = QJsonDocument::fromJson(json, &error);

View File

@@ -28,9 +28,11 @@ class Element;
namespace ChatHelpers {
struct EmojiInteractionPlayRequest {
QString emoji;
not_null<HistoryItem*> item;
std::shared_ptr<Data::DocumentMedia> media;
crl::time shouldHaveStartedAt = 0;
bool incoming = false;
};
struct EmojiInteractionsBunch {
@@ -58,6 +60,7 @@ public:
[[nodiscard]] rpl::producer<PlayRequest> playRequests() const {
return _playRequests.events();
}
void playStarted(not_null<PeerData*> peer, QString emoji);
[[nodiscard]] static EmojiInteractionsBunch Parse(const QByteArray &json);
[[nodiscard]] static QByteArray ToJson(
@@ -70,6 +73,7 @@ private:
std::shared_ptr<Data::DocumentMedia> media;
crl::time scheduledAt = 0;
crl::time startedAt = 0;
bool incoming = false;
int index = 0;
};
struct CheckResult {
@@ -93,6 +97,7 @@ private:
std::vector<Animation> &animations);
void setWaitingForDownload(bool waiting);
void checkSeenRequests(crl::time now);
void checkEdition(
not_null<HistoryItem*> item,
base::flat_map<not_null<HistoryItem*>, std::vector<Animation>> &map);
@@ -103,6 +108,9 @@ private:
base::flat_map<not_null<HistoryItem*>, std::vector<Animation>> _incoming;
base::Timer _checkTimer;
rpl::event_stream<PlayRequest> _playRequests;
base::flat_map<
not_null<PeerData*>,
base::flat_map<QString, crl::time>> _playStarted;
bool _waitingForDownload = false;
rpl::lifetime _downloadCheckLifetime;