diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index 510f2d473..cfb5258e3 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -468,8 +468,7 @@ void System::showNext() { }; auto ms = crl::now(), nextAlert = crl::time(0); - auto alert = false; - std::shared_ptr customSound = nullptr; + auto alertPeer = (PeerData*)nullptr; for (auto i = _whenAlerts.begin(); i != _whenAlerts.end();) { while (!i->second.empty() && i->second.begin()->first <= ms) { const auto peer = i->first->peer; @@ -477,19 +476,13 @@ void System::showNext() { const auto peerUnknown = notifySettings.muteUnknown(peer); const auto peerAlert = !peerUnknown && !notifySettings.isMuted(peer); - const auto peerSoundId = !notifySettings.soundUnknown(peer) - ? notifySettings.sound(peer).id - : 0; const auto from = i->second.begin()->second; const auto fromUnknown = (!from || notifySettings.muteUnknown(from)); const auto fromAlert = !fromUnknown && !notifySettings.isMuted(from); if (peerAlert || fromAlert) { - alert = true; - if (peerSoundId) { - customSound = notifySettings.lookupRingtone(peerSoundId); - } + alertPeer = peer; } while (!i->second.empty() && i->second.begin()->first <= ms + kMinimalAlertDelay) { @@ -506,7 +499,7 @@ void System::showNext() { } } const auto &settings = Core::App().settings(); - if (alert) { + if (alertPeer) { if (settings.flashBounceNotify() && !_manager->skipFlashBounce()) { if (const auto window = Core::App().primaryWindow()) { if (const auto handle = window->widget()->windowHandle()) { @@ -516,16 +509,9 @@ void System::showNext() { } } if (settings.soundNotify() && !_manager->skipAudio()) { - const auto custom = customSound - && !customSound->bytes().isEmpty(); - if (custom) { - const auto bytes = customSound->bytes(); - _customSoundTrack = Media::Audio::Current().createTrack(); - _customSoundTrack->fillFromData(bytes::make_vector(bytes)); - } else { - ensureSoundCreated(); - } - const auto &track = custom ? _customSoundTrack : _soundTrack; + const auto track = lookupSound( + alertPeer, + alertPeer->owner().notifySettings().sound(alertPeer).id); track->playOnce(); Media::Player::mixer()->suppressAll(track->getLengthMs()); Media::Player::mixer()->faderOnTimer(); @@ -712,6 +698,31 @@ void System::showNext() { } } +not_null System::lookupSound( + not_null peer, + DocumentId id) { + if (!id) { + ensureSoundCreated(); + return _soundTrack.get(); + } + const auto i = _customSoundTracks.find(id); + if (i != end(_customSoundTracks)) { + return i->second.get(); + } + const auto ¬ifySettings = peer->owner().notifySettings(); + const auto custom = notifySettings.lookupRingtone(id); + if (custom && !custom->bytes().isEmpty()) { + const auto j = _customSoundTracks.emplace( + id, + Media::Audio::Current().createTrack() + ).first; + j->second->fillFromData(bytes::make_vector(custom->bytes())); + return j->second.get(); + } + ensureSoundCreated(); + return _soundTrack.get(); +} + void System::ensureSoundCreated() { if (_soundTrack) { return; diff --git a/Telegram/SourceFiles/window/notifications_manager.h b/Telegram/SourceFiles/window/notifications_manager.h index 62debf726..283dc221e 100644 --- a/Telegram/SourceFiles/window/notifications_manager.h +++ b/Telegram/SourceFiles/window/notifications_manager.h @@ -158,6 +158,9 @@ private: void showNext(); void showGrouped(); void ensureSoundCreated(); + [[nodiscard]] not_null lookupSound( + not_null peer, + DocumentId id); base::flat_map< not_null, @@ -181,7 +184,9 @@ private: rpl::event_stream _settingsChanged; std::unique_ptr _soundTrack; - std::unique_ptr _customSoundTrack; + base::flat_map< + DocumentId, + std::unique_ptr> _customSoundTracks; int _lastForwardedCount = 0; uint64 _lastHistorySessionId = 0;