2020-02-23 14:02:47 +01:00
|
|
|
#ifndef _ACCOUNT_DATA_H
|
|
|
|
#define _ACCOUNT_DATA_H
|
|
|
|
|
|
|
|
#include <td/telegram/td_api.h>
|
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2020-05-31 15:55:28 +02:00
|
|
|
#include <set>
|
2020-05-22 13:05:11 +02:00
|
|
|
#include <purple.h>
|
2020-02-23 14:02:47 +01:00
|
|
|
|
2020-05-04 17:32:33 +02:00
|
|
|
bool isPhoneNumber(const char *s);
|
|
|
|
const char *getCanonicalPhoneNumber(const char *s);
|
2020-05-13 11:21:19 +02:00
|
|
|
int32_t stringToUserId(const char *s);
|
2020-05-20 15:13:06 +02:00
|
|
|
bool isPrivateChat(const td::td_api::chat &chat);
|
|
|
|
int32_t getUserIdByPrivateChat(const td::td_api::chat &chat); // return 0 if not private chat
|
2020-05-29 21:29:07 +02:00
|
|
|
bool isChatInContactList(const td::td_api::chat &chat, const td::td_api::user *privateChatUser);
|
2020-05-09 14:07:21 +02:00
|
|
|
int32_t getBasicGroupId(const td::td_api::chat &chat); // returns 0 if not chatTypeBasicGroup
|
|
|
|
int32_t getSupergroupId(const td::td_api::chat &chat); // returns 0 if not chatTypeSupergroup
|
2020-05-10 14:24:15 +02:00
|
|
|
bool isGroupMember(const td::td_api::object_ptr<td::td_api::ChatMemberStatus> &status);
|
2020-05-04 17:32:33 +02:00
|
|
|
|
2020-02-23 14:02:47 +01:00
|
|
|
enum {
|
|
|
|
CHAT_HISTORY_REQUEST_LIMIT = 50,
|
|
|
|
CHAT_HISTORY_RETRIEVE_LIMIT = 100
|
|
|
|
};
|
|
|
|
|
2020-05-11 19:35:27 +02:00
|
|
|
class PendingRequest {
|
|
|
|
public:
|
|
|
|
uint64_t requestId;
|
|
|
|
|
|
|
|
PendingRequest(uint64_t requestId) : requestId(requestId) {}
|
2020-05-22 14:48:17 +02:00
|
|
|
virtual ~PendingRequest() {}
|
2020-05-11 19:35:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class GroupInfoRequest: public PendingRequest {
|
|
|
|
public:
|
|
|
|
int32_t groupId;
|
|
|
|
|
|
|
|
GroupInfoRequest(uint64_t requestId, int32_t groupId)
|
|
|
|
: PendingRequest(requestId), groupId(groupId) {}
|
|
|
|
};
|
|
|
|
|
2020-05-13 12:31:03 +02:00
|
|
|
class ContactRequest: public PendingRequest {
|
|
|
|
public:
|
|
|
|
std::string phoneNumber;
|
|
|
|
std::string alias;
|
|
|
|
std::string groupName;
|
|
|
|
int32_t userId;
|
|
|
|
|
|
|
|
ContactRequest(uint64_t requestId, const std::string &phoneNumber, const std::string &alias,
|
|
|
|
const std::string &groupName, int32_t userId)
|
|
|
|
: PendingRequest(requestId), phoneNumber(phoneNumber), alias(alias), groupName(groupName),
|
|
|
|
userId(userId) {}
|
|
|
|
};
|
|
|
|
|
2020-05-12 19:37:59 +02:00
|
|
|
class GroupJoinRequest: public PendingRequest {
|
|
|
|
public:
|
|
|
|
std::string inviteLink;
|
|
|
|
|
|
|
|
GroupJoinRequest(uint64_t requestId, const std::string &inviteLink)
|
|
|
|
: PendingRequest(requestId), inviteLink(inviteLink) {}
|
|
|
|
};
|
|
|
|
|
2020-05-16 00:16:06 +02:00
|
|
|
class SendMessageRequest: public PendingRequest {
|
|
|
|
public:
|
|
|
|
std::string tempFile;
|
|
|
|
|
|
|
|
SendMessageRequest(uint64_t requestId, const std::string &tempFile)
|
|
|
|
: PendingRequest(requestId), tempFile(tempFile) {}
|
|
|
|
};
|
|
|
|
|
2020-05-17 14:19:59 +02:00
|
|
|
class PendingMessage: public PendingRequest {
|
|
|
|
public:
|
2020-05-30 10:07:55 +02:00
|
|
|
td::td_api::object_ptr<td::td_api::message> message;
|
2020-05-17 14:19:59 +02:00
|
|
|
|
2020-05-30 10:07:55 +02:00
|
|
|
PendingMessage(uint64_t requestId, td::td_api::message *message)
|
|
|
|
: PendingRequest(requestId), message(message) {}
|
2020-05-17 14:19:59 +02:00
|
|
|
};
|
|
|
|
|
2020-05-28 21:29:58 +02:00
|
|
|
class UploadRequest: public PendingRequest {
|
|
|
|
public:
|
|
|
|
PurpleXfer *xfer;
|
|
|
|
int64_t chatId;
|
|
|
|
|
|
|
|
UploadRequest(uint64_t requestId, PurpleXfer *xfer, int64_t chatId)
|
|
|
|
: PendingRequest(requestId), xfer(xfer), chatId(chatId) {}
|
|
|
|
};
|
|
|
|
|
2020-02-23 14:02:47 +01:00
|
|
|
class TdAccountData {
|
|
|
|
public:
|
2020-05-09 14:07:21 +02:00
|
|
|
using TdUserPtr = td::td_api::object_ptr<td::td_api::user>;
|
|
|
|
using TdChatPtr = td::td_api::object_ptr<td::td_api::chat>;
|
|
|
|
using TdGroupPtr = td::td_api::object_ptr<td::td_api::basicGroup>;
|
2020-05-11 19:35:27 +02:00
|
|
|
using TdGroupInfoPtr = td::td_api::object_ptr<td::td_api::basicGroupFullInfo>;
|
2020-05-09 14:07:21 +02:00
|
|
|
using TdSupergroupPtr = td::td_api::object_ptr<td::td_api::supergroup>;
|
2020-02-23 14:02:47 +01:00
|
|
|
|
2020-05-22 13:05:11 +02:00
|
|
|
PurpleAccount *const purpleAccount;
|
|
|
|
TdAccountData(PurpleAccount *purpleAccount) : purpleAccount(purpleAccount) {}
|
|
|
|
|
2020-02-23 14:02:47 +01:00
|
|
|
void updateUser(TdUserPtr user);
|
2020-05-16 21:08:05 +02:00
|
|
|
void setUserStatus(int32_t userId, td::td_api::object_ptr<td::td_api::UserStatus> status);
|
2020-05-09 14:07:21 +02:00
|
|
|
void updateBasicGroup(TdGroupPtr group);
|
2020-05-20 21:31:58 +02:00
|
|
|
void setBasicGroupInfoRequested(int32_t groupId);
|
|
|
|
bool isBasicGroupInfoRequested(int32_t groupId);
|
2020-05-11 19:35:27 +02:00
|
|
|
void updateBasicGroupInfo(int32_t groupId, TdGroupInfoPtr groupInfo);
|
2020-05-09 14:07:21 +02:00
|
|
|
void updateSupergroup(TdSupergroupPtr group);
|
|
|
|
|
2020-05-02 12:35:31 +02:00
|
|
|
void addChat(TdChatPtr chat); // Updates existing chat if any
|
2020-05-19 16:36:58 +02:00
|
|
|
void updateChatChatList(int64_t chatId, td::td_api::object_ptr<td::td_api::ChatList> list);
|
2020-05-20 13:33:47 +02:00
|
|
|
void updateChatTitle(int64_t chatId, const std::string &title);
|
2020-05-02 12:35:31 +02:00
|
|
|
void setContacts(const std::vector<std::int32_t> &userIds);
|
|
|
|
void getContactsWithNoChat(std::vector<std::int32_t> &userIds);
|
2020-05-20 21:31:58 +02:00
|
|
|
void getChats(std::vector<const td::td_api::chat *> &chats) const;
|
2020-05-21 14:51:52 +02:00
|
|
|
void deleteChat(int64_t id);
|
2020-05-05 18:35:06 +02:00
|
|
|
|
2020-05-09 14:07:21 +02:00
|
|
|
const td::td_api::chat *getChat(int64_t chatId) const;
|
2020-05-09 18:58:04 +02:00
|
|
|
int getPurpleChatId(int64_t tdChatId);
|
2020-05-10 14:24:15 +02:00
|
|
|
const td::td_api::chat *getChatByPurpleId(int32_t purpleChatId) const;
|
2020-05-09 14:07:21 +02:00
|
|
|
const td::td_api::chat *getPrivateChatByUserId(int32_t userId) const;
|
|
|
|
const td::td_api::user *getUser(int32_t userId) const;
|
|
|
|
const td::td_api::user *getUserByPhone(const char *phoneNumber) const;
|
|
|
|
const td::td_api::user *getUserByPrivateChat(const td::td_api::chat &chat);
|
2020-05-29 19:39:22 +02:00
|
|
|
std::string getDisplayName(const td::td_api::user &user) const;
|
|
|
|
std::string getDisplayName(int32_t userId) const;
|
2020-05-16 20:38:58 +02:00
|
|
|
void getUsersByDisplayName(const char *displayName,
|
|
|
|
std::vector<const td::td_api::user*> &users);
|
2020-05-09 14:07:21 +02:00
|
|
|
const td::td_api::basicGroup *getBasicGroup(int32_t groupId) const;
|
2020-05-11 19:35:27 +02:00
|
|
|
const td::td_api::basicGroupFullInfo *getBasicGroupInfo(int32_t groupId) const;
|
2020-05-09 14:07:21 +02:00
|
|
|
const td::td_api::supergroup *getSupergroup(int32_t groupId) const;
|
|
|
|
const td::td_api::chat *getBasicGroupChatByGroup(int32_t groupId) const;
|
|
|
|
const td::td_api::chat *getSupergroupChatByGroup(int32_t groupId) const;
|
2020-05-10 14:24:15 +02:00
|
|
|
bool isGroupChatWithMembership(const td::td_api::chat &chat);
|
2020-05-09 14:07:21 +02:00
|
|
|
|
2020-05-11 19:35:27 +02:00
|
|
|
template<typename ReqType, typename... ArgsType>
|
|
|
|
void addPendingRequest(ArgsType... args)
|
|
|
|
{
|
|
|
|
m_requests.push_back(std::make_unique<ReqType>(args...));
|
|
|
|
}
|
|
|
|
template<typename ReqType>
|
2020-05-13 12:31:03 +02:00
|
|
|
void addPendingRequest(uint64_t requestId, std::unique_ptr<ReqType> &&request)
|
|
|
|
{
|
|
|
|
m_requests.push_back(std::move(request));
|
|
|
|
m_requests.back()->requestId = requestId;
|
|
|
|
}
|
|
|
|
template<typename ReqType>
|
2020-05-11 19:35:27 +02:00
|
|
|
std::unique_ptr<ReqType> getPendingRequest(uint64_t requestId)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<ReqType>(dynamic_cast<ReqType *>(getPendingRequestImpl(requestId).release()));
|
|
|
|
}
|
2020-03-21 14:32:33 +01:00
|
|
|
|
2020-05-28 21:29:58 +02:00
|
|
|
const ContactRequest * findContactRequest(int32_t userId);
|
2020-05-16 13:38:00 +02:00
|
|
|
void addTempFileUpload(int64_t messageId, const std::string &path);
|
|
|
|
std::string extractTempFileUpload(int64_t messageId);
|
|
|
|
|
2020-05-28 21:29:58 +02:00
|
|
|
std::unique_ptr<UploadRequest> getUploadRequest(PurpleXfer *xfer);
|
|
|
|
void addUpload(int32_t fileId, PurpleXfer *xfer, int64_t chatId);
|
|
|
|
bool getUpload(int32_t fileId, PurpleXfer *&xfer, int64_t &chatId);
|
|
|
|
bool getFileIdForUpload(PurpleXfer *xfer, int &fileId);
|
|
|
|
void removeUpload(int32_t fileId);
|
2020-05-31 15:55:28 +02:00
|
|
|
|
|
|
|
void addSecretChat(td::td_api::object_ptr<td::td_api::secretChat> secretChat);
|
|
|
|
bool getSecretChat(int32_t id);
|
2020-05-13 12:31:03 +02:00
|
|
|
private:
|
2020-05-29 12:43:09 +02:00
|
|
|
TdAccountData(const TdAccountData &other) = delete;
|
|
|
|
TdAccountData &operator=(const TdAccountData &other) = delete;
|
|
|
|
|
2020-05-29 19:39:22 +02:00
|
|
|
struct UserInfo {
|
|
|
|
TdUserPtr user;
|
|
|
|
std::string displayName;
|
|
|
|
};
|
|
|
|
|
2020-05-09 18:58:04 +02:00
|
|
|
struct ChatInfo {
|
|
|
|
int32_t purpleId;
|
|
|
|
TdChatPtr chat;
|
|
|
|
|
|
|
|
ChatInfo() : purpleId(0), chat() {}
|
2020-05-08 17:01:12 +02:00
|
|
|
};
|
|
|
|
|
2020-05-11 19:35:27 +02:00
|
|
|
struct GroupInfo {
|
|
|
|
TdGroupPtr group;
|
|
|
|
TdGroupInfoPtr fullInfo;
|
2020-05-20 21:31:58 +02:00
|
|
|
bool fullInfoRequested = false;
|
2020-05-11 19:35:27 +02:00
|
|
|
};
|
|
|
|
|
2020-05-16 00:16:06 +02:00
|
|
|
struct SendMessageInfo {
|
|
|
|
int64_t messageId;
|
|
|
|
std::string tempFile;
|
|
|
|
};
|
|
|
|
|
2020-05-28 21:29:58 +02:00
|
|
|
struct UploadInfo {
|
|
|
|
int32_t fileId;
|
|
|
|
int64_t chatId;
|
|
|
|
PurpleXfer *xfer;
|
|
|
|
};
|
|
|
|
|
2020-05-10 14:24:15 +02:00
|
|
|
using ChatMap = std::map<int64_t, ChatInfo>;
|
2020-05-29 19:39:22 +02:00
|
|
|
using UserMap = std::map<int32_t, UserInfo>;
|
2020-05-16 20:38:58 +02:00
|
|
|
UserMap m_userInfo;
|
|
|
|
ChatMap m_chatInfo;
|
2020-05-11 19:35:27 +02:00
|
|
|
std::map<int32_t, GroupInfo> m_groups;
|
2020-05-09 14:07:21 +02:00
|
|
|
std::map<int32_t, TdSupergroupPtr> m_supergroups;
|
2020-05-31 15:55:28 +02:00
|
|
|
std::set<int32_t> m_secretChats;
|
2020-05-09 19:46:02 +02:00
|
|
|
int m_lastChatPurpleId = 0;
|
2020-05-05 18:35:06 +02:00
|
|
|
|
2020-05-02 12:53:25 +02:00
|
|
|
// List of contacts for which private chat is not known yet.
|
2020-05-09 14:07:21 +02:00
|
|
|
std::vector<int32_t> m_contactUserIdsNoChat;
|
2020-05-05 18:35:06 +02:00
|
|
|
|
|
|
|
// Used to remember stuff during asynchronous communication when adding contact
|
2020-05-09 14:07:21 +02:00
|
|
|
std::vector<ContactRequest> m_addContactRequests;
|
2020-05-05 18:35:06 +02:00
|
|
|
|
2020-05-11 19:35:27 +02:00
|
|
|
std::vector<std::unique_ptr<PendingRequest>> m_requests;
|
2020-05-31 15:55:28 +02:00
|
|
|
|
|
|
|
// Newly sent messages containing inline images, for which a temporary file must be removed when
|
|
|
|
// transfer is completed
|
2020-05-16 00:16:06 +02:00
|
|
|
std::vector<SendMessageInfo> m_sentMessages;
|
2020-05-31 15:55:28 +02:00
|
|
|
|
|
|
|
// Currently active file uploads other than inline images, for which PurpleXfer is used
|
2020-05-28 21:29:58 +02:00
|
|
|
std::vector<UploadInfo> m_uploads;
|
2020-05-11 19:35:27 +02:00
|
|
|
|
2020-05-28 21:29:58 +02:00
|
|
|
std::unique_ptr<PendingRequest> getPendingRequestImpl(uint64_t requestId);
|
2020-05-29 21:29:07 +02:00
|
|
|
void setDisplayNameWithoutSuffix(UserInfo &entry);
|
2020-02-23 14:02:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|