180 lines
5.9 KiB
Go
180 lines
5.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// ConversationParticipantHandler handles conversation participant API endpoints.
|
|
// Reference: Chatwoot app/controllers/api/v1/conversations/participants_controller.rb
|
|
type ConversationParticipantHandler struct {
|
|
participantSvc *service.ConversationParticipantService
|
|
}
|
|
|
|
// NewConversationParticipantHandler creates a new ConversationParticipantHandler.
|
|
func NewConversationParticipantHandler(participantSvc *service.ConversationParticipantService) *ConversationParticipantHandler {
|
|
return &ConversationParticipantHandler{participantSvc: participantSvc}
|
|
}
|
|
|
|
// List retrieves all participants for a conversation.
|
|
// GET /api/v1/accounts/:account_id/conversations/:conversation_id/participants
|
|
func (h *ConversationParticipantHandler) List(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
participants, svcErr := h.participantSvc.List(c.Request.Context(), accountID, conversationID)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, toInterfaceSlice(participants))
|
|
}
|
|
|
|
// Add adds a participant to a conversation.
|
|
// POST /api/v1/accounts/:account_id/conversations/:conversation_id/participants
|
|
func (h *ConversationParticipantHandler) Add(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
UserID uint `json:"user_id" binding:"required"`
|
|
Role string `json:"role"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
participant, svcErr := h.participantSvc.Add(c.Request.Context(), accountID, conversationID, req.UserID, req.Role)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, participant)
|
|
}
|
|
|
|
// Update updates a participant's role in a conversation.
|
|
// PATCH /api/v1/accounts/:account_id/conversations/:conversation_id/participants/:user_id
|
|
func (h *ConversationParticipantHandler) Update(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
userID, err := parseUintParam(c, "user_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid user_id")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Role string `json:"role" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
participant, svcErr := h.participantSvc.Update(c.Request.Context(), accountID, conversationID, userID, req.Role)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, participant)
|
|
}
|
|
|
|
// Remove removes a participant from a conversation.
|
|
// DELETE /api/v1/accounts/:account_id/conversations/:conversation_id/participants/:user_id
|
|
func (h *ConversationParticipantHandler) Remove(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
userID, err := parseUintParam(c, "user_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid user_id")
|
|
return
|
|
}
|
|
|
|
if svcErr := h.participantSvc.Remove(c.Request.Context(), accountID, conversationID, userID); svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, nil)
|
|
}
|
|
|
|
// BatchUpdate adds and/or removes multiple participants in a single call.
|
|
// PATCH /api/v1/accounts/:account_id/conversations/:conversation_id/participants
|
|
// Accepts: { "user_ids": [1,2,3], "remove_user_ids": [4,5], "role": "participant" }
|
|
// Reference: Chatwoot app/controllers/api/v1/conversations/participants_controller.rb#update
|
|
func (h *ConversationParticipantHandler) BatchUpdate(c *gin.Context) {
|
|
accountID, err := parseUintParam(c, "account_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
conversationID, err := parseUintParam(c, "conversation_id")
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid conversation_id")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
UserIDs []uint `json:"user_ids"`
|
|
RemoveUserIDs []uint `json:"remove_user_ids"`
|
|
Role string `json:"role"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
|
return
|
|
}
|
|
|
|
participants, svcErr := h.participantSvc.BatchUpdate(c.Request.Context(), accountID, conversationID, req.UserIDs, req.RemoveUserIDs, req.Role)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, toInterfaceSlice(participants))
|
|
} |