data_business_common.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "base/flags.h"
  9. #include "data/data_location.h"
  10. class UserData;
  11. namespace Data {
  12. class Session;
  13. enum class BusinessChatType {
  14. NewChats = (1 << 0),
  15. ExistingChats = (1 << 1),
  16. Contacts = (1 << 2),
  17. NonContacts = (1 << 3),
  18. };
  19. inline constexpr bool is_flag_type(BusinessChatType) { return true; }
  20. using BusinessChatTypes = base::flags<BusinessChatType>;
  21. struct BusinessChats {
  22. BusinessChatTypes types;
  23. std::vector<not_null<UserData*>> list;
  24. [[nodiscard]] bool empty() const {
  25. return !types && list.empty();
  26. }
  27. friend inline bool operator==(
  28. const BusinessChats &a,
  29. const BusinessChats &b) = default;
  30. };
  31. struct BusinessRecipients {
  32. BusinessChats included;
  33. BusinessChats excluded;
  34. bool allButExcluded = false;
  35. [[nodiscard]] static BusinessRecipients MakeValid(
  36. BusinessRecipients value);
  37. friend inline bool operator==(
  38. const BusinessRecipients &a,
  39. const BusinessRecipients &b) = default;
  40. };
  41. enum class BusinessRecipientsType : uchar {
  42. Messages,
  43. Bots,
  44. };
  45. [[nodiscard]] MTPInputBusinessRecipients ForMessagesToMTP(
  46. const BusinessRecipients &data);
  47. [[nodiscard]] MTPInputBusinessBotRecipients ForBotsToMTP(
  48. const BusinessRecipients &data);
  49. [[nodiscard]] BusinessRecipients FromMTP(
  50. not_null<Session*> owner,
  51. const MTPBusinessRecipients &recipients);
  52. [[nodiscard]] BusinessRecipients FromMTP(
  53. not_null<Session*> owner,
  54. const MTPBusinessBotRecipients &recipients);
  55. struct Timezone {
  56. QString id;
  57. QString name;
  58. TimeId utcOffset = 0;
  59. friend inline bool operator==(
  60. const Timezone &a,
  61. const Timezone &b) = default;
  62. };
  63. struct Timezones {
  64. std::vector<Timezone> list;
  65. friend inline bool operator==(
  66. const Timezones &a,
  67. const Timezones &b) = default;
  68. };;
  69. struct WorkingInterval {
  70. static constexpr auto kDay = 24 * 3600;
  71. static constexpr auto kWeek = 7 * kDay;
  72. static constexpr auto kInNextDayMax = 6 * 3600;
  73. TimeId start = 0;
  74. TimeId end = 0;
  75. explicit operator bool() const {
  76. return start < end;
  77. }
  78. [[nodiscard]] WorkingInterval shifted(TimeId offset) const {
  79. return { start + offset, end + offset };
  80. }
  81. [[nodiscard]] WorkingInterval united(WorkingInterval other) const {
  82. if (!*this) {
  83. return other;
  84. } else if (!other) {
  85. return *this;
  86. }
  87. return {
  88. std::min(start, other.start),
  89. std::max(end, other.end),
  90. };
  91. }
  92. [[nodiscard]] WorkingInterval intersected(WorkingInterval other) const {
  93. const auto result = WorkingInterval{
  94. std::max(start, other.start),
  95. std::min(end, other.end),
  96. };
  97. return result ? result : WorkingInterval();
  98. }
  99. friend inline bool operator==(
  100. const WorkingInterval &a,
  101. const WorkingInterval &b) = default;
  102. };
  103. struct WorkingIntervals {
  104. std::vector<WorkingInterval> list;
  105. [[nodiscard]] WorkingIntervals normalized() const;
  106. explicit operator bool() const {
  107. for (const auto &interval : list) {
  108. if (interval) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. friend inline bool operator==(
  115. const WorkingIntervals &a,
  116. const WorkingIntervals &b) = default;
  117. };
  118. struct WorkingHours {
  119. WorkingIntervals intervals;
  120. QString timezoneId;
  121. [[nodiscard]] WorkingHours normalized() const {
  122. return { intervals.normalized(), timezoneId };
  123. }
  124. explicit operator bool() const {
  125. return !timezoneId.isEmpty() && !intervals.list.empty();
  126. }
  127. friend inline bool operator==(
  128. const WorkingHours &a,
  129. const WorkingHours &b) = default;
  130. };
  131. [[nodiscard]] WorkingIntervals ExtractDayIntervals(
  132. const WorkingIntervals &intervals,
  133. int dayIndex);
  134. [[nodiscard]] bool IsFullOpen(const WorkingIntervals &extractedDay);
  135. [[nodiscard]] WorkingIntervals RemoveDayIntervals(
  136. const WorkingIntervals &intervals,
  137. int dayIndex);
  138. [[nodiscard]] WorkingIntervals ReplaceDayIntervals(
  139. const WorkingIntervals &intervals,
  140. int dayIndex,
  141. WorkingIntervals replacement);
  142. struct BusinessLocation {
  143. QString address;
  144. std::optional<LocationPoint> point;
  145. explicit operator bool() const {
  146. return !address.isEmpty();
  147. }
  148. friend inline bool operator==(
  149. const BusinessLocation &a,
  150. const BusinessLocation &b) = default;
  151. };
  152. struct ChatIntro {
  153. QString title;
  154. QString description;
  155. DocumentData *sticker = nullptr;
  156. [[nodiscard]] bool customPhrases() const {
  157. return !title.isEmpty() || !description.isEmpty();
  158. }
  159. explicit operator bool() const {
  160. return customPhrases() || sticker;
  161. }
  162. friend inline bool operator==(
  163. const ChatIntro &a,
  164. const ChatIntro &b) = default;
  165. };
  166. struct BusinessDetails {
  167. WorkingHours hours;
  168. BusinessLocation location;
  169. ChatIntro intro;
  170. explicit operator bool() const {
  171. return hours || location || intro;
  172. }
  173. friend inline bool operator==(
  174. const BusinessDetails &a,
  175. const BusinessDetails &b) = default;
  176. };
  177. [[nodiscard]] BusinessDetails FromMTP(
  178. not_null<Session*> owner,
  179. const tl::conditional<MTPBusinessWorkHours> &hours,
  180. const tl::conditional<MTPBusinessLocation> &location,
  181. const tl::conditional<MTPBusinessIntro> &intro);
  182. enum class AwayScheduleType : uchar {
  183. Never = 0,
  184. Always = 1,
  185. OutsideWorkingHours = 2,
  186. Custom = 3,
  187. };
  188. struct AwaySchedule {
  189. AwayScheduleType type = AwayScheduleType::Never;
  190. WorkingInterval customInterval;
  191. friend inline bool operator==(
  192. const AwaySchedule &a,
  193. const AwaySchedule &b) = default;
  194. };
  195. struct AwaySettings {
  196. BusinessRecipients recipients;
  197. AwaySchedule schedule;
  198. BusinessShortcutId shortcutId = 0;
  199. bool offlineOnly = false;
  200. explicit operator bool() const {
  201. return schedule.type != AwayScheduleType::Never;
  202. }
  203. friend inline bool operator==(
  204. const AwaySettings &a,
  205. const AwaySettings &b) = default;
  206. };
  207. [[nodiscard]] AwaySettings FromMTP(
  208. not_null<Session*> owner,
  209. const tl::conditional<MTPBusinessAwayMessage> &message);
  210. struct GreetingSettings {
  211. BusinessRecipients recipients;
  212. int noActivityDays = 0;
  213. BusinessShortcutId shortcutId = 0;
  214. explicit operator bool() const {
  215. return noActivityDays > 0;
  216. }
  217. friend inline bool operator==(
  218. const GreetingSettings &a,
  219. const GreetingSettings &b) = default;
  220. };
  221. [[nodiscard]] GreetingSettings FromMTP(
  222. not_null<Session*> owner,
  223. const tl::conditional<MTPBusinessGreetingMessage> &message);
  224. } // namespace Data