2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Refactor NotifySettings in PeerData.

This commit is contained in:
John Preston
2017-12-04 21:46:03 +04:00
parent 116e3fd9c5
commit 62568daffe
21 changed files with 562 additions and 290 deletions

View File

@@ -0,0 +1,217 @@
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#include "data/data_notify_settings.h"
namespace Data {
namespace {
MTPinputPeerNotifySettings DefaultSettings() {
const auto flags = MTPDpeerNotifySettings::Flag::f_show_previews;
const auto muteValue = TimeId(0);
return MTP_inputPeerNotifySettings(
MTP_flags(mtpCastFlags(flags)),
MTP_int(muteValue),
MTP_string("default"));
}
} // namespace
class NotifySettingsValue {
public:
NotifySettingsValue(const MTPDpeerNotifySettings &data);
using MuteChange = NotifySettings::MuteChange;
using SilentPostsChange = NotifySettings::SilentPostsChange;
bool change(const MTPDpeerNotifySettings &data);
bool change(
MuteChange mute,
SilentPostsChange silent,
int muteForSeconds);
TimeMs muteFinishesIn() const;
bool silentPosts() const;
MTPinputPeerNotifySettings serialize() const;
private:
bool change(
MTPDpeerNotifySettings::Flags flags,
TimeId mute,
QString sound);
MTPDpeerNotifySettings::Flags _flags;
TimeId _mute;
QString _sound;
};
NotifySettingsValue::NotifySettingsValue(const MTPDpeerNotifySettings &data)
: _flags(data.vflags.v)
, _mute(data.vmute_until.v)
, _sound(qs(data.vsound)) {
}
bool NotifySettingsValue::silentPosts() const {
return _flags & MTPDpeerNotifySettings::Flag::f_silent;
}
bool NotifySettingsValue::change(const MTPDpeerNotifySettings &data) {
return change(data.vflags.v, data.vmute_until.v, qs(data.vsound));
}
bool NotifySettingsValue::change(
MuteChange mute,
SilentPostsChange silent,
int muteForSeconds) {
const auto newFlags = [&] {
auto result = _flags;
if (silent == SilentPostsChange::Silent) {
result |= MTPDpeerNotifySettings::Flag::f_silent;
} else if (silent == SilentPostsChange::Notify) {
result &= ~MTPDpeerNotifySettings::Flag::f_silent;
}
return result;
}();
const auto newMute = (mute == MuteChange::Mute)
? (unixtime() + muteForSeconds)
: (mute == MuteChange::Ignore) ? _mute : 0;
const auto newSound = (newMute == 0 && _sound.isEmpty())
? qsl("default")
: _sound;
return change(newFlags, newMute, newSound);
}
bool NotifySettingsValue::change(
MTPDpeerNotifySettings::Flags flags,
TimeId mute,
QString sound) {
if (_flags == flags && _mute == mute && _sound == sound) {
return false;
}
_flags = flags;
_mute = mute;
_sound = sound;
return true;
}
TimeMs NotifySettingsValue::muteFinishesIn() const {
auto now = unixtime();
if (_mute > now) {
return (_mute - now + 1) * 1000LL;
}
return 0;
}
MTPinputPeerNotifySettings NotifySettingsValue::serialize() const {
return MTP_inputPeerNotifySettings(
MTP_flags(mtpCastFlags(_flags)),
MTP_int(_mute),
MTP_string(_sound));
}
bool NotifySettings::change(const MTPPeerNotifySettings &settings) {
switch (settings.type()) {
case mtpc_peerNotifySettingsEmpty: {
if (!_known || _value) {
_known = true;
_value = nullptr;
return true;
}
return false;
} break;
case mtpc_peerNotifySettings: {
auto &data = settings.c_peerNotifySettings();
if (_value) {
return _value->change(data);
}
_known = true;
_value = std::make_unique<NotifySettingsValue>(data);
return true;
} break;
}
Unexpected("Type in NotifySettings::change()");
}
NotifySettings::NotifySettings() = default;
bool NotifySettings::change(
MuteChange mute,
SilentPostsChange silent,
int muteForSeconds) {
Expects(mute != MuteChange::Mute || muteForSeconds > 0);
if (mute == MuteChange::Ignore && silent == SilentPostsChange::Ignore) {
return false;
}
if (_value) {
return _value->change(mute, silent, muteForSeconds);
}
const auto asEmpty = [&] {
if (mute == MuteChange::Mute) {
return false;
}
if (silent == SilentPostsChange::Silent) {
return false;
}
return true;
}();
if (asEmpty) {
return change(MTP_peerNotifySettingsEmpty());
}
const auto flags = MTPDpeerNotifySettings::Flag::f_show_previews
| ((silent == SilentPostsChange::Silent)
? MTPDpeerNotifySettings::Flag::f_silent
: MTPDpeerNotifySettings::Flag(0));
const auto muteUntil = (mute == MuteChange::Mute)
? (unixtime() + muteForSeconds)
: 0;
return change(MTP_peerNotifySettings(
MTP_flags(flags),
MTP_int(muteUntil),
MTP_string("default")));
}
TimeMs NotifySettings::muteFinishesIn() const {
return _value
? _value->muteFinishesIn()
: 0LL;
}
bool NotifySettings::settingsUnknown() const {
return !_known;
}
bool NotifySettings::silentPosts() const {
return _value
? _value->silentPosts()
: false;
}
MTPinputPeerNotifySettings NotifySettings::serialize() const {
return _value
? _value->serialize()
: DefaultSettings();
}
NotifySettings::~NotifySettings() = default;
} // namespace Data

