history_unread_things.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. class History;
  9. namespace Data {
  10. class Thread;
  11. } // namespace Data
  12. namespace HistoryUnreadThings {
  13. enum class AddType {
  14. New,
  15. Existing,
  16. };
  17. enum class Type {
  18. Mentions,
  19. Reactions,
  20. };
  21. class List final {
  22. public:
  23. [[nodiscard]] int loadedCount() const {
  24. return _messages.size();
  25. }
  26. [[nodiscard]] MsgId minLoaded() const {
  27. return _messages.empty() ? 0 : _messages.front();
  28. }
  29. [[nodiscard]] MsgId maxLoaded() const {
  30. return _messages.empty() ? 0 : _messages.back();
  31. }
  32. [[nodiscard]] int count(int notKnownValue = -1) const {
  33. return _count.value_or(notKnownValue);
  34. }
  35. [[nodiscard]] bool has() const {
  36. return (count() > 0);
  37. }
  38. [[nodiscard]] bool contains(MsgId msgId) const {
  39. return _messages.contains(msgId);
  40. }
  41. [[nodiscard]] const base::flat_set<MsgId> &ids() const {
  42. return _messages;
  43. }
  44. void setCount(int count) {
  45. _count = count;
  46. }
  47. void insert(MsgId msgId) {
  48. _messages.insert(msgId);
  49. }
  50. void erase(MsgId msgId) {
  51. _messages.remove(msgId);
  52. }
  53. void clear() {
  54. _messages.clear();
  55. }
  56. private:
  57. std::optional<int> _count;
  58. base::flat_set<MsgId> _messages;
  59. };
  60. struct All {
  61. List mentions;
  62. List reactions;
  63. };
  64. class ConstProxy {
  65. public:
  66. ConstProxy(const List *list, bool known) : _list(list), _known(known) {
  67. }
  68. ConstProxy(const ConstProxy &) = delete;
  69. ConstProxy &operator=(const ConstProxy &) = delete;
  70. [[nodiscard]] int loadedCount() const {
  71. return _list ? _list->loadedCount() : 0;
  72. }
  73. [[nodiscard]] MsgId minLoaded() const {
  74. return _list ? _list->minLoaded() : 0;
  75. }
  76. [[nodiscard]] MsgId maxLoaded() const {
  77. return _list ? _list->maxLoaded() : 0;
  78. }
  79. [[nodiscard]] int count(int notKnownValue = -1) const {
  80. return _list
  81. ? _list->count(notKnownValue)
  82. : _known
  83. ? 0
  84. : notKnownValue;
  85. }
  86. [[nodiscard]] bool has() const {
  87. return _list && _list->has();
  88. }
  89. private:
  90. const List *_list = nullptr;
  91. const bool _known = false;
  92. };
  93. class Proxy final : public ConstProxy {
  94. public:
  95. Proxy(
  96. not_null<Data::Thread*> thread,
  97. std::unique_ptr<All> &data,
  98. Type type,
  99. bool known)
  100. : ConstProxy(
  101. (!data
  102. ? nullptr
  103. : (type == Type::Mentions)
  104. ? &data->mentions
  105. : &data->reactions),
  106. known)
  107. , _thread(thread)
  108. , _data(data)
  109. , _type(type)
  110. , _known(known) {
  111. }
  112. void setCount(int count);
  113. bool add(MsgId msgId, AddType type);
  114. void erase(MsgId msgId);
  115. void clear();
  116. void addSlice(const MTPmessages_Messages &slice, int alreadyLoaded);
  117. void checkAdd(MsgId msgId, bool resolved = false);
  118. private:
  119. void createData();
  120. void notifyUpdated();
  121. [[nodiscard]] List &resolveList();
  122. const not_null<Data::Thread*> _thread;
  123. std::unique_ptr<All> &_data;
  124. Type _type = Type::Mentions;
  125. bool _known = false;
  126. };
  127. } // namespace HistoryUnreadThings