Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions brood/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
DEFAULT_USER_GROUP_LIMIT,
group_invite_link_from_env,
TEMPLATE_ID_BUGOUT_WELCOME_EMAIL,
TEMPLATE_ID_MOONSTREAM_WELCOME_EMAIL,
MOONSTREAM_APPLICATION_ID,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -601,23 +603,34 @@ def change_password(
return user


def send_welcome_email(user: User) -> None:
def send_welcome_email(user: User, application_id: Optional[uuid.UUID] = None) -> None:
"""
Send welcome email to each new user.
"""
user_id = str(user.id)
if SENDGRID_API_KEY is None:
logger.error(
"Missed SENDGRID_API_KEY, message was not sent to user with id: {user_id}"
f"Missed SENDGRID_API_KEY, message was not sent to user with id: {user_id}"
)
return

logger.info(f"Sending welcome email for user with id={user_id}...")
message = Mail(
from_email=f"Sophia from Bugout <{BUGOUT_FROM_EMAIL}>", to_emails=user.email
)
message.dynamic_template_data = {"username": user.username}
message.template_id = TEMPLATE_ID_BUGOUT_WELCOME_EMAIL

if application_id is not None:
if str(application_id) != MOONSTREAM_APPLICATION_ID:
logger.error(
f"Unhandled welcome email for application with id: {str(application_id)}"
)
return
else:
message.template_id = TEMPLATE_ID_MOONSTREAM_WELCOME_EMAIL
else:
message.template_id = TEMPLATE_ID_BUGOUT_WELCOME_EMAIL

logger.info(f"Sending welcome email for user with id={user_id}...")

try:
sg = SendGridAPIClient(SENDGRID_API_KEY)
Expand Down
4 changes: 2 additions & 2 deletions brood/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ async def create_user_handler(
if autogenerated_user:
return user

if SEND_EMAIL_WELCOME and application_id is None:
if SEND_EMAIL_WELCOME:
background_tasks.add_task(
actions.send_welcome_email, user=user,
actions.send_welcome_email, user=user, application_id=application_id,
)

if not REQUIRE_EMAIL_VERIFICATION:
Expand Down
4 changes: 4 additions & 0 deletions brood/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
TEMPLATE_ID_BUGOUT_WELCOME_EMAIL = os.environ.get(
"SENDGRID_TEMPLATE_ID_BUGOUT_WELCOME_EMAIL"
)
TEMPLATE_ID_MOONSTREAM_WELCOME_EMAIL = os.environ.get(
"SENDGRID_TEMPLATE_ID_MOONSTREAM_WELCOME_EMAIL"
)
MOONSTREAM_APPLICATION_ID = os.environ.get("MOONSTREAM_APPLICATION_ID")

DB_URI = os.environ.get("BROOD_DB_URI")

Expand Down
2 changes: 2 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export STRIPE_SECRET_KEY="<Stripe API secret key>"
export STRIPE_SIGNING_SECRET="<Stripe Webhook signing key>"
export BROOD_OPENAPI_LIST="resources"
export SENDGRID_TEMPLATE_ID_BUGOUT_WELCOME_EMAIL="<template id welcome email>"
export MOONSTREAM_APPLICATION_ID="<moonstream app id>"
export SENDGRID_TEMPLATE_ID_MOONSTREAM_WELCOME_EMAIL="<template id welcome email for moonstream app>"