diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 6da68120c4..64c8eaf2f2 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -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(); @@ -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)); diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 85565b6e7d..41d97ea42b 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -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); @@ -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) { diff --git a/Telegram/SourceFiles/app.h b/Telegram/SourceFiles/app.h index 1f9fb1e3eb..480129879a 100644 --- a/Telegram/SourceFiles/app.h +++ b/Telegram/SourceFiles/app.h @@ -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); diff --git a/Telegram/SourceFiles/boxes/confirm_box.cpp b/Telegram/SourceFiles/boxes/confirm_box.cpp index f068edd43e..7abf13013c 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_box.cpp @@ -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()) { diff --git a/Telegram/SourceFiles/calls/calls_panel.cpp b/Telegram/SourceFiles/calls/calls_panel.cpp index d0a4ee271e..4bb59dc360 100644 --- a/Telegram/SourceFiles/calls/calls_panel.cpp +++ b/Telegram/SourceFiles/calls/calls_panel.cpp @@ -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 photo) { + return photo->full->loaded() + && (photo->id != _userPhotoId || !_userPhotoFull); + }; + if (isGoodUserPhoto(photo) && isNewPhoto(photo)) { _userPhotoId = photo->id; _userPhotoFull = true; createUserpicCache(photo->full); diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 5ba023b6dd..b6eb50dc13 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include #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" @@ -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(); @@ -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()) { @@ -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(); @@ -449,38 +467,47 @@ 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; - - 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(); - } - newPhotoLoc = StorageImageLocation(); - } break; - } - if (newPhotoId != photoId || newPhoto.v() != _userpic.v() || newPhotoLoc != _userpicLocation) { - photoId = newPhotoId; - setUserpic(newPhoto, newPhotoLoc); +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); } } +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"); + } + 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(); + } +} + void PeerData::fillNames() { _nameWords.clear(); _nameFirstChars.clear(); @@ -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(); } } @@ -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(); } } diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index 79faee7942..208c87f6c6 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -70,8 +70,6 @@ private: }; -static const PhotoId UnknownPeerPhotoId = 0xFFFFFFFFFFFFFFFFULL; - class PeerData; class PeerClickHandler : public ClickHandler { @@ -190,7 +188,11 @@ public: LoadedStatus loadedStatus = NotLoaded; MTPinputPeer input; - void setUserpic(ImagePtr userpic, StorageImageLocation location); + void setUserpic( + PhotoId photoId, + const StorageImageLocation &location, + ImagePtr userpic); + void setUserpicPhoto(const MTPPhoto &data); void paintUserpic( Painter &p, int x, @@ -233,8 +235,12 @@ public: StorageImageLocation userpicLocation() const { return _userpicLocation; } - - PhotoId photoId = UnknownPeerPhotoId; + bool userpicPhotoUnknown() const { + return (_userpicPhotoId == kUnknownPhotoId); + } + PhotoId userpicPhotoId() const { + return userpicPhotoUnknown() ? 0 : _userpicPhotoId; + } int nameVersion = 1; @@ -259,14 +265,19 @@ protected: const QString &newName, const QString &newNameOrPhone, const QString &newUsername); - - ImagePtr _userpic; - mutable EmptyUserpic _userpicEmpty; - StorageImageLocation _userpicLocation; + void updateUserpic(PhotoId photoId, const MTPFileLocation &location); + void clearUserpic(); private: void fillNames(); + static constexpr auto kUnknownPhotoId = PhotoId(0xFFFFFFFFFFFFFFFFULL); + + ImagePtr _userpic; + PhotoId _userpicPhotoId = kUnknownPhotoId; + mutable EmptyUserpic _userpicEmpty; + StorageImageLocation _userpicLocation; + Data::NotifySettings _notify; ClickHandlerPtr _openLink; @@ -519,9 +530,8 @@ public: : PeerData(id) , inputChat(MTP_int(bareId())) { } - void setPhoto( - const MTPChatPhoto &photo, - const PhotoId &phId = UnknownPeerPhotoId); + void setPhoto(const MTPChatPhoto &photo); + void setPhoto(PhotoId photoId, const MTPChatPhoto &photo); void setName(const QString &newName); @@ -768,9 +778,8 @@ public: ChannelData(const PeerId &id); - void setPhoto( - const MTPChatPhoto &photo, - const PhotoId &phId = UnknownPeerPhotoId); + void setPhoto(const MTPChatPhoto &photo); + void setPhoto(PhotoId photoId, const MTPChatPhoto &photo); void setName(const QString &name, const QString &username); diff --git a/Telegram/SourceFiles/data/data_shared_media.cpp b/Telegram/SourceFiles/data/data_shared_media.cpp index 49f59d59de..e366638f1f 100644 --- a/Telegram/SourceFiles/data/data_shared_media.cpp +++ b/Telegram/SourceFiles/data/data_shared_media.cpp @@ -231,7 +231,8 @@ base::optional SharedMediaWithLastSlice::indexOfImpl(Value value) const { return base::get_if(&value) ? _slice.indexOf(*base::get_if(&value)) : (isolatedInSlice() - || (*base::get_if>(&value))->id != _lastPhotoId) + || !_lastPhotoId + || (*base::get_if>(&value))->id != *_lastPhotoId) ? base::none : Add(_slice.size() - 1, lastPhotoSkip()); } @@ -256,7 +257,7 @@ SharedMediaWithLastSlice::Value SharedMediaWithLastSlice::operator[](int index) } return (index < _slice.size()) ? Value(_slice[index]) - : Value(App::photo(_lastPhotoId)); + : Value(App::photo(*_lastPhotoId)); } base::optional SharedMediaWithLastSlice::distance( @@ -274,20 +275,23 @@ void SharedMediaWithLastSlice::reverse() { _reversed = !_reversed; } -PhotoId SharedMediaWithLastSlice::LastPeerPhotoId(PeerId peerId) { +base::optional SharedMediaWithLastSlice::LastPeerPhotoId( + PeerId peerId) { if (auto peer = App::peerLoaded(peerId)) { - return peer->photoId; + return peer->userpicPhotoUnknown() + ? base::none + : base::make_optional(peer->userpicPhotoId()); } - return UnknownPeerPhotoId; + return base::none; } base::optional SharedMediaWithLastSlice::IsLastIsolated( const SparseIdsMergedSlice &slice, const base::optional &ending, - PhotoId lastPeerPhotoId) { - if (lastPeerPhotoId == UnknownPeerPhotoId) { + base::optional lastPeerPhotoId) { + if (!lastPeerPhotoId) { return base::none; - } else if (!lastPeerPhotoId) { + } else if (!*lastPeerPhotoId) { return false; } return LastFullMsgId(ending ? *ending : slice) @@ -299,7 +303,7 @@ base::optional SharedMediaWithLastSlice::IsLastIsolated( : nullptr; } | [](PhotoData *photo) { return photo ? photo->id : 0; } - | [&](PhotoId photoId) { return lastPeerPhotoId != photoId; }; + | [&](PhotoId photoId) { return *lastPeerPhotoId != photoId; }; } base::optional SharedMediaWithLastSlice::LastFullMsgId( diff --git a/Telegram/SourceFiles/data/data_shared_media.h b/Telegram/SourceFiles/data/data_shared_media.h index 59ce8c34aa..5bc559bc27 100644 --- a/Telegram/SourceFiles/data/data_shared_media.h +++ b/Telegram/SourceFiles/data/data_shared_media.h @@ -142,11 +142,11 @@ private: : base::none; } - static PhotoId LastPeerPhotoId(PeerId peerId); + static base::optional LastPeerPhotoId(PeerId peerId); static base::optional IsLastIsolated( const SparseIdsMergedSlice &slice, const base::optional &ending, - PhotoId lastPeerPhotoId); + base::optional lastPeerPhotoId); static base::optional LastFullMsgId( const SparseIdsMergedSlice &slice); static base::optional Add( @@ -183,7 +183,7 @@ private: Key _key; SparseIdsMergedSlice _slice; base::optional _ending; - PhotoId _lastPhotoId = 0; + base::optional _lastPhotoId; base::optional _isolatedLastPhoto; bool _reversed = false; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 8b4a51466a..baa24173d4 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -984,8 +984,9 @@ HistoryItem *History::createItem(const MTPMessage &msg, bool applyServiceAction, } break; case mtpc_messageActionChatDeletePhoto: { - auto chat = peer->asChat(); - if (chat) chat->setPhoto(MTP_chatPhotoEmpty()); + if (const auto chat = peer->asChat()) { + chat->setPhoto(MTP_chatPhotoEmpty()); + } } break; case mtpc_messageActionChatDeleteUser: { @@ -1048,10 +1049,11 @@ HistoryItem *History::createItem(const MTPMessage &msg, bool applyServiceAction, case mtpc_photoCachedSize: bigLoc = &bigSize.c_photoCachedSize().vlocation; break; } if (smallLoc && bigLoc) { - if (peer->isChat()) { - peer->asChat()->setPhoto(MTP_chatPhoto(*smallLoc, *bigLoc), photo ? photo->id : 0); - } else if (peer->isChannel()) { - peer->asChannel()->setPhoto(MTP_chatPhoto(*smallLoc, *bigLoc), photo ? photo->id : 0); + const auto newPhotoId = photo ? photo->id : 0; + if (const auto chat = peer->asChat()) { + chat->setPhoto(newPhotoId, MTP_chatPhoto(*smallLoc, *bigLoc)); + } else if (const auto channel = peer->asChannel()) { + channel->setPhoto(newPhotoId, MTP_chatPhoto(*smallLoc, *bigLoc)); } peer->loadUserpic(); } diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 39c0689cb3..796c3ea381 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -1048,9 +1048,9 @@ void MainWidget::deletePhotoLayer(PhotoData *photo) { auto me = App::self(); if (!me) return; - if (me->photoId == photo->id) { + if (me->userpicPhotoId() == photo->id) { Messenger::Instance().peerClearPhoto(me->id); - } else if (photo->peer && !photo->peer->isUser() && photo->peer->photoId == photo->id) { + } else if (photo->peer && !photo->peer->isUser() && photo->peer->userpicPhotoId() == photo->id) { Messenger::Instance().peerClearPhoto(photo->peer->id); } else { MTP::send(MTPphotos_DeletePhotos(MTP_vector(1, MTP_inputPhoto(MTP_long(photo->id), MTP_long(photo->access))))); @@ -5318,16 +5318,14 @@ void MainWidget::feedUpdate(const MTPUpdate &update) { if (auto user = App::userLoaded(d.vuser_id.v)) { user->setPhoto(d.vphoto); user->loadUserpic(); - if (mtpIsTrue(d.vprevious) - || !user->photoId - || user->photoId == UnknownPeerPhotoId) { + if (mtpIsTrue(d.vprevious) || !user->userpicPhotoId()) { Auth().storage().remove(Storage::UserPhotosRemoveAfter( user->bareId(), - user->photoId)); + user->userpicPhotoId())); } else { Auth().storage().add(Storage::UserPhotosAddNew( user->bareId(), - user->photoId)); + user->userpicPhotoId())); } } } break; diff --git a/Telegram/SourceFiles/mediaview.cpp b/Telegram/SourceFiles/mediaview.cpp index 13d07f267b..9291e3fdbb 100644 --- a/Telegram/SourceFiles/mediaview.cpp +++ b/Telegram/SourceFiles/mediaview.cpp @@ -398,7 +398,7 @@ void MediaView::updateActions() { return true; } else if (!_msgid && _photo && App::self() && _user == App::self()) { return _userPhotosData && _fullIndex && _fullCount; - } else if (_photo && _photo->peer && _photo->peer->photoId == _photo->id) { + } else if (_photo && _photo->peer && _photo->peer->userpicPhotoId() == _photo->id) { if (auto chat = _photo->peer->asChat()) { return chat->canEdit(); } else if (auto channel = _photo->peer->asChannel()) { @@ -948,7 +948,7 @@ void MediaView::onDelete() { auto deletingPeerPhoto = [this]() { if (!_msgid) return true; if (_photo && _history) { - if (_history->peer->photoId == _photo->id) { + if (_history->peer->userpicPhotoId() == _photo->id) { return _firstOpenedPeerPhoto; } } @@ -1010,12 +1010,12 @@ base::optional MediaView::sharedMediaType() const { } base::optional MediaView::sharedMediaKey() const { - if (!_msgid && _peer && !_user && _photo && _peer->photoId == _photo->id) { + if (!_msgid && _peer && !_user && _photo && _peer->userpicPhotoId() == _photo->id) { return SharedMediaKey { _history->peer->id, _migrated ? _migrated->peer->id : 0, SharedMediaType::ChatPhoto, - _peer->photoId + _peer->userpicPhotoId() }; } if (!IsServerMsgId(_msgid.msg)) { diff --git a/Telegram/SourceFiles/settings/settings_cover.cpp b/Telegram/SourceFiles/settings/settings_cover.cpp index d3da922c50..5c6da4b0e8 100644 --- a/Telegram/SourceFiles/settings/settings_cover.cpp +++ b/Telegram/SourceFiles/settings/settings_cover.cpp @@ -94,9 +94,11 @@ CoverWidget::CoverWidget(QWidget *parent, UserData *self) } PhotoData *CoverWidget::validatePhoto() const { - auto photo = (_self->photoId && _self->photoId != UnknownPeerPhotoId) ? App::photo(_self->photoId) : nullptr; + const auto photo = _self->userpicPhotoId() + ? App::photo(_self->userpicPhotoId()) + : nullptr; _userpicButton->setPointerCursor(photo != nullptr && photo->date != 0); - if ((_self->photoId == UnknownPeerPhotoId) || (_self->photoId && (!photo || !photo->date))) { + if (_self->userpicPhotoUnknown() || (photo && !photo->date)) { Auth().api().requestFullPeer(_self); return nullptr; } diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index d61d21807d..c115f1096d 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -4010,11 +4010,9 @@ uint32 _peerSize(PeerData *peer) { } void _writePeer(QDataStream &stream, PeerData *peer) { - stream << quint64(peer->id) << quint64(peer->photoId); + stream << quint64(peer->id) << quint64(peer->userpicPhotoId()); Serialize::writeStorageImageLocation(stream, peer->userpicLocation()); - if (peer->isUser()) { - UserData *user = peer->asUser(); - + if (const auto user = peer->asUser()) { stream << user->firstName << user->lastName << user->phone() << user->username << quint64(user->accessHash()); if (AppVersion >= 9012) { stream << qint32(user->flags()); @@ -4023,14 +4021,10 @@ void _writePeer(QDataStream &stream, PeerData *peer) { stream << (user->botInfo ? user->botInfo->inlinePlaceholder : QString()); } stream << qint32(user->onlineTill) << qint32(user->contact) << qint32(user->botInfo ? user->botInfo->version : -1); - } else if (peer->isChat()) { - auto chat = peer->asChat(); - + } else if (const auto chat = peer->asChat()) { stream << chat->name << qint32(chat->count) << qint32(chat->date) << qint32(chat->version) << qint32(chat->creator); stream << qint32(0) << quint32(chat->flags()) << chat->inviteLink(); - } else if (peer->isChannel()) { - auto channel = peer->asChannel(); - + } else if (auto channel = peer->asChannel()) { stream << channel->name << quint64(channel->access) << qint32(channel->date) << qint32(channel->version); stream << qint32(0) << quint32(channel->flags()) << channel->inviteLink(); } @@ -4048,9 +4042,7 @@ PeerData *_readPeer(FileReadDescriptor &from, int32 fileVersion = 0) { result = App::peer(peerId); result->loadedStatus = PeerData::FullLoaded; } - if (result->isUser()) { - UserData *user = result->asUser(); - + if (const auto user = result->asUser()) { QString first, last, phone, username, inlinePlaceholder; quint64 access; qint32 flags = 0, onlineTill, contact, botInfoVersion; @@ -4086,14 +4078,8 @@ PeerData *_readPeer(FileReadDescriptor &from, int32 fileVersion = 0) { user->input = MTP_inputPeerUser(MTP_int(peerToUser(user->id)), MTP_long(user->accessHash())); user->inputUser = MTP_inputUser(MTP_int(peerToUser(user->id)), MTP_long(user->accessHash())); } - - user->setUserpic( - photoLoc.isNull() ? ImagePtr() : ImagePtr(photoLoc), - photoLoc); } - } else if (result->isChat()) { - ChatData *chat = result->asChat(); - + } else if (const auto chat = result->asChat()) { QString name, inviteLink; qint32 count, date, version, creator, oldForbidden; quint32 flagsData, flags; @@ -4121,14 +4107,8 @@ PeerData *_readPeer(FileReadDescriptor &from, int32 fileVersion = 0) { chat->input = MTP_inputPeerChat(MTP_int(peerToChat(chat->id))); chat->inputChat = MTP_int(peerToChat(chat->id)); - - chat->setUserpic( - photoLoc.isNull() ? ImagePtr() : ImagePtr(photoLoc), - photoLoc); } - } else if (result->isChannel()) { - ChannelData *channel = result->asChannel(); - + } else if (const auto channel = result->asChannel()) { QString name, inviteLink; quint64 access; qint32 date, version, oldForbidden; @@ -4147,12 +4127,14 @@ PeerData *_readPeer(FileReadDescriptor &from, int32 fileVersion = 0) { channel->input = MTP_inputPeerChannel(MTP_int(peerToChannel(channel->id)), MTP_long(access)); channel->inputChannel = MTP_inputChannel(MTP_int(peerToChannel(channel->id)), MTP_long(access)); - - channel->setUserpic( - photoLoc.isNull() ? ImagePtr() : ImagePtr(photoLoc), - photoLoc); } } + if (!wasLoaded) { + result->setUserpic( + photoId, + photoLoc, + photoLoc.isNull() ? ImagePtr() : ImagePtr(photoLoc)); + } return result; } diff --git a/Telegram/SourceFiles/ui/images.h b/Telegram/SourceFiles/ui/images.h index c9246c32e4..7e1be656d5 100644 --- a/Telegram/SourceFiles/ui/images.h +++ b/Telegram/SourceFiles/ui/images.h @@ -115,6 +115,30 @@ public: return _secret; } + static StorageImageLocation FromMTP( + int32 width, + int32 height, + const MTPFileLocation &location) { + if (location.type() == mtpc_fileLocation) { + const auto &data = location.c_fileLocation(); + return StorageImageLocation(width, height, data); + } + return StorageImageLocation(width, height, 0, 0, 0, 0); + } + static StorageImageLocation FromMTP(const MTPPhotoSize &size) { + switch (size.type()) { + case mtpc_photoSize: { + const auto &data = size.c_photoSize(); + return FromMTP(data.vw.v, data.vh.v, data.vlocation); + } break; + case mtpc_photoCachedSize: { + const auto &data = size.c_photoCachedSize(); + return FromMTP(data.vw.v, data.vh.v, data.vlocation); + } break; + } + return StorageImageLocation(); + } + static StorageImageLocation Null; private: diff --git a/Telegram/SourceFiles/ui/special_buttons.cpp b/Telegram/SourceFiles/ui/special_buttons.cpp index c3d0130a06..fa386e25ca 100644 --- a/Telegram/SourceFiles/ui/special_buttons.cpp +++ b/Telegram/SourceFiles/ui/special_buttons.cpp @@ -483,11 +483,11 @@ void UserpicButton::openPeerPhoto() { return; } - auto id = _peer->photoId; - if (!id || id == UnknownPeerPhotoId) { + const auto id = _peer->userpicPhotoId(); + if (!id) { return; } - auto photo = App::photo(id); + const auto photo = App::photo(id); if (photo->date) { Messenger::Instance().showPhoto(photo, _peer); } @@ -632,18 +632,15 @@ QPoint UserpicButton::prepareRippleStartPosition() const { void UserpicButton::processPeerPhoto() { Expects(_peer != nullptr); - auto hasPhoto = (_peer->photoId - && _peer->photoId != UnknownPeerPhotoId); _waiting = !_peer->userpicLoaded(); if (_waiting) { _peer->loadUserpic(true); } if (_role == Role::OpenPhoto) { - auto id = _peer->photoId; - if (id == UnknownPeerPhotoId) { + if (_peer->userpicPhotoUnknown()) { _peer->updateFullForced(); } - _canOpenPhoto = (id != 0 && id != UnknownPeerPhotoId); + _canOpenPhoto = (_peer->userpicPhotoId() != 0); updateCursor(); } }