regirstation works but shared links broken

This commit is contained in:
2025-12-12 20:38:35 +05:00
parent 4d3085623a
commit 1f088c8fb0
23 changed files with 1739 additions and 5 deletions

102
app/routers/map_share.py Normal file
View File

@@ -0,0 +1,102 @@
"""Map sharing routes."""
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from uuid import UUID
from app.database import get_db
from app.dependencies import get_current_user
from app.models.user import User
from app.schemas.map_share import (
MapShareCreate,
MapShareUpdate,
MapShareResponse,
MapShareLinkCreate,
MapShareLinkResponse
)
from app.services import map_share_service
router = APIRouter(prefix="/api/maps/{map_id}/share", tags=["map-sharing"])
@router.post("/users", response_model=MapShareResponse, status_code=status.HTTP_201_CREATED)
async def share_map_with_user(
map_id: UUID,
share_data: MapShareCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Share a map with a specific user."""
share = map_share_service.create_map_share(db, map_id, share_data, current_user)
return share
@router.get("/users", response_model=List[MapShareResponse])
async def get_map_shares_list(
map_id: UUID,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Get all users this map is shared with."""
shares = map_share_service.get_map_shares(db, map_id, current_user)
return shares
@router.put("/users/{share_id}", response_model=MapShareResponse)
async def update_map_share_permissions(
map_id: UUID,
share_id: UUID,
update_data: MapShareUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Update share permissions for a user."""
share = map_share_service.update_map_share(db, map_id, share_id, update_data, current_user)
return share
@router.delete("/users/{share_id}", status_code=status.HTTP_204_NO_CONTENT)
async def revoke_map_share(
map_id: UUID,
share_id: UUID,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Revoke map share from a user."""
map_share_service.delete_map_share(db, map_id, share_id, current_user)
return None
@router.post("/links", response_model=MapShareLinkResponse, status_code=status.HTTP_201_CREATED)
async def create_map_share_link(
map_id: UUID,
link_data: MapShareLinkCreate,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Create a public/guest share link."""
link = map_share_service.create_share_link(db, map_id, link_data, current_user)
return link
@router.get("/links", response_model=List[MapShareLinkResponse])
async def get_map_share_links_list(
map_id: UUID,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Get all share links for a map."""
links = map_share_service.get_share_links(db, map_id, current_user)
return links
@router.delete("/links/{link_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_map_share_link(
map_id: UUID,
link_id: UUID,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Delete a share link."""
map_share_service.delete_share_link(db, map_id, link_id, current_user)
return None

View File

@@ -9,6 +9,7 @@ from app.dependencies import get_current_user, get_optional_current_user
from app.models.user import User
from app.schemas.map import MapCreate, MapUpdate, MapResponse
from app.services import map_service
from app.services import map_share_service
router = APIRouter(prefix="/api/maps", tags=["maps"])
@@ -49,6 +50,16 @@ async def get_public_map(db: Session = Depends(get_db)):
return public_map
@router.get("/shared/{token}", response_model=MapResponse)
async def get_shared_map(
token: str,
db: Session = Depends(get_db)
):
"""Get a map by share token (no authentication required)."""
map_obj, permission = map_share_service.get_map_by_share_token(db, token)
return map_obj
@router.get("/{map_id}", response_model=MapResponse)
async def get_map(
map_id: UUID,

77
app/routers/websocket.py Normal file
View File

@@ -0,0 +1,77 @@
"""WebSocket routes for real-time updates."""
from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Depends, Query
from sqlalchemy.orm import Session
from uuid import UUID
from typing import Optional
import logging
from app.database import get_db
from app.websocket.connection_manager import manager
from app.services.map_share_service import check_map_access
from app.dependencies import get_user_from_token
logger = logging.getLogger(__name__)
router = APIRouter(tags=["websocket"])
@router.websocket("/ws/maps/{map_id}")
async def websocket_endpoint(
websocket: WebSocket,
map_id: UUID,
token: Optional[str] = Query(None),
share_token: Optional[str] = Query(None),
db: Session = Depends(get_db)
):
"""
WebSocket endpoint for real-time map updates.
Clients can connect using:
- JWT token (authenticated users)
- Share token (guest access)
Example: ws://localhost:8000/ws/maps/{map_id}?token={jwt_token}
Example: ws://localhost:8000/ws/maps/{map_id}?share_token={share_token}
"""
# Verify access to the map
user = None
if token:
try:
user = get_user_from_token(token, db)
except Exception as e:
logger.error(f"Invalid token: {e}")
await websocket.close(code=1008, reason="Invalid token")
return
# Check map access
has_access, permission = check_map_access(db, map_id, user, share_token)
if not has_access:
await websocket.close(code=1008, reason="Access denied")
return
await manager.connect(websocket, map_id)
try:
# Send initial connection message
await websocket.send_json({
"type": "connected",
"data": {
"map_id": str(map_id),
"permission": permission.value
}
})
# Keep connection alive and listen for messages
while True:
# Receive messages (for potential future use like cursor position, etc.)
data = await websocket.receive_json()
# Echo back for now (can add more features later)
logger.info(f"Received message from client: {data}")
except WebSocketDisconnect:
manager.disconnect(websocket, map_id)
logger.info(f"Client disconnected from map {map_id}")
except Exception as e:
logger.error(f"WebSocket error: {e}")
manager.disconnect(websocket, map_id)