2025-12-04 08:51:40 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* package api
|
|
|
|
|
|
* 作用:会话列表(最近聊天)相关 API 处理器
|
|
|
|
|
|
*/
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"xk-websocket-v2/internal/service"
|
|
|
|
|
|
"xk-websocket-v2/internal/utils"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-12-05 16:21:43 +08:00
|
|
|
|
"gorm.io/gorm"
|
2025-12-04 08:51:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* GetConversationListHandler
|
|
|
|
|
|
* 功能:获取当前用户的会话列表
|
|
|
|
|
|
* 路径:GET /api/conversations
|
|
|
|
|
|
*/
|
|
|
|
|
|
func GetConversationListHandler(c *gin.Context) {
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
|
|
|
|
|
|
|
list, err := service.ConversationSvc.GetConversations(userID.(string))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
utils.InternalError(c, "查询失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utils.SuccessWithData(c, list, "获取成功")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* ResetConversationUnreadHandler
|
|
|
|
|
|
* 功能:重置会话未读数
|
|
|
|
|
|
* 路径:POST /api/conversations/reset-unread
|
|
|
|
|
|
*/
|
|
|
|
|
|
func ResetConversationUnreadHandler(c *gin.Context) {
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
|
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
TargetID string `json:"target_id" binding:"required"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
utils.BadRequest(c, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := service.ConversationSvc.ResetUnread(userID.(string), req.TargetID); err != nil {
|
|
|
|
|
|
utils.InternalError(c, "操作失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utils.Success(c, "已重置未读")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* UpdateConversationHandler
|
|
|
|
|
|
* 功能:更新会话标记(置顶、免打扰、特别关心)
|
|
|
|
|
|
* 路径:POST /api/conversations/update
|
|
|
|
|
|
*/
|
|
|
|
|
|
func UpdateConversationHandler(c *gin.Context) {
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
|
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
TargetID string `json:"target_id" binding:"required"`
|
|
|
|
|
|
IsTop *bool `json:"is_top"`
|
|
|
|
|
|
IsMuted *bool `json:"is_muted"`
|
|
|
|
|
|
IsSpecialCare *bool `json:"is_special_care"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
utils.BadRequest(c, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updates := make(map[string]interface{})
|
|
|
|
|
|
if req.IsTop != nil {
|
|
|
|
|
|
updates["is_top"] = *req.IsTop
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.IsMuted != nil {
|
|
|
|
|
|
updates["is_muted"] = *req.IsMuted
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.IsSpecialCare != nil {
|
|
|
|
|
|
updates["is_special_care"] = *req.IsSpecialCare
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(updates) == 0 {
|
|
|
|
|
|
utils.BadRequest(c, "没有可更新的字段")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := service.ConversationSvc.UpdateConversationFlags(userID.(string), req.TargetID, updates); err != nil {
|
|
|
|
|
|
utils.InternalError(c, "更新失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utils.Success(c, "更新成功")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* DeleteConversationHandler
|
|
|
|
|
|
* 功能:删除会话
|
|
|
|
|
|
* 路径:POST /api/conversations/delete
|
|
|
|
|
|
*/
|
|
|
|
|
|
func DeleteConversationHandler(c *gin.Context) {
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
|
|
|
|
|
|
|
var req struct {
|
|
|
|
|
|
TargetID string `json:"target_id" binding:"required"`
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
utils.BadRequest(c, "参数错误")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := service.ConversationSvc.DeleteConversation(userID.(string), req.TargetID); err != nil {
|
|
|
|
|
|
utils.InternalError(c, "删除失败")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utils.Success(c, "删除成功")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-05 16:21:43 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* GetConversationByRoomHandler
|
2025-12-05 16:35:14 +08:00
|
|
|
|
* 功能:根据 room_id 获取会话(不自动创建)
|
2025-12-05 16:21:43 +08:00
|
|
|
|
* 路径:GET /api/conversations/by-room/:room_id
|
|
|
|
|
|
*/
|
|
|
|
|
|
func GetConversationByRoomHandler(c *gin.Context) {
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
|
roomID := c.Param("room_id")
|
|
|
|
|
|
|
|
|
|
|
|
if roomID == "" {
|
|
|
|
|
|
utils.BadRequest(c, "room_id 不能为空")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
conv, err := service.ConversationSvc.GetOrCreateConversationByRoom(userID.(string), roomID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
2025-12-05 16:35:14 +08:00
|
|
|
|
utils.NotFound(c, "会话不存在")
|
2025-12-05 16:21:43 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
utils.InternalError(c, "查询失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
utils.SuccessWithData(c, conv, "获取成功")
|
|
|
|
|
|
}
|
2025-12-04 08:51:40 +08:00
|
|
|
|
|