mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.6 KiB
106 lines
2.6 KiB
2 years ago
|
// Copyright 2022 ROC. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package api
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
|
"github.com/rocboss/paopao-ce/internal/servants/web/broker"
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/pkg/app"
|
||
|
"github.com/rocboss/paopao-ce/pkg/errcode"
|
||
3 years ago
|
"github.com/sirupsen/logrus"
|
||
3 years ago
|
)
|
||
|
|
||
|
func GetUnreadMsgCount(c *gin.Context) {
|
||
|
response := app.NewResponse(c)
|
||
|
|
||
2 years ago
|
user := &core.User{}
|
||
3 years ago
|
if u, exists := c.Get("USER"); exists {
|
||
2 years ago
|
user = u.(*core.User)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
count, _ := broker.GetUnreadCount(user.ID)
|
||
3 years ago
|
|
||
|
response.ToResponse(gin.H{
|
||
|
"count": count,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func GetMessages(c *gin.Context) {
|
||
|
response := app.NewResponse(c)
|
||
|
|
||
|
userID, _ := c.Get("UID")
|
||
2 years ago
|
messages, totalRows, err := broker.GetMessages(userID.(int64), (app.GetPage(c)-1)*app.GetPageSize(c), app.GetPageSize(c))
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
3 years ago
|
logrus.Errorf("service.GetMessages err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.GetMessagesFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponseList(messages, totalRows)
|
||
|
}
|
||
|
|
||
|
func ReadMessage(c *gin.Context) {
|
||
2 years ago
|
param := broker.ReadMessageReq{}
|
||
3 years ago
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
3 years ago
|
logrus.Errorf("app.BindAndValid errs: %v", errs)
|
||
3 years ago
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
userID, _ := c.Get("UID")
|
||
2 years ago
|
err := broker.ReadMessage(param.ID, userID.(int64))
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
logrus.Errorf("service.ReadMessage err: %v\n", err)
|
||
3 years ago
|
response.ToErrorResponse(errcode.ReadMessageFailed)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(nil)
|
||
|
}
|
||
3 years ago
|
|
||
|
func SendUserWhisper(c *gin.Context) {
|
||
2 years ago
|
param := broker.WhisperReq{}
|
||
3 years ago
|
response := app.NewResponse(c)
|
||
|
valid, errs := app.BindAndValid(c, ¶m)
|
||
|
if !valid {
|
||
3 years ago
|
logrus.Errorf("app.BindAndValid errs: %v", errs)
|
||
3 years ago
|
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
userID, _ := c.Get("UID")
|
||
|
|
||
|
// 不允许发送私信给自己
|
||
|
if userID.(int64) == param.UserID {
|
||
|
response.ToErrorResponse(errcode.NoWhisperToSelf)
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
_, err := broker.CreateWhisper(c, &core.Message{
|
||
3 years ago
|
SenderUserID: userID.(int64),
|
||
|
ReceiverUserID: param.UserID,
|
||
2 years ago
|
Type: core.MsgTypeWhisper,
|
||
3 years ago
|
Brief: "给你发送新私信了",
|
||
3 years ago
|
Content: param.Content,
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
3 years ago
|
logrus.Errorf("service.CreateWhisper err: %v\n", err)
|
||
3 years ago
|
|
||
|
if err == errcode.TooManyWhisperNum {
|
||
|
response.ToErrorResponse(errcode.TooManyWhisperNum)
|
||
|
} else {
|
||
|
response.ToErrorResponse(errcode.SendWhisperFailed)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.ToResponse(nil)
|
||
|
}
|