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 = [=] {
const auto muteForSeconds = group->value() * 3600;
_peer->owner().notifySettings().update(_peer, muteForSeconds);
_peer->owner().notifySettings().update(
_peer,
{ .period = muteForSeconds });
closeBox();
};
addButton(tr::lng_box_ok(), _save);

View file

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

View file

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

View file

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

View file

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

View file

@ -18,6 +18,17 @@ struct NotifySound {
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) {
return (a.id == b.id)
&& (a.none == b.none)
@ -29,11 +40,9 @@ class PeerNotifySettings {
public:
PeerNotifySettings();
static constexpr auto kDefaultMutePeriod = 86400 * 365;
bool change(const MTPPeerNotifySettings &settings);
bool change(
std::optional<int> muteForSeconds,
MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound);

View file

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

View file

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

View file

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

View file

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