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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/timer.h"
|
|
|
|
|
|
|
|
class History;
|
|
|
|
class HistoryItem;
|
|
|
|
|
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
|
|
|
|
2022-09-27 16:52:35 +00:00
|
|
|
namespace MTP {
|
|
|
|
class Error;
|
|
|
|
struct Response;
|
|
|
|
} // namespace MTP
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
namespace Data {
|
|
|
|
|
|
|
|
class Session;
|
2020-02-21 07:58:50 +00:00
|
|
|
class Folder;
|
2020-02-18 11:39:24 +00:00
|
|
|
|
2023-05-25 09:32:13 +00:00
|
|
|
[[nodiscard]] MTPInputReplyTo ReplyToForMTP(
|
|
|
|
not_null<Session*> owner,
|
|
|
|
FullReplyTo replyTo);
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
class Histories final {
|
|
|
|
public:
|
2020-02-21 09:22:01 +00:00
|
|
|
enum class RequestType : uchar {
|
|
|
|
None,
|
|
|
|
History,
|
|
|
|
ReadInbox,
|
|
|
|
Delete,
|
|
|
|
Send,
|
|
|
|
};
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
explicit Histories(not_null<Session*> owner);
|
|
|
|
|
|
|
|
[[nodiscard]] Session &owner() const;
|
|
|
|
[[nodiscard]] Main::Session &session() const;
|
|
|
|
|
|
|
|
[[nodiscard]] History *find(PeerId peerId);
|
|
|
|
[[nodiscard]] not_null<History*> findOrCreate(PeerId peerId);
|
|
|
|
|
2020-03-25 18:25:50 +00:00
|
|
|
void applyPeerDialogs(const MTPmessages_PeerDialogs &dialogs);
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
void unloadAll();
|
|
|
|
void clearAll();
|
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
void readInbox(not_null<History*> history);
|
|
|
|
void readInboxTill(not_null<HistoryItem*> item);
|
2020-02-18 12:15:43 +00:00
|
|
|
void readInboxTill(not_null<History*> history, MsgId tillId);
|
2020-02-19 15:35:26 +00:00
|
|
|
void readInboxOnNewMessage(not_null<HistoryItem*> item);
|
|
|
|
void readClientSideMessage(not_null<HistoryItem*> item);
|
2020-02-18 11:39:24 +00:00
|
|
|
void sendPendingReadInbox(not_null<History*> history);
|
|
|
|
|
2020-02-21 07:58:50 +00:00
|
|
|
void requestDialogEntry(not_null<Data::Folder*> folder);
|
|
|
|
void requestDialogEntry(
|
|
|
|
not_null<History*> history,
|
|
|
|
Fn<void()> callback = nullptr);
|
|
|
|
void dialogEntryApplied(not_null<History*> history);
|
|
|
|
void changeDialogUnreadMark(not_null<History*> history, bool unread);
|
2020-02-21 11:51:37 +00:00
|
|
|
void requestFakeChatListMessage(not_null<History*> history);
|
2020-02-21 07:58:50 +00:00
|
|
|
|
2021-10-04 19:37:55 +00:00
|
|
|
void requestGroupAround(not_null<HistoryItem*> item);
|
|
|
|
|
2020-02-21 10:29:48 +00:00
|
|
|
void deleteMessages(
|
|
|
|
not_null<History*> history,
|
|
|
|
const QVector<MTPint> &ids,
|
|
|
|
bool revoke);
|
|
|
|
void deleteAllMessages(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId deleteTillId,
|
|
|
|
bool justClear,
|
|
|
|
bool revoke);
|
|
|
|
|
2021-11-16 07:26:35 +00:00
|
|
|
void deleteMessagesByDates(
|
|
|
|
not_null<History*> history,
|
|
|
|
QDate firstDayToDelete,
|
|
|
|
QDate lastDayToDelete,
|
|
|
|
bool revoke);
|
|
|
|
void deleteMessagesByDates(
|
|
|
|
not_null<History*> history,
|
|
|
|
TimeId minDate,
|
|
|
|
TimeId maxDate,
|
|
|
|
bool revoke);
|
|
|
|
|
2020-09-17 13:26:53 +00:00
|
|
|
void deleteMessages(const MessageIdsList &ids, bool revoke);
|
|
|
|
|
2020-02-21 09:22:01 +00:00
|
|
|
int sendRequest(
|
|
|
|
not_null<History*> history,
|
|
|
|
RequestType type,
|
2020-02-21 10:29:48 +00:00
|
|
|
Fn<mtpRequestId(Fn<void()> finish)> generator);
|
2020-02-21 12:57:06 +00:00
|
|
|
void cancelRequest(int id);
|
2020-02-21 09:22:01 +00:00
|
|
|
|
2022-09-27 16:52:35 +00:00
|
|
|
using PreparedMessage = std::variant<
|
|
|
|
MTPmessages_SendMessage,
|
|
|
|
MTPmessages_SendMedia,
|
|
|
|
MTPmessages_SendInlineBotResult,
|
|
|
|
MTPmessages_SendMultiMedia>;
|
|
|
|
int sendPreparedMessage(
|
|
|
|
not_null<History*> history,
|
2023-05-25 09:32:13 +00:00
|
|
|
FullReplyTo replyTo,
|
2022-09-27 16:52:35 +00:00
|
|
|
uint64 randomId,
|
2023-05-25 09:32:13 +00:00
|
|
|
Fn<PreparedMessage(not_null<Session*>, FullReplyTo)> message,
|
2022-10-04 15:34:45 +00:00
|
|
|
Fn<void(const MTPUpdates&, const MTP::Response&)> done,
|
2022-09-27 16:52:35 +00:00
|
|
|
Fn<void(const MTP::Error&, const MTP::Response&)> fail);
|
|
|
|
|
2022-10-04 15:34:45 +00:00
|
|
|
struct ReplyToPlaceholder {
|
|
|
|
};
|
|
|
|
template <typename RequestType, typename ...Args>
|
2023-05-25 09:32:13 +00:00
|
|
|
static auto PrepareMessage(const Args &...args)
|
|
|
|
-> Fn<Histories::PreparedMessage(not_null<Session*>, FullReplyTo)> {
|
|
|
|
return [=](not_null<Session*> owner, FullReplyTo replyTo)
|
|
|
|
-> RequestType {
|
|
|
|
return { ReplaceReplyIds(owner, args, replyTo)... };
|
2022-10-04 15:34:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:29:48 +00:00
|
|
|
void checkTopicCreated(FullMsgId rootId, MsgId realRoot);
|
2023-05-25 09:32:13 +00:00
|
|
|
[[nodiscard]] MsgId convertTopicReplyToId(
|
2022-10-04 15:34:45 +00:00
|
|
|
not_null<History*> history,
|
2023-05-25 09:32:13 +00:00
|
|
|
MsgId replyToId) const;
|
2022-10-04 15:34:45 +00:00
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
private:
|
2020-02-21 09:22:01 +00:00
|
|
|
struct PostponedHistoryRequest {
|
2020-02-21 10:29:48 +00:00
|
|
|
Fn<mtpRequestId(Fn<void()> finish)> generator;
|
2020-02-18 11:39:24 +00:00
|
|
|
};
|
|
|
|
struct SentRequest {
|
2020-02-21 10:29:48 +00:00
|
|
|
Fn<mtpRequestId(Fn<void()> finish)> generator;
|
2020-02-18 11:39:24 +00:00
|
|
|
mtpRequestId id = 0;
|
|
|
|
RequestType type = RequestType::None;
|
|
|
|
};
|
|
|
|
struct State {
|
2020-02-21 09:22:01 +00:00
|
|
|
base::flat_map<int, PostponedHistoryRequest> postponed;
|
2020-02-18 11:39:24 +00:00
|
|
|
base::flat_map<int, SentRequest> sent;
|
2020-02-23 07:54:15 +00:00
|
|
|
MsgId willReadTill = 0;
|
|
|
|
MsgId sentReadTill = 0;
|
|
|
|
crl::time willReadWhen = 0;
|
|
|
|
bool sentReadDone = false;
|
2020-02-21 09:22:01 +00:00
|
|
|
bool postponedRequestEntry = false;
|
2020-02-18 11:39:24 +00:00
|
|
|
};
|
2021-10-04 19:37:55 +00:00
|
|
|
struct ChatListGroupRequest {
|
|
|
|
MsgId aroundId = 0;
|
|
|
|
mtpRequestId requestId = 0;
|
|
|
|
};
|
2022-10-04 15:34:45 +00:00
|
|
|
struct DelayedByTopicMessage {
|
|
|
|
uint64 randomId = 0;
|
2022-10-17 16:29:48 +00:00
|
|
|
MsgId replyTo = 0;
|
2023-05-25 09:32:13 +00:00
|
|
|
Fn<PreparedMessage(not_null<Session*>, FullReplyTo)> message;
|
2022-10-04 15:34:45 +00:00
|
|
|
Fn<void(const MTPUpdates&, const MTP::Response&)> done;
|
|
|
|
Fn<void(const MTP::Error&, const MTP::Response&)> fail;
|
|
|
|
int requestId = 0;
|
|
|
|
};
|
2022-10-06 15:11:26 +00:00
|
|
|
struct GroupRequestKey {
|
|
|
|
not_null<History*> history;
|
|
|
|
MsgId rootId = 0;
|
|
|
|
|
2022-10-07 13:56:07 +00:00
|
|
|
friend inline auto operator<=>(
|
2022-10-06 15:11:26 +00:00
|
|
|
GroupRequestKey,
|
|
|
|
GroupRequestKey) = default;
|
|
|
|
};
|
2022-10-04 15:34:45 +00:00
|
|
|
|
|
|
|
template <typename Arg>
|
2023-05-25 09:32:13 +00:00
|
|
|
static auto ReplaceReplyIds(
|
|
|
|
not_null<Session*> owner,
|
|
|
|
Arg arg,
|
|
|
|
FullReplyTo replyTo) {
|
2022-10-07 13:56:07 +00:00
|
|
|
if constexpr (std::is_same_v<Arg, ReplyToPlaceholder>) {
|
2023-05-25 09:32:13 +00:00
|
|
|
return ReplyToForMTP(owner, replyTo);
|
2022-10-07 13:56:07 +00:00
|
|
|
} else {
|
|
|
|
return arg;
|
|
|
|
}
|
2022-10-04 15:34:45 +00:00
|
|
|
}
|
2020-02-18 11:39:24 +00:00
|
|
|
|
2020-02-19 15:35:26 +00:00
|
|
|
void readInboxTill(not_null<History*> history, MsgId tillId, bool force);
|
2020-02-18 11:39:24 +00:00
|
|
|
void sendReadRequests();
|
|
|
|
void sendReadRequest(not_null<History*> history, State &state);
|
|
|
|
[[nodiscard]] State *lookup(not_null<History*> history);
|
|
|
|
void checkEmptyState(not_null<History*> history);
|
2020-02-21 09:22:01 +00:00
|
|
|
void checkPostponed(not_null<History*> history, int id);
|
|
|
|
void finishSentRequest(
|
2020-02-18 11:39:24 +00:00
|
|
|
not_null<History*> history,
|
2020-02-21 09:22:01 +00:00
|
|
|
not_null<State*> state,
|
|
|
|
int id);
|
|
|
|
[[nodiscard]] bool postponeHistoryRequest(const State &state) const;
|
|
|
|
[[nodiscard]] bool postponeEntryRequest(const State &state) const;
|
2020-02-24 10:40:02 +00:00
|
|
|
void postponeRequestDialogEntries();
|
2020-02-18 11:39:24 +00:00
|
|
|
|
2020-02-21 07:58:50 +00:00
|
|
|
void sendDialogRequests();
|
|
|
|
|
2022-10-04 15:34:45 +00:00
|
|
|
[[nodiscard]] bool isCreatingTopic(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId rootId) const;
|
|
|
|
void sendCreateTopicRequest(not_null<History*> history, MsgId rootId);
|
|
|
|
void cancelDelayedByTopicRequest(int id);
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
const not_null<Session*> _owner;
|
|
|
|
|
|
|
|
std::unordered_map<PeerId, std::unique_ptr<History>> _map;
|
|
|
|
base::flat_map<not_null<History*>, State> _states;
|
2020-02-21 12:57:06 +00:00
|
|
|
base::flat_map<int, not_null<History*>> _historyByRequest;
|
|
|
|
int _requestAutoincrement = 0;
|
2020-02-18 11:39:24 +00:00
|
|
|
base::Timer _readRequestsTimer;
|
|
|
|
|
2020-02-21 07:58:50 +00:00
|
|
|
base::flat_set<not_null<Data::Folder*>> _dialogFolderRequests;
|
|
|
|
base::flat_map<
|
|
|
|
not_null<History*>,
|
|
|
|
std::vector<Fn<void()>>> _dialogRequests;
|
|
|
|
base::flat_map<
|
|
|
|
not_null<History*>,
|
|
|
|
std::vector<Fn<void()>>> _dialogRequestsPending;
|
|
|
|
|
2020-02-21 11:51:37 +00:00
|
|
|
base::flat_set<not_null<History*>> _fakeChatListRequests;
|
|
|
|
|
2021-10-04 19:37:55 +00:00
|
|
|
base::flat_map<
|
2022-10-06 15:11:26 +00:00
|
|
|
GroupRequestKey,
|
2021-10-04 19:37:55 +00:00
|
|
|
ChatListGroupRequest> _chatListGroupRequests;
|
|
|
|
|
2022-10-04 15:34:45 +00:00
|
|
|
base::flat_map<
|
|
|
|
FullMsgId,
|
|
|
|
std::vector<DelayedByTopicMessage>> _creatingTopics;
|
|
|
|
base::flat_map<FullMsgId, MsgId> _createdTopicIds;
|
|
|
|
base::flat_set<mtpRequestId> _creatingTopicRequests;
|
|
|
|
|
2020-02-18 11:39:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Data
|