function mark_notification_read
Updates a notification's read status in a Neo4j graph database and sets a timestamp when marked as read.
/tf/active/vicechatdev/CDocs/utils/notifications.py
963 - 990
simple
Purpose
This function manages the read/unread state of notifications in a document management system. It updates the notification node in Neo4j by setting the 'read' property and optionally recording a 'readTimestamp' when marking as read. This is typically used in notification management workflows where users need to track which notifications they have viewed.
Source Code
def mark_notification_read(notification_uid: str, read: bool = True) -> bool:
"""
Mark a notification as read or unread.
Args:
notification_uid: UID of notification
read: Whether to mark as read (True) or unread (False)
Returns:
Boolean indicating success
"""
try:
db.run_query(
"""
MATCH (n:Notification {UID: $notification_uid})
SET n.read = $read, n.readTimestamp = $timestamp
""",
{
"notification_uid": notification_uid,
"read": read,
"timestamp": datetime.now() if read else None
}
)
return True
except Exception as e:
logger.error(f"Error marking notification {notification_uid} as read={read}: {e}")
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
notification_uid |
str | - | positional_or_keyword |
read |
bool | True | positional_or_keyword |
Parameter Details
notification_uid: String identifier (UID) of the notification to update. This should be a unique identifier that matches a Notification node's UID property in the Neo4j database. Expected to be a valid UUID or similar unique string.
read: Boolean flag indicating the desired read state. True marks the notification as read and sets a timestamp; False marks it as unread and clears the timestamp. Defaults to True if not specified.
Return Value
Type: bool
Returns a boolean value indicating the success of the operation. Returns True if the database query executed successfully and the notification was updated. Returns False if any exception occurred during the database operation, with the error logged for debugging.
Dependencies
CDocsdatetimelogging
Required Imports
from datetime import datetime
import logging
from CDocs import db
Usage Example
from datetime import datetime
import logging
from CDocs import db
# Configure logger
logger = logging.getLogger(__name__)
# Mark a notification as read
notification_id = "550e8400-e29b-41d4-a716-446655440000"
success = mark_notification_read(notification_id, read=True)
if success:
print(f"Notification {notification_id} marked as read")
else:
print(f"Failed to mark notification {notification_id} as read")
# Mark a notification as unread
success = mark_notification_read(notification_id, read=False)
if success:
print(f"Notification {notification_id} marked as unread")
Best Practices
- Always check the return value to ensure the notification was successfully updated before proceeding with dependent operations
- Ensure the notification_uid exists in the database before calling this function to avoid silent failures
- The function logs errors but does not raise exceptions, so implement proper error handling based on the boolean return value
- When marking as unread (read=False), the readTimestamp is set to None, effectively clearing any previous read timestamp
- Consider implementing retry logic for transient database connection failures
- The function uses parameterized queries to prevent Cypher injection attacks
- Ensure the logger is properly configured before using this function to capture error messages
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function delete_notification 68.2% similar
-
function delete_all_notifications 67.8% similar
-
function count_user_notifications 63.1% similar
-
function get_user_notifications 55.7% similar
-
function create_notification 55.0% similar