data_notify_settings.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "data/notify/data_peer_notify_settings.h"
  9. #include "base/timer.h"
  10. class PeerData;
  11. namespace Data {
  12. class DocumentMedia;
  13. class Session;
  14. class Thread;
  15. class Forum;
  16. class ForumTopic;
  17. enum class DefaultNotify {
  18. User,
  19. Group,
  20. Broadcast,
  21. };
  22. [[nodiscard]] DefaultNotify DefaultNotifyType(
  23. not_null<const PeerData*> peer);
  24. [[nodiscard]] MTPInputNotifyPeer DefaultNotifyToMTP(DefaultNotify type);
  25. class NotifySettings final {
  26. public:
  27. NotifySettings(not_null<Session*> owner);
  28. void request(not_null<PeerData*> peer);
  29. void request(not_null<Thread*> thread);
  30. void apply(
  31. const MTPNotifyPeer &notifyPeer,
  32. const MTPPeerNotifySettings &settings);
  33. void apply(
  34. const MTPInputNotifyPeer &notifyPeer,
  35. const MTPPeerNotifySettings &settings);
  36. void apply(DefaultNotify type, const MTPPeerNotifySettings &settings);
  37. void apply(PeerId peerId, const MTPPeerNotifySettings &settings);
  38. void apply(
  39. not_null<PeerData*> peer,
  40. const MTPPeerNotifySettings &settings);
  41. void apply(
  42. PeerId peerId,
  43. MsgId topicRootId,
  44. const MTPPeerNotifySettings &settings);
  45. void apply(
  46. not_null<ForumTopic*> topic,
  47. const MTPPeerNotifySettings &settings);
  48. void update(
  49. not_null<Thread*> thread,
  50. MuteValue muteForSeconds,
  51. std::optional<bool> silentPosts = std::nullopt,
  52. std::optional<NotifySound> sound = std::nullopt,
  53. std::optional<bool> storiesMuted = std::nullopt);
  54. void resetToDefault(not_null<Thread*> thread);
  55. void update(
  56. not_null<PeerData*> peer,
  57. MuteValue muteForSeconds,
  58. std::optional<bool> silentPosts = std::nullopt,
  59. std::optional<NotifySound> sound = std::nullopt,
  60. std::optional<bool> storiesMuted = std::nullopt);
  61. void resetToDefault(not_null<PeerData*> peer);
  62. void forumParentMuteUpdated(not_null<Forum*> forum);
  63. void cacheSound(DocumentId id);
  64. void cacheSound(not_null<DocumentData*> document);
  65. [[nodiscard]] std::shared_ptr<DocumentMedia> lookupRingtone(
  66. DocumentId id) const;
  67. [[nodiscard]] rpl::producer<> defaultUpdates(DefaultNotify type) const;
  68. [[nodiscard]] rpl::producer<> defaultUpdates(
  69. not_null<const PeerData*> peer) const;
  70. [[nodiscard]] const PeerNotifySettings &defaultSettings(
  71. DefaultNotify type) const;
  72. [[nodiscard]] bool isMuted(DefaultNotify type) const;
  73. void defaultUpdate(
  74. DefaultNotify type,
  75. MuteValue muteForSeconds,
  76. std::optional<bool> silentPosts = std::nullopt,
  77. std::optional<NotifySound> sound = std::nullopt,
  78. std::optional<bool> storiesMuted = std::nullopt);
  79. [[nodiscard]] bool isMuted(not_null<const Thread*> thread) const;
  80. [[nodiscard]] NotifySound sound(not_null<const Thread*> thread) const;
  81. [[nodiscard]] bool muteUnknown(not_null<const Thread*> thread) const;
  82. [[nodiscard]] bool soundUnknown(not_null<const Thread*> thread) const;
  83. [[nodiscard]] bool isMuted(not_null<const PeerData*> peer) const;
  84. [[nodiscard]] bool silentPosts(not_null<const PeerData*> peer) const;
  85. [[nodiscard]] NotifySound sound(not_null<const PeerData*> peer) const;
  86. [[nodiscard]] bool muteUnknown(not_null<const PeerData*> peer) const;
  87. [[nodiscard]] bool silentPostsUnknown(
  88. not_null<const PeerData*> peer) const;
  89. [[nodiscard]] bool soundUnknown(not_null<const PeerData*> peer) const;
  90. void loadExceptions();
  91. [[nodiscard]] rpl::producer<DefaultNotify> exceptionsUpdates() const;
  92. [[nodiscard]] auto exceptionsUpdatesRealtime() const
  93. -> rpl::producer<DefaultNotify>;
  94. [[nodiscard]] const base::flat_set<not_null<PeerData*>> &exceptions(
  95. DefaultNotify type) const;
  96. void clearExceptions(DefaultNotify type);
  97. private:
  98. static constexpr auto kDefaultNotifyTypes = 3;
  99. struct DefaultValue {
  100. PeerNotifySettings settings;
  101. rpl::event_stream<> updates;
  102. };
  103. void cacheSound(const std::optional<NotifySound> &sound);
  104. [[nodiscard]] bool isMuted(
  105. not_null<const Thread*> thread,
  106. crl::time *changesIn) const;
  107. [[nodiscard]] bool isMuted(
  108. not_null<const PeerData*> peer,
  109. crl::time *changesIn) const;
  110. [[nodiscard]] DefaultValue &defaultValue(DefaultNotify type);
  111. [[nodiscard]] const DefaultValue &defaultValue(DefaultNotify type) const;
  112. [[nodiscard]] const PeerNotifySettings &defaultSettings(
  113. not_null<const PeerData*> peer) const;
  114. [[nodiscard]] bool settingsUnknown(not_null<const PeerData*> peer) const;
  115. [[nodiscard]] bool settingsUnknown(
  116. not_null<const Thread*> thread) const;
  117. void unmuteByFinished();
  118. void unmuteByFinishedDelayed(crl::time delay);
  119. void updateLocal(not_null<Thread*> thread);
  120. void updateLocal(not_null<PeerData*> peer);
  121. void updateLocal(DefaultNotify type);
  122. void updateException(not_null<PeerData*> peer);
  123. void exceptionsUpdated(DefaultNotify type);
  124. const not_null<Session*> _owner;
  125. DefaultValue _defaultValues[3];
  126. std::unordered_set<not_null<const PeerData*>> _mutedPeers;
  127. std::unordered_map<not_null<ForumTopic*>, rpl::lifetime> _mutedTopics;
  128. base::Timer _unmuteByFinishedTimer;
  129. struct {
  130. base::flat_map<
  131. DocumentId,
  132. std::shared_ptr<DocumentMedia>> views;
  133. std::vector<DocumentId> pendingIds;
  134. rpl::lifetime pendingLifetime;
  135. } _ringtones;
  136. rpl::event_stream<DefaultNotify> _exceptionsUpdates;
  137. rpl::event_stream<DefaultNotify> _exceptionsUpdatesRealtime;
  138. std::array<
  139. base::flat_set<not_null<PeerData*>>,
  140. kDefaultNotifyTypes> _exceptions;
  141. std::array<mtpRequestId, kDefaultNotifyTypes> _exceptionsRequestId = {};
  142. std::array<bool, kDefaultNotifyTypes> _exceptionsUpdatesScheduled = {};
  143. };
  144. } // namespace Data