Skip to content

Commit

Permalink
Refactor userpic storage and access in PeerData.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Dec 5, 2017
1 parent 62568da commit 68009b6
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 215 deletions.
20 changes: 2 additions & 18 deletions Telegram/SourceFiles/apiwrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,7 @@ void ApiWrap::gotChatFull(PeerData *peer, const MTPmessages_ChatFull &result, mt
} break;
}
}
auto newPhotoId = PhotoId(0);
if (auto photo = App::feedPhoto(f.vchat_photo)) {
newPhotoId = photo->id;
photo->peer = chat;
}
if (chat->photoId != newPhotoId) {
chat->photoId = newPhotoId;
Notify::peerUpdatedDelayed(chat, UpdateFlag::PhotoChanged);
}
chat->setUserpicPhoto(f.vchat_photo);
chat->setInviteLink((f.vexported_invite.type() == mtpc_chatInviteExported) ? qs(f.vexported_invite.c_chatInviteExported().vlink) : QString());
chat->fullUpdated();

Expand All @@ -345,15 +337,7 @@ void ApiWrap::gotChatFull(PeerData *peer, const MTPmessages_ChatFull &result, mt
auto canEditStickers = channel->canEditStickers();

channel->setFullFlags(f.vflags.v);
auto newPhotoId = PhotoId(0);
if (auto photo = App::feedPhoto(f.vchat_photo)) {
newPhotoId = photo->id;
photo->peer = channel;
}
if (channel->photoId != newPhotoId) {
channel->photoId = newPhotoId;
Notify::peerUpdatedDelayed(channel, UpdateFlag::PhotoChanged);
}
channel->setUserpicPhoto(f.vchat_photo);
if (f.has_migrated_from_chat_id()) {
channel->addFlags(MTPDchannel::Flag::f_megagroup);
auto cfrom = App::chat(peerFromChat(f.vmigrated_from_chat_id));
Expand Down
35 changes: 12 additions & 23 deletions Telegram/SourceFiles/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,28 +1159,6 @@ namespace {
return ImagePtr();
}

StorageImageLocation imageLocation(int32 w, int32 h, const MTPFileLocation &loc) {
if (loc.type() == mtpc_fileLocation) {
const auto &l(loc.c_fileLocation());
return StorageImageLocation(w, h, l.vdc_id.v, l.vvolume_id.v, l.vlocal_id.v, l.vsecret.v);
}
return StorageImageLocation(w, h, 0, 0, 0, 0);
}

StorageImageLocation imageLocation(const MTPPhotoSize &size) {
switch (size.type()) {
case mtpc_photoSize: {
const auto &d(size.c_photoSize());
return imageLocation(d.vw.v, d.vh.v, d.vlocation);
} break;
case mtpc_photoCachedSize: {
const auto &d(size.c_photoCachedSize());
return imageLocation(d.vw.v, d.vh.v, d.vlocation);
} break;
}
return StorageImageLocation();
}

void feedInboxRead(const PeerId &peer, MsgId upTo) {
if (auto history = App::historyLoaded(peer)) {
history->inboxRead(upTo);
Expand Down Expand Up @@ -1418,7 +1396,18 @@ namespace {
}

DocumentData *feedDocument(const MTPDdocument &document, DocumentData *convert) {
return App::documentSet(document.vid.v, convert, document.vaccess_hash.v, document.vversion.v, document.vdate.v, document.vattributes.v, qs(document.vmime_type), App::image(document.vthumb), document.vdc_id.v, document.vsize.v, App::imageLocation(document.vthumb));
return App::documentSet(
document.vid.v,
convert,
document.vaccess_hash.v,
document.vversion.v,
document.vdate.v,
document.vattributes.v,
qs(document.vmime_type),
App::image(document.vthumb),
document.vdc_id.v,
document.vsize.v,
StorageImageLocation::FromMTP(document.vthumb));
}

WebPageData *feedWebPage(const MTPDwebPage &webpage, WebPageData *convert) {
Expand Down
2 changes: 0 additions & 2 deletions Telegram/SourceFiles/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ namespace App {
void feedUserLink(MTPint userId, const MTPContactLink &myLink, const MTPContactLink &foreignLink);

ImagePtr image(const MTPPhotoSize &size);
StorageImageLocation imageLocation(int32 w, int32 h, const MTPFileLocation &loc);
StorageImageLocation imageLocation(const MTPPhotoSize &size);

PhotoData *feedPhoto(const MTPPhoto &photo, const PreparedPhotoThumbs &thumbs);
PhotoData *feedPhoto(const MTPPhoto &photo, PhotoData *convert = nullptr);
Expand Down
8 changes: 6 additions & 2 deletions Telegram/SourceFiles/boxes/confirm_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,12 @@ ConfirmInviteBox::ConfirmInviteBox(QWidget*, const QString &title, bool isChanne
}
_status->setText(status);
if (photo.type() == mtpc_chatPhoto) {
auto &d = photo.c_chatPhoto();
auto location = App::imageLocation(160, 160, d.vphoto_small);
const auto &data = photo.c_chatPhoto();
const auto size = 160;
const auto location = StorageImageLocation::FromMTP(
size,
size,
data.vphoto_small);
if (!location.isNull()) {
_photo = ImagePtr(location);
if (!_photo->loaded()) {
Expand Down
20 changes: 13 additions & 7 deletions Telegram/SourceFiles/calls/calls_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,20 +436,26 @@ void Panel::processUserPhoto() {
if (!_user->userpicLoaded()) {
_user->loadUserpic(true);
}
auto photo = (_user->photoId && _user->photoId != UnknownPeerPhotoId) ? App::photo(_user->photoId) : nullptr;
const auto photo = _user->userpicPhotoId()
? App::photo(_user->userpicPhotoId())
: nullptr;
if (isGoodUserPhoto(photo)) {
photo->full->load(true);
} else {
if ((_user->photoId == UnknownPeerPhotoId) || (_user->photoId && (!photo || !photo->date))) {
Auth().api().requestFullPeer(_user);
}
} else if (_user->userpicPhotoUnknown() || (photo && !photo->date)) {
Auth().api().requestFullPeer(_user);
}
refreshUserPhoto();
}

void Panel::refreshUserPhoto() {
auto photo = (_user->photoId && _user->photoId != UnknownPeerPhotoId) ? App::photo(_user->photoId) : nullptr;
if (isGoodUserPhoto(photo) && photo->full->loaded() && (photo->id != _userPhotoId || !_userPhotoFull)) {
const auto photo = _user->userpicPhotoId()
? App::photo(_user->userpicPhotoId())
: nullptr;
const auto isNewPhoto = [&](not_null<PhotoData*> photo) {
return photo->full->loaded()
&& (photo->id != _userPhotoId || !_userPhotoFull);
};
if (isGoodUserPhoto(photo) && isNewPhoto(photo)) {
_userPhotoId = photo->id;
_userPhotoFull = true;
createUserpicCache(photo->full);
Expand Down
153 changes: 74 additions & 79 deletions Telegram/SourceFiles/data/data_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <rpl/map.h>
#include "data/data_peer_values.h"
#include "data/data_channel_admins.h"
#include "data/data_photo.h"
#include "lang/lang_keys.h"
#include "observer_peer.h"
#include "mainwidget.h"
Expand All @@ -38,6 +39,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace {

constexpr auto kUpdateFullPeerTimeout = TimeMs(5000); // Not more than once in 5 seconds.
constexpr auto kUserpicSize = 160;

int peerColorIndex(const PeerId &peer) {
auto myId = Auth().userId();
Expand Down Expand Up @@ -347,8 +349,10 @@ ClickHandlerPtr PeerData::createOpenLink() {
}

void PeerData::setUserpic(
ImagePtr userpic,
StorageImageLocation location) {
PhotoId photoId,
const StorageImageLocation &location,
ImagePtr userpic) {
_userpicPhotoId = photoId;
_userpic = userpic;
_userpicLocation = location;
if (useEmptyUserpic()) {
Expand All @@ -358,6 +362,20 @@ void PeerData::setUserpic(
}
}

void PeerData::setUserpicPhoto(const MTPPhoto &data) {
auto photoId = [&]() -> PhotoId {
if (auto photo = App::feedPhoto(data)) {
photo->peer = this;
return photo->id;
}
return 0;
}();
if (_userpicPhotoId != photoId) {
_userpicPhotoId = photoId;
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
}
}

ImagePtr PeerData::currentUserpic() const {
if (_userpic) {
_userpic->load();
Expand Down Expand Up @@ -449,35 +467,44 @@ bool UserData::canShareThisContact() const {
return canShareThisContactFast() || !App::phoneFromSharedContact(peerToUser(id)).isEmpty();
}

void UserData::setPhoto(const MTPUserProfilePhoto &p) { // see Local::readPeer as well
auto newPhotoId = photoId;
auto newPhoto = _userpic;
auto newPhotoLoc = _userpicLocation;
void PeerData::updateUserpic(
PhotoId photoId,
const MTPFileLocation &location) {
const auto size = kUserpicSize;
const auto loc = StorageImageLocation::FromMTP(size, size, location);
const auto photo = loc.isNull() ? ImagePtr() : ImagePtr(loc);
if (_userpicPhotoId != photoId
|| _userpic.v() != photo.v()
|| _userpicLocation != loc) {
setUserpic(photoId, loc, photo);
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
}
}

switch (p.type()) {
case mtpc_userProfilePhoto: {
const auto &d(p.c_userProfilePhoto());
newPhotoId = d.vphoto_id.v;
newPhotoLoc = App::imageLocation(160, 160, d.vphoto_small);
newPhoto = newPhotoLoc.isNull() ? ImagePtr() : ImagePtr(newPhotoLoc);
//App::feedPhoto(App::photoFromUserPhoto(peerToUser(id), MTP_int(unixtime()), p));
} break;
default: {
newPhotoId = 0;
if (id == ServiceUserId) {
if (!_userpic) {
newPhoto = ImagePtr(App::pixmapFromImageInPlace(Messenger::Instance().logoNoMargin().scaledToWidth(160, Qt::SmoothTransformation)), "PNG");
}
} else {
newPhoto = ImagePtr();
void PeerData::clearUserpic() {
const auto photoId = PhotoId(0);
const auto loc = StorageImageLocation();
const auto photo = [&] {
if (id == peerFromUser(ServiceUserId)) {
auto image = Messenger::Instance().logoNoMargin().scaledToWidth(
kUserpicSize,
Qt::SmoothTransformation);
auto pixmap = App::pixmapFromImageInPlace(std::move(image));
return _userpic
? _userpic
: ImagePtr(std::move(pixmap), "PNG");
}
newPhotoLoc = StorageImageLocation();
} break;
}
if (newPhotoId != photoId || newPhoto.v() != _userpic.v() || newPhotoLoc != _userpicLocation) {
photoId = newPhotoId;
setUserpic(newPhoto, newPhotoLoc);
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
return ImagePtr();
}();
}

// see Local::readPeer as well
void UserData::setPhoto(const MTPUserProfilePhoto &photo) {
if (photo.type() == mtpc_userProfilePhoto) {
const auto &data = photo.c_userProfilePhoto();
updateUserpic(data.vphoto_id.v, data.vphoto_small);
} else {
clearUserpic();
}
}

Expand Down Expand Up @@ -668,32 +695,16 @@ bool UserData::hasCalls() const {
return (callsStatus() != CallsStatus::Disabled) && (callsStatus() != CallsStatus::Unknown);
}

void ChatData::setPhoto(const MTPChatPhoto &p, const PhotoId &phId) { // see Local::readPeer as well
auto newPhotoId = photoId;
auto newPhoto = _userpic;
auto newPhotoLoc = _userpicLocation;
void ChatData::setPhoto(const MTPChatPhoto &photo) {
setPhoto(userpicPhotoId(), photo);
}

switch (p.type()) {
case mtpc_chatPhoto: {
auto &d = p.c_chatPhoto();
if (phId != UnknownPeerPhotoId) {
newPhotoId = phId;
}
newPhotoLoc = App::imageLocation(160, 160, d.vphoto_small);
newPhoto = newPhotoLoc.isNull() ? ImagePtr() : ImagePtr(newPhotoLoc);
// photoFull = newPhoto ? ImagePtr(640, 640, d.vphoto_big, ImagePtr()) : ImagePtr();
} break;
default: {
newPhotoId = 0;
newPhotoLoc = StorageImageLocation();
newPhoto = ImagePtr();
// photoFull = ImagePtr();
} break;
}
if (newPhotoId != photoId || newPhoto.v() != _userpic.v() || newPhotoLoc != _userpicLocation) {
photoId = newPhotoId;
setUserpic(newPhoto, newPhotoLoc);
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
void ChatData::setPhoto(PhotoId photoId, const MTPChatPhoto &photo) {
if (photo.type() == mtpc_chatPhoto) {
const auto &data = photo.c_chatPhoto();
updateUserpic(photoId, data.vphoto_small);
} else {
clearUserpic();
}
}

Expand Down Expand Up @@ -736,32 +747,16 @@ ChannelData::ChannelData(const PeerId &id)
}, _lifetime);
}

void ChannelData::setPhoto(const MTPChatPhoto &p, const PhotoId &phId) { // see Local::readPeer as well
auto newPhotoId = photoId;
auto newPhoto = _userpic;
auto newPhotoLoc = _userpicLocation;
void ChannelData::setPhoto(const MTPChatPhoto &photo) {
setPhoto(userpicPhotoId(), photo);
}

switch (p.type()) {
case mtpc_chatPhoto: {
auto &d = p.c_chatPhoto();
if (phId != UnknownPeerPhotoId) {
newPhotoId = phId;
}
newPhotoLoc = App::imageLocation(160, 160, d.vphoto_small);
newPhoto = newPhotoLoc.isNull() ? ImagePtr() : ImagePtr(newPhotoLoc);
// photoFull = newPhoto ? ImagePtr(640, 640, d.vphoto_big, newPhoto) : ImagePtr();
} break;
default: {
newPhotoId = 0;
newPhotoLoc = StorageImageLocation();
newPhoto = ImagePtr();
// photoFull = ImagePtr();
} break;
}
if (newPhotoId != photoId || newPhoto.v() != _userpic.v() || newPhotoLoc != _userpicLocation) {
photoId = newPhotoId;
setUserpic(newPhoto, newPhotoLoc);
Notify::peerUpdatedDelayed(this, UpdateFlag::PhotoChanged);
void ChannelData::setPhoto(PhotoId photoId, const MTPChatPhoto &photo) {
if (photo.type() == mtpc_chatPhoto) {
const auto &data = photo.c_chatPhoto();
updateUserpic(photoId, data.vphoto_small);
} else {
clearUserpic();
}
}

Expand Down
Loading

0 comments on commit 68009b6

Please sign in to comment.