Added initial support of fallback user photo to storage module.
This commit is contained in:
parent
6327d5ea38
commit
b135a09e00
7 changed files with 106 additions and 4 deletions
|
@ -8,10 +8,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "data/data_user.h"
|
#include "data/data_user.h"
|
||||||
|
|
||||||
#include "storage/localstorage.h"
|
#include "storage/localstorage.h"
|
||||||
|
#include "storage/storage_user_photos.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
#include "data/data_session.h"
|
#include "data/data_session.h"
|
||||||
#include "data/data_changes.h"
|
#include "data/data_changes.h"
|
||||||
#include "data/data_peer_bot_command.h"
|
#include "data/data_peer_bot_command.h"
|
||||||
|
#include "data/data_photo.h"
|
||||||
#include "data/data_emoji_statuses.h"
|
#include "data/data_emoji_statuses.h"
|
||||||
#include "data/data_user_names.h"
|
#include "data/data_user_names.h"
|
||||||
#include "data/notify/data_notify_settings.h"
|
#include "data/notify/data_notify_settings.h"
|
||||||
|
@ -380,6 +382,13 @@ void ApplyUserUpdate(not_null<UserData*> user, const MTPDuserFull &update) {
|
||||||
} else {
|
} else {
|
||||||
user->session().api().peerPhoto().unregisterNonPersonalPhoto(user);
|
user->session().api().peerPhoto().unregisterNonPersonalPhoto(user);
|
||||||
}
|
}
|
||||||
|
if (const auto photo = update.vfallback_photo()) {
|
||||||
|
const auto data = user->owner().processPhoto(*photo);
|
||||||
|
user->session().storage().add(Storage::UserPhotosSetBack(
|
||||||
|
peerToUser(user->id),
|
||||||
|
data->id
|
||||||
|
));
|
||||||
|
}
|
||||||
user->setSettings(update.vsettings());
|
user->setSettings(update.vsettings());
|
||||||
user->owner().notifySettings().apply(user, update.vnotify_settings());
|
user->owner().notifySettings().apply(user, update.vnotify_settings());
|
||||||
|
|
||||||
|
|
|
@ -237,3 +237,21 @@ rpl::producer<UserPhotosSlice> UserPhotosReversedViewer(
|
||||||
return std::move(slice);
|
return std::move(slice);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "storage/storage_user_photos.h"
|
#include "storage/storage_user_photos.h"
|
||||||
#include "base/weak_ptr.h"
|
#include "base/weak_ptr.h"
|
||||||
|
|
||||||
|
class UserData;
|
||||||
|
|
||||||
namespace Main {
|
namespace Main {
|
||||||
class Session;
|
class Session;
|
||||||
} // namespace Main
|
} // namespace Main
|
||||||
|
@ -48,3 +50,6 @@ rpl::producer<UserPhotosSlice> UserPhotosReversedViewer(
|
||||||
UserPhotosSlice::Key key,
|
UserPhotosSlice::Key key,
|
||||||
int limitBefore,
|
int limitBefore,
|
||||||
int limitAfter);
|
int limitAfter);
|
||||||
|
|
||||||
|
[[nodiscard]] std::optional<PhotoId> SyncUserFallbackPhotoViewer(
|
||||||
|
not_null<UserData*> user);
|
||||||
|
|
|
@ -29,6 +29,7 @@ public:
|
||||||
rpl::producer<SharedMediaRemoveAll> sharedMediaAllRemoved() const;
|
rpl::producer<SharedMediaRemoveAll> sharedMediaAllRemoved() const;
|
||||||
rpl::producer<SharedMediaInvalidateBottom> sharedMediaBottomInvalidated() const;
|
rpl::producer<SharedMediaInvalidateBottom> sharedMediaBottomInvalidated() const;
|
||||||
|
|
||||||
|
void add(UserPhotosSetBack &&query);
|
||||||
void add(UserPhotosAddNew &&query);
|
void add(UserPhotosAddNew &&query);
|
||||||
void add(UserPhotosAddSlice &&query);
|
void add(UserPhotosAddSlice &&query);
|
||||||
void remove(UserPhotosRemoveOne &&query);
|
void remove(UserPhotosRemoveOne &&query);
|
||||||
|
@ -98,6 +99,10 @@ rpl::producer<SharedMediaInvalidateBottom> Facade::Impl::sharedMediaBottomInvali
|
||||||
return _sharedMedia.bottomInvalidated();
|
return _sharedMedia.bottomInvalidated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Facade::Impl::add(UserPhotosSetBack &&query) {
|
||||||
|
return _userPhotos.add(std::move(query));
|
||||||
|
}
|
||||||
|
|
||||||
void Facade::Impl::add(UserPhotosAddNew &&query) {
|
void Facade::Impl::add(UserPhotosAddNew &&query) {
|
||||||
return _userPhotos.add(std::move(query));
|
return _userPhotos.add(std::move(query));
|
||||||
}
|
}
|
||||||
|
@ -181,6 +186,10 @@ rpl::producer<SharedMediaInvalidateBottom> Facade::sharedMediaBottomInvalidated(
|
||||||
return _impl->sharedMediaBottomInvalidated();
|
return _impl->sharedMediaBottomInvalidated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Facade::add(UserPhotosSetBack &&query) {
|
||||||
|
return _impl->add(std::move(query));
|
||||||
|
}
|
||||||
|
|
||||||
void Facade::add(UserPhotosAddNew &&query) {
|
void Facade::add(UserPhotosAddNew &&query) {
|
||||||
return _impl->add(std::move(query));
|
return _impl->add(std::move(query));
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ struct SharedMediaKey;
|
||||||
using SharedMediaResult = SparseIdsListResult;
|
using SharedMediaResult = SparseIdsListResult;
|
||||||
struct SharedMediaSliceUpdate;
|
struct SharedMediaSliceUpdate;
|
||||||
|
|
||||||
|
struct UserPhotosSetBack;
|
||||||
struct UserPhotosAddNew;
|
struct UserPhotosAddNew;
|
||||||
struct UserPhotosAddSlice;
|
struct UserPhotosAddSlice;
|
||||||
struct UserPhotosRemoveOne;
|
struct UserPhotosRemoveOne;
|
||||||
|
@ -58,6 +59,7 @@ public:
|
||||||
rpl::producer<SharedMediaRemoveAll> sharedMediaAllRemoved() const;
|
rpl::producer<SharedMediaRemoveAll> sharedMediaAllRemoved() const;
|
||||||
rpl::producer<SharedMediaInvalidateBottom> sharedMediaBottomInvalidated() const;
|
rpl::producer<SharedMediaInvalidateBottom> sharedMediaBottomInvalidated() const;
|
||||||
|
|
||||||
|
void add(UserPhotosSetBack &&query);
|
||||||
void add(UserPhotosAddNew &&query);
|
void add(UserPhotosAddNew &&query);
|
||||||
void add(UserPhotosAddSlice &&query);
|
void add(UserPhotosAddSlice &&query);
|
||||||
void remove(UserPhotosRemoveOne &&query);
|
void remove(UserPhotosRemoveOne &&query);
|
||||||
|
|
|
@ -9,12 +9,38 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
namespace Storage {
|
namespace Storage {
|
||||||
|
|
||||||
|
void UserPhotos::List::setBack(PhotoId photoId) {
|
||||||
|
if (_backPhotoId != photoId) {
|
||||||
|
detachBack();
|
||||||
|
_backPhotoId = photoId;
|
||||||
|
attachBack();
|
||||||
|
sendUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UserPhotos::List::detachBack() {
|
||||||
|
if (_backPhotoId) {
|
||||||
|
removeOne(_backPhotoId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UserPhotos::List::attachBack() {
|
||||||
|
if (_backPhotoId) {
|
||||||
|
_photoIds.push_front(_backPhotoId);
|
||||||
|
if (_count) {
|
||||||
|
++*_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void UserPhotos::List::addNew(PhotoId photoId) {
|
void UserPhotos::List::addNew(PhotoId photoId) {
|
||||||
if (!base::contains(_photoIds, photoId)) {
|
if (!base::contains(_photoIds, photoId)) {
|
||||||
|
detachBack();
|
||||||
_photoIds.push_back(photoId);
|
_photoIds.push_back(photoId);
|
||||||
if (_count) {
|
if (_count) {
|
||||||
++*_count;
|
++*_count;
|
||||||
}
|
}
|
||||||
|
attachBack();
|
||||||
sendUpdate();
|
sendUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +48,7 @@ void UserPhotos::List::addNew(PhotoId photoId) {
|
||||||
void UserPhotos::List::addSlice(
|
void UserPhotos::List::addSlice(
|
||||||
std::vector<PhotoId> &&photoIds,
|
std::vector<PhotoId> &&photoIds,
|
||||||
int count) {
|
int count) {
|
||||||
|
detachBack();
|
||||||
for (auto photoId : photoIds) {
|
for (auto photoId : photoIds) {
|
||||||
if (!base::contains(_photoIds, photoId)) {
|
if (!base::contains(_photoIds, photoId)) {
|
||||||
_photoIds.push_front(photoId);
|
_photoIds.push_front(photoId);
|
||||||
|
@ -32,6 +59,7 @@ void UserPhotos::List::addSlice(
|
||||||
if ((_count && *_count < _photoIds.size()) || photoIds.empty()) {
|
if ((_count && *_count < _photoIds.size()) || photoIds.empty()) {
|
||||||
_count = _photoIds.size();
|
_count = _photoIds.size();
|
||||||
}
|
}
|
||||||
|
attachBack();
|
||||||
sendUpdate();
|
sendUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +100,7 @@ void UserPhotos::List::sendUpdate() {
|
||||||
rpl::producer<UserPhotosResult> UserPhotos::List::query(
|
rpl::producer<UserPhotosResult> UserPhotos::List::query(
|
||||||
UserPhotosQuery &&query) const {
|
UserPhotosQuery &&query) const {
|
||||||
return [this, query = std::move(query)](auto consumer) {
|
return [this, query = std::move(query)](auto consumer) {
|
||||||
auto result = UserPhotosResult {};
|
auto result = UserPhotosResult();
|
||||||
result.count = _count;
|
result.count = _count;
|
||||||
|
|
||||||
auto position = ranges::find(_photoIds, query.key.photoId);
|
auto position = ranges::find(_photoIds, query.key.photoId);
|
||||||
|
@ -91,6 +119,10 @@ rpl::producer<UserPhotosResult> UserPhotos::List::query(
|
||||||
result.skippedBefore = haveBefore - before;
|
result.skippedBefore = haveBefore - before;
|
||||||
result.skippedAfter = (haveEqualOrAfter - equalOrAfter);
|
result.skippedAfter = (haveEqualOrAfter - equalOrAfter);
|
||||||
consumer.put_next(std::move(result));
|
consumer.put_next(std::move(result));
|
||||||
|
} else if (query.key.back && _backPhotoId) {
|
||||||
|
result.photoIds.push_front(_backPhotoId);
|
||||||
|
result.count = 1;
|
||||||
|
consumer.put_next(std::move(result));
|
||||||
} else if (_count) {
|
} else if (_count) {
|
||||||
consumer.put_next(std::move(result));
|
consumer.put_next(std::move(result));
|
||||||
}
|
}
|
||||||
|
@ -107,7 +139,8 @@ rpl::producer<UserPhotosSliceUpdate> UserPhotos::sliceUpdated() const {
|
||||||
return _sliceUpdated.events();
|
return _sliceUpdated.events();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<UserId, UserPhotos::List>::iterator UserPhotos::enforceLists(UserId user) {
|
std::map<UserId, UserPhotos::List>::iterator UserPhotos::enforceLists(
|
||||||
|
UserId user) {
|
||||||
auto result = _lists.find(user);
|
auto result = _lists.find(user);
|
||||||
if (result != _lists.end()) {
|
if (result != _lists.end()) {
|
||||||
return result;
|
return result;
|
||||||
|
@ -124,6 +157,11 @@ std::map<UserId, UserPhotos::List>::iterator UserPhotos::enforceLists(UserId use
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserPhotos::add(UserPhotosSetBack &&query) {
|
||||||
|
auto userIt = enforceLists(query.userId);
|
||||||
|
userIt->second.setBack(query.photoId);
|
||||||
|
}
|
||||||
|
|
||||||
void UserPhotos::add(UserPhotosAddNew &&query) {
|
void UserPhotos::add(UserPhotosAddNew &&query) {
|
||||||
auto userIt = enforceLists(query.userId);
|
auto userIt = enforceLists(query.userId);
|
||||||
userIt->second.addNew(query.photoId);
|
userIt->second.addNew(query.photoId);
|
||||||
|
@ -150,7 +188,8 @@ void UserPhotos::remove(UserPhotosRemoveAfter &&query) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rpl::producer<UserPhotosResult> UserPhotos::query(UserPhotosQuery &&query) const {
|
rpl::producer<UserPhotosResult> UserPhotos::query(
|
||||||
|
UserPhotosQuery &&query) const {
|
||||||
auto userIt = _lists.find(query.key.userId);
|
auto userIt = _lists.find(query.key.userId);
|
||||||
if (userIt != _lists.end()) {
|
if (userIt != _lists.end()) {
|
||||||
return userIt->second.query(std::move(query));
|
return userIt->second.query(std::move(query));
|
||||||
|
|
|
@ -12,6 +12,16 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
namespace Storage {
|
namespace Storage {
|
||||||
|
|
||||||
|
struct UserPhotosSetBack {
|
||||||
|
UserPhotosSetBack(UserId userId, PhotoId photoId)
|
||||||
|
: userId(userId), photoId(photoId) {
|
||||||
|
}
|
||||||
|
|
||||||
|
UserId userId = 0;
|
||||||
|
PhotoId photoId = 0;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
struct UserPhotosAddNew {
|
struct UserPhotosAddNew {
|
||||||
UserPhotosAddNew(UserId userId, PhotoId photoId)
|
UserPhotosAddNew(UserId userId, PhotoId photoId)
|
||||||
: userId(userId), photoId(photoId) {
|
: userId(userId), photoId(photoId) {
|
||||||
|
@ -71,10 +81,13 @@ struct UserPhotosKey {
|
||||||
: userId(userId)
|
: userId(userId)
|
||||||
, photoId(photoId) {
|
, photoId(photoId) {
|
||||||
}
|
}
|
||||||
|
UserPhotosKey(UserId userId, bool back) : userId(userId), back(back) {
|
||||||
|
}
|
||||||
|
|
||||||
bool operator==(const UserPhotosKey &other) const {
|
bool operator==(const UserPhotosKey &other) const {
|
||||||
return (userId == other.userId)
|
return (userId == other.userId)
|
||||||
&& (photoId == other.photoId);
|
&& (photoId == other.photoId)
|
||||||
|
&& (back == other.back);
|
||||||
}
|
}
|
||||||
bool operator!=(const UserPhotosKey &other) const {
|
bool operator!=(const UserPhotosKey &other) const {
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
|
@ -82,6 +95,7 @@ struct UserPhotosKey {
|
||||||
|
|
||||||
UserId userId = 0;
|
UserId userId = 0;
|
||||||
PhotoId photoId = 0;
|
PhotoId photoId = 0;
|
||||||
|
bool back = false;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -125,6 +139,7 @@ struct UserPhotosSliceUpdate {
|
||||||
|
|
||||||
class UserPhotos {
|
class UserPhotos {
|
||||||
public:
|
public:
|
||||||
|
void add(UserPhotosSetBack &&query);
|
||||||
void add(UserPhotosAddNew &&query);
|
void add(UserPhotosAddNew &&query);
|
||||||
void add(UserPhotosAddSlice &&query);
|
void add(UserPhotosAddSlice &&query);
|
||||||
void remove(UserPhotosRemoveOne &&query);
|
void remove(UserPhotosRemoveOne &&query);
|
||||||
|
@ -136,6 +151,7 @@ public:
|
||||||
private:
|
private:
|
||||||
class List {
|
class List {
|
||||||
public:
|
public:
|
||||||
|
void setBack(PhotoId photoId);
|
||||||
void addNew(PhotoId photoId);
|
void addNew(PhotoId photoId);
|
||||||
void addSlice(
|
void addSlice(
|
||||||
std::vector<PhotoId> &&photoIds,
|
std::vector<PhotoId> &&photoIds,
|
||||||
|
@ -152,10 +168,14 @@ private:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void sendUpdate();
|
void sendUpdate();
|
||||||
|
void detachBack();
|
||||||
|
void attachBack();
|
||||||
|
|
||||||
std::optional<int> _count;
|
std::optional<int> _count;
|
||||||
std::deque<PhotoId> _photoIds;
|
std::deque<PhotoId> _photoIds;
|
||||||
|
|
||||||
|
PhotoId _backPhotoId = PhotoId(0);
|
||||||
|
|
||||||
rpl::event_stream<SliceUpdate> _sliceUpdated;
|
rpl::event_stream<SliceUpdate> _sliceUpdated;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue