data_thread.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include "data/data_thread.h"
  8. #include "data/data_forum_topic.h"
  9. #include "data/data_changes.h"
  10. #include "data/data_peer.h"
  11. #include "history/history.h"
  12. #include "history/history_item.h"
  13. #include "history/history_unread_things.h"
  14. #include "main/main_session.h"
  15. namespace Data {
  16. Thread::~Thread() = default;
  17. not_null<Thread*> Thread::migrateToOrMe() const {
  18. const auto history = asHistory();
  19. return history ? history->migrateToOrMe() : const_cast<Thread*>(this);
  20. }
  21. MsgId Thread::topicRootId() const {
  22. if (const auto topic = asTopic()) {
  23. return topic->rootId();
  24. }
  25. return MsgId();
  26. }
  27. not_null<PeerData*> Thread::peer() const {
  28. return owningHistory()->peer;
  29. }
  30. PeerNotifySettings &Thread::notify() {
  31. const auto topic = asTopic();
  32. return topic ? topic->notify() : peer()->notify();
  33. }
  34. const PeerNotifySettings &Thread::notify() const {
  35. return const_cast<Thread*>(this)->notify();
  36. }
  37. void Thread::setUnreadThingsKnown() {
  38. _flags |= Flag::UnreadThingsKnown;
  39. }
  40. HistoryUnreadThings::Proxy Thread::unreadMentions() {
  41. return {
  42. this,
  43. _unreadThings,
  44. HistoryUnreadThings::Type::Mentions,
  45. !!(_flags & Flag::UnreadThingsKnown),
  46. };
  47. }
  48. HistoryUnreadThings::ConstProxy Thread::unreadMentions() const {
  49. return {
  50. _unreadThings ? &_unreadThings->mentions : nullptr,
  51. !!(_flags & Flag::UnreadThingsKnown),
  52. };
  53. }
  54. HistoryUnreadThings::Proxy Thread::unreadReactions() {
  55. return {
  56. this,
  57. _unreadThings,
  58. HistoryUnreadThings::Type::Reactions,
  59. !!(_flags & Flag::UnreadThingsKnown),
  60. };
  61. }
  62. HistoryUnreadThings::ConstProxy Thread::unreadReactions() const {
  63. return {
  64. _unreadThings ? &_unreadThings->reactions : nullptr,
  65. !!(_flags & Flag::UnreadThingsKnown),
  66. };
  67. }
  68. const base::flat_set<MsgId> &Thread::unreadMentionsIds() const {
  69. if (!_unreadThings) {
  70. static const auto Result = base::flat_set<MsgId>();
  71. return Result;
  72. }
  73. return _unreadThings->mentions.ids();
  74. }
  75. const base::flat_set<MsgId> &Thread::unreadReactionsIds() const {
  76. if (!_unreadThings) {
  77. static const auto Result = base::flat_set<MsgId>();
  78. return Result;
  79. }
  80. return _unreadThings->reactions.ids();
  81. }
  82. void Thread::clearNotifications() {
  83. _notifications.clear();
  84. }
  85. void Thread::clearIncomingNotifications() {
  86. if (!peer()->isSelf()) {
  87. const auto proj = [](ItemNotification notification) {
  88. return notification.item->out();
  89. };
  90. _notifications.erase(
  91. ranges::remove(_notifications, false, proj),
  92. end(_notifications));
  93. }
  94. }
  95. void Thread::removeNotification(not_null<HistoryItem*> item) {
  96. _notifications.erase(
  97. ranges::remove(_notifications, item, &ItemNotification::item),
  98. end(_notifications));
  99. }
  100. std::optional<ItemNotification> Thread::currentNotification() const {
  101. return empty(_notifications)
  102. ? std::nullopt
  103. : std::make_optional(_notifications.front());
  104. }
  105. bool Thread::hasNotification() const {
  106. return !empty(_notifications);
  107. }
  108. void Thread::skipNotification() {
  109. if (!empty(_notifications)) {
  110. _notifications.pop_front();
  111. }
  112. }
  113. void Thread::pushNotification(ItemNotification notification) {
  114. _notifications.push_back(notification);
  115. }
  116. void Thread::popNotification(ItemNotification notification) {
  117. if (!empty(_notifications) && (_notifications.back() == notification)) {
  118. _notifications.pop_back();
  119. }
  120. }
  121. void Thread::setMuted(bool muted) {
  122. if (muted) {
  123. _flags |= Flag::Muted;
  124. } else {
  125. _flags &= ~Flag::Muted;
  126. }
  127. }
  128. void Thread::setUnreadMarkFlag(bool unread) {
  129. if (unread) {
  130. _flags |= Flag::UnreadMark;
  131. } else {
  132. _flags &= ~Flag::UnreadMark;
  133. }
  134. }
  135. [[nodiscard]] bool Thread::hasPinnedMessages() const {
  136. return (_flags & Flag::HasPinnedMessages);
  137. }
  138. void Thread::setHasPinnedMessages(bool has) {
  139. if (hasPinnedMessages() == has) {
  140. return;
  141. } else if (has) {
  142. _flags |= Flag::HasPinnedMessages;
  143. } else {
  144. _flags &= ~Flag::HasPinnedMessages;
  145. }
  146. session().changes().entryUpdated(
  147. this,
  148. EntryUpdate::Flag::HasPinnedMessages);
  149. }
  150. } // namespace Data