2020-02-18 11:39:24 +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 "data/data_histories.h"
|
|
|
|
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_channel.h"
|
2020-02-21 07:58:50 +00:00
|
|
|
#include "data/data_folder.h"
|
2020-02-18 11:39:24 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/view/history_view_element.h"
|
2020-02-21 07:58:50 +00:00
|
|
|
#include "core/application.h"
|
2020-02-18 11:39:24 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kReadRequestTimeout = 3 * crl::time(1000);
|
|
|
|
constexpr auto kReadRequestSent = std::numeric_limits<crl::time>::max();
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Histories::Histories(not_null<Session*> owner)
|
|
|
|
: _owner(owner)
|
|
|
|
, _readRequestsTimer([=] { sendReadRequests(); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Session &Histories::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
Main::Session &Histories::session() const {
|
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
|
|
|
History *Histories::find(PeerId peerId) {
|
|
|
|
const auto i = peerId ? _map.find(peerId) : end(_map);
|
|
|
|
return (i != end(_map)) ? i->second.get() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<History*> Histories::findOrCreate(PeerId peerId) {
|
|
|
|
Expects(peerId != 0);
|
|
|
|
|
|
|
|
if (const auto result = find(peerId)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const auto [i, ok] = _map.emplace(
|
|
|
|
peerId,
|
|
|
|
std::make_unique<History>(&owner(), peerId));
|
|
|
|
return i->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::unloadAll() {
|
|
|
|
for (const auto &[peerId, history] : _map) {
|
|
|
|
history->clear(History::ClearType::Unload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::clearAll() {
|
|
|
|
_map.clear();
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
void Histories::readInbox(not_null<History*> history) {
|
|
|
|
if (history->lastServerMessageKnown()) {
|
|
|
|
const auto last = history->lastServerMessage();
|
|
|
|
readInboxTill(history, last ? last->id : 0);
|
|
|
|
return;
|
|
|
|
} else if (history->loadedAtBottom()) {
|
|
|
|
if (const auto lastId = history->maxMsgId()) {
|
|
|
|
readInboxTill(history, lastId);
|
|
|
|
return;
|
|
|
|
} else if (history->loadedAtTop()) {
|
|
|
|
readInboxTill(history, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 07:58:50 +00:00
|
|
|
requestDialogEntry(history, [=] {
|
2020-02-19 15:35:26 +00:00
|
|
|
Expects(history->lastServerMessageKnown());
|
|
|
|
|
|
|
|
const auto last = history->lastServerMessage();
|
|
|
|
readInboxTill(history, last ? last->id : 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::readInboxTill(not_null<HistoryItem*> item) {
|
|
|
|
const auto history = item->history();
|
2020-02-18 11:39:24 +00:00
|
|
|
if (!IsServerMsgId(item->id)) {
|
2020-02-19 15:35:26 +00:00
|
|
|
readClientSideMessage(item);
|
2020-02-18 11:39:24 +00:00
|
|
|
auto view = item->mainView();
|
|
|
|
if (!view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto block = view->block();
|
|
|
|
auto blockIndex = block->indexInHistory();
|
|
|
|
auto itemIndex = view->indexInBlock();
|
|
|
|
while (blockIndex > 0 || itemIndex > 0) {
|
|
|
|
if (itemIndex > 0) {
|
|
|
|
view = block->messages[--itemIndex].get();
|
|
|
|
} else {
|
|
|
|
while (blockIndex > 0) {
|
|
|
|
block = history->blocks[--blockIndex].get();
|
|
|
|
itemIndex = block->messages.size();
|
|
|
|
if (itemIndex > 0) {
|
|
|
|
view = block->messages[--itemIndex].get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item = view->data();
|
|
|
|
if (IsServerMsgId(item->id)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!IsServerMsgId(item->id)) {
|
|
|
|
LOG(("App Error: "
|
|
|
|
"Can't read history till unknown local message."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 12:15:43 +00:00
|
|
|
readInboxTill(history, item->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::readInboxTill(not_null<History*> history, MsgId tillId) {
|
2020-02-19 15:35:26 +00:00
|
|
|
readInboxTill(history, tillId, false);
|
|
|
|
}
|
2020-02-19 16:54:19 +00:00
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
void Histories::readInboxTill(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId tillId,
|
|
|
|
bool force) {
|
2020-02-20 09:37:00 +00:00
|
|
|
Expects(IsServerMsgId(tillId) || (!tillId && !force));
|
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
if (!history->readInboxTillNeedsRequest(tillId) && !force) {
|
2020-02-18 11:39:24 +00:00
|
|
|
return;
|
2020-02-21 07:34:24 +00:00
|
|
|
} else if (!history->trackUnreadMessages()) {
|
|
|
|
return;
|
2020-02-19 15:35:26 +00:00
|
|
|
} else if (!force) {
|
|
|
|
const auto maybeState = lookup(history);
|
|
|
|
if (maybeState && maybeState->readTill >= tillId) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
const auto stillUnread = history->countStillUnreadLocal(tillId);
|
2020-02-19 15:35:26 +00:00
|
|
|
if (!force
|
|
|
|
&& stillUnread
|
2020-02-18 11:39:24 +00:00
|
|
|
&& history->unreadCountKnown()
|
|
|
|
&& *stillUnread == history->unreadCount()) {
|
|
|
|
history->setInboxReadTill(tillId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &state = _states[history];
|
2020-02-21 13:03:03 +00:00
|
|
|
if (state.readTillSent >= tillId) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
state.readTillSent = 0;
|
|
|
|
}
|
2020-02-19 12:40:01 +00:00
|
|
|
const auto wasReadTill = state.readTill;
|
2020-02-18 11:39:24 +00:00
|
|
|
state.readTill = tillId;
|
2020-02-19 15:35:26 +00:00
|
|
|
if (force || !stillUnread || !*stillUnread) {
|
2020-02-18 11:39:24 +00:00
|
|
|
state.readWhen = 0;
|
|
|
|
sendReadRequests();
|
2020-02-19 15:35:26 +00:00
|
|
|
if (!stillUnread) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-19 12:40:01 +00:00
|
|
|
} else if (!wasReadTill) {
|
2020-02-18 11:39:24 +00:00
|
|
|
state.readWhen = crl::now() + kReadRequestTimeout;
|
|
|
|
if (!_readRequestsTimer.isActive()) {
|
|
|
|
_readRequestsTimer.callOnce(kReadRequestTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
history->setInboxReadTill(tillId);
|
|
|
|
history->setUnreadCount(*stillUnread);
|
|
|
|
history->updateChatListEntry();
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
void Histories::readInboxOnNewMessage(not_null<HistoryItem*> item) {
|
|
|
|
if (!IsServerMsgId(item->id)) {
|
|
|
|
readClientSideMessage(item);
|
|
|
|
} else {
|
|
|
|
readInboxTill(item->history(), item->id, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::readClientSideMessage(not_null<HistoryItem*> item) {
|
|
|
|
if (item->out() || !item->unread()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto history = item->history();
|
|
|
|
item->markClientSideAsRead();
|
|
|
|
if (const auto unread = history->unreadCount()) {
|
|
|
|
history->setUnreadCount(unread - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 07:58:50 +00:00
|
|
|
void Histories::requestDialogEntry(not_null<Data::Folder*> folder) {
|
|
|
|
if (_dialogFolderRequests.contains(folder)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_dialogFolderRequests.emplace(folder);
|
|
|
|
|
|
|
|
auto peers = QVector<MTPInputDialogPeer>(
|
|
|
|
1,
|
|
|
|
MTP_inputDialogPeerFolder(MTP_int(folder->id())));
|
|
|
|
session().api().request(MTPmessages_GetPeerDialogs(
|
|
|
|
MTP_vector(std::move(peers))
|
|
|
|
)).done([=](const MTPmessages_PeerDialogs &result) {
|
|
|
|
applyPeerDialogs(result);
|
|
|
|
_dialogFolderRequests.remove(folder);
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_dialogFolderRequests.remove(folder);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::requestDialogEntry(
|
|
|
|
not_null<History*> history,
|
|
|
|
Fn<void()> callback) {
|
|
|
|
const auto i = _dialogRequests.find(history);
|
|
|
|
if (i != end(_dialogRequests)) {
|
|
|
|
if (callback) {
|
|
|
|
i->second.push_back(std::move(callback));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto [j, ok] = _dialogRequestsPending.try_emplace(history);
|
|
|
|
if (callback) {
|
|
|
|
j->second.push_back(std::move(callback));
|
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_dialogRequestsPending.size() > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Core::App().postponeCall(crl::guard(&session(), [=] {
|
|
|
|
sendDialogRequests();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::sendDialogRequests() {
|
|
|
|
if (_dialogRequestsPending.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-21 09:22:01 +00:00
|
|
|
const auto histories = ranges::view::all(
|
|
|
|
_dialogRequestsPending
|
|
|
|
) | ranges::view::transform([](const auto &pair) {
|
|
|
|
return pair.first;
|
|
|
|
}) | ranges::view::filter([&](not_null<History*> history) {
|
|
|
|
const auto state = lookup(history);
|
|
|
|
if (!state) {
|
|
|
|
return true;
|
|
|
|
} else if (!postponeEntryRequest(*state)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
state->postponedRequestEntry = true;
|
|
|
|
return false;
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
|
2020-02-21 07:58:50 +00:00
|
|
|
auto peers = QVector<MTPInputDialogPeer>();
|
|
|
|
const auto dialogPeer = [](not_null<History*> history) {
|
|
|
|
return MTP_inputDialogPeer(history->peer->input);
|
|
|
|
};
|
|
|
|
ranges::transform(
|
|
|
|
histories,
|
|
|
|
ranges::back_inserter(peers),
|
|
|
|
dialogPeer);
|
|
|
|
for (auto &[history, callbacks] : base::take(_dialogRequestsPending)) {
|
|
|
|
_dialogRequests.emplace(history, std::move(callbacks));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto finalize = [=] {
|
|
|
|
for (const auto history : histories) {
|
2020-02-21 09:22:01 +00:00
|
|
|
const auto state = lookup(history);
|
|
|
|
if (!state || !state->postponedRequestEntry) {
|
|
|
|
dialogEntryApplied(history);
|
|
|
|
history->updateChatListExistence();
|
|
|
|
}
|
2020-02-21 07:58:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
session().api().request(MTPmessages_GetPeerDialogs(
|
|
|
|
MTP_vector(std::move(peers))
|
|
|
|
)).done([=](const MTPmessages_PeerDialogs &result) {
|
|
|
|
applyPeerDialogs(result);
|
|
|
|
finalize();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
finalize();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::dialogEntryApplied(not_null<History*> history) {
|
|
|
|
history->dialogEntryApplied();
|
|
|
|
if (const auto callbacks = _dialogRequestsPending.take(history)) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto callbacks = _dialogRequests.take(history)) {
|
|
|
|
for (const auto &callback : *callbacks) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::applyPeerDialogs(const MTPmessages_PeerDialogs &dialogs) {
|
|
|
|
Expects(dialogs.type() == mtpc_messages_peerDialogs);
|
|
|
|
|
|
|
|
const auto &data = dialogs.c_messages_peerDialogs();
|
|
|
|
_owner->processUsers(data.vusers());
|
|
|
|
_owner->processChats(data.vchats());
|
|
|
|
_owner->processMessages(data.vmessages(), NewMessageType::Last);
|
|
|
|
for (const auto &dialog : data.vdialogs().v) {
|
|
|
|
dialog.match([&](const MTPDdialog &data) {
|
|
|
|
if (const auto peerId = peerFromMTP(data.vpeer())) {
|
|
|
|
_owner->history(peerId)->applyDialog(nullptr, data);
|
|
|
|
}
|
|
|
|
}, [&](const MTPDdialogFolder &data) {
|
|
|
|
const auto folder = _owner->processFolder(data.vfolder());
|
|
|
|
folder->applyDialog(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_owner->sendHistoryChangeNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::changeDialogUnreadMark(
|
|
|
|
not_null<History*> history,
|
|
|
|
bool unread) {
|
|
|
|
history->setUnreadMark(unread);
|
|
|
|
|
|
|
|
using Flag = MTPmessages_MarkDialogUnread::Flag;
|
|
|
|
session().api().request(MTPmessages_MarkDialogUnread(
|
|
|
|
MTP_flags(unread ? Flag::f_unread : Flag(0)),
|
|
|
|
MTP_inputDialogPeer(history->peer->input)
|
|
|
|
)).send();
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:51:37 +00:00
|
|
|
void Histories::requestFakeChatListMessage(
|
|
|
|
not_null<History*> history) {
|
|
|
|
if (_fakeChatListRequests.contains(history)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fakeChatListRequests.emplace(history);
|
|
|
|
sendRequest(history, RequestType::History, [=](Fn<void()> finish) {
|
|
|
|
return session().api().request(MTPmessages_GetHistory(
|
|
|
|
history->peer->input,
|
|
|
|
MTP_int(0), // offset_id
|
|
|
|
MTP_int(0), // offset_date
|
|
|
|
MTP_int(0), // add_offset
|
|
|
|
MTP_int(2), // limit
|
|
|
|
MTP_int(0), // max_id
|
|
|
|
MTP_int(0), // min_id
|
|
|
|
MTP_int(0)
|
|
|
|
)).done([=](const MTPmessages_Messages &result) {
|
|
|
|
_fakeChatListRequests.erase(history);
|
|
|
|
history->setFakeChatListMessageFrom(result);
|
|
|
|
finish();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
_fakeChatListRequests.erase(history);
|
|
|
|
history->setFakeChatListMessageFrom(MTP_messages_messages(
|
|
|
|
MTP_vector<MTPMessage>(0),
|
|
|
|
MTP_vector<MTPChat>(0),
|
|
|
|
MTP_vector<MTPUser>(0)));
|
|
|
|
finish();
|
|
|
|
}).send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
void Histories::sendPendingReadInbox(not_null<History*> history) {
|
|
|
|
if (const auto state = lookup(history)) {
|
2020-02-21 07:11:27 +00:00
|
|
|
if (state->readTill
|
|
|
|
&& state->readWhen
|
|
|
|
&& state->readWhen != kReadRequestSent) {
|
2020-02-18 11:39:24 +00:00
|
|
|
state->readWhen = 0;
|
|
|
|
sendReadRequests();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::sendReadRequests() {
|
|
|
|
if (_states.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto now = crl::now();
|
|
|
|
auto next = std::optional<crl::time>();
|
|
|
|
for (auto &[history, state] : _states) {
|
2020-02-20 09:37:00 +00:00
|
|
|
if (!state.readTill) {
|
|
|
|
continue;
|
|
|
|
} else if (state.readWhen <= now) {
|
2020-02-18 11:39:24 +00:00
|
|
|
sendReadRequest(history, state);
|
|
|
|
} else if (!next || *next > state.readWhen) {
|
|
|
|
next = state.readWhen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (next.has_value()) {
|
|
|
|
_readRequestsTimer.callOnce(*next - now);
|
2020-02-19 12:40:01 +00:00
|
|
|
} else {
|
|
|
|
_readRequestsTimer.cancel();
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::sendReadRequest(not_null<History*> history, State &state) {
|
|
|
|
const auto tillId = state.readTill;
|
|
|
|
state.readWhen = kReadRequestSent;
|
2020-02-21 13:03:03 +00:00
|
|
|
state.readTillSent = tillId;
|
2020-02-21 10:29:48 +00:00
|
|
|
sendRequest(history, RequestType::ReadInbox, [=](Fn<void()> finish) {
|
2020-02-18 11:39:24 +00:00
|
|
|
const auto finished = [=] {
|
|
|
|
const auto state = lookup(history);
|
|
|
|
Assert(state != nullptr);
|
2020-02-19 12:40:01 +00:00
|
|
|
Assert(state->readTill >= tillId);
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
if (history->unreadCountRefreshNeeded(tillId)) {
|
2020-02-21 07:58:50 +00:00
|
|
|
requestDialogEntry(history);
|
2020-02-21 13:03:03 +00:00
|
|
|
} else if (state->readTillSent == tillId) {
|
|
|
|
state->readTillSent = 0;
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
if (state->readWhen == kReadRequestSent) {
|
|
|
|
state->readWhen = 0;
|
2020-02-19 12:40:01 +00:00
|
|
|
if (state->readTill == tillId) {
|
|
|
|
state->readTill = 0;
|
|
|
|
} else {
|
|
|
|
sendReadRequests();
|
|
|
|
}
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
2020-02-21 10:29:48 +00:00
|
|
|
finish();
|
2020-02-18 11:39:24 +00:00
|
|
|
};
|
|
|
|
if (const auto channel = history->peer->asChannel()) {
|
|
|
|
return session().api().request(MTPchannels_ReadHistory(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_int(tillId)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
finished();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
finished();
|
|
|
|
}).send();
|
|
|
|
} else {
|
|
|
|
return session().api().request(MTPmessages_ReadHistory(
|
|
|
|
history->peer->input,
|
|
|
|
MTP_int(tillId)
|
|
|
|
)).done([=](const MTPmessages_AffectedMessages &result) {
|
|
|
|
session().api().applyAffectedMessages(history->peer, result);
|
|
|
|
finished();
|
|
|
|
}).fail([=](const RPCError &error) {
|
|
|
|
finished();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::checkEmptyState(not_null<History*> history) {
|
|
|
|
const auto empty = [](const State &state) {
|
|
|
|
return state.postponed.empty()
|
2020-02-21 09:22:01 +00:00
|
|
|
&& !state.postponedRequestEntry
|
2020-02-18 11:39:24 +00:00
|
|
|
&& state.sent.empty()
|
2020-02-21 13:03:03 +00:00
|
|
|
&& (state.readTill == 0)
|
|
|
|
&& (state.readTillSent == 0);
|
2020-02-18 11:39:24 +00:00
|
|
|
};
|
|
|
|
const auto i = _states.find(history);
|
|
|
|
if (i != end(_states) && empty(i->second)) {
|
|
|
|
_states.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:22:01 +00:00
|
|
|
bool Histories::postponeHistoryRequest(const State &state) const {
|
|
|
|
const auto proj = [](const auto &pair) {
|
|
|
|
return pair.second.type;
|
|
|
|
};
|
|
|
|
const auto i = ranges::find(state.sent, RequestType::Delete, proj);
|
|
|
|
return (i != end(state.sent));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Histories::postponeEntryRequest(const State &state) const {
|
|
|
|
const auto i = ranges::find_if(state.sent, [](const auto &pair) {
|
|
|
|
return pair.second.type != RequestType::History;
|
|
|
|
});
|
|
|
|
return (i != end(state.sent));
|
|
|
|
}
|
|
|
|
|
2020-02-21 10:29:48 +00:00
|
|
|
void Histories::deleteMessages(
|
|
|
|
not_null<History*> history,
|
|
|
|
const QVector<MTPint> &ids,
|
|
|
|
bool revoke) {
|
|
|
|
sendRequest(history, RequestType::Delete, [=](Fn<void()> finish) {
|
|
|
|
const auto done = [=](const MTPmessages_AffectedMessages &result) {
|
|
|
|
session().api().applyAffectedMessages(history->peer, result);
|
|
|
|
finish();
|
|
|
|
history->requestChatListMessage();
|
|
|
|
};
|
|
|
|
const auto fail = [=](const RPCError &error) {
|
|
|
|
finish();
|
|
|
|
};
|
|
|
|
if (const auto channel = history->peer->asChannel()) {
|
|
|
|
return session().api().request(MTPchannels_DeleteMessages(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_vector<MTPint>(ids)
|
|
|
|
)).done(done).fail(fail).send();
|
|
|
|
} else {
|
|
|
|
using Flag = MTPmessages_DeleteMessages::Flag;
|
|
|
|
return session().api().request(MTPmessages_DeleteMessages(
|
|
|
|
MTP_flags(revoke ? Flag::f_revoke : Flag(0)),
|
|
|
|
MTP_vector<MTPint>(ids)
|
|
|
|
)).done(done).fail(fail).send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::deleteAllMessages(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId deleteTillId,
|
|
|
|
bool justClear,
|
|
|
|
bool revoke) {
|
|
|
|
sendRequest(history, RequestType::Delete, [=](Fn<void()> finish) {
|
|
|
|
const auto peer = history->peer;
|
|
|
|
const auto fail = [=](const RPCError &error) {
|
|
|
|
finish();
|
|
|
|
};
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
|
|
|
return session().api().request(MTPchannels_DeleteHistory(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_int(deleteTillId)
|
|
|
|
)).done([=](const MTPBool &result) {
|
|
|
|
finish();
|
|
|
|
}).fail(fail).send();
|
|
|
|
} else {
|
|
|
|
using Flag = MTPmessages_DeleteHistory::Flag;
|
|
|
|
const auto flags = Flag(0)
|
|
|
|
| (justClear ? Flag::f_just_clear : Flag(0))
|
|
|
|
| ((peer->isUser() && revoke) ? Flag::f_revoke : Flag(0));
|
|
|
|
return session().api().request(MTPmessages_DeleteHistory(
|
|
|
|
MTP_flags(flags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(0)
|
|
|
|
)).done([=](const MTPmessages_AffectedHistory &result) {
|
|
|
|
const auto offset = session().api().applyAffectedHistory(
|
|
|
|
peer,
|
|
|
|
result);
|
|
|
|
if (offset > 0) {
|
|
|
|
deleteAllMessages(
|
|
|
|
history,
|
|
|
|
deleteTillId,
|
|
|
|
justClear,
|
|
|
|
revoke);
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}).fail(fail).send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
int Histories::sendRequest(
|
|
|
|
not_null<History*> history,
|
|
|
|
RequestType type,
|
2020-02-21 10:29:48 +00:00
|
|
|
Fn<mtpRequestId(Fn<void()> finish)> generator) {
|
2020-02-18 11:39:24 +00:00
|
|
|
Expects(type != RequestType::None);
|
|
|
|
|
|
|
|
auto &state = _states[history];
|
2020-02-21 12:57:06 +00:00
|
|
|
const auto id = ++_requestAutoincrement;
|
|
|
|
_historyByRequest.emplace(id, history);
|
2020-02-21 09:22:01 +00:00
|
|
|
if (type == RequestType::History && postponeHistoryRequest(state)) {
|
2020-02-18 11:39:24 +00:00
|
|
|
state.postponed.emplace(
|
|
|
|
id,
|
2020-02-21 09:22:01 +00:00
|
|
|
PostponedHistoryRequest{ std::move(generator) });
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
const auto requestId = generator([=] { checkPostponed(history, id); });
|
|
|
|
state.sent.emplace(id, SentRequest{
|
|
|
|
std::move(generator),
|
|
|
|
requestId,
|
|
|
|
type
|
|
|
|
});
|
|
|
|
if (!state.postponedRequestEntry
|
|
|
|
&& postponeEntryRequest(state)
|
|
|
|
&& _dialogRequests.contains(history)) {
|
|
|
|
state.postponedRequestEntry = true;
|
|
|
|
}
|
|
|
|
if (postponeHistoryRequest(state)) {
|
|
|
|
const auto resendHistoryRequest = [&](auto &pair) {
|
|
|
|
auto &[id, sent] = pair;
|
|
|
|
if (sent.type != RequestType::History) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
state.postponed.emplace(
|
|
|
|
id,
|
|
|
|
PostponedHistoryRequest{ std::move(sent.generator) });
|
|
|
|
session().api().request(sent.id).cancel();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
state.sent.erase(
|
|
|
|
ranges::remove_if(state.sent, resendHistoryRequest),
|
|
|
|
end(state.sent));
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:22:01 +00:00
|
|
|
void Histories::checkPostponed(not_null<History*> history, int id) {
|
2020-02-18 11:39:24 +00:00
|
|
|
const auto state = lookup(history);
|
|
|
|
Assert(state != nullptr);
|
|
|
|
|
2020-02-21 09:22:01 +00:00
|
|
|
finishSentRequest(history, state, id);
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 12:57:06 +00:00
|
|
|
void Histories::cancelRequest(int id) {
|
|
|
|
const auto history = _historyByRequest.take(id);
|
|
|
|
if (!history) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto state = lookup(*history);
|
2020-02-21 09:22:01 +00:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->postponed.remove(id);
|
2020-02-21 12:57:06 +00:00
|
|
|
finishSentRequest(*history, state, id);
|
2020-02-21 09:22:01 +00:00
|
|
|
}
|
2020-02-18 11:39:24 +00:00
|
|
|
|
2020-02-21 09:22:01 +00:00
|
|
|
void Histories::finishSentRequest(
|
|
|
|
not_null<History*> history,
|
|
|
|
not_null<State*> state,
|
|
|
|
int id) {
|
2020-02-21 12:57:06 +00:00
|
|
|
_historyByRequest.remove(id);
|
2020-02-21 13:36:49 +00:00
|
|
|
const auto i = state->sent.find(id);
|
|
|
|
if (i != end(state->sent)) {
|
|
|
|
session().api().request(i->second.id).cancel();
|
|
|
|
state->sent.erase(i);
|
|
|
|
}
|
2020-02-21 09:22:01 +00:00
|
|
|
if (!state->postponed.empty() && !postponeHistoryRequest(*state)) {
|
|
|
|
for (auto &[id, postponed] : base::take(state->postponed)) {
|
|
|
|
const auto requestId = postponed.generator([=] {
|
|
|
|
checkPostponed(history, id);
|
|
|
|
});
|
|
|
|
state->sent.emplace(id, SentRequest{
|
|
|
|
std::move(postponed.generator),
|
|
|
|
requestId,
|
|
|
|
RequestType::History
|
|
|
|
});
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-21 09:22:01 +00:00
|
|
|
if (state->postponedRequestEntry && !postponeEntryRequest(*state)) {
|
|
|
|
const auto i = _dialogRequests.find(history);
|
|
|
|
Assert(i != end(_dialogRequests));
|
|
|
|
const auto [j, ok] = _dialogRequestsPending.emplace(
|
|
|
|
history,
|
|
|
|
std::move(i->second));
|
|
|
|
Assert(ok);
|
2020-02-21 12:57:06 +00:00
|
|
|
_dialogRequests.erase(i);
|
2020-02-21 09:22:01 +00:00
|
|
|
state->postponedRequestEntry = false;
|
|
|
|
}
|
|
|
|
checkEmptyState(history);
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Histories::State *Histories::lookup(not_null<History*> history) {
|
|
|
|
const auto i = _states.find(history);
|
|
|
|
return (i != end(_states)) ? &i->second : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|