✨ add comment ent code
This commit is contained in:
@@ -21,7 +21,7 @@ func SubmitCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
l := comment.NewSubmitCommentLogic(r.Context(), svcCtx)
|
||||
resp, err := l.SubmitComment(&req)
|
||||
resp, err := l.SubmitComment(r, &req)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
httpx.WriteJsonCtx(
|
||||
|
@@ -21,7 +21,7 @@ func SubmitReplyCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
l := comment.NewSubmitReplyCommentLogic(r.Context(), svcCtx)
|
||||
resp, err := l.SubmitReplyComment(&req)
|
||||
resp, err := l.SubmitReplyComment(r, &req)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
httpx.WriteJsonCtx(
|
||||
|
@@ -2,9 +2,16 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mssola/useragent"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
|
||||
@@ -25,12 +32,94 @@ func NewSubmitCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sub
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SubmitCommentLogic) SubmitComment(req *types.CommentRequest) (resp *types.Response, err error) {
|
||||
func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRequest) (resp *types.Response, err error) {
|
||||
|
||||
res := verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key)
|
||||
if !res {
|
||||
return response.ErrorWithI18n(l.ctx, "captcha.verificationFailure"), nil
|
||||
}
|
||||
if len(req.Images) > 3 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.tooManyImages"), nil
|
||||
}
|
||||
userAgent := r.Header.Get("User-Agent")
|
||||
if userAgent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
ua := useragent.New(userAgent)
|
||||
|
||||
return
|
||||
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()
|
||||
isAuthor := 0
|
||||
session, wrong := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if wrong == nil {
|
||||
return nil, wrong
|
||||
}
|
||||
uid := session.Values["uid"].(string)
|
||||
if uid == req.Author {
|
||||
isAuthor = 1
|
||||
}
|
||||
xssFilterContent := utils.XssFilter(req.Content)
|
||||
if xssFilterContent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
|
||||
comment, err := l.svcCtx.MySQLClient.ScaCommentReply.Create().
|
||||
SetContent(commentContent).
|
||||
SetUserID(uid).
|
||||
SetTopicID(req.TopicId).
|
||||
SetCommentType(constant.CommentTopicType).
|
||||
SetCommentType(constant.COMMENT).
|
||||
SetAuthor(isAuthor).
|
||||
SetCommentIP(ip).
|
||||
SetLocation(location).
|
||||
SetBrowser(browser).
|
||||
SetOperatingSystem(operatingSystem).
|
||||
SetAgent(userAgent).Save(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(req.Images) > 0 {
|
||||
imagesDataCh := make(chan [][]byte)
|
||||
go func() {
|
||||
imagesData, err := utils.ProcessImages(req.Images)
|
||||
if err != nil {
|
||||
imagesDataCh <- nil
|
||||
return
|
||||
}
|
||||
imagesDataCh <- imagesData
|
||||
}()
|
||||
imagesData := <-imagesDataCh
|
||||
if imagesData == nil {
|
||||
return nil, errors.New("process images failed")
|
||||
}
|
||||
commentImages := types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: comment.ID,
|
||||
Images: imagesData,
|
||||
CreatedAt: comment.CreatedAt.String(),
|
||||
}
|
||||
if _, err = l.svcCtx.MongoClient.Collection("comment_images").InsertOne(l.ctx, commentImages); 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(),
|
||||
}
|
||||
return response.SuccessWithData(commentResponse), nil
|
||||
}
|
||||
|
@@ -2,7 +2,14 @@ package comment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/mssola/useragent"
|
||||
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/constant"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/response"
|
||||
"schisandra-album-cloud-microservices/app/core/api/common/utils"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/core/api/internal/types"
|
||||
|
||||
@@ -23,8 +30,43 @@ func NewSubmitReplyCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SubmitReplyCommentLogic) SubmitReplyComment(req *types.ReplyCommentRequest) (resp *types.Response, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types.ReplyCommentRequest) (resp *types.Response, err error) {
|
||||
res := verify.VerifySlideCaptcha(l.ctx, l.svcCtx.RedisClient, req.Point, req.Key)
|
||||
if !res {
|
||||
return response.ErrorWithI18n(l.ctx, "captcha.verificationFailure"), nil
|
||||
}
|
||||
if len(req.Images) > 3 {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.tooManyImages"), nil
|
||||
}
|
||||
userAgent := r.Header.Get("User-Agent")
|
||||
if userAgent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
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()
|
||||
isAuthor := 0
|
||||
session, wrong := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
|
||||
if wrong == nil {
|
||||
return nil, wrong
|
||||
}
|
||||
uid := session.Values["uid"].(string)
|
||||
if uid == req.Author {
|
||||
isAuthor = 1
|
||||
}
|
||||
xssFilterContent := utils.XssFilter(req.Content)
|
||||
if xssFilterContent == "" {
|
||||
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
|
||||
}
|
||||
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -122,6 +122,7 @@ func (c *MessageWebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {
|
||||
if conn, ok := c.sessions.Load(clientId); ok {
|
||||
_ = conn.WriteMessage(gws.OpcodeText, message.Bytes())
|
||||
}
|
||||
// fmt.Printf("received message from client %s\n", message.Data)
|
||||
}
|
||||
|
||||
// SendMessageToClient 向指定客户端发送消息
|
||||
|
24
app/core/api/internal/types/comment_types.go
Normal file
24
app/core/api/internal/types/comment_types.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package types
|
||||
|
||||
import "time"
|
||||
|
||||
type CommentResponse struct {
|
||||
Id int64 `json:"id"`
|
||||
Content string `json:"content"`
|
||||
UserId string `json:"user_id"`
|
||||
TopicId string `json:"topic_id"`
|
||||
Author int `json:"author"`
|
||||
Location string `json:"location"`
|
||||
Browser string `json:"browser"`
|
||||
OperatingSystem string `json:"operating_system"`
|
||||
CreatedTime time.Time `json:"created_time"`
|
||||
}
|
||||
|
||||
// CommentImages 评论图片
|
||||
type CommentImages struct {
|
||||
TopicId string `json:"topic_id" bson:"topic_id"`
|
||||
CommentId int64 `json:"comment_id" bson:"comment_id"`
|
||||
UserId string `json:"user_id" bson:"user_id"`
|
||||
Images [][]byte `json:"images" bson:"images"`
|
||||
CreatedAt string `json:"created_at" bson:"created_at"`
|
||||
}
|
Reference in New Issue
Block a user