From 243b16398becf22caed0c40c091a82e6b748cdc8 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 16 May 2022 04:05:17 +0300 Subject: [PATCH] Moved out api for user photos to related separated module. --- Telegram/SourceFiles/api/api_peer_photo.cpp | 49 +++++++++++++ Telegram/SourceFiles/api/api_peer_photo.h | 6 ++ Telegram/SourceFiles/apiwrap.cpp | 73 ++----------------- Telegram/SourceFiles/apiwrap.h | 11 --- .../SourceFiles/data/data_user_photos.cpp | 26 ++++--- 5 files changed, 76 insertions(+), 89 deletions(-) diff --git a/Telegram/SourceFiles/api/api_peer_photo.cpp b/Telegram/SourceFiles/api/api_peer_photo.cpp index 1ed2729fc..be7fcadac 100644 --- a/Telegram/SourceFiles/api/api_peer_photo.cpp +++ b/Telegram/SourceFiles/api/api_peer_photo.cpp @@ -28,6 +28,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Api { namespace { +constexpr auto kSharedMediaLimit = 100; + SendMediaReady PreparePeerPhoto( MTP::DcId dcId, PeerId peerId, @@ -240,4 +242,51 @@ void PeerPhoto::ready(const FullMsgId &msgId, const MTPInputFile &file) { } } +void PeerPhoto::requestUserPhotos( + not_null user, + UserPhotoId afterId) { + if (_userPhotosRequests.contains(user)) { + return; + } + + const auto requestId = _api.request(MTPphotos_GetUserPhotos( + user->inputUser, + MTP_int(0), + MTP_long(afterId), + MTP_int(kSharedMediaLimit) + )).done([this, user](const MTPphotos_Photos &result) { + _userPhotosRequests.remove(user); + + const auto fullCount = result.match([](const MTPDphotos_photos &d) { + return int(d.vphotos().v.size()); + }, [](const MTPDphotos_photosSlice &d) { + return d.vcount().v; + }); + + auto photoIds = result.match([&](const auto &data) { + auto &owner = _session->data(); + owner.processUsers(data.vusers()); + + auto photoIds = std::vector(); + photoIds.reserve(data.vphotos().v.size()); + + for (const auto &photo : data.vphotos().v) { + if (const auto photoData = owner.processPhoto(photo)) { + photoIds.push_back(photoData->id); + } + } + return photoIds; + }); + + _session->storage().add(Storage::UserPhotosAddSlice( + peerToUser(user->id), + std::move(photoIds), + fullCount + )); + }).fail([this, user] { + _userPhotosRequests.remove(user); + }).send(); + _userPhotosRequests.emplace(user, requestId); +} + } // namespace Api diff --git a/Telegram/SourceFiles/api/api_peer_photo.h b/Telegram/SourceFiles/api/api_peer_photo.h index f0050e18f..ca7b64119 100644 --- a/Telegram/SourceFiles/api/api_peer_photo.h +++ b/Telegram/SourceFiles/api/api_peer_photo.h @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL class ApiWrap; class PeerData; +class UserData; namespace Main { class Session; @@ -20,12 +21,15 @@ namespace Api { class PeerPhoto final { public: + using UserPhotoId = PhotoId; explicit PeerPhoto(not_null api); void upload(not_null peer, QImage &&image); void clear(not_null photo); void set(not_null peer, not_null photo); + void requestUserPhotos(not_null user, UserPhotoId afterId); + private: void ready(const FullMsgId &msgId, const MTPInputFile &file); @@ -34,6 +38,8 @@ private: base::flat_map> _uploads; + base::flat_map, mtpRequestId> _userPhotosRequests; + }; } // namespace Api diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 9a62d866f..7761b3acf 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -90,7 +90,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/file_upload.h" #include "storage/storage_facade.h" #include "storage/storage_shared_media.h" -#include "storage/storage_user_photos.h" #include "storage/storage_media_prepare.h" #include "storage/storage_account.h" #include "facades.h" @@ -103,7 +102,6 @@ constexpr auto kSaveCloudDraftTimeout = 1000; constexpr auto kTopPromotionInterval = TimeId(60 * 60); constexpr auto kTopPromotionMinDelay = TimeId(10); constexpr auto kSmallDelayMs = 5; -constexpr auto kSharedMediaLimit = 100; constexpr auto kReadFeaturedSetsTimeout = crl::time(1000); constexpr auto kFileLoaderQueueStopTimeout = crl::time(5000); constexpr auto kStickersByEmojiInvalidateTimeout = crl::time(6 * 1000); @@ -2057,15 +2055,16 @@ void ApiWrap::saveDraftsToCloud() { history->finishSavingCloudDraft( UnixtimeFromMsgId(response.outerMsgId)); + const auto requestId = response.requestId; if (const auto cloudDraft = history->cloudDraft()) { - if (cloudDraft->saveRequestId == response.requestId) { + if (cloudDraft->saveRequestId == requestId) { cloudDraft->saveRequestId = 0; history->draftSavedToCloud(); } } auto i = _draftsSaveRequestIds.find(history); if (i != _draftsSaveRequestIds.cend() - && i->second == response.requestId) { + && i->second == requestId) { _draftsSaveRequestIds.erase(history); checkQuitPreventFinished(); } @@ -2073,14 +2072,15 @@ void ApiWrap::saveDraftsToCloud() { history->finishSavingCloudDraft( UnixtimeFromMsgId(response.outerMsgId)); + const auto requestId = response.requestId; if (const auto cloudDraft = history->cloudDraft()) { - if (cloudDraft->saveRequestId == response.requestId) { + if (cloudDraft->saveRequestId == requestId) { history->clearCloudDraft(); } } auto i = _draftsSaveRequestIds.find(history); if (i != _draftsSaveRequestIds.cend() - && i->second == response.requestId) { + && i->second == requestId) { _draftsSaveRequestIds.erase(history); checkQuitPreventFinished(); } @@ -2959,67 +2959,6 @@ void ApiWrap::sharedMediaDone( } } -void ApiWrap::requestUserPhotos( - not_null user, - PhotoId afterId) { - if (_userPhotosRequests.contains(user)) { - return; - } - - auto limit = kSharedMediaLimit; - - auto requestId = request(MTPphotos_GetUserPhotos( - user->inputUser, - MTP_int(0), - MTP_long(afterId), - MTP_int(limit) - )).done([this, user, afterId](const MTPphotos_Photos &result) { - _userPhotosRequests.remove(user); - userPhotosDone(user, afterId, result); - }).fail([this, user] { - _userPhotosRequests.remove(user); - }).send(); - _userPhotosRequests.emplace(user, requestId); -} - -void ApiWrap::userPhotosDone( - not_null user, - PhotoId photoId, - const MTPphotos_Photos &result) { - auto fullCount = 0; - auto &photos = *[&] { - switch (result.type()) { - case mtpc_photos_photos: { - auto &d = result.c_photos_photos(); - _session->data().processUsers(d.vusers()); - fullCount = d.vphotos().v.size(); - return &d.vphotos().v; - } break; - - case mtpc_photos_photosSlice: { - auto &d = result.c_photos_photosSlice(); - _session->data().processUsers(d.vusers()); - fullCount = d.vcount().v; - return &d.vphotos().v; - } break; - } - Unexpected("photos.Photos type in userPhotosDone()"); - }(); - - auto photoIds = std::vector(); - photoIds.reserve(photos.size()); - for (auto &photo : photos) { - if (auto photoData = _session->data().processPhoto(photo)) { - photoIds.push_back(photoData->id); - } - } - _session->storage().add(Storage::UserPhotosAddSlice( - peerToUser(user->id), - std::move(photoIds), - fullCount - )); -} - void ApiWrap::sendAction(const SendAction &action) { if (!action.options.scheduled) { _session->data().histories().readInbox(action.history); diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 707d0790a..fdbff5d26 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -266,10 +266,6 @@ public: not_null peer, Storage::SharedMediaType type); - void requestUserPhotos( - not_null user, - PhotoId afterId); - void readFeaturedSetDelayed(uint64 setId); rpl::producer sendActions() const { @@ -460,11 +456,6 @@ private: SliceType slice, const MTPmessages_Messages &result); - void userPhotosDone( - not_null user, - PhotoId photoId, - const MTPphotos_Photos &result); - void sendSharedContact( const QString &phone, const QString &firstName, @@ -579,8 +570,6 @@ private: MsgId, SliceType>> _sharedMediaRequests; - base::flat_map, mtpRequestId> _userPhotosRequests; - std::unique_ptr _dialogsLoadState; TimeId _dialogsLoadTill = 0; rpl::variable _dialogsLoadMayBlockByDate = false; diff --git a/Telegram/SourceFiles/data/data_user_photos.cpp b/Telegram/SourceFiles/data/data_user_photos.cpp index 07ae87dd3..ccd27c51f 100644 --- a/Telegram/SourceFiles/data/data_user_photos.cpp +++ b/Telegram/SourceFiles/data/data_user_photos.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "apiwrap.h" +#include "api/api_peer_photo.h" #include "data/data_session.h" #include "data/data_user.h" #include "storage/storage_facade.h" @@ -45,7 +46,7 @@ private: int _limitBefore = 0; int _limitAfter = 0; - rpl::event_stream _insufficientPhotosAround; + rpl::event_stream _insufficientPhotosAround; }; @@ -109,7 +110,7 @@ bool UserPhotosSliceBuilder::applyUpdate(const Storage::UserPhotosSliceUpdate &u if (update.userId != _key.userId) { return false; } - auto idsCount = update.photoIds ? int(update.photoIds->size()) : 0; + const auto idsCount = update.photoIds ? int(update.photoIds->size()) : 0; mergeSliceData( update.count, update.photoIds ? *update.photoIds : std::deque {}, @@ -152,9 +153,9 @@ void UserPhotosSliceBuilder::mergeSliceData( } void UserPhotosSliceBuilder::sliceToLimits() { - auto aroundIt = ranges::find(_ids, _key.photoId); - auto removeFromBegin = (aroundIt - _ids.begin() - _limitBefore); - auto removeFromEnd = (_ids.end() - aroundIt - _limitAfter - 1); + const auto aroundIt = ranges::find(_ids, _key.photoId); + const auto removeFromBegin = (aroundIt - _ids.begin() - _limitBefore); + const auto removeFromEnd = (_ids.end() - aroundIt - _limitAfter - 1); if (removeFromEnd > 0) { _ids.erase(_ids.end() - removeFromEnd, _ids.end()); _skippedAfter += removeFromEnd; @@ -164,7 +165,8 @@ void UserPhotosSliceBuilder::sliceToLimits() { if (_skippedBefore) { *_skippedBefore += removeFromBegin; } - } else if (removeFromBegin < 0 && (!_skippedBefore || *_skippedBefore > 0)) { + } else if (removeFromBegin < 0 + && (!_skippedBefore || *_skippedBefore > 0)) { _insufficientPhotosAround.fire(_ids.empty() ? 0 : _ids.front()); } } @@ -185,21 +187,23 @@ rpl::producer UserPhotosViewer( int limitAfter) { return [=](auto consumer) { auto lifetime = rpl::lifetime(); - auto builder = lifetime.make_state( + const auto builder = lifetime.make_state( key, limitBefore, limitAfter); - auto applyUpdate = [=](auto &&update) { + const auto applyUpdate = [=](auto &&update) { if (builder->applyUpdate(std::forward(update))) { consumer.put_next(builder->snapshot()); } }; auto requestPhotosAround = [user = session->data().user(key.userId)]( - PhotoId photoId) { - user->session().api().requestUserPhotos(user, photoId); + Api::PeerPhoto::UserPhotoId photoId) { + user->session().api().peerPhoto().requestUserPhotos( + user, + photoId); }; builder->insufficientPhotosAround() - | rpl::start_with_next(requestPhotosAround, lifetime); + | rpl::start_with_next(std::move(requestPhotosAround), lifetime); session->storage().userPhotosSliceUpdated() | rpl::start_with_next(applyUpdate, lifetime);