Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement WebSocket message sending on ticket update #2098

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading