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"
|
2021-01-21 04:20:29 +00:00
|
|
|
#include "data/data_chat.h"
|
2020-02-21 07:58:50 +00:00
|
|
|
#include "data/data_folder.h"
|
2022-10-04 15:34:45 +00:00
|
|
|
#include "data/data_forum.h"
|
|
|
|
#include "data/data_forum_topic.h"
|
2020-09-17 13:26:53 +00:00
|
|
|
#include "data/data_scheduled_messages.h"
|
2021-11-16 07:26:35 +00:00
|
|
|
#include "base/unixtime.h"
|
2022-10-04 15:34:45 +00:00
|
|
|
#include "base/random.h"
|
2020-02-18 11:39:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-02-23 08:48:39 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2020-02-18 11:39:24 +00:00
|
|
|
#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);
|
|
|
|
|
|
|
|
} // 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) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: readInbox called."));
|
2020-02-19 15:35:26 +00:00
|
|
|
if (history->lastServerMessageKnown()) {
|
|
|
|
const auto last = history->lastServerMessage();
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: last known, reading till %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(last ? last->id.bare : 0));
|
2020-02-19 15:35:26 +00:00
|
|
|
readInboxTill(history, last ? last->id : 0);
|
|
|
|
return;
|
|
|
|
} else if (history->loadedAtBottom()) {
|
|
|
|
if (const auto lastId = history->maxMsgId()) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: loaded at bottom, maxMsgId %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(lastId.bare));
|
2020-02-19 15:35:26 +00:00
|
|
|
readInboxTill(history, lastId);
|
|
|
|
return;
|
|
|
|
} else if (history->loadedAtTop()) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: loaded at bottom, loaded at top."));
|
2020-02-19 15:35:26 +00:00
|
|
|
readInboxTill(history, 0);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: loaded at bottom, but requesting entry."));
|
2020-02-19 15:35:26 +00:00
|
|
|
}
|
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();
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: got entry, reading till %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(last ? last->id.bare : 0));
|
2020-02-19 15:35:26 +00:00
|
|
|
readInboxTill(history, last ? last->id : 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::readInboxTill(not_null<HistoryItem*> item) {
|
|
|
|
const auto history = item->history();
|
2021-11-07 08:06:00 +00:00
|
|
|
if (!item->isRegular()) {
|
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();
|
2021-11-07 08:06:00 +00:00
|
|
|
if (item->isRegular()) {
|
2020-02-18 11:39:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 08:06:00 +00:00
|
|
|
if (!item->isRegular()) {
|
2020-02-18 11:39:24 +00:00
|
|
|
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-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: readInboxTill %1, force %2."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare
|
2020-05-12 12:18:19 +00:00
|
|
|
).arg(Logs::b(force)));
|
|
|
|
|
2020-02-24 10:53:38 +00:00
|
|
|
const auto syncGuard = gsl::finally([&] {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: in guard, unread %1."
|
|
|
|
).arg(history->unreadCount()));
|
2020-02-24 10:53:38 +00:00
|
|
|
if (history->unreadCount() > 0) {
|
|
|
|
if (const auto last = history->lastServerMessage()) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: checking last %1 and %2."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(last->id.bare
|
|
|
|
).arg(tillId.bare));
|
2020-02-24 10:53:38 +00:00
|
|
|
if (last->id == tillId) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: locally marked as read."));
|
2020-02-24 10:53:38 +00:00
|
|
|
history->setUnreadCount(0);
|
|
|
|
history->updateChatListEntry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
Core::App().notifications().clearIncomingFromHistory(history);
|
2020-02-23 08:48:39 +00:00
|
|
|
|
2020-02-23 07:54:15 +00:00
|
|
|
const auto needsRequest = history->readInboxTillNeedsRequest(tillId);
|
|
|
|
if (!needsRequest && !force) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: readInboxTill finish 1."));
|
2020-02-18 11:39:24 +00:00
|
|
|
return;
|
2020-02-21 07:34:24 +00:00
|
|
|
} else if (!history->trackUnreadMessages()) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: readInboxTill finish 2."));
|
2020-02-21 07:34:24 +00:00
|
|
|
return;
|
2020-02-23 07:54:15 +00:00
|
|
|
}
|
|
|
|
const auto maybeState = lookup(history);
|
|
|
|
if (maybeState && maybeState->sentReadTill >= tillId) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: readInboxTill finish 3 with %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(maybeState->sentReadTill.bare));
|
2020-02-23 07:54:15 +00:00
|
|
|
return;
|
|
|
|
} else if (maybeState && maybeState->willReadTill >= tillId) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: readInboxTill finish 4 with %1 and force %2."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(maybeState->sentReadTill.bare
|
2020-05-12 12:18:19 +00:00
|
|
|
).arg(Logs::b(force)));
|
2020-02-23 07:54:15 +00:00
|
|
|
if (force) {
|
|
|
|
sendPendingReadInbox(history);
|
2020-02-19 15:35:26 +00:00
|
|
|
}
|
2020-02-23 07:54:15 +00:00
|
|
|
return;
|
|
|
|
} else if (!needsRequest
|
|
|
|
&& (!maybeState || !maybeState->willReadTill)) {
|
|
|
|
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()) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: count didn't change so just update till %1"
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare));
|
2020-02-18 11:39:24 +00:00
|
|
|
history->setInboxReadTill(tillId);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-23 07:54:15 +00:00
|
|
|
auto &state = maybeState ? *maybeState : _states[history];
|
|
|
|
state.willReadTill = tillId;
|
2020-02-19 15:35:26 +00:00
|
|
|
if (force || !stillUnread || !*stillUnread) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: will read till %1 with still unread %2"
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare
|
2020-05-12 12:18:19 +00:00
|
|
|
).arg(stillUnread.value_or(-666)));
|
2020-02-23 07:54:15 +00:00
|
|
|
state.willReadWhen = 0;
|
2020-02-18 11:39:24 +00:00
|
|
|
sendReadRequests();
|
2020-02-19 15:35:26 +00:00
|
|
|
if (!stillUnread) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-23 07:54:15 +00:00
|
|
|
} else if (!state.willReadWhen) {
|
2021-09-30 06:04:55 +00:00
|
|
|
DEBUG_LOG(("Reading: will read till %1 with postponed"
|
|
|
|
).arg(tillId.bare));
|
2020-02-23 07:54:15 +00:00
|
|
|
state.willReadWhen = crl::now() + kReadRequestTimeout;
|
2020-02-18 11:39:24 +00:00
|
|
|
if (!_readRequestsTimer.isActive()) {
|
|
|
|
_readRequestsTimer.callOnce(kReadRequestTimeout);
|
|
|
|
}
|
2020-05-12 12:18:19 +00:00
|
|
|
} else {
|
|
|
|
DEBUG_LOG(("Reading: will read till %1 postponed already"
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare));
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: marking now with till %1 and still %2"
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare
|
2020-05-12 12:18:19 +00:00
|
|
|
).arg(*stillUnread));
|
2020-02-18 11:39:24 +00:00
|
|
|
history->setInboxReadTill(tillId);
|
|
|
|
history->setUnreadCount(*stillUnread);
|
|
|
|
history->updateChatListEntry();
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
void Histories::readInboxOnNewMessage(not_null<HistoryItem*> item) {
|
2021-11-07 08:06:00 +00:00
|
|
|
if (!item->isRegular()) {
|
2020-02-19 15:35:26 +00:00
|
|
|
readClientSideMessage(item);
|
|
|
|
} else {
|
|
|
|
readInboxTill(item->history(), item->id, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::readClientSideMessage(not_null<HistoryItem*> item) {
|
2022-10-17 16:29:48 +00:00
|
|
|
if (item->out() || !item->unread(item->history())) {
|
2020-02-19 15:35:26 +00:00
|
|
|
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);
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2020-02-21 07:58:50 +00:00
|
|
|
_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;
|
|
|
|
}
|
2020-02-24 10:40:02 +00:00
|
|
|
postponeRequestDialogEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::postponeRequestDialogEntries() {
|
2020-02-21 07:58:50 +00:00
|
|
|
if (_dialogRequestsPending.size() > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Core::App().postponeCall(crl::guard(&session(), [=] {
|
|
|
|
sendDialogRequests();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::sendDialogRequests() {
|
|
|
|
if (_dialogRequestsPending.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-13 12:12:08 +00:00
|
|
|
const auto histories = ranges::views::all(
|
2020-02-21 09:22:01 +00:00
|
|
|
_dialogRequestsPending
|
2021-03-13 12:12:08 +00:00
|
|
|
) | ranges::views::transform([](const auto &pair) {
|
2020-02-21 09:22:01 +00:00
|
|
|
return pair.first;
|
2021-03-13 12:12:08 +00:00
|
|
|
}) | ranges::views::filter([&](not_null<History*> history) {
|
2020-02-21 09:22:01 +00:00
|
|
|
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 = [=] {
|
2021-09-08 10:53:54 +00:00
|
|
|
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();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2020-02-21 07:58:50 +00:00
|
|
|
finalize();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::dialogEntryApplied(not_null<History*> history) {
|
2020-02-23 08:56:33 +00:00
|
|
|
const auto state = lookup(history);
|
2020-02-23 11:13:23 +00:00
|
|
|
if (state && state->postponedRequestEntry) {
|
2020-02-23 08:56:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-02-21 07:58:50 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 08:56:33 +00:00
|
|
|
if (state && state->sentReadTill && state->sentReadDone) {
|
|
|
|
history->setInboxReadTill(base::take(state->sentReadTill));
|
|
|
|
checkEmptyState(history);
|
2020-02-23 07:54:15 +00:00
|
|
|
}
|
2020-02-21 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-08-25 08:15:05 +00:00
|
|
|
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_long(0) // hash
|
2020-02-21 11:51:37 +00:00
|
|
|
)).done([=](const MTPmessages_Messages &result) {
|
|
|
|
_fakeChatListRequests.erase(history);
|
|
|
|
history->setFakeChatListMessageFrom(result);
|
|
|
|
finish();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2020-02-21 11:51:37 +00:00
|
|
|
_fakeChatListRequests.erase(history);
|
|
|
|
history->setFakeChatListMessageFrom(MTP_messages_messages(
|
|
|
|
MTP_vector<MTPMessage>(0),
|
|
|
|
MTP_vector<MTPChat>(0),
|
|
|
|
MTP_vector<MTPUser>(0)));
|
|
|
|
finish();
|
|
|
|
}).send();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:37:55 +00:00
|
|
|
void Histories::requestGroupAround(not_null<HistoryItem*> item) {
|
|
|
|
const auto history = item->history();
|
|
|
|
const auto id = item->id;
|
2022-10-06 15:11:26 +00:00
|
|
|
const auto key = GroupRequestKey{ history, item->topicRootId() };
|
|
|
|
const auto i = _chatListGroupRequests.find(key);
|
2021-10-04 19:37:55 +00:00
|
|
|
if (i != end(_chatListGroupRequests)) {
|
|
|
|
if (i->second.aroundId == id) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
cancelRequest(i->second.requestId);
|
|
|
|
_chatListGroupRequests.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constexpr auto kMaxAlbumCount = 10;
|
|
|
|
const auto requestId = sendRequest(history, RequestType::History, [=](
|
|
|
|
Fn<void()> finish) {
|
|
|
|
return session().api().request(MTPmessages_GetHistory(
|
|
|
|
history->peer->input,
|
|
|
|
MTP_int(id),
|
|
|
|
MTP_int(0), // offset_date
|
|
|
|
MTP_int(-kMaxAlbumCount),
|
|
|
|
MTP_int(2 * kMaxAlbumCount - 1),
|
|
|
|
MTP_int(0), // max_id
|
|
|
|
MTP_int(0), // min_id
|
|
|
|
MTP_long(0) // hash
|
|
|
|
)).done([=](const MTPmessages_Messages &result) {
|
|
|
|
_owner->processExistingMessages(
|
|
|
|
history->peer->asChannel(),
|
|
|
|
result);
|
2022-10-06 15:11:26 +00:00
|
|
|
_chatListGroupRequests.remove(key);
|
2021-10-04 19:37:55 +00:00
|
|
|
history->migrateToOrMe()->applyChatListGroup(
|
2021-12-09 07:32:54 +00:00
|
|
|
history->peer->id,
|
2021-10-04 19:37:55 +00:00
|
|
|
result);
|
|
|
|
finish();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2022-10-06 15:11:26 +00:00
|
|
|
_chatListGroupRequests.remove(key);
|
2021-10-04 19:37:55 +00:00
|
|
|
finish();
|
|
|
|
}).send();
|
|
|
|
});
|
|
|
|
_chatListGroupRequests.emplace(
|
2022-10-06 15:11:26 +00:00
|
|
|
key,
|
2021-10-04 19:37:55 +00:00
|
|
|
ChatListGroupRequest{ .aroundId = id, .requestId = requestId });
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
void Histories::sendPendingReadInbox(not_null<History*> history) {
|
|
|
|
if (const auto state = lookup(history)) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: send pending now with till %1 and when %2"
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(state->willReadTill.bare
|
2020-05-12 12:18:19 +00:00
|
|
|
).arg(state->willReadWhen));
|
2020-02-23 07:54:15 +00:00
|
|
|
if (state->willReadTill && state->willReadWhen) {
|
|
|
|
state->willReadWhen = 0;
|
2020-02-18 11:39:24 +00:00
|
|
|
sendReadRequests();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::sendReadRequests() {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: send requests with count %1.").arg(_states.size()));
|
2020-02-18 11:39:24 +00:00
|
|
|
if (_states.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto now = crl::now();
|
|
|
|
auto next = std::optional<crl::time>();
|
|
|
|
for (auto &[history, state] : _states) {
|
2020-02-23 07:54:15 +00:00
|
|
|
if (!state.willReadTill) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: skipping zero till."));
|
2020-02-20 09:37:00 +00:00
|
|
|
continue;
|
2020-02-23 07:54:15 +00:00
|
|
|
} else if (state.willReadWhen <= now) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: sending with till %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(state.willReadTill.bare));
|
2020-02-18 11:39:24 +00:00
|
|
|
sendReadRequest(history, state);
|
2020-02-23 07:54:15 +00:00
|
|
|
} else if (!next || *next > state.willReadWhen) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: scheduling for later send."));
|
2020-02-23 07:54:15 +00:00
|
|
|
next = state.willReadWhen;
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2020-02-23 07:54:15 +00:00
|
|
|
Expects(state.willReadTill > state.sentReadTill);
|
|
|
|
|
|
|
|
const auto tillId = state.sentReadTill = base::take(state.willReadTill);
|
|
|
|
state.willReadWhen = 0;
|
|
|
|
state.sentReadDone = false;
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: sending request now with till %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare));
|
2020-02-21 10:29:48 +00:00
|
|
|
sendRequest(history, RequestType::ReadInbox, [=](Fn<void()> finish) {
|
2020-05-12 12:18:19 +00:00
|
|
|
DEBUG_LOG(("Reading: sending request invoked with till %1."
|
2021-09-30 06:04:55 +00:00
|
|
|
).arg(tillId.bare));
|
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
|
|
|
|
2020-02-23 07:54:15 +00:00
|
|
|
if (state->sentReadTill == tillId) {
|
|
|
|
state->sentReadDone = true;
|
|
|
|
if (history->unreadCountRefreshNeeded(tillId)) {
|
|
|
|
requestDialogEntry(history);
|
2020-02-19 12:40:01 +00:00
|
|
|
} else {
|
2020-02-23 07:54:15 +00:00
|
|
|
state->sentReadTill = 0;
|
2020-02-19 12:40:01 +00:00
|
|
|
}
|
2020-02-24 08:57:24 +00:00
|
|
|
} else {
|
|
|
|
Assert(!state->sentReadTill || state->sentReadTill > tillId);
|
2020-02-18 11:39:24 +00:00
|
|
|
}
|
2020-02-23 07:54:15 +00:00
|
|
|
sendReadRequests();
|
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)
|
2021-11-26 20:54:46 +00:00
|
|
|
)).done(finished).fail(finished).send();
|
2020-02-18 11:39:24 +00:00
|
|
|
} 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();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2020-02-18 11:39:24 +00:00
|
|
|
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-23 07:54:15 +00:00
|
|
|
&& (state.willReadTill == 0)
|
|
|
|
&& (state.sentReadTill == 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 {
|
2020-05-18 19:33:14 +00:00
|
|
|
return ranges::any_of(state.sent, [](const auto &pair) {
|
2020-02-21 09:22:01 +00:00
|
|
|
return pair.second.type != RequestType::History;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
if (const auto channel = history->peer->asChannel()) {
|
|
|
|
return session().api().request(MTPchannels_DeleteMessages(
|
|
|
|
channel->inputChannel,
|
|
|
|
MTP_vector<MTPint>(ids)
|
2021-11-26 20:46:53 +00:00
|
|
|
)).done(done).fail(finish).send();
|
2020-02-21 10:29:48 +00:00
|
|
|
} else {
|
|
|
|
using Flag = MTPmessages_DeleteMessages::Flag;
|
|
|
|
return session().api().request(MTPmessages_DeleteMessages(
|
|
|
|
MTP_flags(revoke ? Flag::f_revoke : Flag(0)),
|
|
|
|
MTP_vector<MTPint>(ids)
|
2021-11-26 20:46:53 +00:00
|
|
|
)).done(done).fail(finish).send();
|
2020-02-21 10:29:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-01-21 13:31:27 +00:00
|
|
|
const auto chat = peer->asChat();
|
|
|
|
const auto channel = peer->asChannel();
|
2021-02-15 10:58:01 +00:00
|
|
|
if (!justClear && revoke && channel && channel->canDelete()) {
|
2021-01-21 13:31:27 +00:00
|
|
|
return session().api().request(MTPchannels_DeleteChannel(
|
|
|
|
channel->inputChannel
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
session().api().applyUpdates(result);
|
2021-03-12 12:48:00 +00:00
|
|
|
//}).fail([=](const MTP::Error &error) {
|
2022-11-26 21:20:17 +00:00
|
|
|
// if (error.type() == u"CHANNEL_TOO_LARGE"_q) {
|
2021-10-18 22:28:08 +00:00
|
|
|
// Ui::show(Box<Ui::InformBox>(tr::lng_cant_delete_channel(tr::now)));
|
2021-01-21 13:31:27 +00:00
|
|
|
// }
|
|
|
|
}).send();
|
|
|
|
} else if (channel) {
|
2022-06-20 07:18:38 +00:00
|
|
|
using Flag = MTPchannels_DeleteHistory::Flag;
|
2020-02-21 10:29:48 +00:00
|
|
|
return session().api().request(MTPchannels_DeleteHistory(
|
2022-06-20 07:18:38 +00:00
|
|
|
MTP_flags(revoke ? Flag::f_for_everyone : Flag(0)),
|
2020-02-21 10:29:48 +00:00
|
|
|
channel->inputChannel,
|
|
|
|
MTP_int(deleteTillId)
|
2021-11-26 20:54:46 +00:00
|
|
|
)).done(finish).fail(finish).send();
|
2021-01-21 13:31:27 +00:00
|
|
|
} else if (revoke && chat && chat->amCreator()) {
|
2021-01-21 04:20:29 +00:00
|
|
|
return session().api().request(MTPmessages_DeleteChat(
|
2021-01-21 13:31:27 +00:00
|
|
|
chat->inputChat
|
2021-11-26 20:54:46 +00:00
|
|
|
)).done(finish).fail([=](const MTP::Error &error) {
|
2021-01-21 13:31:27 +00:00
|
|
|
if (error.type() == "PEER_ID_INVALID") {
|
|
|
|
// Try to join and delete,
|
|
|
|
// while delete fails for non-joined.
|
|
|
|
session().api().request(MTPmessages_AddChatUser(
|
|
|
|
chat->inputChat,
|
|
|
|
MTP_inputUserSelf(),
|
|
|
|
MTP_int(0)
|
|
|
|
)).done([=](const MTPUpdates &updates) {
|
|
|
|
session().api().applyUpdates(updates);
|
|
|
|
deleteAllMessages(
|
|
|
|
history,
|
|
|
|
deleteTillId,
|
|
|
|
justClear,
|
|
|
|
revoke);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}).send();
|
2020-02-21 10:29:48 +00:00
|
|
|
} else {
|
|
|
|
using Flag = MTPmessages_DeleteHistory::Flag;
|
|
|
|
const auto flags = Flag(0)
|
|
|
|
| (justClear ? Flag::f_just_clear : Flag(0))
|
2021-01-21 04:20:29 +00:00
|
|
|
| (revoke ? Flag::f_revoke : Flag(0));
|
2020-02-21 10:29:48 +00:00
|
|
|
return session().api().request(MTPmessages_DeleteHistory(
|
|
|
|
MTP_flags(flags),
|
|
|
|
peer->input,
|
2021-10-22 18:14:44 +00:00
|
|
|
MTP_int(0),
|
|
|
|
MTPint(), // min_date
|
|
|
|
MTPint() // max_date
|
2020-02-21 10:29:48 +00:00
|
|
|
)).done([=](const MTPmessages_AffectedHistory &result) {
|
|
|
|
const auto offset = session().api().applyAffectedHistory(
|
|
|
|
peer,
|
|
|
|
result);
|
|
|
|
if (offset > 0) {
|
|
|
|
deleteAllMessages(
|
|
|
|
history,
|
|
|
|
deleteTillId,
|
|
|
|
justClear,
|
|
|
|
revoke);
|
|
|
|
}
|
|
|
|
finish();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail(finish).send();
|
2020-02-21 10:29:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-16 07:26:35 +00:00
|
|
|
void Histories::deleteMessagesByDates(
|
|
|
|
not_null<History*> history,
|
|
|
|
QDate firstDayToDelete,
|
|
|
|
QDate lastDayToDelete,
|
|
|
|
bool revoke) {
|
|
|
|
const auto firstSecondToDelete = base::unixtime::serialize(
|
|
|
|
{ firstDayToDelete, QTime(0, 0) }
|
|
|
|
);
|
|
|
|
const auto lastSecondToDelete = base::unixtime::serialize(
|
|
|
|
{ lastDayToDelete, QTime(23, 59, 59) }
|
|
|
|
);
|
|
|
|
deleteMessagesByDates(
|
|
|
|
history,
|
|
|
|
firstSecondToDelete - 1,
|
|
|
|
lastSecondToDelete + 1,
|
|
|
|
revoke);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Histories::deleteMessagesByDates(
|
|
|
|
not_null<History*> history,
|
|
|
|
TimeId minDate,
|
|
|
|
TimeId maxDate,
|
|
|
|
bool revoke) {
|
|
|
|
sendRequest(history, RequestType::Delete, [=](Fn<void()> finish) {
|
|
|
|
const auto peer = history->peer;
|
|
|
|
using Flag = MTPmessages_DeleteHistory::Flag;
|
|
|
|
const auto flags = Flag::f_just_clear
|
|
|
|
| Flag::f_min_date
|
|
|
|
| Flag::f_max_date
|
|
|
|
| (revoke ? Flag::f_revoke : Flag(0));
|
|
|
|
return session().api().request(MTPmessages_DeleteHistory(
|
|
|
|
MTP_flags(flags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(0),
|
|
|
|
MTP_int(minDate),
|
|
|
|
MTP_int(maxDate)
|
|
|
|
)).done([=](const MTPmessages_AffectedHistory &result) {
|
|
|
|
const auto offset = session().api().applyAffectedHistory(
|
|
|
|
peer,
|
|
|
|
result);
|
|
|
|
if (offset > 0) {
|
|
|
|
deleteMessagesByDates(history, minDate, maxDate, revoke);
|
|
|
|
}
|
|
|
|
finish();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail(finish).send();
|
2021-11-16 07:26:35 +00:00
|
|
|
});
|
|
|
|
history->destroyMessagesByDates(minDate, maxDate);
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:26:53 +00:00
|
|
|
void Histories::deleteMessages(const MessageIdsList &ids, bool revoke) {
|
|
|
|
auto remove = std::vector<not_null<HistoryItem*>>();
|
|
|
|
remove.reserve(ids.size());
|
|
|
|
base::flat_map<not_null<History*>, QVector<MTPint>> idsByPeer;
|
|
|
|
base::flat_map<not_null<PeerData*>, QVector<MTPint>> scheduledIdsByPeer;
|
2021-09-08 10:53:54 +00:00
|
|
|
for (const auto &itemId : ids) {
|
2020-09-17 13:26:53 +00:00
|
|
|
if (const auto item = _owner->message(itemId)) {
|
|
|
|
const auto history = item->history();
|
|
|
|
if (item->isScheduled()) {
|
|
|
|
const auto wasOnServer = !item->isSending()
|
|
|
|
&& !item->hasFailed();
|
|
|
|
if (wasOnServer) {
|
|
|
|
scheduledIdsByPeer[history->peer].push_back(MTP_int(
|
|
|
|
_owner->scheduledMessages().lookupId(item)));
|
|
|
|
} else {
|
|
|
|
_owner->scheduledMessages().removeSending(item);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
remove.push_back(item);
|
2021-11-07 08:06:00 +00:00
|
|
|
if (item->isRegular()) {
|
2020-09-17 13:26:53 +00:00
|
|
|
idsByPeer[history].push_back(MTP_int(itemId.msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[history, ids] : idsByPeer) {
|
|
|
|
history->owner().histories().deleteMessages(history, ids, revoke);
|
|
|
|
}
|
|
|
|
for (const auto &[peer, ids] : scheduledIdsByPeer) {
|
|
|
|
peer->session().api().request(MTPmessages_DeleteScheduledMessages(
|
|
|
|
peer->input,
|
|
|
|
MTP_vector<MTPint>(ids)
|
|
|
|
)).done([peer = peer](const MTPUpdates &result) {
|
|
|
|
peer->session().api().applyUpdates(result);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto item : remove) {
|
|
|
|
const auto history = item->history();
|
|
|
|
const auto wasLast = (history->lastMessage() == item);
|
|
|
|
const auto wasInChats = (history->chatListMessage() == item);
|
|
|
|
item->destroy();
|
|
|
|
|
|
|
|
if (wasLast || wasInChats) {
|
|
|
|
history->requestChatListMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:34:45 +00:00
|
|
|
void Histories::sendCreateTopicRequest(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId rootId) {
|
|
|
|
Expects(history->peer->isChannel());
|
|
|
|
|
2022-11-01 09:00:17 +00:00
|
|
|
const auto forum = history->asForum();
|
2022-10-04 15:34:45 +00:00
|
|
|
Assert(forum != nullptr);
|
|
|
|
const auto topic = forum->topicFor(rootId);
|
|
|
|
Assert(topic != nullptr);
|
|
|
|
const auto randomId = base::RandomValue<uint64>();
|
|
|
|
session().data().registerMessageRandomId(
|
|
|
|
randomId,
|
|
|
|
{ history->peer->id, rootId });
|
|
|
|
const auto api = &session().api();
|
|
|
|
using Flag = MTPchannels_CreateForumTopic::Flag;
|
|
|
|
api->request(MTPchannels_CreateForumTopic(
|
2022-10-05 07:42:44 +00:00
|
|
|
MTP_flags(Flag::f_icon_color
|
|
|
|
| (topic->iconId() ? Flag::f_icon_emoji_id : Flag(0))),
|
2022-10-04 15:34:45 +00:00
|
|
|
history->peer->asChannel()->inputChannel,
|
|
|
|
MTP_string(topic->title()),
|
2022-10-05 07:42:44 +00:00
|
|
|
MTP_int(topic->colorId()),
|
2022-10-04 15:34:45 +00:00
|
|
|
MTP_long(topic->iconId()),
|
|
|
|
MTP_long(randomId),
|
|
|
|
MTPInputPeer() // send_as
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
api->applyUpdates(result, randomId);
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
api->sendMessageFail(error, history->peer, randomId);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Histories::isCreatingTopic(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId rootId) const {
|
2022-11-01 09:00:17 +00:00
|
|
|
const auto forum = history->asForum();
|
2022-10-04 15:34:45 +00:00
|
|
|
return forum && forum->creating(rootId);
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:52:35 +00:00
|
|
|
int Histories::sendPreparedMessage(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId replyTo,
|
2022-10-17 16:29:48 +00:00
|
|
|
MsgId topicRootId,
|
2022-09-27 16:52:35 +00:00
|
|
|
uint64 randomId,
|
2022-10-17 16:29:48 +00:00
|
|
|
Fn<PreparedMessage(MsgId replyTo, MsgId topicRootId)> message,
|
2022-09-27 16:52:35 +00:00
|
|
|
Fn<void(const MTPUpdates&, const MTP::Response&)> done,
|
|
|
|
Fn<void(const MTP::Error&, const MTP::Response&)> fail) {
|
2022-10-17 16:29:48 +00:00
|
|
|
if (isCreatingTopic(history, topicRootId)) {
|
2022-10-04 15:34:45 +00:00
|
|
|
const auto id = ++_requestAutoincrement;
|
2022-10-17 16:29:48 +00:00
|
|
|
const auto creatingId = FullMsgId(history->peer->id, topicRootId);
|
2022-10-04 15:34:45 +00:00
|
|
|
auto i = _creatingTopics.find(creatingId);
|
|
|
|
if (i == end(_creatingTopics)) {
|
2022-10-17 16:29:48 +00:00
|
|
|
sendCreateTopicRequest(history, topicRootId);
|
2022-10-04 15:34:45 +00:00
|
|
|
i = _creatingTopics.emplace(creatingId).first;
|
|
|
|
}
|
|
|
|
i->second.push_back({
|
|
|
|
.randomId = randomId,
|
2022-10-17 16:29:48 +00:00
|
|
|
.replyTo = replyTo,
|
2022-10-04 15:34:45 +00:00
|
|
|
.message = std::move(message),
|
|
|
|
.done = std::move(done),
|
|
|
|
.fail = std::move(fail),
|
|
|
|
.requestId = id,
|
|
|
|
});
|
|
|
|
_creatingTopicRequests.emplace(id);
|
|
|
|
return id;
|
|
|
|
}
|
2022-10-17 16:29:48 +00:00
|
|
|
const auto realReply = convertTopicReplyTo(history, replyTo);
|
|
|
|
const auto realRoot = convertTopicReplyTo(history, topicRootId);
|
|
|
|
return v::match(message(realReply, realRoot), [&](const auto &request) {
|
2022-09-27 16:52:35 +00:00
|
|
|
const auto type = RequestType::Send;
|
|
|
|
return sendRequest(history, type, [=](Fn<void()> finish) {
|
|
|
|
const auto session = &_owner->session();
|
|
|
|
const auto api = &session->api();
|
|
|
|
history->sendRequestId = api->request(
|
|
|
|
base::duplicate(request)
|
|
|
|
).done([=](
|
|
|
|
const MTPUpdates &result,
|
|
|
|
const MTP::Response &response) {
|
|
|
|
api->applyUpdates(result, randomId);
|
|
|
|
done(result, response);
|
|
|
|
finish();
|
|
|
|
}).fail([=](
|
|
|
|
const MTP::Error &error,
|
|
|
|
const MTP::Response &response) {
|
|
|
|
fail(error, response);
|
|
|
|
finish();
|
|
|
|
}).afterRequest(
|
|
|
|
history->sendRequestId
|
|
|
|
).send();
|
|
|
|
return history->sendRequestId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
void Histories::checkTopicCreated(FullMsgId rootId, MsgId realRoot) {
|
2022-10-04 15:34:45 +00:00
|
|
|
const auto i = _creatingTopics.find(rootId);
|
|
|
|
if (i != end(_creatingTopics)) {
|
|
|
|
auto scheduled = base::take(i->second);
|
|
|
|
_creatingTopics.erase(i);
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
_createdTopicIds.emplace(rootId, realRoot);
|
2022-10-04 15:34:45 +00:00
|
|
|
|
2022-10-21 09:42:21 +00:00
|
|
|
const auto history = _owner->history(rootId.peer);
|
2022-11-01 09:00:17 +00:00
|
|
|
if (const auto forum = history->asForum()) {
|
2022-10-17 16:29:48 +00:00
|
|
|
forum->created(rootId.msg, realRoot);
|
2022-10-04 15:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &entry : scheduled) {
|
|
|
|
_creatingTopicRequests.erase(entry.requestId);
|
2022-10-17 16:29:48 +00:00
|
|
|
sendPreparedMessage(
|
|
|
|
history,
|
|
|
|
entry.replyTo,
|
|
|
|
realRoot,
|
|
|
|
entry.randomId,
|
|
|
|
std::move(entry.message),
|
|
|
|
std::move(entry.done),
|
|
|
|
std::move(entry.fail));
|
2022-10-04 15:34:45 +00:00
|
|
|
}
|
|
|
|
for (const auto &item : history->clientSideMessages()) {
|
|
|
|
const auto replace = [&](MsgId nowId) {
|
2022-10-17 16:29:48 +00:00
|
|
|
return (nowId == rootId.msg) ? realRoot : nowId;
|
2022-10-04 15:34:45 +00:00
|
|
|
};
|
2022-10-17 16:29:48 +00:00
|
|
|
if (item->topicRootId() == rootId.msg) {
|
2022-10-04 15:34:45 +00:00
|
|
|
item->setReplyFields(
|
|
|
|
replace(item->replyToId()),
|
2022-10-17 16:29:48 +00:00
|
|
|
realRoot,
|
2022-10-04 15:34:45 +00:00
|
|
|
true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MsgId Histories::convertTopicReplyTo(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId replyTo) const {
|
|
|
|
if (!replyTo) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto i = _createdTopicIds.find({ history->peer->id, replyTo });
|
|
|
|
return (i != end(_createdTopicIds)) ? i->second : replyTo;
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:22:01 +00:00
|
|
|
void Histories::checkPostponed(not_null<History*> history, int id) {
|
2020-02-21 14:02:13 +00:00
|
|
|
if (const auto state = lookup(history)) {
|
|
|
|
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) {
|
2020-08-28 10:01:55 +00:00
|
|
|
if (!id) {
|
|
|
|
return;
|
2022-10-04 15:34:45 +00:00
|
|
|
} else if (_creatingTopicRequests.contains(id)) {
|
|
|
|
cancelDelayedByTopicRequest(id);
|
|
|
|
return;
|
2020-08-28 10:01:55 +00:00
|
|
|
}
|
2020-02-21 12:57:06 +00:00
|
|
|
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
|
|
|
|
2022-10-04 15:34:45 +00:00
|
|
|
void Histories::cancelDelayedByTopicRequest(int id) {
|
|
|
|
for (auto &[rootId, messages] : _creatingTopics) {
|
|
|
|
messages.erase(
|
|
|
|
ranges::remove(messages, id, &DelayedByTopicMessage::requestId),
|
|
|
|
end(messages));
|
|
|
|
}
|
|
|
|
_creatingTopicRequests.remove(id);
|
|
|
|
}
|
|
|
|
|
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)) {
|
2020-02-21 14:02:43 +00:00
|
|
|
const auto requestId = postponed.generator([=, id=id] {
|
2020-02-21 09:22:01 +00:00
|
|
|
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;
|
2020-02-24 10:40:02 +00:00
|
|
|
postponeRequestDialogEntries();
|
2020-02-21 09:22:01 +00:00
|
|
|
}
|
|
|
|
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
|