Added support of sound notification setting to Data::Session.
This commit is contained in:
		
							parent
							
								
									93d4581443
								
							
						
					
					
						commit
						f32215f77d
					
				
					 6 changed files with 81 additions and 20 deletions
				
			
		| 
						 | 
					@ -60,10 +60,12 @@ public:
 | 
				
			||||||
	bool change(const MTPDpeerNotifySettings &data);
 | 
						bool change(const MTPDpeerNotifySettings &data);
 | 
				
			||||||
	bool change(
 | 
						bool change(
 | 
				
			||||||
		std::optional<int> muteForSeconds,
 | 
							std::optional<int> muteForSeconds,
 | 
				
			||||||
		std::optional<bool> silentPosts);
 | 
							std::optional<bool> silentPosts,
 | 
				
			||||||
 | 
							std::optional<NotifySound> sound);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	std::optional<TimeId> muteUntil() const;
 | 
						std::optional<TimeId> muteUntil() const;
 | 
				
			||||||
	std::optional<bool> silentPosts() const;
 | 
						std::optional<bool> silentPosts() const;
 | 
				
			||||||
 | 
						std::optional<NotifySound> sound() const;
 | 
				
			||||||
	MTPinputPeerNotifySettings serialize() const;
 | 
						MTPinputPeerNotifySettings serialize() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
