2017-08-29 19:52:52 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-08-29 19:52:52 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-08-29 19:52:52 +00:00
|
|
|
*/
|
2017-11-06 07:31:18 +00:00
|
|
|
#include "data/data_user_photos.h"
|
2017-08-29 19:52:52 +00:00
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2017-08-29 19:52:52 +00:00
|
|
|
#include "apiwrap.h"
|
2022-05-16 01:05:17 +00:00
|
|
|
#include "api/api_peer_photo.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2020-06-08 09:06:50 +00:00
|
|
|
#include "data/data_user.h"
|
2017-08-29 19:52:52 +00:00
|
|
|
#include "storage/storage_facade.h"
|
|
|
|
#include "storage/storage_user_photos.h"
|
|
|
|
|
2017-09-04 19:14:44 +00:00
|
|
|
class UserPhotosSliceBuilder {
|
|
|
|
public:
|
|
|
|
using Key = UserPhotosSlice::Key;
|
|
|
|
|
|
|
|
UserPhotosSliceBuilder(Key key, int limitBefore, int limitAfter);
|
|
|
|
|
|
|
|
bool applyUpdate(const Storage::UserPhotosResult &update);
|
|
|
|
bool applyUpdate(const Storage::UserPhotosSliceUpdate &update);
|
|
|
|
void checkInsufficientPhotos();
|
2017-09-27 12:04:19 +00:00
|
|
|
auto insufficientPhotosAround() const {
|
2017-09-04 19:14:44 +00:00
|
|
|
return _insufficientPhotosAround.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
UserPhotosSlice snapshot() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void mergeSliceData(
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> count,
|
2017-09-04 19:14:44 +00:00
|
|
|
const std::deque<PhotoId> &photoIds,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> skippedBefore,
|
2017-09-04 19:14:44 +00:00
|
|
|
int skippedAfter);
|
|
|
|
void sliceToLimits();
|
|
|
|
|
|
|
|
Key _key;
|
|
|
|
std::deque<PhotoId> _ids;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> _fullCount;
|
|
|
|
std::optional<int> _skippedBefore;
|
2017-09-04 19:14:44 +00:00
|
|
|
int _skippedAfter = 0;
|
|
|
|
int _limitBefore = 0;
|
|
|
|
int _limitAfter = 0;
|
|
|
|
|
2022-05-16 01:05:17 +00:00
|
|
|
rpl::event_stream<Api::PeerPhoto::UserPhotoId> _insufficientPhotosAround;
|
2017-09-04 19:14:44 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-11-20 12:23:20 +00:00
|
|
|
UserPhotosSlice::UserPhotosSlice(Key key)
|
|
|
|
: UserPhotosSlice(
|
2017-09-06 14:50:11 +00:00
|
|
|
key,
|
|
|
|
{},
|
2018-09-21 16:28:46 +00:00
|
|
|
std::nullopt,
|
|
|
|
std::nullopt,
|
|
|
|
std::nullopt) {
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UserPhotosSlice::UserPhotosSlice(
|
|
|
|
Key key,
|
2017-11-23 09:58:12 +00:00
|
|
|
std::deque<PhotoId> &&ids,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> fullCount,
|
|
|
|
std::optional<int> skippedBefore,
|
|
|
|
std::optional<int> skippedAfter)
|
2021-06-30 19:10:53 +00:00
|
|
|
: AbstractSparseIds<std::deque<PhotoId>>(
|
|
|
|
ids,
|
|
|
|
fullCount,
|
|
|
|
skippedBefore,
|
|
|
|
skippedAfter)
|
|
|
|
, _key(key) {
|
2017-11-23 09:58:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 19:10:53 +00:00
|
|
|
std::optional<int> UserPhotosSlice::distance(
|
|
|
|
const Key &a,
|
|
|
|
const Key &b) const {
|
2017-08-29 19:52:52 +00:00
|
|
|
if (a.userId != _key.userId
|
|
|
|
|| b.userId != _key.userId) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
2021-06-30 19:10:53 +00:00
|
|
|
if (const auto i = indexOf(a.photoId)) {
|
|
|
|
if (const auto j = indexOf(b.photoId)) {
|
2017-08-29 19:52:52 +00:00
|
|
|
return *j - *i;
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
UserPhotosSliceBuilder::UserPhotosSliceBuilder(
|
2017-08-29 19:52:52 +00:00
|
|
|
Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter)
|
2017-11-23 09:58:12 +00:00
|
|
|
: _key(key)
|
|
|
|
, _limitBefore(limitBefore)
|
|
|
|
, _limitAfter(limitAfter) {
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
bool UserPhotosSliceBuilder::applyUpdate(const Storage::UserPhotosResult &update) {
|
|
|
|
mergeSliceData(
|
|
|
|
update.count,
|
|
|
|
update.photoIds,
|
|
|
|
update.skippedBefore,
|
|
|
|
update.skippedAfter);
|
|
|
|
return true;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
bool UserPhotosSliceBuilder::applyUpdate(const Storage::UserPhotosSliceUpdate &update) {
|
|
|
|
if (update.userId != _key.userId) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-16 01:05:17 +00:00
|
|
|
const auto idsCount = update.photoIds ? int(update.photoIds->size()) : 0;
|
2017-09-06 14:50:11 +00:00
|
|
|
mergeSliceData(
|
|
|
|
update.count,
|
|
|
|
update.photoIds ? *update.photoIds : std::deque<PhotoId> {},
|
|
|
|
update.count | func::add(-idsCount),
|
|
|
|
0);
|
|
|
|
return true;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
void UserPhotosSliceBuilder::checkInsufficientPhotos() {
|
|
|
|
sliceToLimits();
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
void UserPhotosSliceBuilder::mergeSliceData(
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> count,
|
2017-08-29 19:52:52 +00:00
|
|
|
const std::deque<PhotoId> &photoIds,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> skippedBefore,
|
2017-08-29 19:52:52 +00:00
|
|
|
int skippedAfter) {
|
|
|
|
if (photoIds.empty()) {
|
2017-09-06 14:50:11 +00:00
|
|
|
if (_fullCount != count) {
|
|
|
|
_fullCount = count;
|
|
|
|
if (_fullCount && *_fullCount <= _ids.size()) {
|
|
|
|
_fullCount = _ids.size();
|
|
|
|
_skippedBefore = _skippedAfter = 0;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 14:50:11 +00:00
|
|
|
} else {
|
|
|
|
if (count) {
|
|
|
|
_fullCount = count;
|
|
|
|
}
|
|
|
|
_skippedAfter = skippedAfter;
|
|
|
|
_ids = photoIds;
|
2017-08-29 19:52:52 +00:00
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
if (_fullCount) {
|
|
|
|
_skippedBefore = *_fullCount
|
|
|
|
- _skippedAfter
|
|
|
|
- int(_ids.size());
|
|
|
|
}
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
sliceToLimits();
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
void UserPhotosSliceBuilder::sliceToLimits() {
|
2022-05-16 01:05:17 +00:00
|
|
|
const auto aroundIt = ranges::find(_ids, _key.photoId);
|
|
|
|
const auto removeFromBegin = (aroundIt - _ids.begin() - _limitBefore);
|
|
|
|
const auto removeFromEnd = (_ids.end() - aroundIt - _limitAfter - 1);
|
2017-08-29 19:52:52 +00:00
|
|
|
if (removeFromEnd > 0) {
|
2017-09-06 14:50:11 +00:00
|
|
|
_ids.erase(_ids.end() - removeFromEnd, _ids.end());
|
|
|
|
_skippedAfter += removeFromEnd;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
if (removeFromBegin > 0) {
|
2017-09-06 14:50:11 +00:00
|
|
|
_ids.erase(_ids.begin(), _ids.begin() + removeFromBegin);
|
|
|
|
if (_skippedBefore) {
|
|
|
|
*_skippedBefore += removeFromBegin;
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
2022-05-16 01:05:17 +00:00
|
|
|
} else if (removeFromBegin < 0
|
|
|
|
&& (!_skippedBefore || *_skippedBefore > 0)) {
|
2017-09-06 14:50:11 +00:00
|
|
|
_insufficientPhotosAround.fire(_ids.empty() ? 0 : _ids.front());
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:50:11 +00:00
|
|
|
UserPhotosSlice UserPhotosSliceBuilder::snapshot() const {
|
2017-11-23 09:58:12 +00:00
|
|
|
return UserPhotosSlice(
|
|
|
|
_key,
|
|
|
|
base::duplicate(_ids),
|
|
|
|
_fullCount,
|
|
|
|
_skippedBefore,
|
|
|
|
_skippedAfter);
|
2017-09-06 14:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<UserPhotosSlice> UserPhotosViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-09-06 14:50:11 +00:00
|
|
|
UserPhotosSlice::Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
2020-06-08 09:06:50 +00:00
|
|
|
return [=](auto consumer) {
|
2017-09-06 14:50:11 +00:00
|
|
|
auto lifetime = rpl::lifetime();
|
2022-05-16 01:05:17 +00:00
|
|
|
const auto builder = lifetime.make_state<UserPhotosSliceBuilder>(
|
2017-09-06 14:50:11 +00:00
|
|
|
key,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
2022-05-16 01:05:17 +00:00
|
|
|
const auto applyUpdate = [=](auto &&update) {
|
2017-09-14 19:27:41 +00:00
|
|
|
if (builder->applyUpdate(std::forward<decltype(update)>(update))) {
|
2017-09-06 14:50:11 +00:00
|
|
|
consumer.put_next(builder->snapshot());
|
|
|
|
}
|
|
|
|
};
|
2020-06-08 09:06:50 +00:00
|
|
|
auto requestPhotosAround = [user = session->data().user(key.userId)](
|
2022-05-16 01:05:17 +00:00
|
|
|
Api::PeerPhoto::UserPhotoId photoId) {
|
|
|
|
user->session().api().peerPhoto().requestUserPhotos(
|
|
|
|
user,
|
|
|
|
photoId);
|
2017-09-06 14:50:11 +00:00
|
|
|
};
|
|
|
|
builder->insufficientPhotosAround()
|
2022-05-16 01:05:17 +00:00
|
|
|
| rpl::start_with_next(std::move(requestPhotosAround), lifetime);
|
2017-09-06 14:50:11 +00:00
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().userPhotosSliceUpdated()
|
2017-09-27 08:43:35 +00:00
|
|
|
| rpl::start_with_next(applyUpdate, lifetime);
|
2017-09-06 14:50:11 +00:00
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
session->storage().query(Storage::UserPhotosQuery(
|
2017-09-06 14:50:11 +00:00
|
|
|
key,
|
|
|
|
limitBefore,
|
2017-12-22 07:05:20 +00:00
|
|
|
limitAfter
|
|
|
|
)) | rpl::start_with_next_done(
|
|
|
|
applyUpdate,
|
|
|
|
[=] { builder->checkInsufficientPhotos(); },
|
|
|
|
lifetime);
|
2017-09-06 14:50:11 +00:00
|
|
|
|
|
|
|
return lifetime;
|
|
|
|
};
|
2017-08-29 19:52:52 +00:00
|
|
|
}
|
2017-11-23 09:58:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
rpl::producer<UserPhotosSlice> UserPhotosReversedViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
not_null<Main::Session*> session,
|
2017-11-23 09:58:12 +00:00
|
|
|
UserPhotosSlice::Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
2017-12-22 07:05:20 +00:00
|
|
|
return UserPhotosViewer(
|
2020-06-08 09:06:50 +00:00
|
|
|
session,
|
2017-12-22 07:05:20 +00:00
|
|
|
key,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
|
|
|
) | rpl::map([](UserPhotosSlice &&slice) {
|
|
|
|
slice.reverse();
|
|
|
|
return std::move(slice);
|
|
|
|
});
|
2021-06-30 19:10:53 +00:00
|
|
|
}
|
2022-12-19 10:01:32 +00:00
|
|
|
|
|
|
|
std::optional<PhotoId> SyncUserFallbackPhotoViewer(not_null<UserData*> user) {
|
|
|
|
auto syncLifetime = rpl::lifetime();
|
|
|
|
auto result = std::optional<PhotoId>(std::nullopt);
|
|
|
|
|
|
|
|
constexpr auto kFallbackCount = 1;
|
|
|
|
user->session().storage().query(Storage::UserPhotosQuery(
|
|
|
|
Storage::UserPhotosKey(peerToUser(user->id), true),
|
|
|
|
kFallbackCount,
|
|
|
|
kFallbackCount
|
|
|
|
)) | rpl::start_with_next([&](Storage::UserPhotosResult &&slice) {
|
|
|
|
if (slice.photoIds.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = slice.photoIds.front();
|
|
|
|
}, syncLifetime);
|
|
|
|
return result;
|
|
|
|
}
|