From f5164fe3e4095e41c461bc639e9bbd8c7215b634 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 7 Apr 2022 01:41:26 +0300 Subject: [PATCH] Added ability to use previous peer userpics. --- Telegram/Resources/langs/lang.strings | 1 + Telegram/SourceFiles/api/api_peer_photo.cpp | 31 ++++++++++++++ Telegram/SourceFiles/api/api_peer_photo.h | 1 + .../media/view/media_view_overlay_widget.cpp | 42 +++++++++++++++++++ Telegram/SourceFiles/ui/menu_icons.style | 1 + 5 files changed, 76 insertions(+) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index e8a2bbbe1..a0bf8bbde 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2043,6 +2043,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_mediaview_today" = "today at {time}"; "lng_mediaview_yesterday" = "yesterday at {time}"; "lng_mediaview_date_time" = "{date} at {time}"; +"lng_mediaview_set_userpic" = "Set as Main"; "lng_mediaview_saved_to" = "Image was saved to your {downloads} folder"; "lng_mediaview_downloads" = "Downloads"; diff --git a/Telegram/SourceFiles/api/api_peer_photo.cpp b/Telegram/SourceFiles/api/api_peer_photo.cpp index 97456fdd6..46b8cb980 100644 --- a/Telegram/SourceFiles/api/api_peer_photo.cpp +++ b/Telegram/SourceFiles/api/api_peer_photo.cpp @@ -165,6 +165,37 @@ void PeerPhoto::clear(not_null photo) { } } +void PeerPhoto::set(not_null peer, not_null photo) { + if (peer->userpicPhotoId() == photo->id) { + return; + } + if (peer == _session->user()) { + _api.request(MTPphotos_UpdateProfilePhoto( + photo->mtpInput() + )).done([=](const MTPphotos_Photo &result) { + result.match([&](const MTPDphotos_photo &data) { + _session->data().processPhoto(data.vphoto()); + _session->data().processUsers(data.vusers()); + }); + }).send(); + } else { + const auto applier = [=](const MTPUpdates &result) { + _session->updates().applyUpdates(result); + }; + if (const auto chat = peer->asChat()) { + _api.request(MTPmessages_EditChatPhoto( + chat->inputChat, + MTP_inputChatPhoto(photo->mtpInput()) + )).done(applier).send(); + } else if (const auto channel = peer->asChannel()) { + _api.request(MTPchannels_EditPhoto( + channel->inputChannel, + MTP_inputChatPhoto(photo->mtpInput()) + )).done(applier).send(); + } + } +} + void PeerPhoto::ready(const FullMsgId &msgId, const MTPInputFile &file) { const auto maybePeer = _uploads.take(msgId); if (!maybePeer) { diff --git a/Telegram/SourceFiles/api/api_peer_photo.h b/Telegram/SourceFiles/api/api_peer_photo.h index 795b498b8..f0050e18f 100644 --- a/Telegram/SourceFiles/api/api_peer_photo.h +++ b/Telegram/SourceFiles/api/api_peer_photo.h @@ -24,6 +24,7 @@ public: void upload(not_null peer, QImage &&image); void clear(not_null photo); + void set(not_null peer, not_null photo); private: void ready(const FullMsgId &msgId, const MTPInputFile &file); diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 24c560bba..8a3ca3d2f 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -46,6 +46,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/media/history_view_media.h" #include "data/data_media_types.h" #include "data/data_session.h" +#include "data/data_changes.h" #include "data/data_channel.h" #include "data/data_chat.h" #include "data/data_user.h" @@ -995,6 +996,47 @@ void OverlayWidget::fillContextMenuActions(const MenuCallback &addAction) { [=] { showMediaOverview(); }, &st::mediaMenuIconShowAll); } + [&] { // Set userpic. + if (!_peer || !_photo || (_peer->userpicPhotoId() == _photo->id)) { + return; + } + using Type = SharedMediaType; + if (sharedMediaType().value_or(Type::File) == Type::ChatPhoto) { + if (const auto chat = _peer->asChat()) { + if (!chat->canEditInformation()) { + return; + } + } else if (const auto channel = _peer->asChannel()) { + if (!channel->canEditInformation()) { + return; + } + } else { + return; + } + } else if (userPhotosKey()) { + if (_user != _user->session().user()) { + return; + } + } else { + return; + } + const auto photo = _photo; + const auto peer = _peer; + addAction(tr::lng_mediaview_set_userpic(tr::now), [=] { + auto lifetime = std::make_shared(); + peer->session().changes().peerFlagsValue( + peer, + Data::PeerUpdate::Flag::Photo + ) | rpl::start_with_next([=]() mutable { + if (lifetime) { + base::take(lifetime)->destroy(); + } + close(); + }, *lifetime); + + peer->session().api().peerPhoto().set(peer, photo); + }, &st::mediaMenuIconProfile); + }(); } auto OverlayWidget::computeOverviewType() const diff --git a/Telegram/SourceFiles/ui/menu_icons.style b/Telegram/SourceFiles/ui/menu_icons.style index 3636500ee..a1b3648ed 100644 --- a/Telegram/SourceFiles/ui/menu_icons.style +++ b/Telegram/SourceFiles/ui/menu_icons.style @@ -129,6 +129,7 @@ mediaMenuIconCopy: icon {{ "menu/copy", mediaviewMenuFg }}; mediaMenuIconForward: icon {{ "menu/forward", mediaviewMenuFg }}; mediaMenuIconDelete: icon {{ "menu/delete", mediaviewMenuFg }}; mediaMenuIconShowAll: icon {{ "menu/all_media", mediaviewMenuFg }}; +mediaMenuIconProfile: icon {{ "menu/profile", mediaviewMenuFg }}; menuIconStartStream: icon {{ "menu/start_stream", menuIconColor }}; menuIconStartStreamWith: icon {{ "menu/start_stream_with", menuIconColor }};