|
| 1 | +import type { Request } from "express"; |
| 2 | +import type WebSocket from "ws"; |
| 3 | +// plane imports |
| 4 | +import { Controller, WebSocket as WSDecorator } from "@plane/decorators"; |
| 5 | +import { logger } from "@plane/logger"; |
| 6 | +// redis |
| 7 | +import { redisManager } from "@/redis"; |
| 8 | +// auth |
| 9 | +import { handleAuthentication } from "@/lib/auth"; |
| 10 | + |
| 11 | +@Controller("/issues") |
| 12 | +export class IssueEventsController { |
| 13 | + @WSDecorator("/") |
| 14 | + async handleConnection(ws: WebSocket, req: Request) { |
| 15 | + const query = req.query as Record<string, string | string[]>; |
| 16 | + |
| 17 | + const projectIdRaw = query.projectId; |
| 18 | + const workspaceSlugRaw = query.workspaceSlug; |
| 19 | + const tokenRaw = query.token; |
| 20 | + |
| 21 | + const projectId = Array.isArray(projectIdRaw) ? projectIdRaw[0] : projectIdRaw; |
| 22 | + const workspaceSlug = Array.isArray(workspaceSlugRaw) ? workspaceSlugRaw[0] : workspaceSlugRaw; |
| 23 | + const token = Array.isArray(tokenRaw) ? tokenRaw[0] : tokenRaw; |
| 24 | + |
| 25 | + if (!projectId || !workspaceSlug || !token) { |
| 26 | + ws.close(4001, "Missing required parameters"); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + let parsedToken: { id?: string; cookie?: string } | undefined; |
| 31 | + try { |
| 32 | + parsedToken = JSON.parse(token); |
| 33 | + } catch (error) { |
| 34 | + logger.error("Invalid token payload for issue events", error); |
| 35 | + ws.close(4002, "Invalid token"); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const cookieString = parsedToken?.cookie || req.headers.cookie || ""; |
| 40 | + if (cookieString) { |
| 41 | + try { |
| 42 | + await handleAuthentication({ |
| 43 | + cookie: cookieString, |
| 44 | + userId: parsedToken?.id ?? "", |
| 45 | + }); |
| 46 | + } catch (error) { |
| 47 | + logger.error("Failed to authenticate issue events connection", error); |
| 48 | + ws.close(4003, "Unauthorized"); |
| 49 | + return; |
| 50 | + } |
| 51 | + } else if (!parsedToken?.id) { |
| 52 | + ws.close(4003, "Unauthorized"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const redisClient = redisManager.getClient(); |
| 57 | + if (!redisClient) { |
| 58 | + ws.close(1011, "Realtime service unavailable"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const channel = `issue_events:${projectId}`; |
| 63 | + const subscriber = redisClient.duplicate(); |
| 64 | + let cleanupInitiated = false; |
| 65 | + |
| 66 | + const cleanup = async () => { |
| 67 | + if (cleanupInitiated) return; |
| 68 | + cleanupInitiated = true; |
| 69 | + subscriber.removeAllListeners("message"); |
| 70 | + subscriber.removeAllListeners("error"); |
| 71 | + try { |
| 72 | + await subscriber.unsubscribe(channel); |
| 73 | + } catch (error) { |
| 74 | + logger.error("Failed to unsubscribe issue events channel", error); |
| 75 | + } |
| 76 | + subscriber.disconnect(); |
| 77 | + }; |
| 78 | + |
| 79 | + try { |
| 80 | + subscriber.on("error", (error) => { |
| 81 | + logger.error("Issue events redis subscriber error", error); |
| 82 | + if (ws.readyState === ws.OPEN || ws.readyState === ws.CONNECTING) { |
| 83 | + ws.close(1011, "Realtime service unavailable"); |
| 84 | + } |
| 85 | + void cleanup(); |
| 86 | + }); |
| 87 | + await subscriber.connect(); |
| 88 | + subscriber.on("message", (incomingChannel, message) => { |
| 89 | + if (incomingChannel === channel && ws.readyState === ws.OPEN) { |
| 90 | + ws.send(message); |
| 91 | + } |
| 92 | + }); |
| 93 | + await subscriber.subscribe(channel); |
| 94 | + ws.on("close", () => { |
| 95 | + void cleanup(); |
| 96 | + }); |
| 97 | + ws.on("error", (error) => { |
| 98 | + logger.error("Issue events websocket error", error); |
| 99 | + if (ws.readyState === ws.OPEN || ws.readyState === ws.CONNECTING) { |
| 100 | + ws.close(1011, "Issue events websocket error"); |
| 101 | + } |
| 102 | + void cleanup(); |
| 103 | + }); |
| 104 | + } catch (error) { |
| 105 | + logger.error("Failed to subscribe to issue events channel", error); |
| 106 | + if (ws.readyState === ws.OPEN || ws.readyState === ws.CONNECTING) { |
| 107 | + ws.close(1011, "Subscription failure"); |
| 108 | + } |
| 109 | + void cleanup(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments