🏗️ update

This commit is contained in:
2024-12-24 18:01:54 +08:00
parent 89d64336f5
commit e31f95b943
168 changed files with 1337 additions and 14789 deletions

View File

@@ -0,0 +1,59 @@
package comment
import (
"context"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
errors2 "schisandra-album-cloud-microservices/common/errors"
"schisandra-album-cloud-microservices/common/i18n"
)
type DislikeCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDislikeCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DislikeCommentLogic {
return &DislikeCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DislikeCommentLogic) DislikeComment(req *types.CommentDisLikeRequest) (err error) {
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return errors.New("user_id not found")
}
tx := l.svcCtx.DB.Begin()
commentLike := l.svcCtx.DB.ScaCommentLike
resultInfo, err := tx.ScaCommentLike.Where(commentLike.TopicID.Eq(req.TopicId), commentLike.CommentID.Eq(req.CommentId), commentLike.UserID.Eq(uid)).Delete()
if err != nil {
_ = tx.Rollback()
return err
}
if resultInfo.RowsAffected == 0 {
_ = tx.Rollback()
return errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.LikeError"))
}
comment := l.svcCtx.DB.ScaCommentReply
updates, err := tx.ScaCommentReply.Where(comment.TopicID.Eq(req.TopicId), comment.ID.Eq(req.CommentId)).Update(comment.Likes, comment.Likes.Sub(1))
if err != nil {
_ = tx.Rollback()
return err
}
if updates.RowsAffected == 0 {
_ = tx.Rollback()
return errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.LikeError"))
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,162 @@
package comment
import (
"context"
"encoding/base64"
"errors"
"fmt"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mongodb"
"schisandra-album-cloud-microservices/common/constant"
"schisandra-album-cloud-microservices/common/utils"
"sync"
"github.com/chenmingyong0423/go-mongox/v2/builder/query"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gen/field"
)
type GetCommentListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
wg sync.WaitGroup
}
func NewGetCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentListLogic {
return &GetCommentListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCommentListLogic) GetCommentList(req *types.CommentListRequest) (resp *types.CommentListPageResponse, err error) {
// 获取用户ID
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return nil, errors.New("user_id not found")
}
var commentQueryList []types.CommentListQueryResult
comment := l.svcCtx.DB.ScaCommentReply
user := l.svcCtx.DB.ScaAuthUser
var orderConditions []field.Expr
if req.IsHot {
orderConditions = append(orderConditions, comment.Likes.Desc(), comment.ReplyCount.Desc())
} else {
orderConditions = append(orderConditions, comment.CreatedAt.Desc())
}
count, err := comment.Select(
comment.ID,
comment.UserID,
comment.TopicID,
comment.Content,
comment.CreatedAt,
comment.Author,
comment.Likes,
comment.ReplyCount,
comment.Browser,
comment.OperatingSystem,
comment.Location,
user.Avatar,
user.Nickname,
).LeftJoin(user, comment.UserID.EqCol(user.UID)).
Where(comment.TopicID.Eq(req.TopicId), comment.CommentType.Eq(constant.COMMENT)).
Order(orderConditions...).
ScanByPage(&commentQueryList, (req.Page-1)*req.Size, req.Size)
if err != nil {
return nil, err
}
if count == 0 || len(commentQueryList) == 0 {
return &types.CommentListPageResponse{
Total: count,
Size: req.Size,
Current: req.Page,
Comments: []types.CommentContent{},
}, nil
}
// **************** 获取评论Id和用户Id ************
commentIds := make([]int64, 0, len(commentQueryList))
for _, commentList := range commentQueryList {
commentIds = append(commentIds, commentList.ID)
}
l.wg.Add(2)
// *************** 获取评论点赞状态 **********
likeMap := make(map[int64]bool)
go func() {
defer l.wg.Done()
commentLike := l.svcCtx.DB.ScaCommentLike
likeList, err := commentLike.Where(
commentLike.TopicID.Eq(req.TopicId),
commentLike.UserID.Eq(uid),
commentLike.CommentID.In(commentIds...)).
Find()
if err != nil {
logx.Error(err)
return
}
for _, like := range likeList {
likeMap[like.CommentID] = true
}
}()
// ***************获取评论图片 **********
commentImageMap := make(map[int64][]string)
go func() {
defer l.wg.Done()
newCollection := mongodb.MustNewCollection[types.CommentImages](l.svcCtx.MongoClient, constant.COMMENT_IMAGES)
commentImages, err := newCollection.Finder().
Filter(query.Eq("topic_id", req.TopicId)).
Filter(query.In("comment_id", commentIds...)).
Find(l.ctx)
if err != nil {
logx.Error(err)
return
}
for _, image := range commentImages {
if len(image.Images) == 0 {
continue
}
imagesBase64 := make([]string, len(image.Images))
for i, img := range image.Images {
imagesBase64[i] = fmt.Sprintf("data:%s;base64,%s", utils.GetMimeType(img), base64.StdEncoding.EncodeToString(img))
}
commentImageMap[image.CommentId] = imagesBase64
}
}()
l.wg.Wait()
// *************** 组装数据 **********
result := make([]types.CommentContent, 0, len(commentQueryList))
for _, commentData := range commentQueryList {
commentContent := types.CommentContent{
Avatar: commentData.Avatar,
NickName: commentData.Nickname,
Content: commentData.Content,
CreatedTime: commentData.CreatedAt.Format(constant.TimeFormat),
Level: 0,
Id: commentData.ID,
UserId: commentData.UserID,
TopicId: commentData.TopicID,
IsAuthor: commentData.Author,
Likes: commentData.Likes,
ReplyCount: commentData.ReplyCount,
Location: commentData.Location,
Browser: commentData.Browser,
OperatingSystem: commentData.OperatingSystem,
IsLiked: likeMap[commentData.ID],
Images: commentImageMap[commentData.ID],
}
result = append(result, commentContent)
}
commentListPageResponse := &types.CommentListPageResponse{
Total: count,
Size: req.Size,
Current: req.Page,
Comments: result,
}
return commentListPageResponse, nil
}

