Skip to content

Commit

Permalink
Merge pull request #2207 from Shoaibdev7/feature-workspace-chat-list
Browse files Browse the repository at this point in the history
  • Loading branch information
humansinstitute authored Dec 17, 2024
2 parents 30f54f6 + 0a5dcf6 commit 4960625
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
14 changes: 14 additions & 0 deletions db/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,17 @@ func (db database) GetChatMessagesForChatID(chatID string) ([]ChatMessage, error

return chatMessages, nil
}

func (db database) GetChatsForWorkspace(workspaceID string) ([]Chat, error) {
var chats []Chat

result := db.db.Where("workspace_id = ?", workspaceID).
Order("updated_at DESC").
Find(&chats)

if result.Error != nil {
return nil, fmt.Errorf("failed to fetch chats: %w", result.Error)
}

return chats, nil
}
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ type Database interface {
AddChatMessage(message *ChatMessage) (ChatMessage, error)
UpdateChatMessage(message *ChatMessage) (ChatMessage, error)
GetChatMessagesForChatID(chatID string) ([]ChatMessage, error)
GetChatsForWorkspace(workspaceID string) ([]Chat, error)
GetCodeGraphByUUID(uuid string) (WorkspaceCodeGraph, error)
GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error)
CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error)
Expand Down
28 changes: 28 additions & 0 deletions handlers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ func (ch *ChatHandler) sendToStakwork(payload StakworkChatPayload) error {
return nil
}

func (ch *ChatHandler) GetChat(w http.ResponseWriter, r *http.Request) {
workspaceID := r.URL.Query().Get("workspace_id")
if workspaceID == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ChatResponse{
Success: false,
Message: "workspace_id query parameter is required",
})
return
}

chats, err := ch.db.GetChatsForWorkspace(workspaceID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(ChatResponse{
Success: false,
Message: fmt.Sprintf("Failed to fetch chats: %v", err),
})
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(ChatResponse{
Success: true,
Data: chats,
})
}

func (ch *ChatHandler) GetChatHistory(w http.ResponseWriter, r *http.Request) {
chatID := chi.URLParam(r, "uuid")
if chatID == "" {
Expand Down
55 changes: 55 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion routes/chat.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package routes

import (
"net/http"

"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/handlers"
"net/http"
)

func ChatRoutes() chi.Router {
Expand All @@ -16,6 +17,7 @@ func ChatRoutes() chi.Router {

r.Group(func(r chi.Router) {
r.Use(auth.PubKeyContext)
r.Get("/", chatHandler.GetChat)
r.Post("/", chatHandler.CreateChat)
r.Post("/send", chatHandler.SendMessage)
r.Get("/history/{uuid}", chatHandler.GetChatHistory)
Expand Down

0 comments on commit 4960625

Please sign in to comment.