Avoid updating chat photo minithumbnail to 40x40 full minithumbnail.

This commit is contained in:
levlam 2022-04-29 10:50:10 +03:00
parent 58ff3d5e5d
commit 6a87f0c6ef
5 changed files with 34 additions and 26 deletions

View File

@ -11323,7 +11323,7 @@ void ContactsManager::do_update_user_photo(User *u, UserId user_id,
void ContactsManager::do_update_user_photo(User *u, UserId user_id, ProfilePhoto &&new_photo,
bool invalidate_photo_cache, const char *source) {
u->is_photo_inited = true;
if (new_photo != u->photo) {
if (need_update_profile_photo(u->photo, new_photo)) {
LOG_IF(ERROR, u->access_hash == -1 && new_photo.small_file_id.is_valid())
<< "Update profile photo of " << user_id << " without access hash from " << source;
u->photo = new_photo;
@ -13458,7 +13458,7 @@ void ContactsManager::on_update_chat_photo(Chat *c, DialogPhoto &&photo) {
photo.minithumbnail.clear();
}
if (photo != c->photo) {
if (need_update_dialog_photo(c->photo, photo)) {
c->photo = std::move(photo);
c->is_photo_changed = true;
c->need_save_to_database = true;
@ -13587,7 +13587,7 @@ void ContactsManager::on_update_channel_photo(Channel *c, DialogPhoto &&photo) {
photo.minithumbnail.clear();
}
if (photo != c->photo) {
if (need_update_dialog_photo(c->photo, photo)) {
c->photo = std::move(photo);
c->is_photo_changed = true;
c->need_save_to_database = true;

View File

@ -67,18 +67,8 @@ tl_object_ptr<td_api::profilePhoto> get_profile_photo_object(FileManager *file_m
profile_photo.has_animation);
}
bool operator==(const ProfilePhoto &lhs, const ProfilePhoto &rhs) {
bool location_differs = lhs.small_file_id != rhs.small_file_id || lhs.big_file_id != rhs.big_file_id;
bool id_differs = lhs.id != rhs.id;
if (location_differs) {
return false;
}
return lhs.has_animation == rhs.has_animation && lhs.minithumbnail == rhs.minithumbnail && !id_differs;
}
bool operator!=(const ProfilePhoto &lhs, const ProfilePhoto &rhs) {
return !(lhs == rhs);
bool need_update_profile_photo(const ProfilePhoto &from, const ProfilePhoto &to) {
return from.id != to.id || need_update_dialog_photo(from, to);
}
StringBuilder &operator<<(StringBuilder &string_builder, const ProfilePhoto &profile_photo) {
@ -204,13 +194,10 @@ bool is_same_dialog_photo(FileManager *file_manager, DialogId dialog_id, const P
get_unique_file_id(fake_photo.big_file_id) == get_unique_file_id(dialog_photo.big_file_id);
}
bool operator==(const DialogPhoto &lhs, const DialogPhoto &rhs) {
return lhs.small_file_id == rhs.small_file_id && lhs.big_file_id == rhs.big_file_id &&
lhs.minithumbnail == rhs.minithumbnail && lhs.has_animation == rhs.has_animation;
}
bool operator!=(const DialogPhoto &lhs, const DialogPhoto &rhs) {
return !(lhs == rhs);
bool need_update_dialog_photo(const DialogPhoto &from, const DialogPhoto &to) {
return from.small_file_id != to.small_file_id || from.big_file_id != to.big_file_id ||
from.has_animation != to.has_animation ||
need_update_dialog_photo_minithumbnail(from.minithumbnail, to.minithumbnail);
}
StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dialog_photo) {

View File

@ -57,8 +57,7 @@ ProfilePhoto get_profile_photo(FileManager *file_manager, UserId user_id, int64
tl_object_ptr<td_api::profilePhoto> get_profile_photo_object(FileManager *file_manager,
const ProfilePhoto &profile_photo);
bool operator==(const ProfilePhoto &lhs, const ProfilePhoto &rhs);
bool operator!=(const ProfilePhoto &lhs, const ProfilePhoto &rhs);
bool need_update_profile_photo(const ProfilePhoto &from, const ProfilePhoto &to);
StringBuilder &operator<<(StringBuilder &string_builder, const ProfilePhoto &profile_photo);
@ -79,8 +78,7 @@ bool is_same_dialog_photo(FileManager *file_manager, DialogId dialog_id, const P
vector<FileId> dialog_photo_get_file_ids(const DialogPhoto &dialog_photo);
bool operator==(const DialogPhoto &lhs, const DialogPhoto &rhs);
bool operator!=(const DialogPhoto &lhs, const DialogPhoto &rhs);
bool need_update_dialog_photo(const DialogPhoto &from, const DialogPhoto &to);
StringBuilder &operator<<(StringBuilder &string_builder, const DialogPhoto &dialog_photo);

View File

@ -57,6 +57,27 @@ StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimen
return string_builder << "(" << dimensions.width << ", " << dimensions.height << ")";
}
static int32 get_minithumbnail_size(const string &packed) {
if (packed.size() < 3) {
return 0;
}
if (packed[0] == '\x01') {
return max(static_cast<unsigned char>(packed[1]), static_cast<unsigned char>(packed[2]));
}
return 0;
}
bool need_update_dialog_photo_minithumbnail(const string &from, const string &to) {
if (from == to) {
return false;
}
auto from_size = get_minithumbnail_size(from);
auto to_size = get_minithumbnail_size(to);
// dialog photo minithumbnail is expected to be 8x8
return to_size != 0 && (to_size <= 8 || from_size > 8);
}
td_api::object_ptr<td_api::minithumbnail> get_minithumbnail_object(const string &packed) {
if (packed.size() < 3) {
return nullptr;

View File

@ -48,6 +48,8 @@ bool operator!=(const Dimensions &lhs, const Dimensions &rhs);
StringBuilder &operator<<(StringBuilder &string_builder, const Dimensions &dimensions);
bool need_update_dialog_photo_minithumbnail(const string &from, const string &to);
td_api::object_ptr<td_api::minithumbnail> get_minithumbnail_object(const string &packed);
FileId register_photo_size(FileManager *file_manager, const PhotoSizeSource &source, int64 id, int64 access_hash,