Spaces:
Paused

File size: 2,677 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
WebSocket Connection Manager for real-time updates.
"""

import json
from typing import Dict, List, Set, Any
from uuid import UUID
from fastapi import WebSocket


class ConnectionManager:
    def __init__(self):
        # Maps workspace_id -> Set of WebSockets
        self.active_connections: Dict[UUID, Set[WebSocket]] = {}
        # Maps ticket_id -> Set of WebSockets
        self.ticket_connections: Dict[UUID, Set[WebSocket]] = {}

    async def connect(self, websocket: WebSocket, workspace_id: UUID):
        await websocket.accept()
        if workspace_id not in self.active_connections:
            self.active_connections[workspace_id] = set()
        self.active_connections[workspace_id].add(websocket)

    def disconnect(self, websocket: WebSocket, workspace_id: UUID):
        if workspace_id in self.active_connections:
            self.active_connections[workspace_id].discard(websocket)

    async def connect_ticket(self, websocket: WebSocket, ticket_id: UUID):
        await websocket.accept()
        if ticket_id not in self.ticket_connections:
            self.ticket_connections[ticket_id] = set()
        self.ticket_connections[ticket_id].add(websocket)

    def disconnect_ticket(self, websocket: WebSocket, ticket_id: UUID):
        if ticket_id in self.ticket_connections:
            self.ticket_connections[ticket_id].discard(websocket)

    async def broadcast_to_workspace(self, workspace_id: UUID, message: Any):
        """Broadcast a message to all users in a specific workspace."""
        if workspace_id not in self.active_connections:
            return
            
        disconnected = set()
        payload = json.dumps(message)
        
        for connection in self.active_connections[workspace_id]:
            try:
                await connection.send_text(payload)
            except Exception:
                disconnected.add(connection)
                
        for conn in disconnected:
            self.active_connections[workspace_id].discard(conn)

    async def broadcast_to_ticket(self, ticket_id: UUID, message: Any):
        """Broadcast a message to all users viewing a specific ticket thread."""
        if ticket_id not in self.ticket_connections:
            return
            
        disconnected = set()
        payload = json.dumps(message)
        
        for connection in self.ticket_connections[ticket_id]:
            try:
                await connection.send_text(payload)
            except Exception:
                disconnected.add(connection)
                
        for conn in disconnected:
            self.ticket_connections[ticket_id].discard(conn)

manager = ConnectionManager()