View File

@@ -0,0 +1,60 @@
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#pragma once
namespace Data {
class NotifySettingsValue;
class NotifySettings {
public:
NotifySettings();
enum class MuteChange {
Ignore,
Mute,
Unmute,
};
enum class SilentPostsChange {
Ignore,
Silent,
Notify,
};
bool change(const MTPPeerNotifySettings &settings);
bool change(
MuteChange mute,
SilentPostsChange silent,
int muteForSeconds);
TimeMs muteFinishesIn() const;
bool settingsUnknown() const;
bool silentPosts() const;
MTPinputPeerNotifySettings serialize() const;
~NotifySettings();
private:
bool _known = false;
std::unique_ptr<NotifySettingsValue> _value;
};
} // namespace Data

View File

@@ -259,9 +259,6 @@ EmptyUserpic::~EmptyUserpic() = default;
using UpdateFlag = Notify::PeerUpdate::Flag;
NotifySettings globalNotifyAll, globalNotifyUsers, globalNotifyChats;
NotifySettingsPtr globalNotifyAllPtr = UnknownNotifySettings, globalNotifyUsersPtr = UnknownNotifySettings, globalNotifyChatsPtr = UnknownNotifySettings;
PeerClickHandler::PeerClickHandler(not_null<PeerData*> peer)
: _peer(peer) {
}
@@ -291,6 +288,8 @@ PeerData::PeerData(const PeerId &id)
_userpicEmpty.set(id, QString());
}
PeerData::~PeerData() = default;
void PeerData::updateNameDelayed(
const QString &newName,
const QString &newNameOrPhone,

View File

@@ -22,50 +22,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "data/data_types.h"
#include "data/data_flags.h"
struct NotifySettings {
NotifySettings() = default;
bool previews() const {
return flags & MTPDpeerNotifySettings::Flag::f_show_previews;
}
bool silent() const {
return flags & MTPDpeerNotifySettings::Flag::f_silent;
}
MTPDpeerNotifySettings::Flags flags
= MTPDpeerNotifySettings::Flag::f_show_previews;
TimeId mute = 0;
QString sound = qsl("default");
};
typedef NotifySettings *NotifySettingsPtr;
static const NotifySettingsPtr UnknownNotifySettings
= NotifySettingsPtr(0);
static const NotifySettingsPtr EmptyNotifySettings
= NotifySettingsPtr(1);
extern NotifySettings globalNotifyAll;
extern NotifySettings globalNotifyUsers;
extern NotifySettings globalNotifyChats;
extern NotifySettingsPtr globalNotifyAllPtr;
extern NotifySettingsPtr globalNotifyUsersPtr;
extern NotifySettingsPtr globalNotifyChatsPtr;
inline bool isNotifyMuted(
NotifySettingsPtr settings,
TimeId *changeIn = nullptr) {
if (settings != UnknownNotifySettings
&& settings != EmptyNotifySettings) {
auto t = unixtime();
if (settings->mute > t) {
if (changeIn) *changeIn = settings->mute - t + 1;
return true;
}
}
if (changeIn) *changeIn = 0;
return false;
}
#include "data/data_notify_settings.h"
int PeerColorIndex(PeerId peerId);
int PeerColorIndex(int32 bareId);
@@ -142,12 +99,7 @@ protected:
PeerData &operator=(const PeerData &other) = delete;
public:
virtual ~PeerData() {
if (notify != UnknownNotifySettings
&& notify != EmptyNotifySettings) {
delete base::take(notify);
}
}
virtual ~PeerData();
bool isUser() const {
return peerIsUser(id);
@@ -163,11 +115,32 @@ public:
}
bool isVerified() const;
bool isMegagroup() const;
bool isMuted() const {
return (notify != EmptyNotifySettings)
&& (notify != UnknownNotifySettings)
&& (notify->mute >= unixtime());
TimeMs notifyMuteFinishesIn() const {
return _notify.muteFinishesIn();
}
bool notifyChange(const MTPPeerNotifySettings &settings) {
return _notify.change(settings);
}
bool notifyChange(
Data::NotifySettings::MuteChange mute,
Data::NotifySettings::SilentPostsChange silent,
int muteForSeconds) {
return _notify.change(mute, silent, muteForSeconds);
}
bool notifySettingsUnknown() const {
return _notify.settingsUnknown();
}
bool notifySilentPosts() const {
return _notify.silentPosts();
}
MTPinputPeerNotifySettings notifySerialize() const {
return _notify.serialize();
}
bool isMuted() const {
return (notifyMuteFinishesIn() > 0);
}
bool canWrite() const;
UserData *asUser();
const UserData *asUser() const;
@@ -265,8 +238,6 @@ public:
int nameVersion = 1;
NotifySettingsPtr notify = UnknownNotifySettings;
// if this string is not empty we must not allow to open the
// conversation and we must show this string instead
virtual QString restrictionReason() const {
@@ -296,6 +267,8 @@ protected:
private:
void fillNames();
Data::NotifySettings _notify;
ClickHandlerPtr _openLink;
NameWords _nameWords; // for filtering
NameFirstChars _nameFirstChars;