Added initial implementation of fallback photo management in settings.

This commit is contained in:
23rd 2022-12-19 17:36:02 +03:00 committed by John Preston
parent a4d3c694bc
commit 4c181b6d08
8 changed files with 281 additions and 48 deletions

View File

@ -1039,6 +1039,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_edit_privacy_forwards_always_empty" = "Always allow";
"lng_edit_privacy_forwards_never_empty" = "Never allow";
"lng_edit_privacy_forwards_exceptions" = "These settings will override the values above.";
"lng_edit_privacy_forwards_exceptions_everyone" = "You can add users or entire groups which will not see your profile photo.";
"lng_edit_privacy_forwards_exceptions_nobody" = "Add users or entire groups which will still see your profile photo.";
"lng_edit_privacy_forwards_always_title" = "Always allow";
"lng_edit_privacy_forwards_never_title" = "Never allow";
"lng_edit_privacy_forwards_sample_message" = "Reinhardt, we need to find you some new tunes 🎶";
@ -1053,6 +1055,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_edit_privacy_profile_photo_exceptions" = "These settings will override the values above.";
"lng_edit_privacy_profile_photo_always_title" = "Always allow";
"lng_edit_privacy_profile_photo_never_title" = "Never allow";
"lng_edit_privacy_profile_photo_public_set" = "Set Public Photo";
"lng_edit_privacy_profile_photo_public_update" = "Update Public Photo";
"lng_edit_privacy_profile_photo_public_remove" = "Remove Public Photo";
"lng_edit_privacy_profile_photo_public_about" = "You can upload a public photo for those who are restricted from viewing your real profile photo.";
"lng_edit_privacy_profile_photo_public_toast" = "This photo is now set for those who are restricted from viewing your main photo.";
"lng_edit_privacy_voices_title" = "Voice messages settings";
"lng_edit_privacy_voices_header" = "Who can send me voice messages";
@ -1183,6 +1190,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_profile_info_section" = "Info";
"lng_info_tab_media" = "Media";
"lng_info_public_photo" = "public photo";
"lng_info_mobile_label" = "Mobile";
"lng_info_mobile_hidden" = "Hidden";
"lng_info_username_label" = "Username";
@ -2468,6 +2476,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_mediaview_group_photo" = "Group Photo";
"lng_mediaview_channel_photo" = "Channel Photo";
"lng_mediaview_profile_photo" = "Profile Photo";
"lng_mediaview_profile_public_photo" = "Public Photo";
"lng_mediaview_file_n_of_amount" = "{file} {n} of {amount}";
"lng_mediaview_n_of_amount" = "Photo {n} of {amount}";
"lng_mediaview_doc_image" = "File";

View File

@ -276,6 +276,11 @@ void EditPrivacyBox::setupContent() {
? tr::lng_edit_privacy_exceptions_count(tr::now, lt_count, count)
: tr::lng_edit_privacy_exceptions_add(tr::now);
});
_controller->handleExceptionsChange(
exception,
update->events_starting_with({}) | rpl::map([=] {
return Settings::ExceptionUsersCount(exceptions(exception));
}));
auto text = _controller->exceptionButtonTextKey(exception);
const auto always = (exception == Exception::Always);
const auto button = content->add(

View File

@ -57,6 +57,10 @@ public:
Exception exception) const = 0;
[[nodiscard]] virtual auto exceptionsDescription()
const -> rpl::producer<QString> = 0;
virtual void handleExceptionsChange(
Exception exception,
rpl::producer<int> value) {
}
[[nodiscard]] virtual object_ptr<Ui::RpWidget> setupAboveWidget(
not_null<QWidget*> parent,
@ -72,7 +76,7 @@ public:
}
[[nodiscard]] virtual object_ptr<Ui::RpWidget> setupBelowWidget(
not_null<Window::SessionController*> controller,
not_null<QWidget*> parent) const {
not_null<QWidget*> parent) {
return { nullptr };
}

View File

