fix(notification+vlog upload)
This commit is contained in:
@@ -9,6 +9,7 @@ from models.notification import Notification, NotificationType
|
||||
from schemas.event import EventCreate, EventUpdate, EventResponse, ParticipationUpdate
|
||||
from utils.security import get_current_active_user
|
||||
from utils.email import send_event_notification
|
||||
from utils.push_service import send_push_to_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -48,15 +49,22 @@ async def create_event(
|
||||
|
||||
# Create notification
|
||||
if user.id != current_user.id:
|
||||
notif_title = f"Invitation à un événement privé: {event.title}"
|
||||
notif_message = f"{current_user.full_name} vous a invité à un événement privé"
|
||||
notif_link = f"/events/{event.id}"
|
||||
|
||||
notification = Notification(
|
||||
user_id=user.id,
|
||||
type=NotificationType.EVENT_INVITATION,
|
||||
title=f"Invitation à un événement privé: {event.title}",
|
||||
message=f"{current_user.full_name} vous a invité à un événement privé",
|
||||
link=f"/events/{event.id}"
|
||||
title=notif_title,
|
||||
message=notif_message,
|
||||
link=notif_link
|
||||
)
|
||||
db.add(notification)
|
||||
|
||||
# Send push notification
|
||||
send_push_to_user(db, user.id, notif_title, notif_message, notif_link)
|
||||
|
||||
# Send email notification
|
||||
try:
|
||||
send_event_notification(user.email, event)
|
||||
@@ -75,15 +83,22 @@ async def create_event(
|
||||
|
||||
# Create notification
|
||||
if user.id != current_user.id:
|
||||
notif_title = f"Nouvel événement: {event.title}"
|
||||
notif_message = f"{current_user.full_name} a créé un nouvel événement"
|
||||
notif_link = f"/events/{event.id}"
|
||||
|
||||
notification = Notification(
|
||||
user_id=user.id,
|
||||
type=NotificationType.EVENT_INVITATION,
|
||||
title=f"Nouvel événement: {event.title}",
|
||||
message=f"{current_user.full_name} a créé un nouvel événement",
|
||||
link=f"/events/{event.id}"
|
||||
title=notif_title,
|
||||
message=notif_message,
|
||||
link=notif_link
|
||||
)
|
||||
db.add(notification)
|
||||
|
||||
# Send push notification
|
||||
send_push_to_user(db, user.id, notif_title, notif_message, notif_link)
|
||||
|
||||
# Send email notification
|
||||
try:
|
||||
send_event_notification(user.email, event)
|
||||
@@ -309,15 +324,22 @@ async def invite_users_to_event(
|
||||
db.add(participation)
|
||||
|
||||
# Créer une notification
|
||||
notif_title = f"Invitation à un événement privé: {event.title}"
|
||||
notif_message = f"{current_user.full_name} vous a invité à un événement privé"
|
||||
notif_link = f"/events/{event.id}"
|
||||
|
||||
notification = Notification(
|
||||
user_id=user.id,
|
||||
type=NotificationType.EVENT_INVITATION,
|
||||
title=f"Invitation à un événement privé: {event.title}",
|
||||
message=f"{current_user.full_name} vous a invité à un événement privé",
|
||||
link=f"/events/{event.id}"
|
||||
title=notif_title,
|
||||
message=notif_message,
|
||||
link=notif_link
|
||||
)
|
||||
db.add(notification)
|
||||
|
||||
# Send push notification
|
||||
send_push_to_user(db, user.id, notif_title, notif_message, notif_link)
|
||||
|
||||
# Envoyer un email
|
||||
try:
|
||||
send_event_notification(user.email, event)
|
||||
|
||||
67
backend/api/routers/push.py
Normal file
67
backend/api/routers/push.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from config.database import get_db
|
||||
from config.settings import settings
|
||||
from models.notification import PushSubscription
|
||||
from models.user import User
|
||||
from schemas.notification import PushSubscriptionCreate, VapidPublicKeyResponse
|
||||
from utils.security import get_current_active_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/vapid-public-key", response_model=VapidPublicKeyResponse)
|
||||
async def get_vapid_public_key(current_user: User = Depends(get_current_active_user)):
|
||||
"""Get the VAPID public key for push notifications."""
|
||||
if not settings.VAPID_PUBLIC_KEY:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="VAPID keys not configured on server"
|
||||
)
|
||||
return {"public_key": settings.VAPID_PUBLIC_KEY}
|
||||
|
||||
@router.post("/subscribe")
|
||||
async def subscribe_push(
|
||||
subscription: PushSubscriptionCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""Subscribe to push notifications."""
|
||||
# Check if subscription already exists
|
||||
existing = db.query(PushSubscription).filter(
|
||||
PushSubscription.endpoint == subscription.endpoint
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
# Update existing subscription
|
||||
existing.user_id = current_user.id
|
||||
existing.p256dh = subscription.keys.p256dh
|
||||
existing.auth = subscription.keys.auth
|
||||
db.commit()
|
||||
return {"message": "Subscription updated"}
|
||||
|
||||
# Create new subscription
|
||||
new_sub = PushSubscription(
|
||||
user_id=current_user.id,
|
||||
endpoint=subscription.endpoint,
|
||||
p256dh=subscription.keys.p256dh,
|
||||
auth=subscription.keys.auth
|
||||
)
|
||||
db.add(new_sub)
|
||||
db.commit()
|
||||
|
||||
return {"message": "Subscribed successfully"}
|
||||
|
||||
@router.delete("/unsubscribe")
|
||||
async def unsubscribe_push(
|
||||
endpoint: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""Unsubscribe from push notifications."""
|
||||
db.query(PushSubscription).filter(
|
||||
PushSubscription.endpoint == endpoint
|
||||
).delete()
|
||||
db.commit()
|
||||
|
||||
return {"message": "Unsubscribed successfully"}
|
||||
|
||||
@@ -13,6 +13,7 @@ from schemas.vlog import VlogCreate, VlogUpdate, VlogResponse, VlogCommentCreate
|
||||
from utils.security import get_current_active_user
|
||||
from utils.video_utils import generate_video_thumbnail, get_video_duration
|
||||
from utils.settings_service import SettingsService
|
||||
from utils.push_service import send_push_to_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -254,12 +255,15 @@ async def upload_vlog_video(
|
||||
"""Upload a vlog video."""
|
||||
# Validate video file
|
||||
if not SettingsService.is_file_type_allowed(db, video.content_type or "unknown"):
|
||||
allowed_types = SettingsService.get_setting(db, "allowed_video_types",
|
||||
["video/mp4", "video/mpeg", "video/quicktime", "video/webm"])
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Invalid video type. Allowed types: {', '.join(allowed_types)}"
|
||||
)
|
||||
# Fallback check for common video types if content_type is generic application/octet-stream
|
||||
filename = video.filename.lower()
|
||||
if not (filename.endswith('.mp4') or filename.endswith('.mov') or filename.endswith('.webm') or filename.endswith('.mkv')):
|
||||
allowed_types = SettingsService.get_setting(db, "allowed_video_types",
|
||||
["video/mp4", "video/mpeg", "video/quicktime", "video/webm"])
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Invalid video type. Allowed types: {', '.join(allowed_types)}"
|
||||
)
|
||||
|
||||
# Check file size
|
||||
video_content = await video.read()
|
||||
@@ -335,17 +339,24 @@ async def upload_vlog_video(
|
||||
|
||||
# Create notifications for all active users (except the creator)
|
||||
users = db.query(User).filter(User.is_active == True).all()
|
||||
notif_title = "Nouveau vlog"
|
||||
notif_message = f"{current_user.full_name} a publié un nouveau vlog : {vlog.title}"
|
||||
notif_link = f"/vlogs/{vlog.id}"
|
||||
|
||||
for user in users:
|
||||
if user.id != current_user.id:
|
||||
notification = Notification(
|
||||
user_id=user.id,
|
||||
type=NotificationType.NEW_VLOG,
|
||||
title="Nouveau vlog",
|
||||
message=f"{current_user.full_name} a publié un nouveau vlog : {vlog.title}",
|
||||
link=f"/vlogs/{vlog.id}",
|
||||
title=notif_title,
|
||||
message=notif_message,
|
||||
link=notif_link,
|
||||
is_read=False
|
||||
)
|
||||
db.add(notification)
|
||||
|
||||
# Envoyer la notification push
|
||||
send_push_to_user(db, user.id, notif_title, notif_message, notif_link)
|
||||
|
||||
db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user