broadcast message
Browse files
App/Messages/MessagesRoute.py
CHANGED
@@ -1,4 +1,13 @@
|
|
1 |
# App/Messages/Routes.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from fastapi import APIRouter, HTTPException, Depends
|
3 |
from typing import List
|
4 |
from .Model import Message
|
@@ -27,6 +36,31 @@ message_router = APIRouter(tags=["Messages"], prefix="/messages")
|
|
27 |
logging.basicConfig(level=logging.WARNING)
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
@message_router.get("/", response_model=List[MessageResponse])
|
31 |
async def get_all_messages(current_user: User = Depends(get_admin_user)):
|
32 |
# Check if the current user is an admin
|
|
|
1 |
# App/Messages/Routes.py
|
2 |
+
|
3 |
+
### In App/Messages/Routes.py
|
4 |
+
|
5 |
+
from fastapi import APIRouter, HTTPException, Depends
|
6 |
+
from typing import List
|
7 |
+
import asyncio
|
8 |
+
from App.Users.Model import User
|
9 |
+
from App.Users.dependencies import get_admin_user, UserType
|
10 |
+
from pydantic import BaseModel
|
11 |
from fastapi import APIRouter, HTTPException, Depends
|
12 |
from typing import List
|
13 |
from .Model import Message
|
|
|
36 |
logging.basicConfig(level=logging.WARNING)
|
37 |
|
38 |
|
39 |
+
class BroadcastMessageRequest(BaseModel):
|
40 |
+
message: str
|
41 |
+
|
42 |
+
|
43 |
+
@message_router.post("/broadcast")
|
44 |
+
async def broadcast_message(request: BroadcastMessageRequest):
|
45 |
+
# # Ensure the requesting user is an admin
|
46 |
+
# if current_user.user_type != UserType.ADMIN:
|
47 |
+
# raise HTTPException(
|
48 |
+
# status_code=403,
|
49 |
+
# detail="User does not have permission to perform a broadcast.",
|
50 |
+
# )
|
51 |
+
|
52 |
+
# Fetch all users
|
53 |
+
users = await User.all()
|
54 |
+
|
55 |
+
# Create a list of coroutines for sending the message
|
56 |
+
tasks = [user.send_message(request.message) for user in users]
|
57 |
+
|
58 |
+
# Run them concurrently
|
59 |
+
await asyncio.gather(*tasks)
|
60 |
+
|
61 |
+
return {"status": 200, "message": "Broadcast message sent to all users."}
|
62 |
+
|
63 |
+
|
64 |
@message_router.get("/", response_model=List[MessageResponse])
|
65 |
async def get_all_messages(current_user: User = Depends(get_admin_user)):
|
66 |
# Check if the current user is an admin
|