View File

@@ -0,0 +1,166 @@
package comment
import (
"context"
"encoding/base64"
"errors"
"fmt"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mongodb"
"schisandra-album-cloud-microservices/common/constant"
"schisandra-album-cloud-microservices/common/utils"
"sync"
"github.com/chenmingyong0423/go-mongox/v2/builder/query"
"github.com/zeromicro/go-zero/core/logx"
)
type GetReplyListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
wg sync.WaitGroup
}
func NewGetReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetReplyListLogic {
return &GetReplyListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetReplyListLogic) GetReplyList(req *types.ReplyListRequest) (resp *types.CommentListPageResponse, err error) {
// 获取用户ID
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return nil, errors.New("user_id not found")
}
var replyQueryList []types.ReplyListQueryResult
reply := l.svcCtx.DB.ScaCommentReply
user := l.svcCtx.DB.ScaAuthUser
commentUser := user.As("comment_user")
replyUser := user.As("reply_user")
count, err := reply.Select(
reply.ID,
reply.UserID,
reply.TopicID,
reply.Content,
reply.CreatedAt,
reply.Author,
reply.ReplyCount,
reply.Likes,
reply.Browser,
reply.OperatingSystem,
reply.Location,
reply.ReplyUser,
reply.ReplyTo,
reply.ReplyID,
commentUser.Avatar,
commentUser.Nickname,
replyUser.Nickname.As("reply_nickname"),
).LeftJoin(commentUser, reply.UserID.EqCol(commentUser.UID)).
LeftJoin(replyUser, reply.ReplyUser.EqCol(replyUser.UID)).
Where(reply.TopicID.Eq(req.TopicId), reply.ReplyID.Eq(req.CommentId), reply.CommentType.Eq(constant.REPLY)).
Order(reply.Likes.Desc(), reply.CreatedAt.Desc()).
ScanByPage(&replyQueryList, (req.Page-1)*req.Size, req.Size)
if err != nil {
return nil, err
}
if count == 0 || len(replyQueryList) == 0 {
return &types.CommentListPageResponse{
Total: count,
Size: req.Size,
Current: req.Page,
}, nil
}
// **************** 获取评论Id和用户Id ************
commentIds := make([]int64, 0, len(replyQueryList))
for _, commentList := range replyQueryList {
commentIds = append(commentIds, commentList.ID)
}
l.wg.Add(2)
// *************** 获取评论点赞状态 **********
likeMap := make(map[int64]bool)
go func() {
defer l.wg.Done()
commentLike := l.svcCtx.DB.ScaCommentLike
likeList, err := commentLike.Where(
commentLike.TopicID.Eq(req.TopicId),
commentLike.UserID.Eq(uid),
commentLike.CommentID.In(commentIds...)).
Find()
if err != nil {
logx.Error(err)
return
}
for _, like := range likeList {
likeMap[like.CommentID] = true
}
}()
// ***************获取评论图片 **********
commentImageMap := make(map[int64][]string)
go func() {
defer l.wg.Done()
newCollection := mongodb.MustNewCollection[types.CommentImages](l.svcCtx.MongoClient, constant.COMMENT_IMAGES)
commentImages, err := newCollection.Finder().
Filter(query.Eq("topic_id", req.TopicId)).
Filter(query.In("comment_id", commentIds...)).
Find(l.ctx)
if err != nil {
logx.Error(err)
return
}
for _, image := range commentImages {
if len(image.Images) == 0 {
continue
}
imagesBase64 := make([]string, len(image.Images))
for i, img := range image.Images {
imagesBase64[i] = fmt.Sprintf("data:%s;base64,%s", utils.GetMimeType(img), base64.StdEncoding.EncodeToString(img))
}
commentImageMap[image.CommentId] = imagesBase64
}
}()
l.wg.Wait()
// *************** 组装数据 **********
result := make([]types.CommentContent, 0, len(replyQueryList))
for _, replyData := range replyQueryList {
commentContent := types.CommentContent{
Avatar: replyData.Avatar,
NickName: replyData.Nickname,
Content: replyData.Content,
CreatedTime: replyData.CreatedAt.Format(constant.TimeFormat),
Level: 0,
Id: replyData.ID,
UserId: replyData.UserID,
TopicId: replyData.TopicID,
IsAuthor: replyData.Author,
Likes: replyData.Likes,
ReplyCount: replyData.ReplyCount,
Location: replyData.Location,
Browser: replyData.Browser,
OperatingSystem: replyData.OperatingSystem,
IsLiked: likeMap[replyData.ID],
Images: commentImageMap[replyData.ID],
ReplyUser: replyData.ReplyUser,
ReplyTo: replyData.ReplyTo,
ReplyId: replyData.ReplyId,
ReplyNickname: replyData.ReplyNickname,
}
result = append(result, commentContent)
}
commentListPageResponse := &types.CommentListPageResponse{
Total: count,
Size: req.Size,
Current: req.Page,
Comments: result,
}
return commentListPageResponse, nil
}

