From c5fa2564e74ccaf3b3d2a2c10a735a86255494a6 Mon Sep 17 00:00:00 2001 From: aliraza556 Date: Thu, 5 Dec 2024 02:12:59 +0500 Subject: [PATCH] feat: Implement websocket message sending on ticket update --- handlers/ticket.go | 26 ++++++++++++++++++++++++++ websocket/client.go | 16 ++++++++++++++++ websocket/pool.go | 12 ++++++++++++ 3 files changed, 54 insertions(+) diff --git a/handlers/ticket.go b/handlers/ticket.go index 781490df6..1c6998648 100644 --- a/handlers/ticket.go +++ b/handlers/ticket.go @@ -15,6 +15,7 @@ import ( "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/utils" + "github.com/stakwork/sphinx-tribes/websocket" ) type ticketHandler struct { @@ -140,6 +141,31 @@ func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) { return } + if updateRequest.Metadata.Source == "websocket" && updateRequest.Metadata.ID != "" { + ticketMsg := websocket.TicketMessage{ + BroadcastType: "direct", + SourceSessionID: updateRequest.Metadata.ID, + Message: fmt.Sprintf("Hive has successfully updated your ticket %s", updateRequest.Ticket.Name), + Action: "message", + TicketDetails: websocket.TicketData{ + FeatureUUID: updateRequest.Ticket.FeatureUUID, + PhaseUUID: updateRequest.Ticket.PhaseUUID, + TicketUUID: updateRequest.Ticket.UUID.String(), + TicketDescription: updateRequest.Ticket.Description, + }, + } + + if err := websocket.WebsocketPool.SendTicketMessage(ticketMsg); err != nil { + log.Printf("Failed to send websocket message: %v", err) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(map[string]interface{}{ + "ticket": updatedTicket, + "websocket_error": err.Error(), + }) + return + } + } + w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(updatedTicket) } diff --git a/websocket/client.go b/websocket/client.go index 98f9c56b3..6260c2b16 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -26,6 +26,22 @@ type Message struct { Body string `json:"body"` } +type TicketMessage struct { + Type int `json:"type"` + BroadcastType string `json:"broadcastType"` + SourceSessionID string `json:"sourceSessionID"` + Message string `json:"message"` + Action string `json:"action"` + TicketDetails TicketData `json:"ticketDetails"` +} + +type TicketData struct { + FeatureUUID string `json:"featureUUID"` + PhaseUUID string `json:"phaseUUID"` + TicketUUID string `json:"ticketUUID"` + TicketDescription string `json:"ticketDescription"` +} + func (c *Client) Read() { defer func() { c.Pool.Unregister <- c diff --git a/websocket/pool.go b/websocket/pool.go index 266e81421..3270e98ae 100644 --- a/websocket/pool.go +++ b/websocket/pool.go @@ -58,3 +58,15 @@ func (pool *Pool) Start() { } } } + +func (pool *Pool) SendTicketMessage(message TicketMessage) error { + if message.BroadcastType == "direct" { + + if client, ok := pool.Clients[message.SourceSessionID]; ok { + return client.Client.Conn.WriteJSON(message) + } + return fmt.Errorf("client not found: %s", message.SourceSessionID) + } + + return nil +}