From 4dd14b81e1b86d3811bd7b3fd5d1964bf2ba2710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Gro=C3=9F?= Date: Fri, 4 Mar 2022 13:46:13 +0100 Subject: [PATCH] Do not remove notification reference on NotificationClosed In general we need to keep a reference to the notification id, so that we can delete the notification later from history - unless the NotificationClosed reason was that the user actively dismissed it, in which case it is not kept in history anyway (so we can dismiss our reference too). -- Background -- Some desktop environments such as KDE keep a history of notifications. An API is provided to delete notifications from that history by calling the org.freedesktop.Notifications.CloseNotification endpoint with the ID of that notification. If the notification was already closed (timed out), then this will delete the notification from history. The intent is to clear these notifications from the notification history as soon as a chat with notifications originating from that person is opened, as the user is then not interested anymore in those notifications and to prevent unnecessary clutter in the history widget. It is also cleared when the chat is read on another device. -- Problem -- Telegram already has all the code in place to support this functionality, but unfortunately this did not work on Linux before, because we listen to the NotificationClosed signal and remove our reference to the notification id from our internal manager as soon as we get that signal. This means that we do not clear that notification from history once we open the chat with that person (unless we open the chat before the notification has timed out, i.e. if we didn't get the NotificationClosed signal). -- Fix -- To fix this, we keep our notification reference (if the notification was not dismissed by the user), which means that our reference will be kept around until we open the chat with that person (or close Telegram entirely). Since all the needed functionality for deleting notifications was already in place, this patch is quite short as we only need to keep the reference around longer than we did before this patch. Note also that code is already in place to clear notifications for messages that were read on another device: History::inboxRead() calls Core::App().notifications().clearIncomingFromHistory() Fixes #17111 --- .../SourceFiles/history/history_inner_widget.cpp | 2 +- .../linux/notifications_manager_linux.cpp | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 38ac13d0f..e47af77b8 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -2570,7 +2570,7 @@ void HistoryInner::checkHistoryActivation() { } adjustCurrent(_visibleAreaBottom); if (_history->loadedAtBottom() && _visibleAreaBottom >= height()) { - // Clear possible scheduled messages notifications. + // Clear possible message notifications. Core::App().notifications().clearFromHistory(_history); } if (_curHistory != _history || _history->isEmpty()) { diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 4b83a1d19..c98634227 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -695,7 +695,21 @@ void NotificationData::setImage(const QString &imagePath) { } void NotificationData::notificationClosed(uint id, uint reason) { - if (id == _notificationId) { + /* + * From: https://specifications.freedesktop.org/notification-spec/latest/ar01s09.html + * The reason the notification was closed + * 1 - The notification expired. + * 2 - The notification was dismissed by the user. + * 3 - The notification was closed by a call to CloseNotification. + * 4 - Undefined/reserved reasons. + * + * If the notification was dismissed by the user (reason == 2), the notification is not kept in notification history. + * We do not need to send a "CloseNotification" call later to clear it from history. + * Therefore we can drop the notification reference now. + * In all other cases we keep the notification reference so that we may clear the notification later from history, + * if the message for that notification is read (e.g. chat is opened or read from another device). + */ + if (id == _notificationId && reason == 2) { _manager->clearNotification(_id); } }