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:
@@ -20,7 +20,8 @@ from backend.shared.dependencies import get_current_user, get_db
|
||||
from backend.shared.utils import convert_user
|
||||
from backend.shared.constants import OWNER_USERNAME
|
||||
from backend.shared.models import Message, SendMessageRequest, EditMessageRequest, User, DMEnvelope, MessageFile, DMFile, Reaction, ReactionRequest, ReactionResponse, DMReaction, DMReactionRequest, DMReactionResponse, UpdateLog
|
||||
import backend.push_service as push_service
|
||||
import os
|
||||
import httpx
|
||||
from PIL import Image
|
||||
import io
|
||||
import json
|
||||
@@ -29,7 +30,7 @@ from better_profanity import profanity as _bp
|
||||
from backend.security.audit import log_access, log_dm, log_public_chat, log_security
|
||||
from backend.security.profanity import contains_profanity
|
||||
from backend.security.rate_limit import rate_limit_per_ip
|
||||
from backend.websocket.utils import authenticate_user
|
||||
from backend.services.messaging.files.websocket.utils import authenticate_user
|
||||
|
||||
from backend.shared.models import FcmToken
|
||||
|
||||
@@ -393,7 +394,16 @@ async def _send_message_internal(
|
||||
|
||||
# Send push notifications for public messages
|
||||
try:
|
||||
await push_service.send_public_message_notification(db, new_message, exclude_user_id=current_user.id)
|
||||
push_service_url = os.getenv("PUSH_SERVICE_URL", "http://push_service:8306")
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(
|
||||
f"{push_service_url}/push/send-public-notification",
|
||||
json={
|
||||
"message_id": new_message.id,
|
||||
"exclude_user_id": current_user.id
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to send push notification for message {new_message.id}: {e}")
|
||||
|
||||
@@ -1560,4 +1570,22 @@ async def get_file_encrypted(filename: str, current_user: User = Depends(get_cur
|
||||
else:
|
||||
raise HTTPException(500)
|
||||
|
||||
return FileResponse(str(path))
|
||||
return FileResponse(str(path))
|
||||
|
||||
|
||||
class SendSuspensionRequest(BaseModel):
|
||||
user_id: int
|
||||
reason: str
|
||||
|
||||
|
||||
@router.post("/send-suspension")
|
||||
async def send_suspension_to_user(
|
||||
request: SendSuspensionRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Send suspension message to user via WebSocket (called by profile service)"""
|
||||
try:
|
||||
await messagingManager.send_suspension_to_user(request.user_id, request.reason)
|
||||
return {"status": "success"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
@@ -13,8 +13,9 @@ from backend.shared.dependencies import get_db, get_current_user
|
||||
from backend.shared.models import User, UpdateBioRequest, UserProfileResponse
|
||||
from pydantic import BaseModel
|
||||
from backend.shared.validation import is_valid_username, is_valid_display_name
|
||||
from backend.similarity import is_user_similar_to_verified
|
||||
from .messaging import messagingManager
|
||||
from backend.shared.similarity import is_user_similar_to_verified
|
||||
import os
|
||||
import httpx
|
||||
from backend.security.audit import log_security
|
||||
from backend.security.profanity import contains_profanity
|
||||
from backend.security.rate_limit import rate_limit_per_ip
|
||||
@@ -479,7 +480,16 @@ async def suspend_user(
|
||||
|
||||
# Send WebSocket suspension message
|
||||
try:
|
||||
await messagingManager.send_suspension_to_user(user_id, request.reason)
|
||||
messaging_service_url = os.getenv("MESSAGING_SERVICE_URL", "http://messaging_service:8305")
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(
|
||||
f"{messaging_service_url}/messaging/send-suspension",
|
||||
json={
|
||||
"user_id": user_id,
|
||||
"reason": request.reason
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
except Exception as e:
|
||||
# Log error but don't fail the request
|
||||
pass
|
||||
|
||||
+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