2022-01-26 16:01:40 +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
|
|
|
|
*/
|
|
|
|
#include "api/api_unread_things.h"
|
|
|
|
|
|
|
|
#include "data/data_peer.h"
|
|
|
|
#include "data/data_channel.h"
|
2022-10-07 13:56:07 +00:00
|
|
|
#include "data/data_forum_topic.h"
|
2022-01-26 16:01:40 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/history_unread_things.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kPreloadIfLess = 5;
|
|
|
|
constexpr auto kFirstRequestLimit = 10;
|
|
|
|
constexpr auto kNextRequestLimit = 100;
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
|
|
|
|
[[nodiscard]] not_null<History*> ResolveHistory(
|
|
|
|
not_null<Dialogs::Entry*> entry) {
|
|
|
|
if (const auto history = entry->asHistory()) {
|
|
|
|
return history;
|
|
|
|
}
|
|
|
|
const auto topic = entry->asTopic();
|
|
|
|
|
|
|
|
Ensures(topic != nullptr);
|
|
|
|
return topic->history();
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:01:40 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
UnreadThings::UnreadThings(not_null<ApiWrap*> api) : _api(api) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnreadThings::trackMentions(PeerData *peer) const {
|
|
|
|
return peer && (peer->isChat() || peer->isMegagroup());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnreadThings::trackReactions(PeerData *peer) const {
|
|
|
|
return trackMentions(peer) || (peer && peer->isUser());
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::preloadEnough(DialogsEntry *entry) {
|
|
|
|
if (!entry) {
|
2022-01-26 16:01:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-10-07 13:56:07 +00:00
|
|
|
const auto history = ResolveHistory(entry);
|
2022-01-26 16:01:40 +00:00
|
|
|
if (trackMentions(history->peer)) {
|
|
|
|
preloadEnoughMentions(history);
|
|
|
|
}
|
|
|
|
if (trackReactions(history->peer)) {
|
|
|
|
preloadEnoughReactions(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnreadThings::mediaAndMentionsRead(
|
|
|
|
const base::flat_set<MsgId> &readIds,
|
|
|
|
ChannelData *channel) {
|
|
|
|
for (const auto &msgId : readIds) {
|
|
|
|
_api->requestMessageData(channel, msgId, [=] {
|
|
|
|
const auto item = channel
|
|
|
|
? _api->session().data().message(channel->id, msgId)
|
|
|
|
: _api->session().data().nonChannelMessage(msgId);
|
|
|
|
if (item && item->mentionsMe()) {
|
|
|
|
item->markMediaAndMentionRead();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::preloadEnoughMentions(not_null<DialogsEntry*> entry) {
|
|
|
|
const auto fullCount = entry->unreadMentions().count();
|
|
|
|
const auto loadedCount = entry->unreadMentions().loadedCount();
|
2022-01-26 16:01:40 +00:00
|
|
|
const auto allLoaded = (fullCount >= 0) && (loadedCount >= fullCount);
|
|
|
|
if (fullCount >= 0 && loadedCount < kPreloadIfLess && !allLoaded) {
|
2022-10-07 13:56:07 +00:00
|
|
|
requestMentions(entry, loadedCount);
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::preloadEnoughReactions(not_null<DialogsEntry*> entry) {
|
|
|
|
const auto fullCount = entry->unreadReactions().count();
|
|
|
|
const auto loadedCount = entry->unreadReactions().loadedCount();
|
2022-01-26 16:01:40 +00:00
|
|
|
const auto allLoaded = (fullCount >= 0) && (loadedCount >= fullCount);
|
|
|
|
if (fullCount >= 0 && loadedCount < kPreloadIfLess && !allLoaded) {
|
2022-10-07 13:56:07 +00:00
|
|
|
requestReactions(entry, loadedCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnreadThings::cancelRequests(not_null<DialogsEntry*> entry) {
|
|
|
|
if (const auto requestId = _mentionsRequests.take(entry)) {
|
|
|
|
_api->request(*requestId).cancel();
|
|
|
|
}
|
|
|
|
if (const auto requestId = _reactionsRequests.take(entry)) {
|
|
|
|
_api->request(*requestId).cancel();
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::requestMentions(
|
|
|
|
not_null<DialogsEntry*> entry,
|
|
|
|
int loaded) {
|
|
|
|
if (_mentionsRequests.contains(entry)) {
|
2022-01-26 16:01:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto offsetId = std::max(
|
2022-10-07 13:56:07 +00:00
|
|
|
entry->unreadMentions().maxLoaded(),
|
2022-01-26 16:01:40 +00:00
|
|
|
MsgId(1));
|
|
|
|
const auto limit = loaded ? kNextRequestLimit : kFirstRequestLimit;
|
|
|
|
const auto addOffset = loaded ? -(limit + 1) : -limit;
|
|
|
|
const auto maxId = 0;
|
|
|
|
const auto minId = 0;
|
2022-10-07 13:56:07 +00:00
|
|
|
const auto history = ResolveHistory(entry);
|
|
|
|
const auto topic = entry->asTopic();
|
|
|
|
using Flag = MTPmessages_GetUnreadMentions::Flag;
|
2022-01-26 16:01:40 +00:00
|
|
|
const auto requestId = _api->request(MTPmessages_GetUnreadMentions(
|
2022-10-07 13:56:07 +00:00
|
|
|
MTP_flags(topic ? Flag::f_top_msg_id : Flag()),
|
2022-01-26 16:01:40 +00:00
|
|
|
history->peer->input,
|
2022-10-07 13:56:07 +00:00
|
|
|
MTP_int(topic ? topic->rootId() : 0),
|
2022-01-26 16:01:40 +00:00
|
|
|
MTP_int(offsetId),
|
|
|
|
MTP_int(addOffset),
|
|
|
|
MTP_int(limit),
|
|
|
|
MTP_int(maxId),
|
|
|
|
MTP_int(minId)
|
|
|
|
)).done([=](const MTPmessages_Messages &result) {
|
|
|
|
_mentionsRequests.remove(history);
|
2022-01-31 15:29:46 +00:00
|
|
|
history->unreadMentions().addSlice(result, loaded);
|
2022-01-26 16:01:40 +00:00
|
|
|
}).fail([=] {
|
|
|
|
_mentionsRequests.remove(history);
|
|
|
|
}).send();
|
|
|
|
_mentionsRequests.emplace(history, requestId);
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::requestReactions(
|
|
|
|
not_null<DialogsEntry*> entry,
|
|
|
|
int loaded) {
|
|
|
|
if (_reactionsRequests.contains(entry)) {
|
2022-01-26 16:01:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-31 15:29:46 +00:00
|
|
|
const auto offsetId = loaded
|
2022-10-07 13:56:07 +00:00
|
|
|
? std::max(entry->unreadReactions().maxLoaded(), MsgId(1))
|
2022-01-31 15:29:46 +00:00
|
|
|
: MsgId(1);
|
2022-01-26 16:01:40 +00:00
|
|
|
const auto limit = loaded ? kNextRequestLimit : kFirstRequestLimit;
|
|
|
|
const auto addOffset = loaded ? -(limit + 1) : -limit;
|
|
|
|
const auto maxId = 0;
|
|
|
|
const auto minId = 0;
|
2022-10-07 13:56:07 +00:00
|
|
|
const auto history = ResolveHistory(entry);
|
|
|
|
const auto topic = entry->asTopic();
|
|
|
|
using Flag = MTPmessages_GetUnreadReactions::Flag;
|
2022-01-26 16:01:40 +00:00
|
|
|
const auto requestId = _api->request(MTPmessages_GetUnreadReactions(
|
2022-10-07 13:56:07 +00:00
|
|
|
MTP_flags(topic ? Flag::f_top_msg_id : Flag()),
|
2022-01-26 16:01:40 +00:00
|
|
|
history->peer->input,
|
2022-10-07 13:56:07 +00:00
|
|
|
MTP_int(topic ? topic->rootId() : 0),
|
2022-01-26 16:01:40 +00:00
|
|
|
MTP_int(offsetId),
|
|
|
|
MTP_int(addOffset),
|
|
|
|
MTP_int(limit),
|
|
|
|
MTP_int(maxId),
|
|
|
|
MTP_int(minId)
|
|
|
|
)).done([=](const MTPmessages_Messages &result) {
|
|
|
|
_reactionsRequests.remove(history);
|
2022-01-31 15:29:46 +00:00
|
|
|
history->unreadReactions().addSlice(result, loaded);
|
|
|
|
}).fail([=] {
|
2022-01-26 16:01:40 +00:00
|
|
|
_reactionsRequests.remove(history);
|
|
|
|
}).send();
|
|
|
|
_reactionsRequests.emplace(history, requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace UnreadThings
|