diff --git a/backend/routers/social.py b/backend/routers/social.py index 524a6ec..35a6d59 100644 --- a/backend/routers/social.py +++ b/backend/routers/social.py @@ -19,24 +19,41 @@ def create_comment( session: Session = Depends(get_session), current_user: User = Depends(get_current_user) ): + from services.email_service import send_reply_notification_email, send_mention_notification_email + db_comment = Comment.model_validate(comment) db_comment.user_id = current_user.id session.add(db_comment) session.commit() session.refresh(db_comment) + # Get commenter's display name + commenter_profile = session.exec(select(Profile).where(Profile.user_id == current_user.id)).first() + commenter_name = commenter_profile.display_name if commenter_profile else current_user.email.split('@')[0] + # Notify parent author if reply if db_comment.parent_id: parent_comment = session.get(Comment, db_comment.parent_id) if parent_comment and parent_comment.user_id != current_user.id: + # Create in-app notification create_notification( session, user_id=parent_comment.user_id, title="New Reply", - message=f"Someone replied to your comment.", + message=f"{commenter_name} replied to your comment.", type="reply", link=f"/activity" ) + + # Send email if user has email_on_reply enabled + parent_user = session.get(User, parent_comment.user_id) + if parent_user and parent_user.preferences and parent_user.preferences.email_on_reply: + send_reply_notification_email( + to_email=parent_user.email, + replier_name=commenter_name, + original_content=parent_comment.content or "", + link="/activity" + ) # Handle Mentions mention_pattern = r"@(\w+)" @@ -50,10 +67,20 @@ def create_comment( session, user_id=profile.user_id, title="You were mentioned!", - message=f"Someone mentioned you in a comment.", + message=f"{commenter_name} mentioned you in a comment.", type="mention", - link=f"/activity" # Generic link for now + link=f"/activity" ) + + # Send email for mention (using reply preference for now) + mentioned_user = session.get(User, profile.user_id) + if mentioned_user and mentioned_user.preferences and mentioned_user.preferences.email_on_reply: + send_mention_notification_email( + to_email=mentioned_user.email, + mentioner_name=commenter_name, + context=db_comment.content or "", + link="/activity" + ) return db_comment diff --git a/backend/services/email_service.py b/backend/services/email_service.py index 96b3b7b..36f536a 100644 --- a/backend/services/email_service.py +++ b/backend/services/email_service.py @@ -251,3 +251,113 @@ If you didn't request a password reset, safely ignore this email. """ return email_service.send_email(to_email, subject, html_content, text_content) + + +def send_reply_notification_email(to_email: str, replier_name: str, original_content: str, link: str): + """Send notification when someone replies to user's comment/review""" + subject = f"{replier_name} replied to your comment on Elmeg" + + html_content = f""" + + +
+

New Reply

+

{replier_name} replied to your comment:

+
+

"{original_content[:100]}{'...' if len(original_content) > 100 else ''}"

+
+
+ View Reply +
+
+

You can disable these notifications in your settings.

+
+ + + """ + + text_content = f""" +{replier_name} replied to your comment on Elmeg + +Your comment: "{original_content[:100]}{'...' if len(original_content) > 100 else ''}" + +View the reply: {email_service.frontend_url}{link} + +To disable these notifications, visit {email_service.frontend_url}/settings + """ + + return email_service.send_email(to_email, subject, html_content, text_content) + + +def send_mention_notification_email(to_email: str, mentioner_name: str, context: str, link: str): + """Send notification when someone mentions user in a comment""" + subject = f"{mentioner_name} mentioned you on Elmeg" + + html_content = f""" + + +
+

You were mentioned!

+

{mentioner_name} mentioned you in a comment:

+
+

"{context[:150]}{'...' if len(context) > 150 else ''}"

+
+
+ View Comment +
+
+

You can disable these notifications in your settings.

+
+ + + """ + + text_content = f""" +{mentioner_name} mentioned you in a comment on Elmeg + +"{context[:150]}{'...' if len(context) > 150 else ''}" + +View the comment: {email_service.frontend_url}{link} + +To disable these notifications, visit {email_service.frontend_url}/settings + """ + + return email_service.send_email(to_email, subject, html_content, text_content) + + +def send_chase_notification_email(to_email: str, song_title: str, show_date: str, venue_name: str, link: str): + """Send notification when a song the user is chasing gets played""" + subject = f"Chase Alert: {song_title} was played!" + + html_content = f""" + + +
+

Chase Alert!

+

A song you're chasing was just played!

+
+

{song_title}

+

{show_date} - {venue_name}

+
+
+ View Performance +
+
+

You can disable chase alerts in your settings.

+
+ + + """ + + text_content = f""" +Chase Alert: {song_title} was played! + +{show_date} at {venue_name} + +View the performance: {email_service.frontend_url}{link} + +To disable chase alerts, visit {email_service.frontend_url}/settings + """ + + return email_service.send_email(to_email, subject, html_content, text_content) +