mirror of
https://github.com/fromchat-messenger/web.git
synced 2026-09-22 19:15:08 +03:00
Fix inter-services communication
This commit is contained in:
+25
-2
@@ -1,11 +1,16 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel
|
||||
from backend.shared.dependencies import get_current_user, get_db
|
||||
from backend.shared.models import User, PushSubscriptionRequest
|
||||
import backend.push_service as push_service
|
||||
from backend.services.push.files import push_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class SendPublicNotificationRequest(BaseModel):
|
||||
message_id: int
|
||||
exclude_user_id: int
|
||||
|
||||
@router.post("/subscribe")
|
||||
async def subscribe_to_push_notifications(
|
||||
request: PushSubscriptionRequest,
|
||||
@@ -37,10 +42,28 @@ async def unsubscribe_from_push_notifications(
|
||||
"""Unsubscribe user from push notifications"""
|
||||
try:
|
||||
success = await push_service.unsubscribe_user(db=db, user_id=current_user.id)
|
||||
|
||||
|
||||
if success:
|
||||
return {"status": "success", "message": "Push notifications disabled"}
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail="Failed to disable push notifications")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.post("/send-public-notification")
|
||||
async def send_public_message_notification(
|
||||
request: SendPublicNotificationRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Send push notification for public message (called by messaging service)"""
|
||||
try:
|
||||
# Get the message from database
|
||||
from backend.shared.models import Message
|
||||
message = db.query(Message).filter(Message.id == request.message_id).first()
|
||||
if not message:
|
||||
raise HTTPException(status_code=404, detail="Message not found")
|
||||
|
||||
await push_service.send_public_message_notification(db, message, exclude_user_id=request.exclude_user_id)
|
||||
return {"status": "success"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user