From 1dc31c7f2f7cc19569209c62a1b4efe4b2060cce Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 30 Jun 2020 22:11:48 +0400 Subject: [PATCH] Allow turning off inactive accounts notifications. --- Telegram/Resources/langs/lang.strings | 3 ++ Telegram/SourceFiles/core/core_settings.cpp | 9 +++- Telegram/SourceFiles/core/core_settings.h | 7 ++++ .../settings/settings_notifications.cpp | 41 +++++++++++++++++++ .../window/notifications_manager.cpp | 3 ++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index acc9ba08df..7787db0a05 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -292,6 +292,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_settings_empty_bio" = "None"; "lng_settings_section_notify" = "Notifications"; +"lng_settings_show_from" = "Show notifications from"; +"lng_settings_notify_all" = "All accounts"; +"lng_settings_notify_all_about" = "Turn this off if you want to receive notifications only from the account you are currently using."; "lng_settings_notify_title" = "Notifications for chats"; "lng_settings_desktop_notify" = "Desktop notifications"; "lng_settings_show_name" = "Show sender's name"; diff --git a/Telegram/SourceFiles/core/core_settings.cpp b/Telegram/SourceFiles/core/core_settings.cpp index 41be395dfb..5e7cb62b56 100644 --- a/Telegram/SourceFiles/core/core_settings.cpp +++ b/Telegram/SourceFiles/core/core_settings.cpp @@ -104,7 +104,8 @@ QByteArray Settings::serialize() const { 0, 1000000)) << qint32(_thirdColumnWidth.current()) - << qint32(_thirdSectionExtendedBy); + << qint32(_thirdSectionExtendedBy) + << qint32(_notifyFromAll ? 1 : 0); } return result; } @@ -167,6 +168,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) { float64 dialogsWidthRatio = _dialogsWidthRatio.current(); qint32 thirdColumnWidth = _thirdColumnWidth.current(); qint32 thirdSectionExtendedBy = _thirdSectionExtendedBy; + qint32 notifyFromAll = _notifyFromAll ? 1 : 0; stream >> themesAccentColors; if (!stream.atEnd()) { @@ -237,7 +239,8 @@ void Settings::addFromSerialized(const QByteArray &serialized) { >> thirdSectionInfoEnabled >> dialogsWidthRatioInt >> thirdColumnWidth - >> thirdSectionExtendedBy; + >> thirdSectionExtendedBy + >> notifyFromAll; dialogsWidthRatio = snap(dialogsWidthRatioInt / 1000000., 0., 1.); } if (stream.status() != QDataStream::Ok) { @@ -331,6 +334,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) { if (_thirdSectionInfoEnabled) { _tabbedSelectorSectionEnabled = false; } + _notifyFromAll = (notifyFromAll == 1); } bool Settings::chatWide() const { @@ -462,6 +466,7 @@ void Settings::resetOnLastLogout() { _thirdSectionExtendedBy = -1; // per-window _dialogsWidthRatio = DefaultDialogsWidthRatio(); // per-window _thirdColumnWidth = kDefaultThirdColumnWidth; // p-w + _notifyFromAll = true; _tabbedReplacedWithInfo = false; // per-window } diff --git a/Telegram/SourceFiles/core/core_settings.h b/Telegram/SourceFiles/core/core_settings.h index 907e37fd5a..cedbe6a94b 100644 --- a/Telegram/SourceFiles/core/core_settings.h +++ b/Telegram/SourceFiles/core/core_settings.h @@ -401,6 +401,12 @@ public: void setThirdColumnWidth(int width); [[nodiscard]] int thirdColumnWidth() const; [[nodiscard]] rpl::producer thirdColumnWidthChanges() const; + void setNotifyFromAll(bool value) { + _notifyFromAll = value; + } + [[nodiscard]] bool notifyFromAll() const { + return _notifyFromAll; + } [[nodiscard]] static bool ThirdColumnByDefault(); [[nodiscard]] float64 DefaultDialogsWidthRatio(); @@ -467,6 +473,7 @@ private: int _thirdSectionExtendedBy = -1; // per-window rpl::variable _dialogsWidthRatio; // per-window rpl::variable _thirdColumnWidth = kDefaultThirdColumnWidth; // p-w + bool _notifyFromAll = true; bool _tabbedReplacedWithInfo = false; // per-window rpl::event_stream _tabbedReplacedWithInfoValue; // per-window diff --git a/Telegram/SourceFiles/settings/settings_notifications.cpp b/Telegram/SourceFiles/settings/settings_notifications.cpp index 2608d57370..daf4d1fcd9 100644 --- a/Telegram/SourceFiles/settings/settings_notifications.cpp +++ b/Telegram/SourceFiles/settings/settings_notifications.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwindow.h" #include "core/application.h" #include "main/main_session.h" +#include "main/main_account.h" #include "main/main_domain.h" #include "apiwrap.h" #include "facades.h" @@ -540,9 +541,49 @@ void SetupAdvancedNotifications( AddSkip(container, st::settingsCheckboxesSkip); } +void SetupMultiAccountNotifications( + not_null controller, + not_null container) { + if (Core::App().domain().accounts().size() < 2) { + return; + } + AddSubsectionTitle(container, tr::lng_settings_show_from()); + + const auto fromAll = container->add( + object_ptr( + container, + tr::lng_settings_notify_all(tr::now), + Core::App().settings().notifyFromAll(), + st::settingsCheckbox), + st::settingsCheckboxPadding); + fromAll->checkedChanges( + ) | rpl::filter([](bool checked) { + return (checked != Core::App().settings().notifyFromAll()); + }) | rpl::start_with_next([=](bool checked) { + Core::App().settings().setNotifyFromAll(checked); + Core::App().saveSettingsDelayed(); + if (!checked) { + auto ¬ifications = Core::App().notifications(); + const auto &list = Core::App().domain().accounts(); + for (const auto &[index, account] : list) { + if (account.get() == &Core::App().domain().active()) { + continue; + } else if (const auto session = account->maybeSession()) { + notifications.clearFromSession(session); + } + } + } + }, fromAll->lifetime()); + + AddSkip(container); + AddDividerText(container, tr::lng_settings_notify_all_about()); +} + void SetupNotificationsContent( not_null controller, not_null container) { + SetupMultiAccountNotifications(controller, container); + AddSubsectionTitle(container, tr::lng_settings_notify_title()); const auto session = &controller->session(); diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index f9f78850f8..e6d7725067 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -89,6 +89,9 @@ System::SkipState System::skipNotification( const auto notifyBy = item->specialNotificationPeer(); if (App::quitting() || !history->currentNotification()) { return { SkipState::Skip }; + } else if (!Core::App().settings().notifyFromAll() + && &history->session().account() != &Core::App().domain().active()) { + return { SkipState::Skip }; } const auto scheduled = item->out() && item->isFromScheduled();