From 74d07effd067dcdf9bb20f1aa613a698fa56d88a Mon Sep 17 00:00:00 2001 From: Nicholas Tindle Date: Tue, 11 Feb 2025 22:36:04 -0600 Subject: [PATCH] feat(backend): clean up + better handling --- autogpt_platform/backend/backend/data/user.py | 8 ++++++++ .../backend/backend/executor/database.py | 2 ++ .../backend/backend/notifications/email.py | 16 ++++++++++++---- .../backend/notifications/notifications.py | 3 +-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.py index 166bc449be..3ea1eb991c 100644 --- a/autogpt_platform/backend/backend/data/user.py +++ b/autogpt_platform/backend/backend/data/user.py @@ -53,6 +53,14 @@ async def get_user_by_id(user_id: str) -> User: return User.model_validate(user) +async def get_user_email_by_id(user_id: str) -> str: + try: + user = await prisma.user.find_unique_or_raise(where={"id": user_id}) + return user.email + except Exception as e: + raise DatabaseError(f"Failed to get user email for user {user_id}: {e}") from e + + async def create_default_user() -> Optional[User]: user = await prisma.user.find_unique(where={"id": DEFAULT_USER_ID}) if not user: diff --git a/autogpt_platform/backend/backend/executor/database.py b/autogpt_platform/backend/backend/executor/database.py index 75730b3726..02e1535f5e 100644 --- a/autogpt_platform/backend/backend/executor/database.py +++ b/autogpt_platform/backend/backend/executor/database.py @@ -28,6 +28,7 @@ from backend.data.user import ( get_active_user_ids_in_timerange, get_active_users_ids, get_user_by_id, + get_user_email_by_id, get_user_integrations, get_user_metadata, get_user_notification_preference, @@ -105,6 +106,7 @@ class DatabaseManager(AppService): get_active_user_ids_in_timerange ) get_user_by_id = exposed_run_and_wait(get_user_by_id) + get_user_email_by_id = exposed_run_and_wait(get_user_email_by_id) get_user_notification_preference = exposed_run_and_wait( get_user_notification_preference ) diff --git a/autogpt_platform/backend/backend/notifications/email.py b/autogpt_platform/backend/backend/notifications/email.py index b24e82ee62..9ede68a6f7 100644 --- a/autogpt_platform/backend/backend/notifications/email.py +++ b/autogpt_platform/backend/backend/notifications/email.py @@ -1,7 +1,6 @@ import logging import pathlib -from backend.util.text import TextFormatter from postmarker.core import PostmarkClient from postmarker.models.emails import EmailManager from prisma.enums import NotificationType @@ -12,6 +11,7 @@ from backend.data.notifications import ( T_co, ) from backend.util.settings import Settings +from backend.util.text import TextFormatter logger = logging.getLogger(__name__) settings = Settings() @@ -26,9 +26,14 @@ class TypedPostmarkClient(PostmarkClient): class EmailSender: def __init__(self): - self.postmark = TypedPostmarkClient( - server_token=settings.secrets.postmark_server_api_token - ) + if settings.secrets.postmark_server_api_token: + self.postmark = TypedPostmarkClient( + server_token=settings.secrets.postmark_server_api_token + ) + else: + logger.warning( + "Postmark server API token not found, email sending disabled" + ) self.formatter = TextFormatter() def send_templated( @@ -37,6 +42,9 @@ class EmailSender: user_email: str, data: NotificationEventModel[T_co] | list[NotificationEventModel[T_co]], ): + if not self.postmark: + logger.warning("Postmark client not initialized, email not sent") + return body = self._get_template(notification) # use the jinja2 library to render the template body = self.formatter.format_string(body, data) diff --git a/autogpt_platform/backend/backend/notifications/notifications.py b/autogpt_platform/backend/backend/notifications/notifications.py index 271a998916..ebb190ce86 100644 --- a/autogpt_platform/backend/backend/notifications/notifications.py +++ b/autogpt_platform/backend/backend/notifications/notifications.py @@ -159,8 +159,7 @@ class NotificationManager(AppService): parsed_event = NotificationEventModel[ get_data_type(event.type) ].model_validate_json(message) - # Implementation of actual notification sending would go here - user_email = get_db_client().get_user_by_id(event.user_id).email + user_email = get_db_client().get_user_email_by_id(event.user_id) should_send = ( get_db_client() .get_user_notification_preference(event.user_id)