| 
						 | 
					@ -101,7 +103,8 @@ bool NotifySettingsValue::change(const MTPDpeerNotifySettings &data) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool NotifySettingsValue::change(
 | 
					bool NotifySettingsValue::change(
 | 
				
			||||||
		std::optional<int> muteForSeconds,
 | 
							std::optional<int> muteForSeconds,
 | 
				
			||||||
		std::optional<bool> silentPosts) {
 | 
							std::optional<bool> silentPosts,
 | 
				
			||||||
 | 
							std::optional<NotifySound> sound) {
 | 
				
			||||||
	const auto now = base::unixtime::now();
 | 
						const auto now = base::unixtime::now();
 | 
				
			||||||
	const auto notMuted = muteForSeconds
 | 
						const auto notMuted = muteForSeconds
 | 
				
			||||||
		? !(*muteForSeconds)
 | 
							? !(*muteForSeconds)
 | 
				
			||||||
| 
						 | 
					@ -114,9 +117,12 @@ bool NotifySettingsValue::change(
 | 
				
			||||||
	const auto newSilentPosts = silentPosts
 | 
						const auto newSilentPosts = silentPosts
 | 
				
			||||||
		? base::make_optional(*silentPosts)
 | 
							? base::make_optional(*silentPosts)
 | 
				
			||||||
		: _silent;
 | 
							: _silent;
 | 
				
			||||||
 | 
						const auto newSound = sound
 | 
				
			||||||
 | 
							? base::make_optional(*sound)
 | 
				
			||||||
 | 
							: _sound;
 | 
				
			||||||
	return change(
 | 
						return change(
 | 
				
			||||||
		newMute,
 | 
							newMute,
 | 
				
			||||||
		_sound,
 | 
							newSound,
 | 
				
			||||||
		_showPreviews,
 | 
							_showPreviews,
 | 
				
			||||||
		newSilentPosts);
 | 
							newSilentPosts);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -147,6 +153,10 @@ std::optional<bool> NotifySettingsValue::silentPosts() const {
 | 
				
			||||||
	return _silent;
 | 
						return _silent;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::optional<NotifySound> NotifySettingsValue::sound() const {
 | 
				
			||||||
 | 
						return _sound;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MTPinputPeerNotifySettings NotifySettingsValue::serialize() const {
 | 
					MTPinputPeerNotifySettings NotifySettingsValue::serialize() const {
 | 
				
			||||||
	using Flag = MTPDinputPeerNotifySettings::Flag;
 | 
						using Flag = MTPDinputPeerNotifySettings::Flag;
 | 
				
			||||||
	const auto flag = [](auto &&optional, Flag flag) {
 | 
						const auto flag = [](auto &&optional, Flag flag) {
 | 
				
			||||||
| 
						 | 
					@ -188,15 +198,20 @@ bool NotifySettings::change(const MTPPeerNotifySettings &settings) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool NotifySettings::change(
 | 
					bool NotifySettings::change(
 | 
				
			||||||
		std::optional<int> muteForSeconds,
 | 
							std::optional<int> muteForSeconds,
 | 
				
			||||||
		std::optional<bool> silentPosts) {
 | 
							std::optional<bool> silentPosts,
 | 
				
			||||||
	if (!muteForSeconds && !silentPosts) {
 | 
							std::optional<bool> soundIsNone) {
 | 
				
			||||||
 | 
						const auto notificationSound = soundIsNone
 | 
				
			||||||
 | 
							? std::make_optional(NotifySound{ .none = (*soundIsNone) })
 | 
				
			||||||
 | 
							: std::nullopt;
 | 
				
			||||||
 | 
						if (!muteForSeconds && !silentPosts && !soundIsNone) {
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	} else if (_value) {
 | 
						} else if (_value) {
 | 
				
			||||||
		return _value->change(muteForSeconds, silentPosts);
 | 
							return _value->change(muteForSeconds, silentPosts, notificationSound);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	using Flag = MTPDpeerNotifySettings::Flag;
 | 
						using Flag = MTPDpeerNotifySettings::Flag;
 | 
				
			||||||
	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))
 | 
				
			||||||
 | 
							| (notificationSound ? Flag::f_other_sound : Flag(0));
 | 
				
			||||||
	const auto muteUntil = muteForSeconds
 | 
						const auto muteUntil = muteForSeconds
 | 
				
			||||||
		? (base::unixtime::now() + *muteForSeconds)
 | 
							? (base::unixtime::now() + *muteForSeconds)
 | 
				
			||||||
		: 0;
 | 
							: 0;
 | 
				
			||||||
| 
						 | 
					@ -207,7 +222,7 @@ bool NotifySettings::change(
 | 
				
			||||||
		MTP_int(muteUntil),
 | 
							MTP_int(muteUntil),
 | 
				
			||||||
		MTPNotificationSound(),
 | 
							MTPNotificationSound(),
 | 
				
			||||||
		MTPNotificationSound(),
 | 
							MTPNotificationSound(),
 | 
				
			||||||
		MTPNotificationSound()));
 | 
							SerializeSound(notificationSound)));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::optional<TimeId> NotifySettings::muteUntil() const {
 | 
					std::optional<TimeId> NotifySettings::muteUntil() const {
 | 
				
			||||||
| 
						 | 
					@ -226,6 +241,12 @@ std::optional<bool> NotifySettings::silentPosts() const {
 | 
				
			||||||
		: std::nullopt;
 | 
							: std::nullopt;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::optional<bool> NotifySettings::soundIsNone() const {
 | 
				
			||||||
 | 
						return (!_value || !_value->sound())
 | 
				
			||||||
 | 
							? std::nullopt
 | 
				
			||||||
 | 
							: std::make_optional(_value->sound()->none);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
MTPinputPeerNotifySettings NotifySettings::serialize() const {
 | 
					MTPinputPeerNotifySettings NotifySettings::serialize() const {
 | 
				
			||||||
	return _value
 | 
						return _value
 | 
				
			||||||
		? _value->serialize()
 | 
							? _value->serialize()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -34,11 +34,13 @@ public:
 | 
				
			||||||
	bool change(const MTPPeerNotifySettings &settings);
 | 
						bool change(const MTPPeerNotifySettings &settings);
 | 
				
			||||||
	bool change(
 | 
						bool change(
 | 
				
			||||||
		std::optional<int> muteForSeconds,
 | 
							std::optional<int> muteForSeconds,
 | 
				
			||||||
		std::optional<bool> silentPosts);
 | 
							std::optional<bool> silentPosts,
 | 
				
			||||||
 | 
							std::optional<bool> soundIsNone);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bool settingsUnknown() const;
 | 
						bool settingsUnknown() const;
 | 
				
			||||||
	std::optional<TimeId> muteUntil() const;
 | 
						std::optional<TimeId> muteUntil() const;
 | 
				
			||||||
	std::optional<bool> silentPosts() const;
 | 
						std::optional<bool> silentPosts() const;
 | 
				
			||||||
 | 
						std::optional<bool> soundIsNone() const;
 | 
				
			||||||
	MTPinputPeerNotifySettings serialize() const;
 | 
						MTPinputPeerNotifySettings serialize() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	~NotifySettings();
 | 
						~NotifySettings();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -194,8 +194,9 @@ public:
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	bool notifyChange(
 | 
						bool notifyChange(
 | 
				
			||||||
			std::optional<int> muteForSeconds,
 | 
								std::optional<int> muteForSeconds,
 | 
				
			||||||
			std::optional<bool> silentPosts) {
 | 
								std::optional<bool> silentPosts,
 | 
				
			||||||
		return _notify.change(muteForSeconds, silentPosts);
 | 
								std::optional<bool> soundIsNone) {
 | 
				
			||||||
 | 
							return _notify.change(muteForSeconds, silentPosts, soundIsNone);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	[[nodiscard]] bool notifySettingsUnknown() const {
 | 
						[[nodiscard]] bool notifySettingsUnknown() const {
 | 
				
			||||||
		return _notify.settingsUnknown();
 | 
							return _notify.settingsUnknown();
 | 
				
			||||||
| 
						 | 
					@ -203,6 +204,9 @@ public:
 | 
				
			||||||
	[[nodiscard]] std::optional<bool> notifySilentPosts() const {
 | 
						[[nodiscard]] std::optional<bool> notifySilentPosts() const {
 | 
				
			||||||
		return _notify.silentPosts();
 | 
							return _notify.silentPosts();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						[[nodiscard]] std::optional<bool> notifySoundIsNone() const {
 | 
				
			||||||
 | 
							return _notify.soundIsNone();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	[[nodiscard]] MTPinputPeerNotifySettings notifySerialize() const {
 | 
						[[nodiscard]] MTPinputPeerNotifySettings notifySerialize() const {
 | 
				
			||||||
		return _notify.serialize();
 | 
							return _notify.serialize();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3896,7 +3896,8 @@ void Session::applyNotifySetting(
 | 
				
			||||||
			const NotifySettings &settings) {
 | 
								const NotifySettings &settings) {
 | 
				
			||||||
		return !peer->notifySettingsUnknown()
 | 
							return !peer->notifySettingsUnknown()
 | 
				
			||||||
			&& ((!peer->notifyMuteUntil() && settings.muteUntil())
 | 
								&& ((!peer->notifyMuteUntil() && settings.muteUntil())
 | 
				
			||||||
				|| (!peer->notifySilentPosts() && settings.silentPosts()));
 | 
									|| (!peer->notifySilentPosts() && settings.silentPosts())
 | 
				
			||||||
 | 
									|| (!peer->notifySoundIsNone() && settings.soundIsNone()));
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (notifyPeer.type()) {
 | 
						switch (notifyPeer.type()) {
 | 
				
			||||||
| 
						 | 
					@ -3947,8 +3948,9 @@ void Session::applyNotifySetting(
 | 
				
			||||||
void Session::updateNotifySettings(
 | 
					void Session::updateNotifySettings(
 | 
				
			||||||
		not_null<PeerData*> peer,
 | 
							not_null<PeerData*> peer,
 | 
				
			||||||
		std::optional<int> muteForSeconds,
 | 
							std::optional<int> muteForSeconds,
 | 
				
			||||||
		std::optional<bool> silentPosts) {
 | 
							std::optional<bool> silentPosts,
 | 
				
			||||||
	if (peer->notifyChange(muteForSeconds, silentPosts)) {
 | 
							std::optional<bool> soundIsNone) {
 | 
				
			||||||
 | 
						if (peer->notifyChange(muteForSeconds, silentPosts, soundIsNone)) {
 | 
				
			||||||
		updateNotifySettingsLocal(peer);
 | 
							updateNotifySettingsLocal(peer);
 | 
				
			||||||
		_session->api().updateNotifySettingsDelayed(peer);
 | 
							_session->api().updateNotifySettingsDelayed(peer);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -3969,6 +3971,10 @@ void Session::resetNotifySettingsToDefault(not_null<PeerData*> peer) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Session::notifyIsMuted(not_null<const PeerData*> peer) const {
 | 
				
			||||||
 | 
						return notifyIsMuted(peer, nullptr);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool Session::notifyIsMuted(
 | 
					bool Session::notifyIsMuted(
 | 
				
			||||||
		not_null<const PeerData*> peer,
 | 
							not_null<const PeerData*> peer,
 | 
				
			||||||
		crl::time *changesIn) const {
 | 
							crl::time *changesIn) const {
 | 
				
			||||||
| 
						 | 
					@ -4003,6 +4009,17 @@ bool Session::notifySilentPosts(not_null<const PeerData*> peer) const {
 | 
				
			||||||
	return false;
 | 
						return false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool Session::notifySoundIsNone(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 Session::notifyMuteUnknown(not_null<const PeerData*> peer) const {
 | 
					bool Session::notifyMuteUnknown(not_null<const PeerData*> peer) const {
 | 
				
			||||||
	if (peer->notifySettingsUnknown()) {
 | 
						if (peer->notifySettingsUnknown()) {
 | 
				
			||||||
		return true;
 | 
							return true;
 | 
				
			||||||
| 
						 | 
					@ -4022,8 +4039,19 @@ bool Session::notifySilentPostsUnknown(
 | 
				
			||||||
	return defaultNotifySettings(peer).settingsUnknown();
 | 
						return 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 defaultNotifySettings(peer).settingsUnknown();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool Session::notifySettingsUnknown(not_null<const PeerData*> peer) const {
 | 
					bool Session::notifySettingsUnknown(not_null<const PeerData*> peer) const {
 | 
				
			||||||
	return notifyMuteUnknown(peer) || notifySilentPostsUnknown(peer);
 | 
						return notifyMuteUnknown(peer)
 | 
				
			||||||
 | 
							|| notifySilentPostsUnknown(peer)
 | 
				
			||||||
 | 
							|| notifySoundIsNoneUnknown(peer);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
rpl::producer<> Session::defaultUserNotifyUpdates() const {
 | 
					rpl::producer<> Session::defaultUserNotifyUpdates() const {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -666,14 +666,15 @@ public:
 | 
				
			||||||
	void updateNotifySettings(
 | 
						void updateNotifySettings(
 | 
				
			||||||
		not_null<PeerData*> peer,
 | 
							not_null<PeerData*> peer,
 | 
				
			||||||
		std::optional<int> muteForSeconds,
 | 
							std::optional<int> muteForSeconds,
 | 
				
			||||||
		std::optional<bool> silentPosts = std::nullopt);
 | 
							std::optional<bool> silentPosts = std::nullopt,
 | 
				
			||||||
 | 
							std::optional<bool> soundIsNone = std::nullopt);
 | 
				
			||||||
	void resetNotifySettingsToDefault(not_null<PeerData*> peer);
 | 
						void resetNotifySettingsToDefault(not_null<PeerData*> peer);
 | 
				
			||||||
	bool notifyIsMuted(
 | 
						bool notifyIsMuted(not_null<const PeerData*> peer) const;
 | 
				
			||||||
		not_null<const PeerData*> peer,
 | 
					 | 
				
			||||||
		crl::time *changesIn = nullptr) const;
 | 
					 | 
				
			||||||
	bool notifySilentPosts(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 notifyMuteUnknown(not_null<const PeerData*> peer) const;
 | 
				
			||||||
	bool notifySilentPostsUnknown(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;
 | 
						bool notifySettingsUnknown(not_null<const PeerData*> peer) const;
 | 
				
			||||||
	rpl::producer<> defaultUserNotifyUpdates() const;
 | 
						rpl::producer<> defaultUserNotifyUpdates() const;
 | 
				
			||||||
	rpl::producer<> defaultChatNotifyUpdates() const;
 | 
						rpl::producer<> defaultChatNotifyUpdates() const;
 | 
				
			||||||
| 
						 | 
					@ -711,6 +712,10 @@ public:
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	using Messages = std::unordered_map<MsgId, not_null<HistoryItem*>>;
 | 
						using Messages = std::unordered_map<MsgId, not_null<HistoryItem*>>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						bool notifyIsMuted(
 | 
				
			||||||
 | 
							not_null<const PeerData*> peer,
 | 
				
			||||||
 | 
							crl::time *changesIn) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void suggestStartExport();
 | 
						void suggestStartExport();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void setupMigrationViewer();
 | 
						void setupMigrationViewer();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -178,7 +178,8 @@ System::SkipState System::computeSkipState(
 | 
				
			||||||
			.value = value,
 | 
								.value = value,
 | 
				
			||||||
			.silent = (forceSilent
 | 
								.silent = (forceSilent
 | 
				
			||||||
				|| !messageNotification
 | 
									|| !messageNotification
 | 
				
			||||||
				|| item->isSilent()),
 | 
									|| item->isSilent()
 | 
				
			||||||
 | 
									|| history->owner().notifySoundIsNone(history->peer)),
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
	const auto showForMuted = messageNotification
 | 
						const auto showForMuted = messageNotification
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue