data_user.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "core/stars_amount.h"
  9. #include "data/components/credits.h"
  10. #include "data/data_birthday.h"
  11. #include "data/data_peer.h"
  12. #include "data/data_chat_participant_status.h"
  13. #include "data/data_lastseen_status.h"
  14. #include "data/data_user_names.h"
  15. #include "dialogs/dialogs_key.h"
  16. namespace Data {
  17. struct BotCommand;
  18. struct BusinessDetails;
  19. } // namespace Data
  20. struct StarRefProgram {
  21. StarsAmount revenuePerUser;
  22. TimeId endDate = 0;
  23. ushort commission = 0;
  24. uint8 durationMonths = 0;
  25. friend inline constexpr bool operator==(
  26. StarRefProgram,
  27. StarRefProgram) = default;
  28. };
  29. struct BotVerifierSettings {
  30. DocumentId iconId = 0;
  31. QString company;
  32. QString customDescription;
  33. bool canModifyDescription = false;
  34. explicit operator bool() const {
  35. return iconId != 0;
  36. }
  37. friend inline bool operator==(
  38. const BotVerifierSettings &a,
  39. const BotVerifierSettings &b) = default;
  40. };
  41. struct BotInfo {
  42. BotInfo();
  43. QString description;
  44. QString inlinePlaceholder;
  45. std::vector<Data::BotCommand> commands;
  46. PhotoData *photo = nullptr;
  47. DocumentData *document = nullptr;
  48. QString botMenuButtonText;
  49. QString botMenuButtonUrl;
  50. QString privacyPolicyUrl;
  51. QColor botAppColorTitleDay = QColor(0, 0, 0, 0);
  52. QColor botAppColorTitleNight = QColor(0, 0, 0, 0);
  53. QColor botAppColorBodyDay = QColor(0, 0, 0, 0);
  54. QColor botAppColorBodyNight = QColor(0, 0, 0, 0);
  55. QString startToken;
  56. Dialogs::EntryState inlineReturnTo;
  57. ChatAdminRights groupAdminRights;
  58. ChatAdminRights channelAdminRights;
  59. StarRefProgram starRefProgram;
  60. std::unique_ptr<BotVerifierSettings> verifierSettings;
  61. int version = 0;
  62. int descriptionVersion = 0;
  63. int activeUsers = 0;
  64. bool inited : 1 = false;
  65. bool readsAllHistory : 1 = false;
  66. bool cantJoinGroups : 1 = false;
  67. bool supportsAttachMenu : 1 = false;
  68. bool canEditInformation : 1 = false;
  69. bool canManageEmojiStatus : 1 = false;
  70. bool supportsBusiness : 1 = false;
  71. bool hasMainApp : 1 = false;
  72. };
  73. enum class UserDataFlag : uint32 {
  74. Contact = (1 << 0),
  75. MutualContact = (1 << 1),
  76. Deleted = (1 << 2),
  77. Verified = (1 << 3),
  78. Scam = (1 << 4),
  79. Fake = (1 << 5),
  80. BotInlineGeo = (1 << 6),
  81. Blocked = (1 << 7),
  82. HasPhoneCalls = (1 << 8),
  83. PhoneCallsPrivate = (1 << 9),
  84. Support = (1 << 10),
  85. CanPinMessages = (1 << 11),
  86. DiscardMinPhoto = (1 << 12),
  87. Self = (1 << 13),
  88. Premium = (1 << 14),
  89. //CanReceiveGifts = (1 << 15),
  90. VoiceMessagesForbidden = (1 << 16),
  91. PersonalPhoto = (1 << 17),
  92. StoriesHidden = (1 << 18),
  93. HasActiveStories = (1 << 19),
  94. HasUnreadStories = (1 << 20),
  95. RequiresPremiumToWrite = (1 << 21),
  96. HasRequirePremiumToWrite = (1 << 22),
  97. HasStarsPerMessage = (1 << 23),
  98. MessageMoneyRestrictionsKnown = (1 << 24),
  99. ReadDatesPrivate = (1 << 25),
  100. };
  101. inline constexpr bool is_flag_type(UserDataFlag) { return true; };
  102. using UserDataFlags = base::flags<UserDataFlag>;
  103. [[nodiscard]] Data::LastseenStatus LastseenFromMTP(
  104. const MTPUserStatus &status,
  105. Data::LastseenStatus currentStatus);
  106. class UserData final : public PeerData {
  107. public:
  108. using Flag = UserDataFlag;
  109. using Flags = Data::Flags<UserDataFlags>;
  110. UserData(not_null<Data::Session*> owner, PeerId id);
  111. ~UserData();
  112. void setPhoto(const MTPUserProfilePhoto &photo);
  113. void setName(
  114. const QString &newFirstName,
  115. const QString &newLastName,
  116. const QString &newPhoneName,
  117. const QString &newUsername);
  118. void setUsernames(const Data::Usernames &newUsernames);
  119. void setUsername(const QString &username);
  120. void setPhone(const QString &newPhone);
  121. void setBotInfoVersion(int version);
  122. void setBotInfo(const MTPBotInfo &info);
  123. void setNameOrPhone(const QString &newNameOrPhone);
  124. void madeAction(TimeId when); // pseudo-online
  125. uint64 accessHash() const {
  126. return _accessHash;
  127. }
  128. void setAccessHash(uint64 accessHash);
  129. auto flags() const {
  130. return _flags.current();
  131. }
  132. auto flagsValue() const {
  133. return _flags.value();
  134. }
  135. void setFlags(UserDataFlags which);
  136. void addFlags(UserDataFlags which);
  137. void removeFlags(UserDataFlags which);
  138. [[nodiscard]] bool isVerified() const;
  139. [[nodiscard]] bool isScam() const;
  140. [[nodiscard]] bool isFake() const;
  141. [[nodiscard]] bool isPremium() const;
  142. [[nodiscard]] bool isBotInlineGeo() const;
  143. [[nodiscard]] bool isBot() const;
  144. [[nodiscard]] bool isSupport() const;
  145. [[nodiscard]] bool isInaccessible() const;
  146. [[nodiscard]] bool applyMinPhoto() const;
  147. [[nodiscard]] bool hasPersonalPhoto() const;
  148. [[nodiscard]] bool hasStoriesHidden() const;
  149. [[nodiscard]] bool hasRequirePremiumToWrite() const;
  150. [[nodiscard]] bool hasStarsPerMessage() const;
  151. [[nodiscard]] bool requiresPremiumToWrite() const;
  152. [[nodiscard]] bool messageMoneyRestrictionsKnown() const;
  153. [[nodiscard]] bool canSendIgnoreMoneyRestrictions() const;
  154. [[nodiscard]] bool readDatesPrivate() const;
  155. void setStarsPerMessage(int stars);
  156. [[nodiscard]] int starsPerMessage() const;
  157. [[nodiscard]] bool canShareThisContact() const;
  158. [[nodiscard]] bool canAddContact() const;
  159. // In Data::Session::processUsers() we check only that.
  160. // When actually trying to share contact we perform
  161. // a full check by canShareThisContact() call.
  162. [[nodiscard]] bool canShareThisContactFast() const;
  163. [[nodiscard]] const QString &phone() const;
  164. [[nodiscard]] QString username() const;
  165. [[nodiscard]] QString editableUsername() const;
  166. [[nodiscard]] const std::vector<QString> &usernames() const;
  167. [[nodiscard]] bool isUsernameEditable(QString username) const;
  168. void setBotVerifyDetails(Ui::BotVerifyDetails details);
  169. void setBotVerifyDetailsIcon(DocumentId iconId);
  170. [[nodiscard]] Ui::BotVerifyDetails *botVerifyDetails() const {
  171. return _botVerifyDetails.get();
  172. }
  173. enum class ContactStatus : char {
  174. Unknown,
  175. Contact,
  176. NotContact,
  177. };
  178. [[nodiscard]] ContactStatus contactStatus() const;
  179. [[nodiscard]] bool isContact() const;
  180. void setIsContact(bool is);
  181. [[nodiscard]] Data::LastseenStatus lastseen() const;
  182. bool updateLastseen(Data::LastseenStatus value);
  183. enum class CallsStatus : char {
  184. Unknown,
  185. Enabled,
  186. Disabled,
  187. Private,
  188. };
  189. CallsStatus callsStatus() const;
  190. bool hasCalls() const;
  191. void setCallsStatus(CallsStatus callsStatus);
  192. [[nodiscard]] Data::Birthday birthday() const;
  193. void setBirthday(Data::Birthday value);
  194. void setBirthday(const tl::conditional<MTPBirthday> &value);
  195. [[nodiscard]] int commonChatsCount() const;
  196. void setCommonChatsCount(int count);
  197. [[nodiscard]] int peerGiftsCount() const;
  198. void setPeerGiftsCount(int count);
  199. [[nodiscard]] bool hasPrivateForwardName() const;
  200. [[nodiscard]] QString privateForwardName() const;
  201. void setPrivateForwardName(const QString &name);
  202. [[nodiscard]] bool hasActiveStories() const;
  203. [[nodiscard]] bool hasUnreadStories() const;
  204. void setStoriesState(StoriesState state);
  205. [[nodiscard]] const Data::BusinessDetails &businessDetails() const;
  206. void setBusinessDetails(Data::BusinessDetails details);
  207. void setStarRefProgram(StarRefProgram program);
  208. [[nodiscard]] ChannelId personalChannelId() const;
  209. [[nodiscard]] MsgId personalChannelMessageId() const;
  210. void setPersonalChannel(ChannelId channelId, MsgId messageId);
  211. MTPInputUser inputUser = MTP_inputUserEmpty();
  212. QString firstName;
  213. QString lastName;
  214. QString nameOrPhone;
  215. std::unique_ptr<BotInfo> botInfo;
  216. private:
  217. auto unavailableReasons() const
  218. -> const std::vector<Data::UnavailableReason> & override;
  219. void setUnavailableReasonsList(
  220. std::vector<Data::UnavailableReason> &&reasons) override;
  221. Flags _flags;
  222. Data::LastseenStatus _lastseen;
  223. Data::Birthday _birthday;
  224. int _commonChatsCount = 0;
  225. int _peerGiftsCount = 0;
  226. int _starsPerMessage = 0;
  227. ContactStatus _contactStatus = ContactStatus::Unknown;
  228. CallsStatus _callsStatus = CallsStatus::Unknown;
  229. Data::UsernamesInfo _username;
  230. std::unique_ptr<Data::BusinessDetails> _businessDetails;
  231. std::vector<Data::UnavailableReason> _unavailableReasons;
  232. QString _phone;
  233. QString _privateForwardName;
  234. std::unique_ptr<Ui::BotVerifyDetails> _botVerifyDetails;
  235. ChannelId _personalChannelId = 0;
  236. MsgId _personalChannelMessageId = 0;
  237. uint64 _accessHash = 0;
  238. static constexpr auto kInaccessibleAccessHashOld
  239. = 0xFFFFFFFFFFFFFFFFULL;
  240. };
  241. namespace Data {
  242. void ApplyUserUpdate(not_null<UserData*> user, const MTPDuserFull &update);
  243. [[nodiscard]] StarRefProgram ParseStarRefProgram(
  244. const MTPStarRefProgram *program);
  245. [[nodiscard]] Ui::BotVerifyDetails ParseBotVerifyDetails(
  246. const MTPBotVerification *info);
  247. } // namespace Data