Moved out public api for notify settings from Data::Session.

This commit is contained in:
23rd 2022-04-01 13:36:33 +03:00 committed by John Preston
parent 36bb23c54c
commit 9478798a47
13 changed files with 113 additions and 114 deletions

View file

@ -3798,66 +3798,6 @@ auto Session::dialogsRowReplacements() const
return _dialogsRowReplacements.events();
}
bool Session::notifyIsMuted(not_null<const PeerData*> peer) const {
return notifySettings().notifyIsMuted(peer, nullptr);
}
bool Session::notifySilentPosts(not_null<const PeerData*> peer) const {
if (const auto silent = peer->notifySilentPosts()) {
return *silent;
}
const auto &settings = notifySettings().defaultNotifySettings(peer);
if (const auto silent = settings.silentPosts()) {
return *silent;
}
return false;
}
bool Session::notifySoundIsNone(not_null<const PeerData*> peer) const {
if (const auto soundIsNone = peer->notifySoundIsNone()) {
return *soundIsNone;
}
const auto &settings = notifySettings().defaultNotifySettings(peer);
if (const auto soundIsNone = settings.soundIsNone()) {
return *soundIsNone;
}
return false;
}
bool Session::notifyMuteUnknown(not_null<const PeerData*> peer) const {
if (peer->notifySettingsUnknown()) {
return true;
} else if (const auto nonDefault = peer->notifyMuteUntil()) {
return false;
}
return notifySettings().defaultNotifySettings(peer).settingsUnknown();
}
bool Session::notifySilentPostsUnknown(
not_null<const PeerData*> peer) const {
if (peer->notifySettingsUnknown()) {
return true;
} else if (const auto nonDefault = peer->notifySilentPosts()) {
return false;
}
return notifySettings().defaultNotifySettings(peer).settingsUnknown();
}
bool Session::notifySoundIsNoneUnknown(not_null<const PeerData*> peer) const {
if (peer->notifySettingsUnknown()) {
return true;
} else if (const auto nonDefault = peer->notifySoundIsNone()) {
return false;
}
return notifySettings().defaultNotifySettings(peer).settingsUnknown();
}
bool Session::notifySettingsUnknown(not_null<const PeerData*> peer) const {
return notifyMuteUnknown(peer)
|| notifySilentPostsUnknown(peer)
|| notifySoundIsNoneUnknown(peer);
}
void Session::serviceNotification(
const TextWithEntities &message,
const MTPMessageMedia &media) {

View file

@ -662,14 +662,6 @@ public:
void dialogsRowReplaced(DialogsRowReplacement replacement);
rpl::producer<DialogsRowReplacement> dialogsRowReplacements() const;
bool notifyIsMuted(not_null<const PeerData*> peer) const;
bool notifySilentPosts(not_null<const PeerData*> peer) const;
bool notifySoundIsNone(not_null<const PeerData*> peer) const;
bool notifyMuteUnknown(not_null<const PeerData*> peer) const;
bool notifySilentPostsUnknown(not_null<const PeerData*> peer) const;
bool notifySoundIsNoneUnknown(not_null<const PeerData*> peer) const;
bool notifySettingsUnknown(not_null<const PeerData*> peer) const;
void serviceNotification(
const TextWithEntities &message,
const MTPMessageMedia &media = MTP_messageMediaEmpty());

View file

@ -130,16 +130,6 @@ void NotifySettings::resetNotifySettingsToDefault(not_null<PeerData*> peer) {
}
}
PeerNotifySettings &NotifySettings::defaultNotifySettings(
not_null<const PeerData*> peer) {
return peer->isUser()
? _defaultUserNotifySettings
: (peer->isChat() || peer->isMegagroup())
? _defaultChatNotifySettings
: _defaultBroadcastNotifySettings;
}
const PeerNotifySettings &NotifySettings::defaultNotifySettings(
not_null<const PeerData*> peer) const {
return peer->isUser()
@ -152,7 +142,7 @@ const PeerNotifySettings &NotifySettings::defaultNotifySettings(
void NotifySettings::updateNotifySettingsLocal(not_null<PeerData*> peer) {
const auto history = _owner->historyLoaded(peer->id);
auto changesIn = crl::time(0);
const auto muted = notifyIsMuted(peer, &changesIn);
const auto muted = isMuted(peer, &changesIn);
if (history && history->changeMute(muted)) {
// Notification already sent.
} else {
@ -185,7 +175,7 @@ void NotifySettings::unmuteByFinished() {
for (auto i = begin(_mutedPeers); i != end(_mutedPeers);) {
const auto history = _owner->historyLoaded((*i)->id);
auto changesIn = crl::time(0);
const auto muted = notifyIsMuted(*i, &changesIn);
const auto muted = isMuted(*i, &changesIn);
if (muted) {
if (history) {
history->changeMute(true);
@ -206,7 +196,7 @@ void NotifySettings::unmuteByFinished() {
}
}
bool NotifySettings::notifyIsMuted(
bool NotifySettings::isMuted(
not_null<const PeerData*> peer,
crl::time *changesIn) const {
const auto resultFromUntil = [&](TimeId until) {
@ -229,6 +219,67 @@ bool NotifySettings::notifyIsMuted(
return true;
}
bool NotifySettings::isMuted(not_null<const PeerData*> peer) const {
return isMuted(peer, nullptr);
}
bool NotifySettings::silentPosts(not_null<const PeerData*> peer) const {
if (const auto silent = peer->notifySilentPosts()) {
return *silent;
}
const auto &settings = defaultNotifySettings(peer);
if (const auto silent = settings.silentPosts()) {
return *silent;
}
return false;
}
bool NotifySettings::soundIsNone(not_null<const PeerData*> peer) const {
if (const auto soundIsNone = peer->notifySoundIsNone()) {
return *soundIsNone;
}
const auto &settings = defaultNotifySettings(peer);
if (const auto soundIsNone = settings.soundIsNone()) {
return *soundIsNone;
}
return false;
}
bool NotifySettings::muteUnknown(not_null<const PeerData*> peer) const {
if (peer->notifySettingsUnknown()) {
return true;
} else if (const auto nonDefault = peer->notifyMuteUntil()) {
return false;
}
return defaultNotifySettings(peer).settingsUnknown();
}
bool NotifySettings::silentPostsUnknown(
not_null<const PeerData*> peer) const {
if (peer->notifySettingsUnknown()) {
return true;
} else if (const auto nonDefault = peer->notifySilentPosts()) {
return false;
}
return defaultNotifySettings(peer).settingsUnknown();
}
bool NotifySettings::soundIsNoneUnknown(
not_null<const PeerData*> peer) const {
if (peer->notifySettingsUnknown()) {
return true;
} else if (const auto nonDefault = peer->notifySoundIsNone()) {
return false;
}
return defaultNotifySettings(peer).settingsUnknown();
}
bool NotifySettings::settingsUnknown(not_null<const PeerData*> peer) const {
return muteUnknown(peer)
|| silentPostsUnknown(peer)
|| soundIsNoneUnknown(peer);
}
rpl::producer<> NotifySettings::defaultUserNotifyUpdates() const {
return _defaultUserNotifyUpdates.events();
}

View file

@ -38,15 +38,24 @@ public:
[[nodiscard]] rpl::producer<> defaultNotifyUpdates(
not_null<const PeerData*> peer) const;
[[nodiscard]] bool notifyIsMuted(
[[nodiscard]] bool isMuted(not_null<const PeerData*> peer) const;
[[nodiscard]] bool silentPosts(not_null<const PeerData*> peer) const;
[[nodiscard]] bool soundIsNone(not_null<const PeerData*> peer) const;
[[nodiscard]] bool muteUnknown(not_null<const PeerData*> peer) const;
[[nodiscard]] bool silentPostsUnknown(
not_null<const PeerData*> peer) const;
[[nodiscard]] bool soundIsNoneUnknown(
not_null<const PeerData*> peer) const;
private:
[[nodiscard]] bool isMuted(
not_null<const PeerData*> peer,
crl::time *changesIn) const;
[[nodiscard]] PeerNotifySettings &defaultNotifySettings(
not_null<const PeerData*> peer);
[[nodiscard]] const PeerNotifySettings &defaultNotifySettings(
not_null<const PeerData*> peer) const;
private:
[[nodiscard]] bool settingsUnknown(not_null<const PeerData*> peer) const;
void unmuteByFinished();
void unmuteByFinishedDelayed(crl::time delay);
void updateNotifySettingsLocal(not_null<PeerData*> peer);

View file

@ -71,7 +71,7 @@ History::History(not_null<Data::Session*> owner, PeerId peerId)
, peer(owner->peer(peerId))
, cloudDraftTextCache(st::dialogsTextWidthMin)
, _delegateMixin(HistoryInner::DelegateMixin())
, _mute(owner->notifyIsMuted(peer))
, _mute(owner->notifySettings().isMuted(peer))
, _chatListNameSortKey(owner->nameSortKey(peer->name))
, _sendActionPainter(this) {
if (const auto user = peer->asUser()) {

View file

@ -28,6 +28,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/ui_integration.h"
#include "storage/storage_shared_media.h"
#include "mtproto/mtproto_config.h"
#include "data/notify/data_notify_settings.h"
#include "data/data_session.h"
#include "data/data_changes.h"
#include "data/data_media_types.h"
@ -198,7 +199,8 @@ bool ShouldSendSilent(
not_null<PeerData*> peer,
const Api::SendOptions &options) {
return options.silent
|| (peer->isBroadcast() && peer->owner().notifySilentPosts(peer))
|| (peer->isBroadcast()
&& peer->owner().notifySettings().silentPosts(peer))
|| (peer->session().supportMode()
&& peer->session().settings().supportAllSilent());
}

View file

@ -2499,9 +2499,10 @@ void HistoryWidget::updateNotifyControls() {
_muteUnmute->setText((_history->mute()
? tr::lng_channel_unmute(tr::now)
: tr::lng_channel_mute(tr::now)).toUpper());
if (!session().data().notifySilentPostsUnknown(_peer)) {
if (!session().data().notifySettings().silentPostsUnknown(_peer)) {
if (_silent) {
_silent->setChecked(session().data().notifySilentPosts(_peer));
_silent->setChecked(
session().data().notifySettings().silentPosts(_peer));
updateFieldPlaceholder();
} else if (hasSilentToggle()) {
refreshSilentToggle();
@ -4286,7 +4287,7 @@ bool HistoryWidget::hasSilentToggle() const {
&& _peer->isChannel()
&& !_peer->isMegagroup()
&& _peer->canWrite()
&& !session().data().notifySilentPostsUnknown(_peer);
&& !session().data().notifySettings().silentPostsUnknown(_peer);
}
void HistoryWidget::handleSupportSwitch(not_null<History*> updated) {
@ -4832,7 +4833,7 @@ void HistoryWidget::updateFieldPlaceholder() {
return rpl::single(_keyboard->placeholder());
} else if (const auto channel = _history->peer->asChannel()) {
if (channel->isBroadcast()) {
return session().data().notifySilentPosts(channel)
return session().data().notifySettings().silentPosts(channel)
? tr::lng_broadcast_silent_ph()
: tr::lng_broadcast_ph();
} else if (channel->adminRights() & ChatAdminRight::Anonymous) {

View file

@ -20,6 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "chat_helpers/field_autocomplete.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "data/notify/data_notify_settings.h"
#include "data/data_changes.h"
#include "data/data_drafts.h"
#include "data/data_messages.h"
@ -1338,7 +1339,7 @@ void ComposeControls::updateFieldPlaceholder() {
return tr::lng_message_ph();
} else if (const auto channel = _history->peer->asChannel()) {
if (channel->isBroadcast()) {
return session().data().notifySilentPosts(channel)
return session().data().notifySettings().silentPosts(channel)
? tr::lng_broadcast_silent_ph()
: tr::lng_broadcast_ph();
} else if (channel->adminRights() & ChatAdminRight::Anonymous) {
@ -1358,8 +1359,9 @@ void ComposeControls::updateSilentBroadcast() {
return;
}
const auto &peer = _history->peer;
if (!session().data().notifySilentPostsUnknown(peer)) {
_silent->setChecked(session().data().notifySilentPosts(peer));
if (!session().data().notifySettings().silentPostsUnknown(peer)) {
_silent->setChecked(
session().data().notifySettings().silentPosts(peer));
updateFieldPlaceholder();
}
}
@ -2465,7 +2467,7 @@ bool ComposeControls::hasSilentBroadcastToggle() const {
&& peer->isChannel()
&& !peer->isMegagroup()
&& peer->canWrite()
&& !session().data().notifySilentPostsUnknown(peer);
&& !session().data().notifySettings().silentPostsUnknown(peer);
}
void ComposeControls::updateInlineBotQuery() {

View file

@ -174,7 +174,7 @@ rpl::producer<bool> NotificationsEnabledValue(not_null<PeerData*> peer) {
) | rpl::to_empty,
peer->owner().notifySettings().defaultNotifyUpdates(peer)
) | rpl::map([=] {
return !peer->owner().notifyIsMuted(peer);
return !peer->owner().notifySettings().isMuted(peer);
}) | rpl::distinct_until_changed();
}

View file

@ -57,7 +57,7 @@ MuteItem::MuteItem(
nullptr,
nullptr)
, _itemIconPosition(st.itemIconPosition)
, _isMuted(peer->owner().notifyIsMuted(peer)) {
, _isMuted(peer->owner().notifySettings().isMuted(peer)) {
Info::Profile::NotificationsEnabledValue(
peer
@ -135,14 +135,14 @@ void FillMuteMenu(
Args args) {
const auto peer = args.peer;
const auto soundIsNone = peer->owner().notifySoundIsNone(peer);
const auto soundIsNone = peer->owner().notifySettings().soundIsNone(peer);
menu->addAction(
soundIsNone
? tr::lng_mute_menu_sound_on(tr::now)
: tr::lng_mute_menu_sound_off(tr::now),
[=] {
const auto soundIsNone = peer->owner().notifySoundIsNone(peer);
auto &notifySettings = peer->owner().notifySettings();
const auto soundIsNone = notifySettings.soundIsNone(peer);
notifySettings.updateNotifySettings(peer, {}, {}, !soundIsNone);
},
soundIsNone ? &st::menuIconSoundOn : &st::menuIconSoundOff);

View file

@ -812,8 +812,8 @@ SilentToggle::SilentToggle(QWidget *parent, not_null<ChannelData*> channel)
: RippleButton(parent, st::historySilentToggle.ripple)
, _st(st::historySilentToggle)
, _channel(channel)
, _checked(channel->owner().notifySilentPosts(_channel)) {
Expects(!channel->owner().notifySilentPostsUnknown(_channel));
, _checked(channel->owner().notifySettings().silentPosts(_channel)) {
Expects(!channel->owner().notifySettings().silentPostsUnknown(_channel));
resize(_st.width, _st.height);

View file

@ -180,7 +180,8 @@ System::SkipState System::computeSkipState(
.silent = (forceSilent
|| !messageNotification
|| item->isSilent()
|| history->owner().notifySoundIsNone(history->peer)),
|| history->owner().notifySettings().soundIsNone(
history->peer)),
};
};
const auto showForMuted = messageNotification
@ -207,20 +208,20 @@ System::SkipState System::computeSkipState(
}
if (messageNotification
&& history->owner().notifyMuteUnknown(history->peer)) {
&& history->owner().notifySettings().muteUnknown(history->peer)) {
return { SkipState::Unknown };
} else if (messageNotification
&& !history->owner().notifyIsMuted(history->peer)) {
&& !history->owner().notifySettings().isMuted(history->peer)) {
return withSilent(SkipState::DontSkip);
} else if (!notifyBy) {
return withSilent(
showForMuted ? SkipState::DontSkip : SkipState::Skip,
showForMuted);
} else if (history->owner().notifyMuteUnknown(notifyBy)
} else if (history->owner().notifySettings().muteUnknown(notifyBy)
|| (!messageNotification
&& notifyBy->blockStatus() == PeerData::BlockStatus::Unknown)) {
return withSilent(SkipState::Unknown);
} else if (!history->owner().notifyIsMuted(notifyBy)
} else if (!history->owner().notifySettings().isMuted(notifyBy)
&& (messageNotification || !notifyBy->isBlocked())) {
return withSilent(SkipState::DontSkip);
} else {
@ -470,14 +471,15 @@ void System::showNext() {
for (auto i = _whenAlerts.begin(); i != _whenAlerts.end();) {
while (!i->second.empty() && i->second.begin()->first <= ms) {
const auto peer = i->first->peer;
const auto peerUnknown = peer->owner().notifyMuteUnknown(peer);
const auto &notifySettings = peer->owner().notifySettings();
const auto peerUnknown = notifySettings.muteUnknown(peer);
const auto peerAlert = !peerUnknown
&& !peer->owner().notifyIsMuted(peer);
&& !notifySettings.isMuted(peer);
const auto from = i->second.begin()->second;
const auto fromUnknown = (!from
|| peer->owner().notifyMuteUnknown(from));
|| notifySettings.muteUnknown(from));
const auto fromAlert = !fromUnknown
&& !peer->owner().notifyIsMuted(from);
&& !notifySettings.isMuted(from);
if (peerAlert || fromAlert) {
alert = true;
}

View file

@ -132,7 +132,7 @@ void PeerMenuAddMuteSubmenuAction(
not_null<PeerData*> peer,
const PeerMenuCallback &addAction) {
peer->owner().notifySettings().requestNotifySettings(peer);
const auto isMuted = peer->owner().notifyIsMuted(peer);
const auto isMuted = peer->owner().notifySettings().isMuted(peer);
if (isMuted) {
const auto text = tr::lng_context_unmute(tr::now)
+ '\t'
@ -146,7 +146,7 @@ void PeerMenuAddMuteSubmenuAction(
addAction(PeerMenuCallback::Args{
.text = tr::lng_context_mute(tr::now),
.handler = nullptr,
.icon = peer->owner().notifySoundIsNone(peer)
.icon = peer->owner().notifySettings().soundIsNone(peer)
? &st::menuIconSilent
: &st::menuIconMute,
.fillSubmenu = [=](not_null<Ui::PopupMenu*> menu) {
@ -1430,14 +1430,14 @@ void PeerMenuAddMuteAction(
: tr::lng_context_unmute(tr::now);
};
const auto muteAction = addAction(QString("-"), [=] {
if (!peer->owner().notifyIsMuted(peer)) {
if (!peer->owner().notifySettings().isMuted(peer)) {
controller->show(
Box<MuteSettingsBox>(peer),
Ui::LayerOption::CloseOther);
} else {
peer->owner().notifySettings().updateNotifySettings(peer, 0);
}
}, (peer->owner().notifyIsMuted(peer)
}, (peer->owner().notifySettings().isMuted(peer)
? &st::menuIconUnmute
: &st::menuIconMute));