2026-09-15 23:23:47 +03:30
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
"dating-app/internal/dto"
|
|
|
|
|
"dating-app/internal/service"
|
|
|
|
|
"dating-app/internal/utils"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ChatHandler struct {
|
|
|
|
|
chatService *service.ChatService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewChatHandler(chatService *service.ChatService) *ChatHandler {
|
|
|
|
|
return &ChatHandler{chatService: chatService}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListMatches GET /matches
|
|
|
|
|
func (h *ChatHandler) ListMatches(c *gin.Context) {
|
|
|
|
|
user, ok := currentUser(c)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
matches, err := h.chatService.ListMatches(user.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fail(c, http.StatusInternalServerError, "خطا در دریافت مچها", nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
utils.Success(c, http.StatusOK, "", matches)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListMessages GET /matches/:id/messages
|
|
|
|
|
func (h *ChatHandler) ListMessages(c *gin.Context) {
|
|
|
|
|
user, ok := currentUser(c)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
matchID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fail(c, http.StatusBadRequest, "شناسه نامعتبر است", nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
messages, err := h.chatService.ListMessages(user.ID, uint(matchID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.handleError(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
utils.Success(c, http.StatusOK, "", messages)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-26 18:57:46 +03:30
|
|
|
// SendMessage POST /matches/:id/messages body: {cipher_text, nonce}
|
|
|
|
|
// متن خام پیام هیچوقت اینجا نمیرسه؛ کلاینت قبل از ارسال با کلید عمومی
|
|
|
|
|
// طرف مقابل (NaCl box / X25519) رمزنگاریش کرده.
|
2026-09-15 23:23:47 +03:30
|
|
|
func (h *ChatHandler) SendMessage(c *gin.Context) {
|
|
|
|
|
user, ok := currentUser(c)
|
|
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
matchID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
utils.Fail(c, http.StatusBadRequest, "شناسه نامعتبر است", nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var req dto.SendMessageRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
2026-09-26 18:57:46 +03:30
|
|
|
utils.Fail(c, http.StatusBadRequest, "پیام رمزنگاریشده نامعتبر است", err.Error())
|
2026-09-15 23:23:47 +03:30
|
|
|
return
|
|
|
|
|
}
|
2026-09-26 18:57:46 +03:30
|
|
|
msg, err := h.chatService.SendMessage(user.ID, uint(matchID), req.CipherText, req.Nonce)
|
2026-09-15 23:23:47 +03:30
|
|
|
if err != nil {
|
|
|
|
|
h.handleError(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
utils.Success(c, http.StatusCreated, "", msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ChatHandler) handleError(c *gin.Context, err error) {
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, service.ErrNotMatchMember):
|
|
|
|
|
utils.Fail(c, http.StatusForbidden, err.Error(), nil)
|
2026-09-26 18:57:46 +03:30
|
|
|
case errors.Is(err, service.ErrInvalidMessageEncoding):
|
2026-09-19 22:17:12 +03:30
|
|
|
utils.Fail(c, http.StatusBadRequest, err.Error(), nil)
|
2026-09-15 23:23:47 +03:30
|
|
|
default:
|
|
|
|
|
utils.Fail(c, http.StatusNotFound, "گفتگو یافت نشد", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|