storage_user_photos.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "storage/storage_user_photos.h"
  8. namespace Storage {
  9. void UserPhotos::List::setBack(PhotoId photoId) {
  10. if (_backPhotoId != photoId) {
  11. detachBack();
  12. _backPhotoId = photoId;
  13. attachBack();
  14. sendUpdate();
  15. }
  16. }
  17. void UserPhotos::List::detachBack() {
  18. if (_backPhotoId) {
  19. removeOne(_backPhotoId);
  20. }
  21. }
  22. void UserPhotos::List::attachBack() {
  23. if (_backPhotoId) {
  24. _photoIds.push_front(_backPhotoId);
  25. if (_count) {
  26. ++*_count;
  27. }
  28. }
  29. }
  30. void UserPhotos::List::addNew(PhotoId photoId) {
  31. if (!base::contains(_photoIds, photoId)) {
  32. detachBack();
  33. _photoIds.push_back(photoId);
  34. if (_count) {
  35. ++*_count;
  36. }
  37. attachBack();
  38. sendUpdate();
  39. }
  40. }
  41. void UserPhotos::List::addSlice(
  42. std::vector<PhotoId> &&photoIds,
  43. int count) {
  44. detachBack();
  45. for (auto photoId : photoIds) {
  46. if (!base::contains(_photoIds, photoId)) {
  47. _photoIds.push_front(photoId);
  48. }
  49. }
  50. _count = count;
  51. if ((_count && *_count < _photoIds.size()) || photoIds.empty()) {
  52. _count = _photoIds.size();
  53. }
  54. attachBack();
  55. sendUpdate();
  56. }
  57. void UserPhotos::List::removeOne(PhotoId photoId) {
  58. auto position = ranges::find(_photoIds, photoId);
  59. if (position == _photoIds.end()) {
  60. _count = std::nullopt;
  61. } else {
  62. if (_count) {
  63. --*_count;
  64. }
  65. _photoIds.erase(position);
  66. }
  67. sendUpdate();
  68. }
  69. void UserPhotos::List::removeAfter(PhotoId photoId) {
  70. auto position = ranges::find(_photoIds, photoId);
  71. if (position == _photoIds.end()) {
  72. _count = std::nullopt;
  73. _photoIds.clear();
  74. } else {
  75. if (_count) {
  76. *_count -= (_photoIds.end() - position);
  77. }
  78. _photoIds.erase(position, _photoIds.end());
  79. }
  80. sendUpdate();
  81. }
  82. void UserPhotos::List::sendUpdate() {
  83. auto update = SliceUpdate();
  84. update.photoIds = &_photoIds;
  85. update.count = _count;
  86. _sliceUpdated.fire(std::move(update));
  87. }
  88. rpl::producer<UserPhotosResult> UserPhotos::List::query(
  89. UserPhotosQuery &&query) const {
  90. return [this, query = std::move(query)](auto consumer) {
  91. auto result = UserPhotosResult();
  92. result.count = _count;
  93. auto position = ranges::find(_photoIds, query.key.photoId);
  94. if (position != _photoIds.end()) {
  95. auto haveBefore = int(position - _photoIds.begin());
  96. auto haveEqualOrAfter = int(_photoIds.end() - position);
  97. auto before = qMin(haveBefore, query.limitBefore);
  98. auto equalOrAfter = qMin(haveEqualOrAfter, query.limitAfter + 1);
  99. result.photoIds = std::deque<PhotoId>(
  100. position - before,
  101. position + equalOrAfter);
  102. auto skippedInIds = (haveBefore - before);
  103. result.skippedBefore = _count
  104. | func::add(-int(_photoIds.size()) + skippedInIds);
  105. result.skippedBefore = haveBefore - before;
  106. result.skippedAfter = (haveEqualOrAfter - equalOrAfter);
  107. consumer.put_next(std::move(result));
  108. } else if (query.key.back && _backPhotoId) {
  109. result.photoIds.push_front(_backPhotoId);
  110. result.count = 1;
  111. consumer.put_next(std::move(result));
  112. } else if (_count) {
  113. consumer.put_next(std::move(result));
  114. }
  115. consumer.put_done();
  116. return rpl::lifetime();
  117. };
  118. }
  119. auto UserPhotos::List::sliceUpdated() const -> rpl::producer<SliceUpdate> {
  120. return _sliceUpdated.events();
  121. }
  122. rpl::producer<UserPhotosSliceUpdate> UserPhotos::sliceUpdated() const {
  123. return _sliceUpdated.events();
  124. }
  125. std::map<UserId, UserPhotos::List>::iterator UserPhotos::enforceLists(
  126. UserId user) {
  127. auto result = _lists.find(user);
  128. if (result != _lists.end()) {
  129. return result;
  130. }
  131. result = _lists.emplace(user, List {}).first;
  132. result->second.sliceUpdated(
  133. ) | rpl::start_with_next([this, user](
  134. const SliceUpdate &update) {
  135. _sliceUpdated.fire(UserPhotosSliceUpdate(
  136. user,
  137. update.photoIds,
  138. update.count));
  139. }, _lifetime);
  140. return result;
  141. }
  142. void UserPhotos::add(UserPhotosSetBack &&query) {
  143. auto userIt = enforceLists(query.userId);
  144. userIt->second.setBack(query.photoId);
  145. }
  146. void UserPhotos::add(UserPhotosAddNew &&query) {
  147. auto userIt = enforceLists(query.userId);
  148. userIt->second.addNew(query.photoId);
  149. }
  150. void UserPhotos::add(UserPhotosAddSlice &&query) {
  151. auto userIt = enforceLists(query.userId);
  152. userIt->second.addSlice(
  153. std::move(query.photoIds),
  154. query.count);
  155. }
  156. void UserPhotos::remove(UserPhotosRemoveOne &&query) {
  157. auto userIt = _lists.find(query.userId);
  158. if (userIt != _lists.end()) {
  159. userIt->second.removeOne(query.photoId);
  160. }
  161. }
  162. void UserPhotos::remove(UserPhotosRemoveAfter &&query) {
  163. auto userIt = _lists.find(query.userId);
  164. if (userIt != _lists.end()) {
  165. userIt->second.removeAfter(query.photoId);
  166. }
  167. }
  168. rpl::producer<UserPhotosResult> UserPhotos::query(
  169. UserPhotosQuery &&query) const {
  170. auto userIt = _lists.find(query.key.userId);
  171. if (userIt != _lists.end()) {
  172. return userIt->second.query(std::move(query));
  173. }
  174. return [](auto consumer) {
  175. consumer.put_done();
  176. return rpl::lifetime();
  177. };
  178. }
  179. } // namespace Storage