Moved out api for user photos to related separated module.

This commit is contained in:
23rd 2022-05-16 04:05:17 +03:00 committed by John Preston
parent 13146e9c06
commit 243b16398b
5 changed files with 76 additions and 89 deletions

View File

@ -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<UserData*> 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<PhotoId>();
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

View File

@ -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<ApiWrap*> api);
void upload(not_null<PeerData*> peer, QImage &&image);
void clear(not_null<PhotoData*> photo);
void set(not_null<PeerData*> peer, not_null<PhotoData*> photo);
void requestUserPhotos(not_null<UserData*> user, UserPhotoId afterId);
private:
void ready(const FullMsgId &msgId, const MTPInputFile &file);
@ -34,6 +38,8 @@ private:
base::flat_map<FullMsgId, not_null<PeerData*>> _uploads;
base::flat_map<not_null<UserData*>, mtpRequestId> _userPhotosRequests;
};
} // namespace Api

View File

@ -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<UserData*> 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<UserData*> 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<PhotoId>();
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);

View File

@ -266,10 +266,6 @@ public:
not_null<PeerData*> peer,
Storage::SharedMediaType type);
void requestUserPhotos(
not_null<UserData*> user,
PhotoId afterId);
void readFeaturedSetDelayed(uint64 setId);
rpl::producer<SendAction> sendActions() const {
@ -460,11 +456,6 @@ private:
SliceType slice,
const MTPmessages_Messages &result);
void userPhotosDone(
not_null<UserData*> 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<not_null<UserData*>, mtpRequestId> _userPhotosRequests;
std::unique_ptr<DialogsLoadState> _dialogsLoadState;
TimeId _dialogsLoadTill = 0;
rpl::variable<bool> _dialogsLoadMayBlockByDate = false;

View File

@ -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<PhotoId> _insufficientPhotosAround;
rpl::event_stream<Api::PeerPhoto::UserPhotoId> _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<PhotoId> {},
@ -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<UserPhotosSlice> UserPhotosViewer(
int limitAfter) {
return [=](auto consumer) {
auto lifetime = rpl::lifetime();
auto builder = lifetime.make_state<UserPhotosSliceBuilder>(
const auto builder = lifetime.make_state<UserPhotosSliceBuilder>(
key,
limitBefore,
limitAfter);
auto applyUpdate = [=](auto &&update) {
const auto applyUpdate = [=](auto &&update) {
if (builder->applyUpdate(std::forward<decltype(update)>(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);