View File

@@ -0,0 +1,61 @@
package comment
import (
"context"
"errors"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mysql/model"
"schisandra-album-cloud-microservices/common/i18n"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
type LikeCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewLikeCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeCommentLogic {
return &LikeCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *LikeCommentLogic) LikeComment(req *types.CommentLikeRequest) (err error) {
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return errors.New("user_id not found")
}
tx := l.svcCtx.DB.Begin()
commentLike := &model.ScaCommentLike{
CommentID: req.CommentId,
TopicID: req.TopicId,
UserID: uid,
LikeTime: time.Now(),
}
err = tx.ScaCommentLike.Create(commentLike)
if err != nil {
_ = tx.Rollback()
return err
}
comment := l.svcCtx.DB.ScaCommentReply
updates, err := tx.ScaCommentReply.Where(comment.TopicID.Eq(req.TopicId), comment.ID.Eq(req.CommentId)).Update(comment.Likes, comment.Likes.Add(1))
if err != nil {
_ = tx.Rollback()
return err
}
if updates.RowsAffected == 0 {
_ = tx.Rollback()
return errors.New(i18n.FormatText(l.ctx, "comment.LikeError"))
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,125 @@
package comment
import (
"context"
"errors"
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mongodb"
"schisandra-album-cloud-microservices/app/auth/model/mysql/model"
"schisandra-album-cloud-microservices/common/captcha/verify"
"schisandra-album-cloud-microservices/common/constant"
errors2 "schisandra-album-cloud-microservices/common/errors"
"schisandra-album-cloud-microservices/common/i18n"
"schisandra-album-cloud-microservices/common/utils"
"time"
"github.com/mssola/useragent"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSubmitCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitCommentLogic {
return &SubmitCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRequest) (resp *types.CommentResponse, err error) {
res := verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key)
if !res {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "captcha.verificationFailure"))
}
if len(req.Images) > 3 {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.tooManyImages"))
}
userAgent := r.Header.Get("User-Agent")
if userAgent == "" {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
ua := useragent.New(userAgent)
ip := utils.GetClientIP(r)
location, err := l.svcCtx.Ip2Region.SearchByStr(ip)
if err != nil {
return nil, err
}
location = utils.RemoveZeroAndAdjust(location)
browser, _ := ua.Browser()
operatingSystem := ua.OS()
var isAuthor int64 = 0
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return nil, errors.New("user_id not found in context")
}
if uid == req.Author {
isAuthor = 1
}
xssFilterContent := utils.XssFilter(req.Content)
if xssFilterContent == "" {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
topicType := constant.CommentTopicType
commentType := constant.COMMENT
comment := &model.ScaCommentReply{
Content: commentContent,
UserID: uid,
TopicID: req.TopicId,
TopicType: topicType,
CommentType: commentType,
Author: isAuthor,
CommentIP: ip,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
Agent: userAgent,
}
err = l.svcCtx.DB.ScaCommentReply.Create(comment)
if err != nil {
return nil, err
}
if len(req.Images) > 0 {
imagesData, err := utils.ProcessImages(req.Images)
if err != nil {
return nil, err
}
commentImages := &types.CommentImages{
UserId: uid,
TopicId: req.TopicId,
CommentId: comment.ID,
Images: imagesData,
}
newCollection := mongodb.MustNewCollection[types.CommentImages](l.svcCtx.MongoClient, constant.COMMENT_IMAGES)
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
if err != nil {
return nil, err
}
}
commentResponse := &types.CommentResponse{
Id: comment.ID,
Content: commentContent,
UserId: uid,
TopicId: req.TopicId,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now().Format(constant.TimeFormat),
}
return commentResponse, nil
}

View File

@@ -0,0 +1,146 @@
package comment
import (
"context"
"errors"
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mongodb"
"schisandra-album-cloud-microservices/app/auth/model/mysql/model"
"schisandra-album-cloud-microservices/common/captcha/verify"
"schisandra-album-cloud-microservices/common/constant"
errors2 "schisandra-album-cloud-microservices/common/errors"
"schisandra-album-cloud-microservices/common/i18n"
"schisandra-album-cloud-microservices/common/utils"
"time"
"github.com/mssola/useragent"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitReplyCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSubmitReplyCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitReplyCommentLogic {
return &SubmitReplyCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types.ReplyCommentRequest) (resp *types.CommentResponse, err error) {
res := verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key)
if !res {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "captcha.verificationFailure"))
}
if len(req.Images) > 3 {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.tooManyImages"))
}
userAgent := r.Header.Get("User-Agent")
if userAgent == "" {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
ua := useragent.New(userAgent)
ip := utils.GetClientIP(r)
location, err := l.svcCtx.Ip2Region.SearchByStr(ip)
if err != nil {
return nil, err
}
location = utils.RemoveZeroAndAdjust(location)
browser, _ := ua.Browser()
operatingSystem := ua.OS()
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return nil, errors.New("user_id not found in context")
}
var isAuthor int64 = 0
if uid == req.Author {
isAuthor = 1
}
xssFilterContent := utils.XssFilter(req.Content)
if xssFilterContent == "" {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
tx := l.svcCtx.DB.Begin()
topicType := constant.CommentTopicType
commentType := constant.REPLY
reply := &model.ScaCommentReply{
Content: commentContent,
UserID: uid,
TopicID: req.TopicId,
TopicType: topicType,
CommentType: commentType,
Author: isAuthor,
CommentIP: ip,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
Agent: userAgent,
ReplyID: req.ReplyId,
ReplyUser: req.ReplyUser,
}
err = tx.ScaCommentReply.Create(reply)
if err != nil {
return nil, err
}
commentReply := l.svcCtx.DB.ScaCommentReply
update, err := tx.ScaCommentReply.Where(commentReply.ID.Eq(req.ReplyId)).Update(commentReply.ReplyCount, commentReply.ReplyCount.Add(1))
if err != nil {
return nil, err
}
if update.RowsAffected == 0 {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
if len(req.Images) > 0 {
imagesData, err := utils.ProcessImages(req.Images)
if err != nil {
return nil, err
}
commentImages := &types.CommentImages{
UserId: uid,
TopicId: req.TopicId,
CommentId: reply.ID,
Images: imagesData,
}
newCollection := mongodb.MustNewCollection[types.CommentImages](l.svcCtx.MongoClient, constant.COMMENT_IMAGES)
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
if err != nil {
return nil, err
}
}
commentResponse := &types.CommentResponse{
Id: reply.ID,
Content: commentContent,
UserId: uid,
TopicId: reply.TopicID,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now().Format(constant.TimeFormat),
ReplyId: reply.ReplyID,
ReplyUser: reply.ReplyUser,
}
err = tx.Commit()
if err != nil {
return nil, err
}
return commentResponse, nil
}

View File

@@ -0,0 +1,156 @@
package comment
import (
"context"
"errors"
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mongodb"
"schisandra-album-cloud-microservices/app/auth/model/mysql/model"
"schisandra-album-cloud-microservices/common/captcha/verify"
"schisandra-album-cloud-microservices/common/constant"
errors2 "schisandra-album-cloud-microservices/common/errors"
"schisandra-album-cloud-microservices/common/i18n"
"schisandra-album-cloud-microservices/common/utils"
"time"
"github.com/mssola/useragent"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitReplyReplyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSubmitReplyReplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitReplyReplyLogic {
return &SubmitReplyReplyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.ReplyReplyRequest) (resp *types.CommentResponse, err error) {
res := verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key)
if !res {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "captcha.verificationFailure"))
}
if len(req.Images) > 3 {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.tooManyImages"))
}
userAgent := r.Header.Get("User-Agent")
if userAgent == "" {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
ua := useragent.New(userAgent)
// 获取客户端IP及位置信息
ip := utils.GetClientIP(r)
location, err := l.svcCtx.Ip2Region.SearchByStr(ip)
if err != nil {
return nil, err
}
location = utils.RemoveZeroAndAdjust(location)
// 获取浏览器与操作系统信息
browser, _ := ua.Browser()
operatingSystem := ua.OS()
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return nil, errors.New("user_id not found in context")
}
// 判断作者身份
var isAuthor int64 = 0
if uid == req.Author {
isAuthor = 1
}
// XSS过滤
xssFilterContent := utils.XssFilter(req.Content)
if xssFilterContent == "" {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
tx := l.svcCtx.DB.Begin()
topicType := constant.CommentTopicType
commentType := constant.REPLY
replyReply := &model.ScaCommentReply{
Content: commentContent,
UserID: uid,
TopicID: req.TopicId,
TopicType: topicType,
CommentType: commentType,
Author: isAuthor,
CommentIP: ip,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
Agent: userAgent,
ReplyID: req.ReplyId,
ReplyUser: req.ReplyUser,
ReplyTo: req.ReplyTo,
}
err = tx.ScaCommentReply.Create(replyReply)
if err != nil {
return nil, err
}
commentReply := l.svcCtx.DB.ScaCommentReply
update, err := tx.ScaCommentReply.Where(commentReply.ID.Eq(req.ReplyId)).Update(commentReply.ReplyCount, commentReply.ReplyCount.Add(1))
if err != nil {
return nil, err
}
if update.RowsAffected == 0 {
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
}
// 处理图片
if len(req.Images) > 0 {
imagesData, err := utils.ProcessImages(req.Images)
if err != nil {
return nil, err
}
commentImages := &types.CommentImages{
UserId: uid,
TopicId: req.TopicId,
CommentId: replyReply.ID,
Images: imagesData,
}
newCollection := mongodb.MustNewCollection[types.CommentImages](l.svcCtx.MongoClient, constant.COMMENT_IMAGES)
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
if err != nil {
return nil, err
}
}
// 构建响应
commentResponse := &types.CommentResponse{
Id: replyReply.ID,
Content: commentContent,
UserId: uid,
TopicId: replyReply.TopicID,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now().Format(constant.TimeFormat),
ReplyId: replyReply.ReplyID,
ReplyUser: replyReply.ReplyUser,
ReplyTo: replyReply.ReplyTo,
}
// 提交事务
if err = tx.Commit(); err != nil {
return nil, err
}
return commentResponse, nil
}