Files
SigHej/backend/app/services/match_service.py

39 lines
1.3 KiB
Python
Raw Normal View History

2026-05-12 18:21:25 +02:00
from app.services.session_service import get_session
from app.services.ws_manager import manager
async def attempt_match(own_token: str, detected_token: str) -> dict:
"""
Check if two nearby users share interests and both consent.
Sends a WebSocket nudge to both if matched.
"""
own_session = await get_session(own_token)
other_session = await get_session(detected_token)
if own_session is None or other_session is None:
return {"match": False, "shared_interests": [], "nudge_sent": False}
own_interests = set(own_session.get("interests", []))
other_interests = set(other_session.get("interests", []))
shared = list(own_interests & other_interests)
if not shared:
return {"match": False, "shared_interests": [], "nudge_sent": False}
nudge = {
"type": "nudge",
"shared_interests": shared,
"message": f"Someone nearby shares your interest in {_format_interests(shared)}.",
}
await manager.send(own_token, nudge)
await manager.send(detected_token, nudge)
return {"match": True, "shared_interests": shared, "nudge_sent": True}
def _format_interests(interests: list[str]) -> str:
if len(interests) == 1:
return interests[0]
return ", ".join(interests[:-1]) + " and " + interests[-1]