Skip to content

Commit

Permalink
feat: Implement websocket message sending on ticket update
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Dec 4, 2024
1 parent 0a02eab commit c5fa256
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
26 changes: 26 additions & 0 deletions handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions websocket/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c5fa256

Please sign in to comment.