|
|
|
@ -3,7 +3,6 @@ import bson
|
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
|
|
|
from fastapi.params import Depends
|
|
|
|
|
import pymongo
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from app.dto import FriendAddDTO
|
|
|
|
|
from app.models import HTTPError, User, Friend, PushSubscription
|
|
|
|
@ -62,7 +61,8 @@ async def add_friend(friend_to_add: FriendAddDTO, current_user: User = Depends(g
|
|
|
|
|
detail="Friend already exists"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
friend_user = users_collection.find_one({"_id": ObjectId(friend["friend_user_id"])})
|
|
|
|
|
friend_user_id = ObjectId(friend["friend_user_id"])
|
|
|
|
|
friend_user = users_collection.find_one({"_id": friend_user_id})
|
|
|
|
|
|
|
|
|
|
# Test if user exists
|
|
|
|
|
if not friend_user:
|
|
|
|
@ -75,30 +75,12 @@ async def add_friend(friend_to_add: FriendAddDTO, current_user: User = Depends(g
|
|
|
|
|
friend["status"] = "pending"
|
|
|
|
|
friend_id = friends_collection.insert_one(friend).inserted_id
|
|
|
|
|
|
|
|
|
|
if friend_user.get("push_subscriptions"):
|
|
|
|
|
print("friend user has subscriptions")
|
|
|
|
|
# Si l'utilisateur a des abonnements push, envoyer une notification
|
|
|
|
|
subscriptions = friend_user["push_subscriptions"]
|
|
|
|
|
|
|
|
|
|
# Pour chaque abonnement, envoyer une notification
|
|
|
|
|
for subscription_str in subscriptions:
|
|
|
|
|
try:
|
|
|
|
|
# Transformation de la chaîne JSON en dictionnaire Python
|
|
|
|
|
subscription_dict = json.loads(subscription_str)
|
|
|
|
|
|
|
|
|
|
print("Sending notification to subscription:", subscription_dict)
|
|
|
|
|
# Assurez-vous que l'abonnement est valide avant d'envoyer la notification
|
|
|
|
|
if subscription_dict and "endpoint" in subscription_dict:
|
|
|
|
|
# Créer le payload de notification
|
|
|
|
|
# Send notification to friend
|
|
|
|
|
payload = push_service.create_notification_payload(
|
|
|
|
|
title="Nouvelle demande d'ami",
|
|
|
|
|
body=f"{current_user.username} vous a envoyé une demande d'ami.",
|
|
|
|
|
#icon="https://memorymap.fr/icons/icon-72x72.png"
|
|
|
|
|
body=f"{current_user.username} vous a envoyé une demande d'ami."
|
|
|
|
|
)
|
|
|
|
|
await push_service.send_notification(subscription_dict, payload)
|
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
|
print(f"Erreur lors du décodage de la souscription: {e}")
|
|
|
|
|
continue
|
|
|
|
|
await push_service.send_notification(friend_user_id, payload);
|
|
|
|
|
|
|
|
|
|
return {"id": str(friend_id)}
|
|
|
|
|
|
|
|
|
|