Move user photos out of UserFull.

GitOrigin-RevId: f70e021e5bad07eff95af4eb9ddc0c7c12779f91
This commit is contained in:
levlam 2019-10-17 23:31:00 +03:00
parent 4e436cdf17
commit 39a87fcb10
2 changed files with 40 additions and 36 deletions

View File

@ -7934,21 +7934,22 @@ void ContactsManager::on_get_user_photos(UserId user_id, int32 offset, int32 lim
return; return;
} }
UserFull *user = &users_full_[user_id]; UserPhotos *user_photos = &user_photos_[user_id];
user->photo_count = total_count; user_photos->photo_count = total_count;
CHECK(user->getting_photos_now); CHECK(user_photos->getting_photos_now);
user->getting_photos_now = false; user_photos->getting_photos_now = false;
if (user->photos_offset == -1) { if (user_photos->photos_offset == -1) {
user->photos_offset = 0; user_photos->photos_offset = 0;
CHECK(user->photos.empty()); CHECK(user_photos->photos.empty());
} }
if (offset != narrow_cast<int32>(user->photos.size()) + user->photos_offset) { if (offset != narrow_cast<int32>(user_photos->photos.size()) + user_photos->photos_offset) {
LOG(INFO) << "Inappropriate offset to append " << user_id << " profile photos to cache: offset = " << offset LOG(INFO) << "Inappropriate offset to append " << user_id << " profile photos to cache: offset = " << offset
<< ", current_offset = " << user->photos_offset << ", photo_count = " << user->photos.size(); << ", current_offset = " << user_photos->photos_offset
user->photos.clear(); << ", photo_count = " << user_photos->photos.size();
user->photos_offset = offset; user_photos->photos.clear();
user_photos->photos_offset = offset;
} }
for (auto &photo : photos) { for (auto &photo : photos) {
@ -7960,8 +7961,8 @@ void ContactsManager::on_get_user_photos(UserId user_id, int32 offset, int32 lim
continue; continue;
} }
user->photos.push_back(std::move(user_photo)); user_photos->photos.push_back(std::move(user_photo));
add_user_photo_id(u, user_id, user->photos.back().id, photo_get_file_ids(user->photos.back())); add_user_photo_id(u, user_id, user_photos->photos.back().id, photo_get_file_ids(user_photos->photos.back()));
} }
} }
@ -8608,15 +8609,16 @@ void ContactsManager::on_delete_profile_photo(int64 profile_photo_id, Promise<Un
} }
void ContactsManager::drop_user_photos(UserId user_id, bool is_empty) { void ContactsManager::drop_user_photos(UserId user_id, bool is_empty) {
UserFull *user_full = get_user_full(user_id); auto it = user_photos_.find(user_id);
if (user_full != nullptr) { if (it != user_photos_.end()) {
user_full->photos.clear(); auto user_photos = &it->second;
user_photos->photos.clear();
if (is_empty) { if (is_empty) {
user_full->photo_count = 0; user_photos->photo_count = 0;
} else { } else {
user_full->photo_count = -1; user_photos->photo_count = -1;
} }
user_full->photos_offset = user_full->photo_count; user_photos->photos_offset = user_photos->photo_count;
} }
} }
@ -10470,32 +10472,32 @@ std::pair<int32, vector<const Photo *>> ContactsManager::get_user_profile_photos
return result; return result;
} }
auto user_full = &users_full_[user_id]; auto user_photos = &user_photos_[user_id];
if (user_full->getting_photos_now) { if (user_photos->getting_photos_now) {
promise.set_error(Status::Error(400, "Request for new profile photos has already been sent")); promise.set_error(Status::Error(400, "Request for new profile photos has already been sent"));
return result; return result;
} }
if (user_full->photo_count != -1) { // know photo count if (user_photos->photo_count != -1) { // know photo count
CHECK(user_full->photos_offset != -1); CHECK(user_photos->photos_offset != -1);
result.first = user_full->photo_count; result.first = user_photos->photo_count;
if (offset >= user_full->photo_count) { if (offset >= user_photos->photo_count) {
// offset if too big // offset if too big
promise.set_value(Unit()); promise.set_value(Unit());
return result; return result;
} }
if (limit > user_full->photo_count - offset) { if (limit > user_photos->photo_count - offset) {
limit = user_full->photo_count - offset; limit = user_photos->photo_count - offset;
} }
int32 cache_begin = user_full->photos_offset; int32 cache_begin = user_photos->photos_offset;
int32 cache_end = cache_begin + narrow_cast<int32>(user_full->photos.size()); int32 cache_end = cache_begin + narrow_cast<int32>(user_photos->photos.size());
if (cache_begin <= offset && offset + limit <= cache_end) { if (cache_begin <= offset && offset + limit <= cache_end) {
// answer query from cache // answer query from cache
for (int i = 0; i < limit; i++) { for (int i = 0; i < limit; i++) {
result.second.push_back(&user_full->photos[i + offset - cache_begin]); result.second.push_back(&user_photos->photos[i + offset - cache_begin]);
} }
promise.set_value(Unit()); promise.set_value(Unit());
return result; return result;
@ -10508,7 +10510,7 @@ std::pair<int32, vector<const Photo *>> ContactsManager::get_user_profile_photos
} }
} }
user_full->getting_photos_now = true; user_photos->getting_photos_now = true;
if (limit < MAX_GET_PROFILE_PHOTOS / 5) { if (limit < MAX_GET_PROFILE_PHOTOS / 5) {
limit = MAX_GET_PROFILE_PHOTOS / 5; // make limit reasonable limit = MAX_GET_PROFILE_PHOTOS / 5; // make limit reasonable

View File

@ -585,21 +585,22 @@ class ContactsManager : public Actor {
void parse(ParserT &parser); void parse(ParserT &parser);
}; };
// do not forget to update drop_user_full and on_get_user_full struct UserPhotos {
struct UserFull {
vector<Photo> photos; vector<Photo> photos;
int32 photo_count = -1; int32 photo_count = -1;
int32 photos_offset = -1; int32 photos_offset = -1;
bool getting_photos_now = false;
};
// do not forget to update drop_user_full and on_get_user_full
struct UserFull {
unique_ptr<BotInfo> bot_info; unique_ptr<BotInfo> bot_info;
string about; string about;
int32 common_chat_count = 0; int32 common_chat_count = 0;
bool getting_photos_now = false; bool is_inited = false; // bot_info may be inited regardless of this flag
bool is_inited = false; // photos and bot_info may be inited regardless this flag
bool is_blocked = false; bool is_blocked = false;
bool can_be_called = false; bool can_be_called = false;
bool has_private_calls = false; bool has_private_calls = false;
@ -1254,6 +1255,7 @@ class ContactsManager : public Actor {
std::unordered_map<UserId, User, UserIdHash> users_; std::unordered_map<UserId, User, UserIdHash> users_;
std::unordered_map<UserId, UserFull, UserIdHash> users_full_; std::unordered_map<UserId, UserFull, UserIdHash> users_full_;
std::unordered_map<UserId, UserPhotos, UserIdHash> user_photos_;
mutable std::unordered_set<UserId, UserIdHash> unknown_users_; mutable std::unordered_set<UserId, UserIdHash> unknown_users_;
std::unordered_map<UserId, tl_object_ptr<telegram_api::UserProfilePhoto>, UserIdHash> pending_user_photos_; std::unordered_map<UserId, tl_object_ptr<telegram_api::UserProfilePhoto>, UserIdHash> pending_user_photos_;
struct UserIdPhotoIdHash { struct UserIdPhotoIdHash {