Fixed muting of peer forever.

This commit is contained in:
23rd 2022-09-14 11:28:59 +03:00 committed by John Preston
parent 84400f5912
commit 557a2e400e
10 changed files with 55 additions and 30 deletions

View file

@ -76,7 +76,9 @@ void MuteSettingsBox::prepare() {
_save = [=] { _save = [=] {
const auto muteForSeconds = group->value() * 3600; const auto muteForSeconds = group->value() * 3600;
_peer->owner().notifySettings().update(_peer, muteForSeconds); _peer->owner().notifySettings().update(
_peer,
{ .period = muteForSeconds });
closeBox(); closeBox();
}; };
addButton(tr::lng_box_ok(), _save); addButton(tr::lng_box_ok(), _save);

View file

@ -201,7 +201,7 @@ public:
return _notify.change(settings); return _notify.change(settings);
} }
bool notifyChange( bool notifyChange(
std::optional<int> muteForSeconds, Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<Data::NotifySound> sound) { std::optional<Data::NotifySound> sound) {
return _notify.change(muteForSeconds, silentPosts, sound); return _notify.change(muteForSeconds, silentPosts, sound);

View file

@ -76,7 +76,7 @@ void NotifySettings::apply(
void NotifySettings::update( void NotifySettings::update(
not_null<PeerData*> peer, not_null<PeerData*> peer,
std::optional<int> muteForSeconds, Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<NotifySound> sound) { std::optional<NotifySound> sound) {
if (peer->notifyChange(muteForSeconds, silentPosts, sound)) { if (peer->notifyChange(muteForSeconds, silentPosts, sound)) {
@ -130,7 +130,7 @@ const PeerNotifySettings &NotifySettings::defaultSettings(
void NotifySettings::defaultUpdate( void NotifySettings::defaultUpdate(
DefaultNotify type, DefaultNotify type,
std::optional<int> muteForSeconds, Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<NotifySound> sound) { std::optional<NotifySound> sound) {
auto &settings = defaultValue(type).settings; auto &settings = defaultValue(type).settings;

View file

@ -34,7 +34,7 @@ public:
const MTPPeerNotifySettings &settings); const MTPPeerNotifySettings &settings);
void update( void update(
not_null<PeerData*> peer, not_null<PeerData*> peer,
std::optional<int> muteForSeconds, Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts = std::nullopt, std::optional<bool> silentPosts = std::nullopt,
std::optional<NotifySound> sound = std::nullopt); std::optional<NotifySound> sound = std::nullopt);
void resetToDefault(not_null<PeerData*> peer); void resetToDefault(not_null<PeerData*> peer);
@ -53,7 +53,7 @@ public:
void defaultUpdate( void defaultUpdate(
DefaultNotify type, DefaultNotify type,
std::optional<int> muteForSeconds, Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts = std::nullopt, std::optional<bool> silentPosts = std::nullopt,
std::optional<NotifySound> sound = std::nullopt); std::optional<NotifySound> sound = std::nullopt);

View file

@ -53,13 +53,23 @@ namespace {
} // namespace } // namespace
int MuteValue::until() const {
return forever
? std::numeric_limits<int>::max()
: (period > 0)
? (base::unixtime::now() + period)
: unmute
? 0
: -1;
}
class NotifyPeerSettingsValue { class NotifyPeerSettingsValue {
public: public:
NotifyPeerSettingsValue(const MTPDpeerNotifySettings &data); NotifyPeerSettingsValue(const MTPDpeerNotifySettings &data);
bool change(const MTPDpeerNotifySettings &data); bool change(const MTPDpeerNotifySettings &data);
bool change( bool change(
std::optional<int> muteForSeconds, MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<NotifySound> sound); std::optional<NotifySound> sound);
@ -102,14 +112,11 @@ bool NotifyPeerSettingsValue::change(const MTPDpeerNotifySettings &data) {
} }
bool NotifyPeerSettingsValue::change( bool NotifyPeerSettingsValue::change(
std::optional<int> muteForSeconds, MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<NotifySound> sound) { std::optional<NotifySound> sound) {
const auto now = base::unixtime::now();
const auto newMute = muteForSeconds const auto newMute = muteForSeconds
? base::make_optional((*muteForSeconds > 0) ? base::make_optional(muteForSeconds.until())
? (now + *muteForSeconds)
: 0)
: _mute; : _mute;
const auto newSilentPosts = silentPosts const auto newSilentPosts = silentPosts
? base::make_optional(*silentPosts) ? base::make_optional(*silentPosts)
@ -194,7 +201,7 @@ bool PeerNotifySettings::change(const MTPPeerNotifySettings &settings) {
} }
bool PeerNotifySettings::change( bool PeerNotifySettings::change(
std::optional<int> muteForSeconds, MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<NotifySound> sound) { std::optional<NotifySound> sound) {
if (!muteForSeconds && !silentPosts && !sound) { if (!muteForSeconds && !silentPosts && !sound) {
@ -206,14 +213,11 @@ bool PeerNotifySettings::change(
const auto flags = (muteForSeconds ? Flag::f_mute_until : Flag(0)) const auto flags = (muteForSeconds ? Flag::f_mute_until : Flag(0))
| (silentPosts ? Flag::f_silent : Flag(0)) | (silentPosts ? Flag::f_silent : Flag(0))
| (sound ? Flag::f_other_sound : Flag(0)); | (sound ? Flag::f_other_sound : Flag(0));
const auto muteUntil = muteForSeconds
? (base::unixtime::now() + *muteForSeconds)
: 0;
return change(MTP_peerNotifySettings( return change(MTP_peerNotifySettings(
MTP_flags(flags), MTP_flags(flags),
MTPBool(), MTPBool(),
silentPosts ? MTP_bool(*silentPosts) : MTPBool(), silentPosts ? MTP_bool(*silentPosts) : MTPBool(),
MTP_int(muteUntil), MTP_int(muteForSeconds.until()),
MTPNotificationSound(), MTPNotificationSound(),
MTPNotificationSound(), MTPNotificationSound(),
SerializeSound(sound))); SerializeSound(sound)));

View file

@ -18,6 +18,17 @@ struct NotifySound {
bool none = false; bool none = false;
}; };
struct MuteValue {
bool unmute = false;
bool forever = false;
int period = 0;
[[nodiscard]] explicit operator bool() const {
return unmute || forever || period;
}
[[nodiscard]] int until() const;
};
inline bool operator==(const NotifySound &a, const NotifySound &b) { inline bool operator==(const NotifySound &a, const NotifySound &b) {
return (a.id == b.id) return (a.id == b.id)
&& (a.none == b.none) && (a.none == b.none)
@ -29,11 +40,9 @@ class PeerNotifySettings {
public: public:
PeerNotifySettings(); PeerNotifySettings();
static constexpr auto kDefaultMutePeriod = 86400 * 365;
bool change(const MTPPeerNotifySettings &settings); bool change(const MTPPeerNotifySettings &settings);
bool change( bool change(
std::optional<int> muteForSeconds, MuteValue muteForSeconds,
std::optional<bool> silentPosts, std::optional<bool> silentPosts,
std::optional<NotifySound> sound); std::optional<NotifySound> sound);

View file

@ -3893,9 +3893,11 @@ void HistoryWidget::joinChannel() {
} }
void HistoryWidget::toggleMuteUnmute() { void HistoryWidget::toggleMuteUnmute() {
const auto muteForSeconds = _history->mute() const auto wasMuted = !!_history->mute();
? 0 const auto muteForSeconds = Data::MuteValue{
: Data::PeerNotifySettings::kDefaultMutePeriod; .unmute = wasMuted,
.forever = !wasMuted,
};
session().data().notifySettings().update(_peer, muteForSeconds); session().data().notifySettings().update(_peer, muteForSeconds);
} }

View file

@ -419,7 +419,9 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupMuteToggle() {
return true; return true;
} }
if (peer->owner().notifySettings().isMuted(peer)) { if (peer->owner().notifySettings().isMuted(peer)) {
peer->owner().notifySettings().update(peer, 0); peer->owner().notifySettings().update(
peer,
{ .unmute = true });
return false; return false;
} else { } else {
return true; return true;

View file

@ -115,7 +115,7 @@ MuteItem::MuteItem(
setClickedCallback([=] { setClickedCallback([=] {
peer->owner().notifySettings().update( peer->owner().notifySettings().update(
peer, peer,
_isMuted ? 0 : Data::PeerNotifySettings::kDefaultMutePeriod); { .unmute = _isMuted, .forever = !_isMuted });
}); });
} }
@ -159,7 +159,9 @@ void MuteBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
}) | rpl::flatten_latest(); }) | rpl::flatten_latest();
Ui::ConfirmBox(box, { Ui::ConfirmBox(box, {
.confirmed = [=] { .confirmed = [=] {
peer->owner().notifySettings().update(peer, state->lastSeconds); peer->owner().notifySettings().update(
peer,
{ .period = state->lastSeconds });
box->getDelegate()->hideLayer(); box->getDelegate()->hideLayer();
}, },
.confirmText = std::move(confirmText), .confirmText = std::move(confirmText),
@ -183,7 +185,9 @@ void PickMuteBox(not_null<Ui::GenericBox*> box, not_null<PeerData*> peer) {
Ui::ConfirmBox(box, { Ui::ConfirmBox(box, {
.confirmed = [=] { .confirmed = [=] {
const auto muteFor = pickerCallback(); const auto muteFor = pickerCallback();
peer->owner().notifySettings().update(peer, muteFor); peer->owner().notifySettings().update(
peer,
{ .period = muteFor });
peer->session().settings().addMutePeriod(muteFor); peer->session().settings().addMutePeriod(muteFor);
peer->session().saveSettings(); peer->session().saveSettings();
box->closeBox(); box->closeBox();
@ -246,7 +250,9 @@ void FillMuteMenu(
+ st::menuIconMuteForAnyTextPosition; + st::menuIconMuteForAnyTextPosition;
for (const auto &muteFor : peer->session().settings().mutePeriods()) { for (const auto &muteFor : peer->session().settings().mutePeriods()) {
const auto callback = [=] { const auto callback = [=] {
peer->owner().notifySettings().update(peer, muteFor); peer->owner().notifySettings().update(
peer,
{ .period = muteFor });
}; };
auto item = base::make_unique_q<IconWithText>( auto item = base::make_unique_q<IconWithText>(

View file

@ -142,7 +142,7 @@ void PeerMenuAddMuteSubmenuAction(
+ Ui::FormatMuteForTiny(peer->notifyMuteUntil().value_or(0) + Ui::FormatMuteForTiny(peer->notifyMuteUntil().value_or(0)
- base::unixtime::now()); - base::unixtime::now());
addAction(text, [=] { addAction(text, [=] {
peer->owner().notifySettings().update(peer, 0); peer->owner().notifySettings().update(peer, { .unmute = true });
}, &st::menuIconUnmute); }, &st::menuIconUnmute);
} else { } else {
const auto show = std::make_shared<Window::Show>(controller); const auto show = std::make_shared<Window::Show>(controller);
@ -1479,7 +1479,7 @@ void PeerMenuAddMuteAction(
Box<MuteSettingsBox>(peer), Box<MuteSettingsBox>(peer),
Ui::LayerOption::CloseOther); Ui::LayerOption::CloseOther);
} else { } else {
peer->owner().notifySettings().update(peer, 0); peer->owner().notifySettings().update(peer, { .unmute = true });
} }
}, (peer->owner().notifySettings().isMuted(peer) }, (peer->owner().notifySettings().isMuted(peer)
? &st::menuIconUnmute ? &st::menuIconUnmute