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;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
UnreadThings::UnreadThings(not_null<ApiWrap*> api) : _api(api) {
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:32:03 +00:00
|
|
|
bool UnreadThings::trackMentions(Data::Thread *thread) const {
|
2022-10-13 17:34:04 +00:00
|
|
|
const auto peer = thread ? thread->peer().get() : nullptr;
|
2022-01-26 16:01:40 +00:00
|
|
|
return peer && (peer->isChat() || peer->isMegagroup());
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:32:03 +00:00
|
|
|
bool UnreadThings::trackReactions(Data::Thread *thread) const {
|
2022-10-13 17:34:04 +00:00
|
|
|
const auto peer = thread ? thread->peer().get() : nullptr;
|
2023-01-03 05:22:46 +00:00
|
|
|
return peer && (peer->isUser() || peer->isChat() || peer->isMegagroup());
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:32:03 +00:00
|
|
|
void UnreadThings::preloadEnough(Data::Thread *thread) {
|
|
|
|
if (trackMentions(thread)) {
|
|
|
|
preloadEnoughMentions(thread);
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
2022-10-13 10:32:03 +00:00
|
|
|
if (trackReactions(thread)) {
|
|
|
|
preloadEnoughReactions(thread);
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-13 10:32:03 +00:00
|
|
|
void UnreadThings::preloadEnoughMentions(not_null<Data::Thread*> thread) {
|
|
|
|
const auto fullCount = thread->unreadMentions().count();
|
|
|
|
const auto loadedCount = thread->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-13 10:32:03 +00:00
|
|
|
requestMentions(thread, loadedCount);
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:32:03 +00:00
|
|
|
void UnreadThings::preloadEnoughReactions(not_null<Data::Thread*> thread) {
|
|
|
|
const auto fullCount = thread->unreadReactions().count();
|
|
|
|
const auto loadedCount = thread->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-13 10:32:03 +00:00
|
|
|
requestReactions(thread, loadedCount);
|
2022-10-07 13:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:32:03 +00:00
|
|
|
void UnreadThings::cancelRequests(not_null<Data::Thread*> thread) {
|
|
|
|
if (const auto requestId = _mentionsRequests.take(thread)) {
|
2022-10-07 13:56:07 +00:00
|
|
|
_api->request(*requestId).cancel();
|
|
|
|
}
|
2022-10-13 10:32:03 +00:00
|
|
|
if (const auto requestId = _reactionsRequests.take(thread)) {
|
2022-10-07 13:56:07 +00:00
|
|
|
_api->request(*requestId).cancel();
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::requestMentions(
|
2022-10-13 10:32:03 +00:00
|
|
|
not_null<Data::Thread*> thread,
|
2022-10-07 13:56:07 +00:00
|
|
|
int loaded) {
|
2022-10-13 10:32:03 +00:00
|
|
|
if (_mentionsRequests.contains(thread)) {
|
2022-01-26 16:01:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto offsetId = std::max(
|
2022-10-13 10:32:03 +00:00
|
|
|
thread->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-13 10:32:03 +00:00
|
|
|
const auto history = thread->owningHistory();
|
|
|
|
const auto topic = thread->asTopic();
|
2022-10-07 13:56:07 +00:00
|
|
|
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) {
|
2022-10-13 10:32:03 +00:00
|
|
|
_mentionsRequests.remove(thread);
|
|
|
|
thread->unreadMentions().addSlice(result, loaded);
|
2022-01-26 16:01:40 +00:00
|
|
|
}).fail([=] {
|
2022-10-13 10:32:03 +00:00
|
|
|
_mentionsRequests.remove(thread);
|
2022-01-26 16:01:40 +00:00
|
|
|
}).send();
|
2022-10-13 10:32:03 +00:00
|
|
|
_mentionsRequests.emplace(thread, requestId);
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
void UnreadThings::requestReactions(
|
2022-10-13 10:32:03 +00:00
|
|
|
not_null<Data::Thread*> thread,
|
2022-10-07 13:56:07 +00:00
|
|
|
int loaded) {
|
2022-10-13 10:32:03 +00:00
|
|
|
if (_reactionsRequests.contains(thread)) {
|
2022-01-26 16:01:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-31 15:29:46 +00:00
|
|
|
const auto offsetId = loaded
|
2022-10-13 10:32:03 +00:00
|
|
|
? std::max(thread->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-13 10:32:03 +00:00
|
|
|
const auto history = thread->owningHistory();
|
|
|
|
const auto topic = thread->asTopic();
|
2022-10-07 13:56:07 +00:00
|
|
|
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) {
|
2022-10-13 10:32:03 +00:00
|
|
|
_reactionsRequests.remove(thread);
|
|
|
|
thread->unreadReactions().addSlice(result, loaded);
|
2022-01-31 15:29:46 +00:00
|
|
|
}).fail([=] {
|
2022-10-13 10:32:03 +00:00
|
|
|
_reactionsRequests.remove(thread);
|
2022-01-26 16:01:40 +00:00
|
|
|
}).send();
|
2022-10-13 10:32:03 +00:00
|
|
|
_reactionsRequests.emplace(thread, requestId);
|
2022-01-26 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace UnreadThings
|