Auto delete in-app notifications #1052

This commit is contained in:
jokob-sk
2025-05-10 14:33:27 +10:00
parent a392803478
commit 01f7a18dce
31 changed files with 208 additions and 90 deletions

View File

@@ -31,8 +31,8 @@ from api import update_api
from scan.session_events import process_scan
from initialise import importConfigs
from database import DB
from reporting import get_notifications
from notification import Notification_obj
from messaging.reporting import get_notifications
from models.notification_instance import NotificationInstance
from plugin import plugin_manager
from scan.device_handling import update_devices_names
from workflows.manager import WorkflowManager
@@ -172,7 +172,7 @@ def main ():
final_json = get_notifications(db)
# Write the notifications into the DB
notification = Notification_obj(db)
notification = NotificationInstance(db)
notificationObj = notification.create(final_json, "")
# run all enabled publisher gateways

View File

@@ -9,8 +9,8 @@ from const import (apiPath, sql_appevents, sql_devices_all, sql_events_pending_a
from logger import mylog
from helper import write_file, get_setting_value, timeNowTZ
from app_state import updateState
from user_events_queue import UserEventsQueue
from notification import write_notification
from models.user_events_queue_instance import UserEventsQueueInstance
from messaging.in_app import write_notification
# Import the start_server function
from graphql_server.graphql_server_start import start_server
@@ -147,7 +147,7 @@ class api_endpoint_class:
# Update user event execution log
# mylog('verbose', [f'[API] api_endpoint_class: is_ad_hoc_user_event {self.is_ad_hoc_user_event}'])
if self.is_ad_hoc_user_event:
execution_log = UserEventsQueue()
execution_log = UserEventsQueueInstance()
execution_log.finalize_event("update_api")
self.is_ad_hoc_user_event = False

View File

@@ -11,7 +11,7 @@ sys.path.extend([f"{INSTALL_PATH}/server"])
from logger import mylog
from helper import get_setting_value, timeNowTZ
from app_state import updateState
from notification import write_notification
from messaging.in_app import write_notification
# Flask application
app = Flask(__name__)

View File

@@ -19,7 +19,7 @@ from api import update_api
from scheduler import schedule_class
from plugin import plugin_manager, print_plugin_info
from plugin_utils import get_plugins_configs, get_set_value_for_init
from notification import write_notification
from messaging.in_app import write_notification
from crypto_utils import get_random_bytes
#===============================================================================

105
server/messaging/in_app.py Executable file
View File

@@ -0,0 +1,105 @@
import datetime
import os
import sys
import _io
import json
import uuid
import socket
import subprocess
import requests
from yattag import indent
from json2table import convert
# Register NetAlertX directories
INSTALL_PATH="/app"
sys.path.extend([f"{INSTALL_PATH}/server"])
# Register NetAlertX modules
import conf
from const import applicationPath, logPath, apiPath, confFileName, reportTemplatesPath
from logger import logResult, mylog
from helper import generate_mac_links, removeDuplicateNewLines, timeNowTZ, get_file_content, write_file, get_setting_value, get_timezone_offset
NOTIFICATION_API_FILE = apiPath + 'user_notifications.json'
# Show Frontend User Notification
def write_notification(content, level, timestamp):
# Generate GUID
guid = str(uuid.uuid4())
# Prepare notification dictionary
notification = {
'timestamp': str(timestamp),
'guid': guid,
'read': 0,
'level': level,
'content': content
}
# If file exists, load existing data, otherwise initialize as empty list
if os.path.exists(NOTIFICATION_API_FILE):
with open(NOTIFICATION_API_FILE, 'r') as file:
# Check if the file object is of type _io.TextIOWrapper
if isinstance(file, _io.TextIOWrapper):
file_contents = file.read() # Read file contents
if file_contents == '':
file_contents = '[]' # If file is empty, initialize as empty list
# mylog('debug', ['[Notification] User Notifications file: ', file_contents])
notifications = json.loads(file_contents) # Parse JSON data
else:
mylog('none', '[Notification] File is not of type _io.TextIOWrapper')
notifications = []
else:
notifications = []
# Append new notification
notifications.append(notification)
# Write updated data back to file
with open(NOTIFICATION_API_FILE, 'w') as file:
json.dump(notifications, file, indent=4)
# Trim notifications
def remove_old(keepNumberOfEntries):
# Check if file exists
if not os.path.exists(NOTIFICATION_API_FILE):
mylog('info', '[Notification] No notifications file to clean.')
return
# Load existing notifications
try:
with open(NOTIFICATION_API_FILE, 'r') as file:
file_contents = file.read().strip()
if file_contents == '':
notifications = []
else:
notifications = json.loads(file_contents)
except Exception as e:
mylog('none', f'[Notification] Error reading notifications file: {e}')
return
if not isinstance(notifications, list):
mylog('none', '[Notification] Invalid format: not a list')
return
# Sort by timestamp descending
try:
notifications.sort(key=lambda x: x['timestamp'], reverse=True)
except KeyError:
mylog('none', '[Notification] Missing timestamp in one or more entries')
return
# Trim to the latest entries
trimmed = notifications[:keepNumberOfEntries]
# Write back the trimmed list
try:
with open(NOTIFICATION_API_FILE, 'w') as file:
json.dump(trimmed, file, indent=4)
mylog('verbose', f'[Notification] Trimmed notifications to latest {keepNumberOfEntries}')
except Exception as e:
mylog('none', f'Error writing trimmed notifications file: {e}')

View File

@@ -12,6 +12,11 @@
import datetime
import json
import sys
# Register NetAlertX directories
INSTALL_PATH="/app"
sys.path.extend([f"{INSTALL_PATH}/server"])
import conf
from const import applicationPath, logPath, apiPath, confFileName

View File

@@ -2,6 +2,7 @@ import datetime
import os
import _io
import json
import sys
import uuid
import socket
import subprocess
@@ -9,16 +10,22 @@ import requests
from yattag import indent
from json2table import convert
# Register NetAlertX directories
INSTALL_PATH="/app"
sys.path.extend([f"{INSTALL_PATH}/server"])
# Register NetAlertX modules
import conf
from const import applicationPath, logPath, apiPath, confFileName, reportTemplatesPath
from logger import logResult, mylog
from helper import generate_mac_links, removeDuplicateNewLines, timeNowTZ, get_file_content, write_file, get_setting_value, get_timezone_offset
from messaging.in_app import write_notification
#-------------------------------------------------------------------------------
# Notification object handling
#-------------------------------------------------------------------------------
class Notification_obj:
class NotificationInstance:
def __init__(self, db):
self.db = db
@@ -290,45 +297,7 @@ class Notification_obj:
# Reporting
#-------------------------------------------------------------------------------
# Handle Frontend User Notifications
def write_notification(content, level, timestamp):
NOTIFICATION_API_FILE = apiPath + 'user_notifications.json'
# Generate GUID
guid = str(uuid.uuid4())
# Prepare notification dictionary
notification = {
'timestamp': str(timestamp),
'guid': guid,
'read': 0,
'level': level,
'content': content
}
# If file exists, load existing data, otherwise initialize as empty list
if os.path.exists(NOTIFICATION_API_FILE):
with open(NOTIFICATION_API_FILE, 'r') as file:
# Check if the file object is of type _io.TextIOWrapper
if isinstance(file, _io.TextIOWrapper):
file_contents = file.read() # Read file contents
if file_contents == '':
file_contents = '[]' # If file is empty, initialize as empty list
# mylog('debug', ['[Notification] User Notifications file: ', file_contents])
notifications = json.loads(file_contents) # Parse JSON data
else:
mylog('error', 'File is not of type _io.TextIOWrapper')
notifications = []
else:
notifications = []
# Append new notification
notifications.append(notification)
# Write updated data back to file
with open(NOTIFICATION_API_FILE, 'w') as file:
json.dump(notifications, file, indent=4)
#-------------------------------------------------------------------------------
def construct_notifications(JSON, section):

View File

@@ -1,10 +1,15 @@
import os
import sys
# Register NetAlertX directories
INSTALL_PATH="/app"
sys.path.extend([f"{INSTALL_PATH}/server"])
# Register NetAlertX modules
from const import pluginsPath, logPath, applicationPath, reportTemplatesPath
from logger import mylog
class UserEventsQueue:
class UserEventsQueueInstance:
"""
Handles the execution queue log file, allowing reading, writing,
and removing processed events.
@@ -30,7 +35,7 @@ class UserEventsQueue:
Returns an empty list if the file doesn't exist.
"""
if not os.path.exists(self.log_file):
mylog('none', ['[UserEventsQueue] Log file not found: ', self.log_file])
mylog('none', ['[UserEventsQueueInstance] Log file not found: ', self.log_file])
return [] # No log file, return empty list
with open(self.log_file, "r") as file:
return file.readlines()
@@ -72,7 +77,7 @@ class UserEventsQueue:
self.write_log(updated_lines)
mylog('minimal', ['[UserEventsQueue] Processed event: ', event])
mylog('minimal', ['[UserEventsQueueInstance] Processed event: ', event])
return removed

View File

@@ -16,11 +16,11 @@ from helper import timeNowTZ, get_file_content, write_file, get_setting, get_set
from app_state import updateState
from api import update_api
from plugin_utils import logEventStatusCounts, get_plugin_string, get_plugin_setting_obj, print_plugin_info, list_to_csv, combine_plugin_objects, resolve_wildcards_arr, handle_empty, custom_plugin_decoder, decode_and_rename_files
from notification import Notification_obj, write_notification
from user_events_queue import UserEventsQueue
from models.notification_instance import NotificationInstance
from messaging.in_app import write_notification
from models.user_events_queue_instance import UserEventsQueueInstance
from crypto_utils import generate_deterministic_guid
#-------------------------------------------------------------------------------
class plugin_manager:
def __init__(self, db, all_plugins):
@@ -79,7 +79,7 @@ class plugin_manager:
"""
Process user events from the execution queue log file and notify the user about executed events.
"""
execution_log = UserEventsQueue()
execution_log = UserEventsQueueInstance()
# Track whether to show notification for executed events
executed_events = []
@@ -151,7 +151,7 @@ class plugin_manager:
sample_json = json.loads(get_file_content(reportTemplatesPath + 'webhook_json_sample.json'))[0]["body"]["attachments"][0]["text"]
# Create fake notification
notification = Notification_obj(self.db)
notification = NotificationInstance(self.db)
notificationObj = notification.create(sample_json, "")
# Run test
@@ -562,7 +562,7 @@ def execute_plugin(db, all_plugins, plugin ):
endpoints = ["plugins_events","plugins_objects", "plugins_history", "appevents"]
# check if we need to update devices api endpoint as well to prevent long user waits on Loading...
userUpdatedDevices = UserEventsQueue().has_update_devices
userUpdatedDevices = UserEventsQueueInstance().has_update_devices
if userUpdatedDevices:
endpoints += ["devices"]

View File

@@ -8,7 +8,7 @@ import conf
from scan.device_handling import create_new_devices, print_scan_stats, save_scanned_devices, update_devices_data_from_scan, exclude_ignored_devices
from helper import timeNowTZ
from logger import mylog
from reporting import skip_repeated_notifications
from messaging.reporting import skip_repeated_notifications
#===============================================================================