From f526a9adb3a95fb46ce9f044d7cb37bbbbb5fa94 Mon Sep 17 00:00:00 2001 From: DJ2LS Date: Tue, 6 Feb 2024 20:33:36 +0100 Subject: [PATCH] cleanup and fix for empty message body --- modem/api_validations.py | 1 - modem/command_message_send.py | 7 ------- modem/message_p2p.py | 2 -- modem/message_system_db_attachments.py | 13 +------------ modem/message_system_db_manager.py | 5 +---- modem/message_system_db_model.py | 2 +- 6 files changed, 3 insertions(+), 27 deletions(-) diff --git a/modem/api_validations.py b/modem/api_validations.py index 755c5a6e..b07cec3e 100644 --- a/modem/api_validations.py +++ b/modem/api_validations.py @@ -6,7 +6,6 @@ def validate_freedata_callsign(callsign): return re.compile(regexp).match(callsign) is not None def validate_message_attachment(attachment): - print(attachment) for field in ['name', 'type', 'data']: if field not in attachment: raise ValueError(f"Attachment missing '{field}'") diff --git a/modem/command_message_send.py b/modem/command_message_send.py index 8f28f0f8..ca22ef96 100644 --- a/modem/command_message_send.py +++ b/modem/command_message_send.py @@ -13,12 +13,8 @@ class SendMessageCommand(TxCommand): """ def set_params_from_api(self, apiParams): - print(apiParams) origin = f"{self.config['STATION']['mycall']}-{self.config['STATION']['myssid']}" self.message = MessageP2P.from_api_params(origin, apiParams) - print(self.message.id) - print(self.message.to_dict()) - print("--------------------------------------- set params from api") DatabaseManagerMessages(self.event_manager).add_message(self.message.to_dict(), direction='transmit', status='queued') def transmit(self, modem): @@ -35,10 +31,7 @@ class SendMessageCommand(TxCommand): self.log(f"Queued message found: {first_queued_message['id']}") DatabaseManagerMessages(self.event_manager).update_message(first_queued_message["id"], update_data={'status': 'transmitting'}) message_dict = DatabaseManagerMessages(self.event_manager).get_message_by_id(first_queued_message["id"]) - print(message_dict["id"]) message = MessageP2P.from_api_params(message_dict['origin'], message_dict) - print(message.id) - print("--------------------------------------- transmit") # Convert JSON string to bytes (using UTF-8 encoding) payload = message.to_payload().encode('utf-8') diff --git a/modem/message_p2p.py b/modem/message_p2p.py index 5bb363c5..e1eb4e99 100644 --- a/modem/message_p2p.py +++ b/modem/message_p2p.py @@ -42,8 +42,6 @@ class MessageP2P: raise ValueError(f"Invalid destination given ({params['destination']})") body = params['body'] - if len(body) < 1: - raise ValueError(f"Body cannot be empty") attachments = [] if 'attachments' in params: diff --git a/modem/message_system_db_attachments.py b/modem/message_system_db_attachments.py index 5f7cff04..e2a10e57 100644 --- a/modem/message_system_db_attachments.py +++ b/modem/message_system_db_attachments.py @@ -11,17 +11,6 @@ class DatabaseManagerAttachments(DatabaseManager): def add_attachment(self, session, message, attachment_data): - """ - Adds an attachment to a message, either by creating a new attachment or reusing an existing one. - - Args: - - session: The current database session. - - message: The P2PMessage instance to which the attachment should be linked. - - attachment_data: A dictionary containing the attachment's data. - - Returns: - - The Attachment instance. - """ hash_sha512 = hashlib.sha512(attachment_data['data'].encode()).hexdigest() existing_attachment = session.query(Attachment).filter_by(hash_sha512=hash_sha512).first() @@ -70,7 +59,7 @@ class DatabaseManagerAttachments(DatabaseManager): try: attachment = session.query(Attachment).filter_by(hash_sha512=hash_sha512).first() if attachment: - return attachment.to_dict() # Assuming you have a to_dict method + return attachment.to_dict() else: self.log(f"No attachment found with SHA-512 hash: {hash_sha512}") return None diff --git a/modem/message_system_db_manager.py b/modem/message_system_db_manager.py index 69faf113..c0d5d1c9 100644 --- a/modem/message_system_db_manager.py +++ b/modem/message_system_db_manager.py @@ -4,12 +4,9 @@ import sqlite3 from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from threading import local -from message_system_db_model import Base, Station, Status, Attachment, P2PMessage, MessageAttachment -from datetime import datetime -import json +from message_system_db_model import Base, Station, Status import structlog import helpers -import hashlib class DatabaseManager: def __init__(self, event_manger, uri='sqlite:///freedata-messages.db'): diff --git a/modem/message_system_db_model.py b/modem/message_system_db_model.py index 6129fa18..b991b94b 100644 --- a/modem/message_system_db_model.py +++ b/modem/message_system_db_model.py @@ -1,6 +1,6 @@ # models.py -from sqlalchemy import Index, Table, Boolean, Column, String, Integer, JSON, ForeignKey, DateTime +from sqlalchemy import Index, Boolean, Column, String, Integer, JSON, ForeignKey, DateTime from sqlalchemy.orm import declarative_base, relationship Base = declarative_base()