mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 12:19:06 +00:00
Replace streams json
This commit is contained in:
@@ -24,6 +24,7 @@ from reflector.views.transcripts_upload import router as transcripts_upload_rout
|
||||
from reflector.views.transcripts_webrtc import router as transcripts_webrtc_router
|
||||
from reflector.views.transcripts_websocket import router as transcripts_websocket_router
|
||||
from reflector.views.user import router as user_router
|
||||
from reflector.views.zulip import router as zulip_router
|
||||
|
||||
try:
|
||||
import sentry_sdk
|
||||
@@ -79,6 +80,7 @@ app.include_router(transcripts_websocket_router, prefix="/v1")
|
||||
app.include_router(transcripts_webrtc_router, prefix="/v1")
|
||||
app.include_router(transcripts_process_router, prefix="/v1")
|
||||
app.include_router(user_router, prefix="/v1")
|
||||
app.include_router(zulip_router, prefix="/v1")
|
||||
add_pagination(app)
|
||||
|
||||
# prepare celery
|
||||
|
||||
46
server/reflector/views/zulip.py
Normal file
46
server/reflector/views/zulip.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from typing import Annotated, Optional
|
||||
|
||||
import reflector.auth as auth
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from reflector.zulip import get_zulip_streams, get_zulip_topics
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class Stream(BaseModel):
|
||||
stream_id: int
|
||||
name: str
|
||||
|
||||
|
||||
class Topic(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
@router.get("/zulip/streams")
|
||||
async def zulip_get_streams(
|
||||
user: Annotated[Optional[auth.UserInfo], Depends(auth.current_user_optional)],
|
||||
) -> list[Stream]:
|
||||
"""
|
||||
Get all Zulip streams.
|
||||
"""
|
||||
if not user:
|
||||
raise HTTPException(status_code=403, detail="Authentication required")
|
||||
|
||||
streams = get_zulip_streams()
|
||||
return streams
|
||||
|
||||
|
||||
@router.get("/zulip/streams/{stream_id}/topics")
|
||||
async def zulip_get_topics(
|
||||
stream_id: int,
|
||||
user: Annotated[Optional[auth.UserInfo], Depends(auth.current_user_optional)],
|
||||
) -> list[Topic]:
|
||||
"""
|
||||
Get all topics for a specific Zulip stream.
|
||||
"""
|
||||
if not user:
|
||||
raise HTTPException(status_code=403, detail="Authentication required")
|
||||
|
||||
topics = get_zulip_topics(stream_id)
|
||||
return topics
|
||||
@@ -10,6 +10,34 @@ class InvalidMessageError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_zulip_topics(stream_id: int) -> list[dict]:
|
||||
try:
|
||||
response = requests.get(
|
||||
f"https://{settings.ZULIP_REALM}/api/v1/users/me/{stream_id}/topics",
|
||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json().get("topics", [])
|
||||
except requests.RequestException as error:
|
||||
raise Exception(f"Failed to get topics: {error}")
|
||||
|
||||
|
||||
def get_zulip_streams() -> list[dict]:
|
||||
try:
|
||||
response = requests.get(
|
||||
f"https://{settings.ZULIP_REALM}/api/v1/streams",
|
||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json().get("streams", [])
|
||||
except requests.RequestException as error:
|
||||
raise Exception(f"Failed to get streams: {error}")
|
||||
|
||||
|
||||
def send_message_to_zulip(stream: str, topic: str, content: str):
|
||||
try:
|
||||
response = requests.post(
|
||||
|
||||
Reference in New Issue
Block a user