Add email notification triggers for replies and mentions
This commit is contained in:
parent
aa18eab9a4
commit
2504b15f29
2 changed files with 140 additions and 3 deletions
|
|
@ -19,25 +19,42 @@ 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+)"
|
||||
mentions = re.findall(mention_pattern, db_comment.content)
|
||||
|
|
@ -50,9 +67,19 @@ 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
|
||||
|
|
|
|||
|
|
@ -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"""
|
||||
<html>
|
||||
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: #333; background-color: #f5f5f5; padding: 20px;">
|
||||
<div style="max-width: 600px; margin: 0 auto; background: white; border-radius: 12px; padding: 40px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
|
||||
<h2 style="color: #2563eb; margin-top: 0;">New Reply</h2>
|
||||
<p><strong>{replier_name}</strong> replied to your comment:</p>
|
||||
<div style="background: #f5f5f5; padding: 15px; border-radius: 8px; margin: 20px 0; border-left: 4px solid #2563eb;">
|
||||
<p style="margin: 0; color: #666; font-style: italic;">"{original_content[:100]}{'...' if len(original_content) > 100 else ''}"</p>
|
||||
</div>
|
||||
<div style="margin: 30px 0; text-align: center;">
|
||||
<a href="{email_service.frontend_url}{link}" style="background: linear-gradient(135deg, #2563eb, #4f46e5); color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: 600; display: inline-block;">View Reply</a>
|
||||
</div>
|
||||
<hr style="border: 0; border-top: 1px solid #eee; margin: 30px 0;">
|
||||
<p style="font-size: 12px; color: #999; margin-bottom: 0;">You can disable these notifications in your <a href="{email_service.frontend_url}/settings" style="color: #2563eb;">settings</a>.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
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"""
|
||||
<html>
|
||||
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: #333; background-color: #f5f5f5; padding: 20px;">
|
||||
<div style="max-width: 600px; margin: 0 auto; background: white; border-radius: 12px; padding: 40px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
|
||||
<h2 style="color: #2563eb; margin-top: 0;">You were mentioned!</h2>
|
||||
<p><strong>{mentioner_name}</strong> mentioned you in a comment:</p>
|
||||
<div style="background: #f5f5f5; padding: 15px; border-radius: 8px; margin: 20px 0; border-left: 4px solid #10b981;">
|
||||
<p style="margin: 0; color: #666;">"{context[:150]}{'...' if len(context) > 150 else ''}"</p>
|
||||
</div>
|
||||
<div style="margin: 30px 0; text-align: center;">
|
||||
<a href="{email_service.frontend_url}{link}" style="background: linear-gradient(135deg, #10b981, #059669); color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: 600; display: inline-block;">View Comment</a>
|
||||
</div>
|
||||
<hr style="border: 0; border-top: 1px solid #eee; margin: 30px 0;">
|
||||
<p style="font-size: 12px; color: #999; margin-bottom: 0;">You can disable these notifications in your <a href="{email_service.frontend_url}/settings" style="color: #2563eb;">settings</a>.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
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"""
|
||||
<html>
|
||||
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; line-height: 1.6; color: #333; background-color: #f5f5f5; padding: 20px;">
|
||||
<div style="max-width: 600px; margin: 0 auto; background: white; border-radius: 12px; padding: 40px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
|
||||
<h2 style="color: #f59e0b; margin-top: 0;">Chase Alert!</h2>
|
||||
<p>A song you're chasing was just played!</p>
|
||||
<div style="background: linear-gradient(135deg, #fef3c7, #fcd34d); padding: 20px; border-radius: 12px; margin: 20px 0; text-align: center;">
|
||||
<h3 style="margin: 0 0 10px 0; color: #92400e;">{song_title}</h3>
|
||||
<p style="margin: 0; color: #78350f;">{show_date} - {venue_name}</p>
|
||||
</div>
|
||||
<div style="margin: 30px 0; text-align: center;">
|
||||
<a href="{email_service.frontend_url}{link}" style="background: linear-gradient(135deg, #f59e0b, #d97706); color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: 600; display: inline-block;">View Performance</a>
|
||||
</div>
|
||||
<hr style="border: 0; border-top: 1px solid #eee; margin: 30px 0;">
|
||||
<p style="font-size: 12px; color: #999; margin-bottom: 0;">You can disable chase alerts in your <a href="{email_service.frontend_url}/settings" style="color: #2563eb;">settings</a>.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue