tdesktop/Telegram/SourceFiles/data/notify/data_peer_notify_settings.cpp

257 lines
6.5 KiB
C++
Raw Normal View History

2017-12-04 17:46:03 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2017-12-04 17:46:03 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2017-12-04 17:46:03 +00:00
*/
#include "data/notify/data_peer_notify_settings.h"
2017-12-04 17:46:03 +00:00
#include "base/unixtime.h"
2017-12-04 17:46:03 +00:00
namespace Data {
namespace {
2022-03-30 06:20:42 +00:00
[[nodiscard]] MTPinputPeerNotifySettings DefaultSettings() {
2017-12-04 17:46:03 +00:00
return MTP_inputPeerNotifySettings(
MTP_flags(0),
MTPBool(),
MTPBool(),
MTPint(),
2022-03-30 06:20:42 +00:00
MTPNotificationSound());
}
[[nodiscard]] NotifySound ParseSound(const MTPNotificationSound &sound) {
return sound.match([&](const MTPDnotificationSoundDefault &data) {
return NotifySound();
}, [&](const MTPDnotificationSoundNone &data) {
return NotifySound{ .none = true };
}, [&](const MTPDnotificationSoundLocal &data) {
return NotifySound{
.title = qs(data.vtitle()),
.data = qs(data.vdata()),
};
}, [&](const MTPDnotificationSoundRingtone &data) {
return NotifySound{ .id = data.vid().v };
});
}
[[nodiscard]] MTPNotificationSound SerializeSound(
const std::optional<NotifySound> &sound) {
return !sound
? MTPNotificationSound()
: sound->none
? MTP_notificationSoundNone()
: sound->id
? MTP_notificationSoundRingtone(MTP_long(sound->id))
: !sound->title.isEmpty()
? MTP_notificationSoundLocal(
MTP_string(sound->title),
MTP_string(sound->data))
: MTP_notificationSoundDefault();
2017-12-04 17:46:03 +00:00
}
} // namespace
2022-09-14 08:28:59 +00:00
int MuteValue::until() const {
return forever
? std::numeric_limits<int>::max()
: (period > 0)
? (base::unixtime::now() + period)
: unmute
? 0
: -1;
}
class NotifyPeerSettingsValue {
2017-12-04 17:46:03 +00:00
public:
NotifyPeerSettingsValue(const MTPDpeerNotifySettings &data);
2017-12-04 17:46:03 +00:00
bool change(const MTPDpeerNotifySettings &data);
bool change(
2022-09-14 08:28:59 +00:00
MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound);
2018-09-21 16:28:46 +00:00
std::optional<TimeId> muteUntil() const;
std::optional<bool> silentPosts() const;
std::optional<NotifySound> sound() const;
2017-12-04 17:46:03 +00:00
MTPinputPeerNotifySettings serialize() const;
private:
bool change(
2018-09-21 16:28:46 +00:00
std::optional<int> mute,
2022-03-30 06:20:42 +00:00
std::optional<NotifySound> sound,
2018-09-21 16:28:46 +00:00
std::optional<bool> showPreviews,
std::optional<bool> silentPosts);
2017-12-04 17:46:03 +00:00
2018-09-21 16:28:46 +00:00
std::optional<TimeId> _mute;
2022-03-30 06:20:42 +00:00
std::optional<NotifySound> _sound;
2018-09-21 16:28:46 +00:00
std::optional<bool> _silent;
std::optional<bool> _showPreviews;
2017-12-04 17:46:03 +00:00
};
NotifyPeerSettingsValue::NotifyPeerSettingsValue(
const MTPDpeerNotifySettings &data) {
change(data);
2017-12-04 17:46:03 +00:00
}
bool NotifyPeerSettingsValue::change(const MTPDpeerNotifySettings &data) {
2019-07-05 13:38:38 +00:00
const auto mute = data.vmute_until();
2022-03-30 06:20:42 +00:00
const auto sound = data.vother_sound();
2019-07-05 13:38:38 +00:00
const auto showPreviews = data.vshow_previews();
const auto silent = data.vsilent();
return change(
mute ? std::make_optional(mute->v) : std::nullopt,
2022-03-30 06:20:42 +00:00
sound ? std::make_optional(ParseSound(*sound)) : std::nullopt,
2019-07-05 13:38:38 +00:00
(showPreviews
? std::make_optional(mtpIsTrue(*showPreviews))
: std::nullopt),
silent ? std::make_optional(mtpIsTrue(*silent)) : std::nullopt);
2017-12-04 17:46:03 +00:00
}
bool NotifyPeerSettingsValue::change(
2022-09-14 08:28:59 +00:00
MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound) {
const auto newMute = muteForSeconds
2022-09-14 08:28:59 +00:00
? base::make_optional(muteForSeconds.until())
: _mute;
const auto newSilentPosts = silentPosts
? base::make_optional(*silentPosts)
: _silent;
const auto newSound = sound
? base::make_optional(*sound)
: _sound;
return change(
newMute,
newSound,
_showPreviews,
newSilentPosts);
2017-12-04 17:46:03 +00:00
}
bool NotifyPeerSettingsValue::change(
2018-09-21 16:28:46 +00:00
std::optional<int> mute,
2022-03-30 06:20:42 +00:00
std::optional<NotifySound> sound,
2018-09-21 16:28:46 +00:00
std::optional<bool> showPreviews,
std::optional<bool> silentPosts) {
if (_mute == mute
&& _sound == sound
&& _showPreviews == showPreviews
&& _silent == silentPosts) {
2017-12-04 17:46:03 +00:00
return false;
}
_mute = mute;
_sound = sound;
_showPreviews = showPreviews;
_silent = silentPosts;
2017-12-04 17:46:03 +00:00
return true;
}
std::optional<TimeId> NotifyPeerSettingsValue::muteUntil() const {
return _mute;
}
std::optional<bool> NotifyPeerSettingsValue::silentPosts() const {
return _silent;
2017-12-04 17:46:03 +00:00
}
std::optional<NotifySound> NotifyPeerSettingsValue::sound() const {
return _sound;
}
MTPinputPeerNotifySettings NotifyPeerSettingsValue::serialize() const {
using Flag = MTPDinputPeerNotifySettings::Flag;
const auto flag = [](auto &&optional, Flag flag) {
return optional.has_value() ? flag : Flag(0);
};
2017-12-04 17:46:03 +00:00
return MTP_inputPeerNotifySettings(
MTP_flags(flag(_mute, Flag::f_mute_until)
| flag(_sound, Flag::f_sound)
| flag(_silent, Flag::f_silent)
| flag(_showPreviews, Flag::f_show_previews)),
MTP_bool(_showPreviews ? *_showPreviews : true),
MTP_bool(_silent ? *_silent : false),
MTP_int(_mute ? *_mute : false),
2022-03-30 06:20:42 +00:00
SerializeSound(_sound));
2017-12-04 17:46:03 +00:00
}
PeerNotifySettings::PeerNotifySettings() = default;
bool PeerNotifySettings::change(const MTPPeerNotifySettings &settings) {
2018-04-25 09:24:48 +00:00
Expects(settings.type() == mtpc_peerNotifySettings);
2017-12-04 17:46:03 +00:00
2018-04-25 09:24:48 +00:00
auto &data = settings.c_peerNotifySettings();
2019-07-05 13:38:38 +00:00
const auto empty = !data.vflags().v;
if (empty) {
if (!_known || _value) {
_known = true;
_value = nullptr;
return true;
}
return false;
}
2018-04-25 09:24:48 +00:00
if (_value) {
return _value->change(data);
2017-12-04 17:46:03 +00:00
}
2018-04-25 09:24:48 +00:00
_known = true;
_value = std::make_unique<NotifyPeerSettingsValue>(data);
2018-04-25 09:24:48 +00:00
return true;
2017-12-04 17:46:03 +00:00
}
bool PeerNotifySettings::change(
2022-09-14 08:28:59 +00:00
MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound) {
if (!muteForSeconds && !silentPosts && !sound) {
2017-12-04 17:46:03 +00:00
return false;
} else if (_value) {
return _value->change(muteForSeconds, silentPosts, sound);
2017-12-04 17:46:03 +00:00
}
using Flag = MTPDpeerNotifySettings::Flag;
const auto flags = (muteForSeconds ? Flag::f_mute_until : Flag(0))
| (silentPosts ? Flag::f_silent : Flag(0))
| (sound ? Flag::f_other_sound : Flag(0));
2017-12-04 17:46:03 +00:00
return change(MTP_peerNotifySettings(
MTP_flags(flags),
MTPBool(),
silentPosts ? MTP_bool(*silentPosts) : MTPBool(),
2022-09-14 08:28:59 +00:00
MTP_int(muteForSeconds.until()),
2022-03-30 06:20:42 +00:00
MTPNotificationSound(),
MTPNotificationSound(),
SerializeSound(sound)));
2017-12-04 17:46:03 +00:00
}
std::optional<TimeId> PeerNotifySettings::muteUntil() const {
2017-12-04 17:46:03 +00:00
return _value
? _value->muteUntil()
2018-09-21 16:28:46 +00:00
: std::nullopt;
2017-12-04 17:46:03 +00:00
}
bool PeerNotifySettings::settingsUnknown() const {
2017-12-04 17:46:03 +00:00
return !_known;
}
std::optional<bool> PeerNotifySettings::silentPosts() const {
2017-12-04 17:46:03 +00:00
return _value
? _value->silentPosts()
2018-09-21 16:28:46 +00:00
: std::nullopt;
2017-12-04 17:46:03 +00:00
}
std::optional<NotifySound> PeerNotifySettings::sound() const {
return _value
? _value->sound()
: std::nullopt;
}
MTPinputPeerNotifySettings PeerNotifySettings::serialize() const {
2017-12-04 17:46:03 +00:00
return _value
? _value->serialize()
: DefaultSettings();
}
PeerNotifySettings::~PeerNotifySettings() = default;
2017-12-04 17:46:03 +00:00
} // namespace Data