🎨 update submit comment response

This commit is contained in:
landaiqing
2024-11-04 17:32:00 +08:00
parent 89a9b21bff
commit a153e0345a
4 changed files with 64 additions and 17 deletions

View File

@@ -8,7 +8,7 @@ p = sub, obj, act
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
e = some(where (p.eft == allow)) && !some(where (p_eft == deny))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

View File

@@ -1,9 +1,12 @@
package comment_controller
import (
"time"
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"github.com/mssola/useragent"
"schisandra-cloud-album/common/enum"
"schisandra-cloud-album/common/result"
"schisandra-cloud-album/global"
@@ -81,12 +84,23 @@ func (CommentController) CommentSubmit(c *gin.Context) {
OperatingSystem: operatingSystem,
Agent: userAgent,
}
response := commentReplyService.SubmitCommentService(&commentReply, commentRequest.TopicId, commentRequest.UserID, commentRequest.Images)
commentId, response := commentReplyService.SubmitCommentService(&commentReply, commentRequest.TopicId, commentRequest.UserID, commentRequest.Images)
if !response {
result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c)
return
}
result.OkWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitSuccess"), c)
responseData := model.ScaCommentReply{
Id: commentId,
Content: commentContent,
UserId: commentRequest.UserID,
TopicId: commentRequest.TopicId,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now(),
}
result.OkWithData(responseData, c)
return
}
@@ -157,12 +171,25 @@ func (CommentController) ReplySubmit(c *gin.Context) {
OperatingSystem: operatingSystem,
Agent: userAgent,
}
response := commentReplyService.SubmitCommentService(&commentReply, replyCommentRequest.TopicId, replyCommentRequest.UserID, replyCommentRequest.Images)
commentReplyId, response := commentReplyService.SubmitCommentService(&commentReply, replyCommentRequest.TopicId, replyCommentRequest.UserID, replyCommentRequest.Images)
if !response {
result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c)
return
}
result.OkWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitSuccess"), c)
responseData := model.ScaCommentReply{
Id: commentReplyId,
Content: commentContent,
UserId: replyCommentRequest.UserID,
TopicId: replyCommentRequest.TopicId,
ReplyId: replyCommentRequest.ReplyId,
ReplyUser: replyCommentRequest.ReplyUser,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now(),
}
result.OkWithData(responseData, c)
return
}
@@ -234,12 +261,26 @@ func (CommentController) ReplyReplySubmit(c *gin.Context) {
OperatingSystem: operatingSystem,
Agent: userAgent,
}
response := commentReplyService.SubmitCommentService(&commentReply, replyReplyRequest.TopicId, replyReplyRequest.UserID, replyReplyRequest.Images)
commentReplyReplyId, response := commentReplyService.SubmitCommentService(&commentReply, replyReplyRequest.TopicId, replyReplyRequest.UserID, replyReplyRequest.Images)
if !response {
result.FailWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitFailed"), c)
return
}
result.OkWithMessage(ginI18n.MustGetMessage(c, "CommentSubmitSuccess"), c)
responseData := model.ScaCommentReply{
Id: commentReplyReplyId,
Content: commentContent,
UserId: replyReplyRequest.UserID,
TopicId: replyReplyRequest.TopicId,
ReplyTo: replyReplyRequest.ReplyTo,
ReplyId: replyReplyRequest.ReplyId,
ReplyUser: replyReplyRequest.ReplyUser,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now(),
}
result.OkWithData(responseData, c)
return
}

View File

@@ -1,8 +1,9 @@
package model
import (
"gorm.io/gorm"
"time"
"gorm.io/gorm"
)
const ScaCommentReplyTable = "sca_comment_reply"
@@ -30,7 +31,6 @@ type ScaCommentReply struct {
Location string `gorm:"column:location;type:varchar(20);comment:评论地址" json:"location"`
Browser string `gorm:"column:browser;type:varchar(20);comment:评论浏览器" json:"browser"`
OperatingSystem string `gorm:"column:operating_system;type:varchar(20);comment:评论操作系统" json:"operating_system"`
CommentOrder int64 `gorm:"column:comment_order;type:bigint(20);default:0;comment:评论排序" json:"-"`
Agent string `gorm:"column:agent;type:varchar(255);comment:客户端设备信息" json:"agent"`
}

View File

@@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"sort"
"strconv"
"sync"
"time"
@@ -80,7 +81,7 @@ type LikeData struct {
}
// SubmitCommentService 提交评论
func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentReply, topicId string, uid string, images []string) bool {
func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentReply, topicId string, uid string, images []string) (int64, bool) {
tx := global.DB.Begin()
defer func() {
if r := recover(); r != nil {
@@ -91,14 +92,14 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep
err := commentReplyDao.CreateCommentReply(comment)
if err != nil {
global.LOG.Errorln(err)
return false
return 0, false
}
if comment.ReplyId != 0 {
err = commentReplyDao.UpdateCommentReplyCount(comment.ReplyId)
if err != nil {
global.LOG.Errorln(err)
tx.Rollback()
return false
return 0, false
}
}
@@ -117,7 +118,7 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep
imagesData := <-imagesDataCh
if imagesData == nil {
tx.Rollback()
return false
return 0, false
}
commentImages := CommentImages{
@@ -136,7 +137,7 @@ func (CommentReplyServiceImpl) SubmitCommentService(comment *model.ScaCommentRep
}()
}
tx.Commit()
return true
return comment.Id, true
}
// GetCommentReplyListService 获取评论回复列表
@@ -330,9 +331,9 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string,
query, u := gplus.NewQuery[model.ScaCommentReply]()
page := gplus.NewPage[model.ScaCommentReply](pageNum, size)
if isHot {
query.OrderByDesc(&u.CommentOrder).OrderByDesc(&u.Likes).OrderByDesc(&u.ReplyCount)
query.OrderByDesc(&u.Likes).OrderByDesc(&u.ReplyCount)
} else {
query.OrderByDesc(&u.CommentOrder).OrderByDesc(&u.CreatedTime)
query.OrderByDesc(&u.CreatedTime)
}
query.Eq(&u.TopicId, topicId).Eq(&u.CommentType, enum.COMMENT)
page, pageDB := gplus.SelectPage(page, query)
@@ -504,7 +505,12 @@ func (CommentReplyServiceImpl) GetCommentListService(uid string, topicId string,
for commentContent := range commentChannel {
commentsWithImages = append(commentsWithImages, commentContent)
}
// 根据CreatedTime进行排序如果isHot为false
if !isHot {
sort.Slice(commentsWithImages, func(i, j int) bool {
return commentsWithImages[i].CreatedTime.After(commentsWithImages[j].CreatedTime)
})
}
response := CommentResponse{
Comments: commentsWithImages,
Size: page.Size,