From 3715fa4b1e4f62b6c42ed1cb2f307aeee50aeed3 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 30 Aug 2019 17:06:21 +0300 Subject: [PATCH] Fix scheduled notifications on inbox read. --- Telegram/SourceFiles/data/data_session.cpp | 2 +- Telegram/SourceFiles/history/history.cpp | 13 +++++--- Telegram/SourceFiles/history/history.h | 32 ++++++++++--------- .../linux/notifications_manager_linux.cpp | 6 ++-- .../linux/notifications_manager_linux.h | 2 +- .../platform/mac/notifications_manager_mac.h | 2 +- .../platform/mac/notifications_manager_mac.mm | 6 ++-- .../win/notifications_manager_win.cpp | 6 ++-- .../platform/win/notifications_manager_win.h | 2 +- .../window/notifications_manager.cpp | 10 ++++-- .../window/notifications_manager.h | 15 +++++---- .../window/notifications_manager_default.cpp | 6 ++-- .../window/notifications_manager_default.h | 4 +-- 13 files changed, 60 insertions(+), 46 deletions(-) diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index cb081870be..c99b2a5139 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -1977,7 +1977,7 @@ void Session::updateNotifySettingsLocal(not_null peer) { _mutedPeers.emplace(peer); unmuteByFinishedDelayed(changesIn); if (history) { - _session->notifications().clearFromHistory(history); + _session->notifications().clearIncomingFromHistory(history); } } else { _mutedPeers.erase(peer); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 3106c226c9..0b222c245e 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1658,10 +1658,7 @@ void History::inboxRead(MsgId upTo, std::optional stillUnread) { } _firstUnreadView = nullptr; - if (!peer->isSelf()) { - // Only reminders generate notifications in Saved Messages. - session().notifications().clearFromHistory(this); - } + session().notifications().clearIncomingFromHistory(this); } void History::inboxRead(not_null wasRead) { @@ -2175,6 +2172,14 @@ void History::clearNotifications() { _notifications.clear(); } +void History::clearIncomingNotifications() { + if (!peer->isSelf()) { + _notifications.erase( + ranges::remove(_notifications, false, &HistoryItem::out), + end(_notifications)); + } +} + bool History::loadedAtBottom() const { return _loadedAtBottom; } diff --git a/Telegram/SourceFiles/history/history.h b/Telegram/SourceFiles/history/history.h index b97378b0e2..327445a3f4 100644 --- a/Telegram/SourceFiles/history/history.h +++ b/Telegram/SourceFiles/history/history.h @@ -168,34 +168,36 @@ public: void inboxRead(not_null wasRead); void outboxRead(MsgId upTo); void outboxRead(not_null wasRead); - bool isServerSideUnread(not_null item) const; - MsgId loadAroundId() const; + [[nodiscard]] bool isServerSideUnread( + not_null item) const; + [[nodiscard]] MsgId loadAroundId() const; - int unreadCount() const; - bool unreadCountKnown() const; + [[nodiscard]] int unreadCount() const; + [[nodiscard]] bool unreadCountKnown() const; void setUnreadCount(int newUnreadCount); void setUnreadMark(bool unread); - bool unreadMark() const; - int unreadCountForBadge() const; // unreadCount || unreadMark ? 1 : 0. - bool mute() const; + [[nodiscard]] bool unreadMark() const; + [[nodiscard]] int unreadCountForBadge() const; // unreadCount || unreadMark ? 1 : 0. + [[nodiscard]] bool mute() const; bool changeMute(bool newMute); void addUnreadBar(); void destroyUnreadBar(); - bool hasNotFreezedUnreadBar() const; - Element *unreadBar() const; + [[nodiscard]] bool hasNotFreezedUnreadBar() const; + [[nodiscard]] Element *unreadBar() const; void calculateFirstUnreadMessage(); void unsetFirstUnreadMessage(); - Element *firstUnreadMessage() const; + [[nodiscard]] Element *firstUnreadMessage() const; void clearNotifications(); + void clearIncomingNotifications(); - bool loadedAtBottom() const; // last message is in the list + [[nodiscard]] bool loadedAtBottom() const; // last message is in the list void setNotLoadedAtBottom(); - bool loadedAtTop() const; // nothing was added after loading history back - bool isReadyFor(MsgId msgId); // has messages for showing history at msgId + [[nodiscard]] bool loadedAtTop() const; // nothing was added after loading history back + [[nodiscard]] bool isReadyFor(MsgId msgId); // has messages for showing history at msgId void getReadyFor(MsgId msgId); - HistoryItem *lastMessage() const; - bool lastMessageKnown() const; + [[nodiscard]] HistoryItem *lastMessage() const; + [[nodiscard]] bool lastMessageKnown() const; void unknownMessageDeleted(MsgId messageId); void applyDialogTopMessage(MsgId topMessageId); void applyDialog(Data::Folder *requestFolder, const MTPDdialog &data); diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index f6c59869e7..3dad9e9d94 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -313,7 +313,7 @@ public: bool hideNameAndPhoto, bool hideReplyButton); void clearAll(); - void clearFromHistory(History *history); + void clearFromHistory(not_null history); void clearNotification(PeerId peerId, MsgId msgId); bool hasPoorSupport() const { @@ -494,7 +494,7 @@ void Manager::Private::clearAll() { } } -void Manager::Private::clearFromHistory(History *history) { +void Manager::Private::clearFromHistory(not_null history) { for (auto i = _queuedNotifications.begin(); i != _queuedNotifications.end();) { if (i->peer == history->peer) { i = _queuedNotifications.erase(i); @@ -573,7 +573,7 @@ void Manager::doClearAllFast() { _private->clearAll(); } -void Manager::doClearFromHistory(History *history) { +void Manager::doClearFromHistory(not_null history) { _private->clearFromHistory(history); } #endif // !TDESKTOP_DISABLE_GTK_INTEGRATION diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h index 32bfc06ef7..f40204a56c 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.h @@ -45,7 +45,7 @@ protected: bool hideNameAndPhoto, bool hideReplyButton) override; void doClearAllFast() override; - void doClearFromHistory(History *history) override; + void doClearFromHistory(not_null history) override; private: class Private; diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h index 0c19b16937..19a2791023 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.h @@ -31,7 +31,7 @@ protected: bool hideNameAndPhoto, bool hideReplyButton) override; void doClearAllFast() override; - void doClearFromHistory(History *history) override; + void doClearFromHistory(not_null history) override; private: class Private; diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm index 7482b0afdb..cca6b0a6c9 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm @@ -170,7 +170,7 @@ public: bool hideNameAndPhoto, bool hideReplyButton); void clearAll(); - void clearFromHistory(History *history); + void clearFromHistory(not_null history); void updateDelegate(); ~Private(); @@ -321,7 +321,7 @@ void Manager::Private::clearAll() { putClearTask(ClearAll()); } -void Manager::Private::clearFromHistory(History *history) { +void Manager::Private::clearFromHistory(not_null history) { putClearTask(ClearFromHistory { history->peer->id }); } @@ -368,7 +368,7 @@ void Manager::doClearAllFast() { _private->clearAll(); } -void Manager::doClearFromHistory(History *history) { +void Manager::doClearFromHistory(not_null history) { _private->clearFromHistory(history); } diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp index c49cf71085..bad199790e 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp @@ -351,7 +351,7 @@ public: bool hideNameAndPhoto, bool hideReplyButton); void clearAll(); - void clearFromHistory(History *history); + void clearFromHistory(not_null history); void beforeNotificationActivated(PeerId peerId, MsgId msgId); void afterNotificationActivated(PeerId peerId, MsgId msgId); void clearNotification(PeerId peerId, MsgId msgId); @@ -420,7 +420,7 @@ void Manager::Private::clearAll() { } } -void Manager::Private::clearFromHistory(History *history) { +void Manager::Private::clearFromHistory(not_null history) { if (!_notifier) return; auto i = _notifications.find(history->peer->id); @@ -600,7 +600,7 @@ void Manager::doClearAllFast() { _private->clearAll(); } -void Manager::doClearFromHistory(History *history) { +void Manager::doClearFromHistory(not_null history) { _private->clearFromHistory(history); } diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.h b/Telegram/SourceFiles/platform/win/notifications_manager_win.h index fb4e343229..8ac5adb837 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.h +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.h @@ -32,7 +32,7 @@ protected: bool hideNameAndPhoto, bool hideReplyButton) override; void doClearAllFast() override; - void doClearFromHistory(History *history) override; + void doClearFromHistory(not_null history) override; void onBeforeNotificationActivated(PeerId peerId, MsgId msgId) override; void onAfterNotificationActivated(PeerId peerId, MsgId msgId) override; diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index 2fde585088..8f021de4a8 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -154,7 +154,7 @@ void System::clearAll() { _settingWaiters.clear(); } -void System::clearFromHistory(History *history) { +void System::clearFromHistory(not_null history) { _manager->clearFromHistory(history); history->clearNotifications(); @@ -167,7 +167,13 @@ void System::clearFromHistory(History *history) { showNext(); } -void System::clearFromItem(HistoryItem *item) { +void System::clearIncomingFromHistory(not_null history) { + _manager->clearFromHistory(history); + history->clearIncomingNotifications(); + _whenAlerts.remove(history); +} + +void System::clearFromItem(not_null item) { _manager->clearFromItem(item); } diff --git a/Telegram/SourceFiles/window/notifications_manager.h b/Telegram/SourceFiles/window/notifications_manager.h index b0cb185fd4..3a0cbc0e73 100644 --- a/Telegram/SourceFiles/window/notifications_manager.h +++ b/Telegram/SourceFiles/window/notifications_manager.h @@ -63,8 +63,9 @@ public: void checkDelayed(); void schedule(not_null item); - void clearFromHistory(History *history); - void clearFromItem(HistoryItem *item); + void clearFromHistory(not_null history); + void clearIncomingFromHistory(not_null history); + void clearFromItem(not_null item); void clearAll(); void clearAllFast(); void updateAll(); @@ -148,10 +149,10 @@ public: void clearAllFast() { doClearAllFast(); } - void clearFromItem(HistoryItem *item) { + void clearFromItem(not_null item) { doClearFromItem(item); } - void clearFromHistory(History *history) { + void clearFromHistory(not_null history) { doClearFromHistory(history); } @@ -181,8 +182,8 @@ protected: int forwardedCount) = 0; virtual void doClearAll() = 0; virtual void doClearAllFast() = 0; - virtual void doClearFromItem(HistoryItem *item) = 0; - virtual void doClearFromHistory(History *history) = 0; + virtual void doClearFromItem(not_null item) = 0; + virtual void doClearFromHistory(not_null history) = 0; virtual void onBeforeNotificationActivated(PeerId peerId, MsgId msgId) { } virtual void onAfterNotificationActivated(PeerId peerId, MsgId msgId) { @@ -207,7 +208,7 @@ protected: void doClearAll() override { doClearAllFast(); } - void doClearFromItem(HistoryItem *item) override { + void doClearFromItem(not_null item) override { } void doShowNotification( not_null item, diff --git a/Telegram/SourceFiles/window/notifications_manager_default.cpp b/Telegram/SourceFiles/window/notifications_manager_default.cpp index e2b8596848..b1596492a4 100644 --- a/Telegram/SourceFiles/window/notifications_manager_default.cpp +++ b/Telegram/SourceFiles/window/notifications_manager_default.cpp @@ -317,7 +317,7 @@ void Manager::doClearAllFast() { base::take(_hideAll); } -void Manager::doClearFromHistory(History *history) { +void Manager::doClearFromHistory(not_null history) { for (auto i = _queuedNotifications.begin(); i != _queuedNotifications.cend();) { if (i->history == history) { i = _queuedNotifications.erase(i); @@ -333,8 +333,8 @@ void Manager::doClearFromHistory(History *history) { showNextFromQueue(); } -void Manager::doClearFromItem(HistoryItem *item) { - _queuedNotifications.erase(std::remove_if(_queuedNotifications.begin(), _queuedNotifications.end(), [item](auto &queued) { +void Manager::doClearFromItem(not_null item) { + _queuedNotifications.erase(std::remove_if(_queuedNotifications.begin(), _queuedNotifications.end(), [&](auto &queued) { return (queued.item == item); }), _queuedNotifications.cend()); diff --git a/Telegram/SourceFiles/window/notifications_manager_default.h b/Telegram/SourceFiles/window/notifications_manager_default.h index a1dce28694..a7ec1a0b1d 100644 --- a/Telegram/SourceFiles/window/notifications_manager_default.h +++ b/Telegram/SourceFiles/window/notifications_manager_default.h @@ -57,8 +57,8 @@ private: int forwardedCount) override; void doClearAll() override; void doClearAllFast() override; - void doClearFromHistory(History *history) override; - void doClearFromItem(HistoryItem *item) override; + void doClearFromHistory(not_null history) override; + void doClearFromItem(not_null item) override; void showNextFromQueue(); void unlinkFromShown(Notification *remove);