Imporvment
This commit is contained in:
1 parent
ac484bc682
commit
630d26d6f0
1 file changed
+85
-60
@@ -294,7 +294,7 @@ class CachedNotifications(Notifications):
|
|||||||
self.clear_all.emit()
|
self.clear_all.emit()
|
||||||
|
|
||||||
def on_notification_added(self, service, notification_id: int) -> None:
|
def on_notification_added(self, service, notification_id: int) -> None:
|
||||||
"""Handle notification added and cache it with enhanced metadata"""
|
"""Handle notification added and cache it with enhanced metadata - GUARANTEED STORAGE"""
|
||||||
# Don't call super() - we're handling this ourselves
|
# Don't call super() - we're handling this ourselves
|
||||||
|
|
||||||
# Import logger at the top of the function
|
# Import logger at the top of the function
|
||||||
@@ -302,48 +302,75 @@ class CachedNotifications(Notifications):
|
|||||||
|
|
||||||
notification = self.get_notification_from_id(notification_id)
|
notification = self.get_notification_from_id(notification_id)
|
||||||
|
|
||||||
if notification:
|
if not notification:
|
||||||
# Import here to avoid circular imports
|
logger.error(f"CRITICAL: Failed to get notification with ID {notification_id}")
|
||||||
from config import data
|
return
|
||||||
from modules.notification.notification import (
|
|
||||||
preload_notification_assets,
|
|
||||||
cache_notification_icon,
|
|
||||||
cache_notification_image,
|
|
||||||
get_cache_key,
|
|
||||||
get_notification_image_cache_key
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check if this app should be ignored for history (don't cache)
|
# Import here to avoid circular imports
|
||||||
if notification.app_name in data.NOTIFICATION_IGNORED_APPS_HISTORY:
|
from config import data
|
||||||
# Don't cache notifications from ignored apps, but still allow popup display
|
from modules.notification.notification import (
|
||||||
logger.debug(f"Ignoring notification from {notification.app_name} (in ignore list)")
|
preload_notification_assets,
|
||||||
return
|
cache_notification_icon,
|
||||||
|
cache_notification_image,
|
||||||
|
get_cache_key,
|
||||||
|
get_notification_image_cache_key
|
||||||
|
)
|
||||||
|
|
||||||
# Check if we already have this notification cached (by notification.id)
|
# Check if this app should be ignored for history (don't cache)
|
||||||
existing_notification = None
|
if notification.app_name in data.NOTIFICATION_IGNORED_APPS_HISTORY:
|
||||||
for cached_notif in self._cached_notifications.values():
|
# Don't cache notifications from ignored apps, but still allow popup display
|
||||||
if cached_notif._notification.id == notification.id:
|
logger.debug(f"Ignoring notification from {notification.app_name} (in ignore list)")
|
||||||
existing_notification = cached_notif
|
return
|
||||||
break
|
|
||||||
|
|
||||||
if existing_notification:
|
# Check if we already have this notification cached (by notification.id)
|
||||||
logger.debug(f"Notification ID {notification.id} already cached, skipping")
|
existing_notification = None
|
||||||
return
|
for cached_notif in self._cached_notifications.values():
|
||||||
|
if cached_notif._notification.id == notification.id:
|
||||||
|
existing_notification = cached_notif
|
||||||
|
break
|
||||||
|
|
||||||
logger.debug(f"Caching new notification: ID={notification.id}, App={notification.app_name}, Summary={notification.summary[:50]}...")
|
if existing_notification:
|
||||||
|
logger.debug(f"Notification ID {notification.id} already cached, skipping")
|
||||||
|
return
|
||||||
|
|
||||||
# Cache notifications (except for ignored apps)
|
logger.debug(f"Caching new notification: ID={notification.id}, App={notification.app_name}, Summary={notification.summary[:50]}...")
|
||||||
# DND only affects popup display, not caching
|
|
||||||
cache_id = self._next_cache_id
|
|
||||||
self._next_cache_id += 1
|
|
||||||
self._count += 1
|
|
||||||
|
|
||||||
cached_notification = CachedNotification(
|
# GUARANTEED STORAGE: Always create and store notification to history first
|
||||||
notification=notification, cache_id=cache_id
|
cache_id = self._next_cache_id
|
||||||
)
|
self._next_cache_id += 1
|
||||||
# Set cache_id directly since it's read-only property
|
self._count += 1
|
||||||
cached_notification._cache_id = cache_id
|
|
||||||
|
|
||||||
|
cached_notification = CachedNotification(
|
||||||
|
notification=notification, cache_id=cache_id
|
||||||
|
)
|
||||||
|
# Set cache_id directly since it's read-only property
|
||||||
|
cached_notification._cache_id = cache_id
|
||||||
|
|
||||||
|
# Initialize cache metadata (will be populated below)
|
||||||
|
cached_notification.cache_metadata = {
|
||||||
|
"app_icon_cache_key": None,
|
||||||
|
"notification_image_cache_key": None,
|
||||||
|
"has_cached_image": False,
|
||||||
|
"cache_timestamp": int(time.time())
|
||||||
|
}
|
||||||
|
|
||||||
|
# IMMEDIATELY store to history before attempting any caching operations
|
||||||
|
handler_id = cached_notification.connect(
|
||||||
|
"removed-from-cache",
|
||||||
|
lambda *args: self.remove_cached_notification(notification_id=cache_id),
|
||||||
|
)
|
||||||
|
self._signal_handlers[cache_id] = handler_id
|
||||||
|
self._cached_notifications[cache_id] = cached_notification
|
||||||
|
|
||||||
|
# Save to JSON file immediately - GUARANTEED STORAGE
|
||||||
|
try:
|
||||||
|
self.cache_notifications()
|
||||||
|
logger.debug(f"GUARANTEED: Notification {cache_id} stored to history")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"CRITICAL: Failed to save notification {cache_id} to history: {e}")
|
||||||
|
|
||||||
|
# Now attempt asset caching (failures here won't affect history storage)
|
||||||
|
try:
|
||||||
# Preload assets and store cache metadata
|
# Preload assets and store cache metadata
|
||||||
preload_notification_assets(notification)
|
preload_notification_assets(notification)
|
||||||
|
|
||||||
@@ -352,8 +379,13 @@ class CachedNotifications(Notifications):
|
|||||||
notification_image_cache_key = None
|
notification_image_cache_key = None
|
||||||
|
|
||||||
if notification.app_icon:
|
if notification.app_icon:
|
||||||
app_icon_cache_key = get_cache_key(notification.app_icon, (35, 35), notification.app_name)
|
try:
|
||||||
cache_notification_icon(notification.app_icon, (35, 35), notification.app_name)
|
app_icon_cache_key = get_cache_key(notification.app_icon, (35, 35), notification.app_name)
|
||||||
|
cache_notification_icon(notification.app_icon, (35, 35), notification.app_name)
|
||||||
|
cached_notification.cache_metadata["app_icon_cache_key"] = app_icon_cache_key
|
||||||
|
logger.debug(f"Cached app icon for notification {cache_id}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to cache app icon for notification {cache_id}: {e}")
|
||||||
|
|
||||||
if hasattr(notification, 'image_pixbuf'):
|
if hasattr(notification, 'image_pixbuf'):
|
||||||
try:
|
try:
|
||||||
@@ -364,33 +396,26 @@ class CachedNotifications(Notifications):
|
|||||||
notification.id, image_pixbuf
|
notification.id, image_pixbuf
|
||||||
)
|
)
|
||||||
cache_notification_image(notification.id, image_pixbuf, (35, 35))
|
cache_notification_image(notification.id, image_pixbuf, (35, 35))
|
||||||
except (AttributeError, OSError, Exception):
|
cached_notification.cache_metadata["notification_image_cache_key"] = notification_image_cache_key
|
||||||
# If temp file is gone or any other error, skip caching
|
cached_notification.cache_metadata["has_cached_image"] = True
|
||||||
pass
|
logger.debug(f"Cached notification image for notification {cache_id}")
|
||||||
|
except (AttributeError, OSError, Exception) as e:
|
||||||
|
logger.warning(f"Failed to cache notification image for notification {cache_id}: {e}")
|
||||||
|
|
||||||
# Store metadata in cached notification
|
# Update cached notification with final metadata
|
||||||
cached_notification.cache_metadata = {
|
|
||||||
"app_icon_cache_key": app_icon_cache_key,
|
|
||||||
"notification_image_cache_key": notification_image_cache_key,
|
|
||||||
"has_cached_image": notification_image_cache_key is not None,
|
|
||||||
"cache_timestamp": int(time.time())
|
|
||||||
}
|
|
||||||
|
|
||||||
handler_id = cached_notification.connect(
|
|
||||||
"removed-from-cache",
|
|
||||||
lambda *args: self.remove_cached_notification(notification_id=cache_id),
|
|
||||||
)
|
|
||||||
|
|
||||||
self._signal_handlers[cache_id] = handler_id
|
|
||||||
self._cached_notifications[cache_id] = cached_notification
|
self._cached_notifications[cache_id] = cached_notification
|
||||||
|
|
||||||
|
# Save updated metadata to JSON
|
||||||
self.cache_notifications()
|
self.cache_notifications()
|
||||||
|
|
||||||
self.notify("count")
|
except Exception as e:
|
||||||
self.emit("cached-notification-added", cached_notification)
|
logger.error(f"Asset caching failed for notification {cache_id}, but notification is still stored: {e}")
|
||||||
|
|
||||||
logger.debug(f"Successfully cached notification: Cache ID={cache_id}, Total cached={len(self._cached_notifications)}")
|
# Always emit signals regardless of caching success
|
||||||
else:
|
self.notify("count")
|
||||||
print(f"WARNING: Failed to get notification with ID {notification_id}")
|
self.emit("cached-notification-added", cached_notification)
|
||||||
|
|
||||||
|
logger.debug(f"Successfully processed notification: Cache ID={cache_id}, Total cached={len(self._cached_notifications)}")
|
||||||
|
|
||||||
def remove_cached_notification(self, notification_id: int):
|
def remove_cached_notification(self, notification_id: int):
|
||||||
"""Remove the notification of given id with enhanced cache cleanup"""
|
"""Remove the notification of given id with enhanced cache cleanup"""
|
||||||
|
|||||||
Reference in new issue
Block a user