@ -4930,16 +4930,24 @@ void OverlayWidget::updateHeader() {
lt_amount,
QString::number(count));
} else {
_headerText = tr::lng_mediaview_n_of_amount(
tr::now,
lt_n,
QString::number(index + 1),
lt_amount,
QString::number(count));
if (_user
&& (index == count - 1)
&& SyncUserFallbackPhotoViewer(_user)) {
_headerText = tr::lng_mediaview_profile_public_photo(tr::now);
} else {
_headerText = tr::lng_mediaview_n_of_amount(
tr::now,
lt_n,
QString::number(index + 1),
lt_amount,
QString::number(count));
}
}
} else {
if (_document) {
_headerText = _document->filename().isEmpty() ? tr::lng_mediaview_doc_image(tr::now) : _document->filename();
_headerText = _document->filename().isEmpty()
? tr::lng_mediaview_doc_image(tr::now)
: _document->filename();
} else if (_message) {
_headerText = tr::lng_mediaview_single_photo(tr::now);
} else if (_user) {

View File

@ -15,6 +15,10 @@ settingsButton: SettingsButton(infoProfileButton) {
padding: margins(60px, 10px, 22px, 10px);
iconLeft: 20px;
}
settingsButtonLight: SettingsButton(settingsButton) {
textFg: lightButtonFg;
textFgOver: lightButtonFgOver;
}
settingsButtonNoIcon: SettingsButton(settingsButton) {
padding: margins(22px, 10px, 22px, 8px);
}
@ -24,6 +28,10 @@ settingsAttentionButton: SettingsButton(settingsButtonNoIcon) {
textFg: attentionButtonFg;
textFgOver: attentionButtonFgOver;
}
settingsAttentionButtonWithIcon: SettingsButton(settingsButton) {
textFg: attentionButtonFg;
textFgOver: attentionButtonFgOver;
}
settingsOptionDisabled: SettingsButton(settingsButtonNoIcon) {
textFg: windowSubTextFg;
textFgOver: windowSubTextFg;
@ -94,6 +102,7 @@ settingsIconDownload: icon {{ "settings/download", settingsIconFg }};
settingsIconMention: icon {{ "settings/mention", settingsIconFg }};
settingsIconTopics: icon {{ "settings/topics", settingsIconFg }};
settingsIconTTL: icon {{ "settings/ttl", settingsIconFg }};
settingsIconPhoto: icon {{ "settings/photo", settingsIconFg }};
settingsPremiumIconChannelsOff: icon {{ "settings/premium/channels_off", settingsIconFg }};
settingsPremiumIconDouble: icon {{ "settings/premium/double", settingsIconFg }};

View File

@ -7,45 +7,55 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "settings/settings_privacy_controllers.h"
#include "settings/settings_common.h"
#include "lang/lang_keys.h"
#include "api/api_peer_photo.h"
#include "apiwrap.h"
#include "main/main_session.h"
#include "data/data_user.h"
#include "data/data_session.h"
#include "data/data_changes.h"
#include "data/data_peer_values.h" // Data::AmPremiumValue.
#include "base/call_delayed.h"
#include "base/event_filter.h"
#include "base/unixtime.h"
#include "boxes/peer_list_controllers.h"
#include "boxes/peers/peer_short_info_box.h"
#include "boxes/peers/prepare_short_info_box.h"
#include "calls/calls_instance.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "data/data_changes.h"
#include "data/data_file_origin.h"
#include "data/data_peer_values.h" // Data::AmPremiumValue.
#include "data/data_photo_media.h"
#include "data/data_session.h"
#include "data/data_user.h"
#include "data/data_user_photos.h" // UserPhotosViewer.
#include "editor/photo_editor_layer_widget.h"
#include "history/admin_log/history_admin_log_item.h"
#include "history/history.h"
#include "history/history_item.h"
#include "history/history_item_components.h"
#include "history/view/history_view_element.h"
#include "history/view/history_view_message.h"
#include "history/history_item_components.h"
#include "history/history_item.h"
#include "history/history.h"
#include "calls/calls_instance.h"
#include "base/unixtime.h"
#include "base/event_filter.h"
#include "ui/chat/chat_theme.h"
#include "ui/chat/chat_style.h"
#include "ui/widgets/checkbox.h"
#include "ui/wrap/padding_wrap.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/slide_wrap.h"
#include "ui/image/image_prepare.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "settings/settings_common.h"
#include "settings/settings_privacy_security.h"
#include "ui/boxes/confirm_box.h"
#include "ui/cached_round_corners.h"
#include "ui/chat/chat_style.h"
#include "ui/chat/chat_theme.h"
#include "ui/image/image_prepare.h"
#include "ui/image/image_prepare.h"
#include "ui/painter.h"
#include "ui/text/format_values.h" // Ui::FormatPhone
#include "ui/text/text_utilities.h"
#include "ui/painter.h"
#include "ui/widgets/checkbox.h"
#include "ui/wrap/padding_wrap.h"
#include "ui/wrap/slide_wrap.h"
#include "ui/wrap/vertical_layout.h"
#include "window/section_widget.h"
#include "window/window_controller.h"
#include "window/window_session_controller.h"
#include "boxes/peer_list_controllers.h"
#include "ui/boxes/confirm_box.h"
#include "settings/settings_privacy_security.h"
#include "styles/style_chat.h"
#include "styles/style_boxes.h"
#include "styles/style_settings.h"
#include "styles/style_info.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QClipboard>
@ -744,7 +754,7 @@ auto CallsPrivacyController::exceptionsDescription() const
object_ptr<Ui::RpWidget> CallsPrivacyController::setupBelowWidget(
not_null<Window::SessionController*> controller,
not_null<QWidget*> parent) const {
not_null<QWidget*> parent) {
auto result = object_ptr<Ui::VerticalLayout>(parent);
const auto content = result.data();
@ -1018,14 +1028,160 @@ rpl::producer<QString> ProfilePhotoPrivacyController::title() const {
return tr::lng_edit_privacy_profile_photo_title();
}
bool ProfilePhotoPrivacyController::hasOption(Option option) const {
return (option != Option::Nobody);
}
rpl::producer<QString> ProfilePhotoPrivacyController::optionsTitleKey() const {
return tr::lng_edit_privacy_profile_photo_header();
}
object_ptr<Ui::RpWidget> ProfilePhotoPrivacyController::setupAboveWidget(
not_null<QWidget*> parent,
rpl::producer<Option> optionValue,
not_null<QWidget*> outerContainer) {
_option = std::move(optionValue);
return nullptr;
}
object_ptr<Ui::RpWidget> ProfilePhotoPrivacyController::setupBelowWidget(
not_null<Window::SessionController*> controller,
not_null<QWidget*> parent) {
const auto self = controller->session().user();
auto widget = object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
parent,
object_ptr<Ui::VerticalLayout>(parent));
const auto container = widget->entity();
struct State {
void updatePhoto(QImage &&image, bool local) {
auto result = image.scaled(
userpicSize * style::DevicePixelRatio(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
result = Images::Round(
std::move(result),
ImageRoundRadius::Ellipse);
result.setDevicePixelRatio(style::DevicePixelRatio());
(local ? localPhoto : photo) = std::move(result);
if (local) {
localOriginal = std::move(image);
}
hasPhoto.fire(!localPhoto.isNull() || !photo.isNull());
}
rpl::event_stream<bool> hasPhoto;
rpl::variable<bool> hiddenByUser = false;
rpl::variable<QString> setUserpicButtonText;
QSize userpicSize;
QImage photo;
QImage localPhoto;
QImage localOriginal;
};
const auto state = container->lifetime().make_state<State>();
state->userpicSize = QSize(
st::inviteLinkUserpics.size,
st::inviteLinkUserpics.size);
AddSkip(container);
const auto setUserpicButton = AddButton(
container,
state->setUserpicButtonText.value(),
st::settingsButtonLight,
{ &st::settingsIconPhoto, kIconLightBlue });
const auto &stRemoveButton = st::settingsAttentionButtonWithIcon;
const auto removeButton = container->add(
object_ptr<Ui::SlideWrap<Ui::SettingsButton>>(
container,
object_ptr<Ui::SettingsButton>(
parent,
tr::lng_edit_privacy_profile_photo_public_remove(),
stRemoveButton)));
AddSkip(container);
AddDividerText(
container,
tr::lng_edit_privacy_profile_photo_public_about());
const auto userpic = Ui::CreateChild<Ui::RpWidget>(
removeButton->entity());
userpic->resize(state->userpicSize);
userpic->paintRequest(
) | rpl::start_with_next([=](const QRect &r) {
auto p = QPainter(userpic);
p.fillRect(r, Qt::transparent);
if (!state->localPhoto.isNull()) {
p.drawImage(0, 0, state->localPhoto);
} else if (!state->photo.isNull()) {
p.drawImage(0, 0, state->photo);
}
}, userpic->lifetime());
removeButton->entity()->heightValue(
) | rpl::start_with_next([=,
left = stRemoveButton.iconLeft,
width = st::settingsIconPhoto.width()](int height) {
userpic->moveToLeft(
left + (width - userpic->width()) / 2,
(height - userpic->height()) / 2);
}, userpic->lifetime());
removeButton->toggleOn(rpl::combine(
state->hasPhoto.events_starting_with(false),
state->hiddenByUser.value()
) | rpl::map(rpl::mappers::_1 && !rpl::mappers::_2));
(
PrepareShortInfoFallbackUserpic(self, st::shortInfoCover).value
) | rpl::start_with_next([=](PeerShortInfoUserpic info) {
state->updatePhoto(base::take(info.photo), false);
userpic->update();
}, userpic->lifetime());
setUserpicButton->setClickedCallback([=] {
base::call_delayed(
st::settingsButton.ripple.hideDuration,
crl::guard(container, [=] {
Editor::PrepareProfilePhotoFromFile(
container,
&controller->window(),
ImageRoundRadius::Ellipse,
[=](QImage &&image) {
state->updatePhoto(std::move(image), true);
state->hiddenByUser = false;
userpic->update();
});
}));
});
removeButton->entity()->setClickedCallback([=] {
state->hiddenByUser = true;
});
state->setUserpicButtonText = removeButton->toggledValue(
) | rpl::map([](bool toggled) {
return !toggled
? tr::lng_edit_privacy_profile_photo_public_set()
: tr::lng_edit_privacy_profile_photo_public_update();
}) | rpl::flatten_latest();
_saveAdditional = [=] {
if (removeButton->isHidden()) {
const auto photoId = SyncUserFallbackPhotoViewer(self);
if (const auto photo = self->owner().photo(*photoId)) {
controller->session().api().peerPhoto().clear(photo);
}
} else if (!state->localOriginal.isNull()) {
controller->session().api().peerPhoto().uploadFallback(
self,
base::take(state->localOriginal));
}
};
widget->toggleOn(rpl::combine(
_option.value(),
_exceptionsNever.value()
) | rpl::map(rpl::mappers::_1 != Option::Everyone || rpl::mappers::_2));
return widget;
}
void ProfilePhotoPrivacyController::saveAdditional() {
if (_saveAdditional) {
_saveAdditional();
}
}
rpl::producer<QString> ProfilePhotoPrivacyController::exceptionButtonTextKey(
Exception exception) const {
switch (exception) {
@ -1054,7 +1210,30 @@ rpl::producer<QString> ProfilePhotoPrivacyController::exceptionBoxTitle(
auto ProfilePhotoPrivacyController::exceptionsDescription() const
-> rpl::producer<QString> {
return tr::lng_edit_privacy_profile_photo_exceptions();
return _option.value(
) | rpl::map([](Option option) {
switch (option) {
case Option::Everyone: {
return tr::lng_edit_privacy_forwards_exceptions_everyone();
};
case Option::Contacts: {
return tr::lng_edit_privacy_forwards_exceptions();
};
case Option::Nobody: {
return tr::lng_edit_privacy_forwards_exceptions_nobody();
};
}
Unexpected("Option value in exceptionsDescription.");
}) | rpl::flatten_latest();
}
void ProfilePhotoPrivacyController::handleExceptionsChange(
Exception exception,
rpl::producer<int> value) {
if (exception == Exception::Never) {
_exceptionsNever = std::move(value);
}
}
VoicesPrivacyController::VoicesPrivacyController(

View File

@ -153,7 +153,7 @@ public:
object_ptr<Ui::RpWidget> setupBelowWidget(
not_null<Window::SessionController*> controller,
not_null<QWidget*> parent) const override;
not_null<QWidget*> parent) override;
};
@ -220,7 +220,6 @@ public:
Key key() const override;
rpl::producer<QString> title() const override;
bool hasOption(Option option) const override;
rpl::producer<QString> optionsTitleKey() const override;
rpl::producer<QString> exceptionButtonTextKey(
Exception exception) const override;
@ -228,6 +227,26 @@ public:
Exception exception) const override;
rpl::producer<QString> exceptionsDescription() const override;
void handleExceptionsChange(
Exception exception,
rpl::producer<int> value) override;
object_ptr<Ui::RpWidget> setupAboveWidget(
not_null<QWidget*> parent,
rpl::producer<Option> optionValue,
not_null<QWidget*> outerContainer) override;
object_ptr<Ui::RpWidget> setupBelowWidget(
not_null<Window::SessionController*> controller,
not_null<QWidget*> parent) override;
void saveAdditional() override;
private:
Fn<void()> _saveAdditional;
rpl::variable<Option> _option;
rpl::variable<int> _exceptionsNever;
};
class VoicesPrivacyController final : public EditPrivacyController {

View File

@ -254,14 +254,14 @@ void UserpicButton::choosePhotoLocally() {
base::call_delayed(
_st.changeButton.ripple.hideDuration,
crl::guard(this, [=] {
Editor::PrepareProfilePhotoFromFile(
this,
_window,
((_peer && _peer->isForum())
? ImageRoundRadius::Large
: ImageRoundRadius::Ellipse),
callback(type));
}));
Editor::PrepareProfilePhotoFromFile(
this,
_window,
((_peer && _peer->isForum())
? ImageRoundRadius::Large
: ImageRoundRadius::Ellipse),
callback(type));
}));
};
if (!IsCameraAvailable()) {
chooseFile();