diff --git a/auto/api/v1/loose.go b/auto/api/v1/loose.go index 8e45824a..11d47e6a 100644 --- a/auto/api/v1/loose.go +++ b/auto/api/v1/loose.go @@ -16,6 +16,7 @@ type Loose interface { // Chain provide handlers chain for gin Chain() gin.HandlersChain + TweetComments(*web.TweetCommentsReq) (*web.TweetCommentsResp, mir.Error) TopicList(*web.TopicListReq) (*web.TopicListResp, mir.Error) GetUserProfile(*web.GetUserProfileReq) (*web.GetUserProfileResp, mir.Error) GetUserTweets(*web.GetUserTweetsReq) (*web.GetUserTweetsResp, mir.Error) @@ -25,6 +26,7 @@ type Loose interface { } type LooseBinding interface { + BindTweetComments(*gin.Context) (*web.TweetCommentsReq, mir.Error) BindTopicList(*gin.Context) (*web.TopicListReq, mir.Error) BindGetUserProfile(*gin.Context) (*web.GetUserProfileReq, mir.Error) BindGetUserTweets(*gin.Context) (*web.GetUserTweetsReq, mir.Error) @@ -34,6 +36,7 @@ type LooseBinding interface { } type LooseRender interface { + RenderTweetComments(*gin.Context, *web.TweetCommentsResp, mir.Error) RenderTopicList(*gin.Context, *web.TopicListResp, mir.Error) RenderGetUserProfile(*gin.Context, *web.GetUserProfileResp, mir.Error) RenderGetUserTweets(*gin.Context, *web.GetUserTweetsResp, mir.Error) @@ -50,6 +53,22 @@ func RegisterLooseServant(e *gin.Engine, s Loose, b LooseBinding, r LooseRender) router.Use(middlewares...) // register routes info to router + router.Handle("GET", "/post/comments", func(c *gin.Context) { + select { + case <-c.Request.Context().Done(): + return + default: + } + + req, err := b.BindTweetComments(c) + if err != nil { + r.RenderTweetComments(c, nil, err) + return + } + resp, err := s.TweetComments(req) + r.RenderTweetComments(c, resp, err) + }) + router.Handle("GET", "/tags", func(c *gin.Context) { select { case <-c.Request.Context().Done(): @@ -124,6 +143,10 @@ func (UnimplementedLooseServant) Chain() gin.HandlersChain { return nil } +func (UnimplementedLooseServant) TweetComments(req *web.TweetCommentsReq) (*web.TweetCommentsResp, mir.Error) { + return nil, mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented)) +} + func (UnimplementedLooseServant) TopicList(req *web.TopicListReq) (*web.TopicListResp, mir.Error) { return nil, mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented)) } @@ -147,6 +170,10 @@ type UnimplementedLooseRender struct { RenderAny func(*gin.Context, any, mir.Error) } +func (r *UnimplementedLooseRender) RenderTweetComments(c *gin.Context, data *web.TweetCommentsResp, err mir.Error) { + r.RenderAny(c, data, err) +} + func (r *UnimplementedLooseRender) RenderTopicList(c *gin.Context, data *web.TopicListResp, err mir.Error) { r.RenderAny(c, data, err) } @@ -170,6 +197,12 @@ type UnimplementedLooseBinding struct { BindAny func(*gin.Context, any) mir.Error } +func (b *UnimplementedLooseBinding) BindTweetComments(c *gin.Context) (*web.TweetCommentsReq, mir.Error) { + obj := new(web.TweetCommentsReq) + err := b.BindAny(c, obj) + return obj, err +} + func (b *UnimplementedLooseBinding) BindTopicList(c *gin.Context) (*web.TopicListReq, mir.Error) { obj := new(web.TopicListReq) err := b.BindAny(c, obj) diff --git a/auto/api/v1/pub.go b/auto/api/v1/pub.go index ab8a4832..c08efcf2 100644 --- a/auto/api/v1/pub.go +++ b/auto/api/v1/pub.go @@ -13,7 +13,6 @@ import ( ) type Pub interface { - TweetComments(*web.TweetCommentsReq) (*web.TweetCommentsResp, mir.Error) TweetDetail(*web.TweetDetailReq) (*web.TweetDetailResp, mir.Error) SendCaptcha(*web.SendCaptchaReq) mir.Error GetCaptcha() (*web.GetCaptchaResp, mir.Error) @@ -25,7 +24,6 @@ type Pub interface { } type PubBinding interface { - BindTweetComments(*gin.Context) (*web.TweetCommentsReq, mir.Error) BindTweetDetail(*gin.Context) (*web.TweetDetailReq, mir.Error) BindSendCaptcha(*gin.Context) (*web.SendCaptchaReq, mir.Error) BindRegister(*gin.Context) (*web.RegisterReq, mir.Error) @@ -35,7 +33,6 @@ type PubBinding interface { } type PubRender interface { - RenderTweetComments(*gin.Context, *web.TweetCommentsResp, mir.Error) RenderTweetDetail(*gin.Context, *web.TweetDetailResp, mir.Error) RenderSendCaptcha(*gin.Context, mir.Error) RenderGetCaptcha(*gin.Context, *web.GetCaptchaResp, mir.Error) @@ -51,22 +48,6 @@ func RegisterPubServant(e *gin.Engine, s Pub, b PubBinding, r PubRender) { router := e.Group("v1") // register routes info to router - router.Handle("GET", "/post/comments", func(c *gin.Context) { - select { - case <-c.Request.Context().Done(): - return - default: - } - - req, err := b.BindTweetComments(c) - if err != nil { - r.RenderTweetComments(c, nil, err) - return - } - resp, err := s.TweetComments(req) - r.RenderTweetComments(c, resp, err) - }) - router.Handle("GET", "/post", func(c *gin.Context) { select { case <-c.Request.Context().Done(): @@ -158,10 +139,6 @@ func RegisterPubServant(e *gin.Engine, s Pub, b PubBinding, r PubRender) { type UnimplementedPubServant struct { } -func (UnimplementedPubServant) TweetComments(req *web.TweetCommentsReq) (*web.TweetCommentsResp, mir.Error) { - return nil, mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented)) -} - func (UnimplementedPubServant) TweetDetail(req *web.TweetDetailReq) (*web.TweetDetailResp, mir.Error) { return nil, mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented)) } @@ -193,10 +170,6 @@ type UnimplementedPubRender struct { RenderAny func(*gin.Context, any, mir.Error) } -func (r *UnimplementedPubRender) RenderTweetComments(c *gin.Context, data *web.TweetCommentsResp, err mir.Error) { - r.RenderAny(c, data, err) -} - func (r *UnimplementedPubRender) RenderTweetDetail(c *gin.Context, data *web.TweetDetailResp, err mir.Error) { r.RenderAny(c, data, err) } @@ -228,12 +201,6 @@ type UnimplementedPubBinding struct { BindAny func(*gin.Context, any) mir.Error } -func (b *UnimplementedPubBinding) BindTweetComments(c *gin.Context) (*web.TweetCommentsReq, mir.Error) { - obj := new(web.TweetCommentsReq) - err := b.BindAny(c, obj) - return obj, err -} - func (b *UnimplementedPubBinding) BindTweetDetail(c *gin.Context) (*web.TweetDetailReq, mir.Error) { obj := new(web.TweetDetailReq) err := b.BindAny(c, obj) diff --git a/internal/core/comments.go b/internal/core/comments.go index 537d7068..88dcf432 100644 --- a/internal/core/comments.go +++ b/internal/core/comments.go @@ -25,7 +25,7 @@ type CommentService interface { GetCommentReplyByID(id int64) (*CommentReply, error) GetCommentContentsByIDs(ids []int64) ([]*CommentContent, error) GetCommentRepliesByID(ids []int64) ([]*CommentReplyFormated, error) - GetCommentThumbsMap(tweetId int64) (cs.CommentThumbsMap, cs.CommentThumbsMap, error) + GetCommentThumbsMap(userId int64, tweetId int64) (cs.CommentThumbsMap, cs.CommentThumbsMap, error) } // CommentManageService 评论管理服务 diff --git a/internal/dao/jinzhu/comments.go b/internal/dao/jinzhu/comments.go index 0f8f50e8..93d56a11 100644 --- a/internal/dao/jinzhu/comments.go +++ b/internal/dao/jinzhu/comments.go @@ -39,9 +39,12 @@ func newCommentManageService(db *gorm.DB) core.CommentManageService { } } -func (s *commentServant) GetCommentThumbsMap(tweetId int64) (cs.CommentThumbsMap, cs.CommentThumbsMap, error) { +func (s *commentServant) GetCommentThumbsMap(userId int64, tweetId int64) (cs.CommentThumbsMap, cs.CommentThumbsMap, error) { + if userId < 0 { + return nil, nil, nil + } commentThumbsList := cs.CommentThumbsList{} - err := s.db.Model(&dbr.TweetCommentThumbs{}).Where("tweet_id=?", tweetId).Find(&commentThumbsList).Error + err := s.db.Model(&dbr.TweetCommentThumbs{}).Where("user_id=? AND tweet_id=?", userId, tweetId).Find(&commentThumbsList).Error if err != nil { return nil, nil, err } @@ -93,6 +96,7 @@ func (s *commentServant) GetCommentRepliesByID(ids []int64) ([]*core.CommentRepl CommentReply := &dbr.CommentReply{} replies, err := CommentReply.List(s.db, &dbr.ConditionsT{ "comment_id IN ?": ids, + "ORDER": "id ASC", }, 0, 0) if err != nil { diff --git a/internal/model/web/loose.go b/internal/model/web/loose.go index 2745259c..e20fecd2 100644 --- a/internal/model/web/loose.go +++ b/internal/model/web/loose.go @@ -16,6 +16,16 @@ const ( TagTypeHotExtral TagType = "hot_extral" ) +type TweetCommentsReq struct { + SimpleInfo `form:"-" binding:"-"` + TweetId int64 `form:"id" binding:"required"` + SortStrategy string `form:"sort_strategy"` + Page int `form:"-" binding:"-"` + PageSize int `form:"-" binding:"-"` +} + +type TweetCommentsResp base.PageResp + type TagType string type TimelineReq struct { @@ -70,3 +80,7 @@ type TopicListResp struct { func (r *GetUserTweetsReq) SetPageInfo(page int, pageSize int) { r.Page, r.PageSize = page, pageSize } + +func (r *TweetCommentsReq) SetPageInfo(page int, pageSize int) { + r.Page, r.PageSize = page, pageSize +} diff --git a/internal/model/web/pub.go b/internal/model/web/pub.go index e8550984..422249cf 100644 --- a/internal/model/web/pub.go +++ b/internal/model/web/pub.go @@ -6,7 +6,6 @@ package web import ( "github.com/rocboss/paopao-ce/internal/core" - "github.com/rocboss/paopao-ce/internal/servants/base" "github.com/rocboss/paopao-ce/pkg/version" ) @@ -16,15 +15,6 @@ type TweetDetailReq struct { type TweetDetailResp core.PostFormated -type TweetCommentsReq struct { - TweetId int64 `form:"id"` - SortStrategy string `form:"sort_strategy"` - Page int `form:"-"` - PageSize int `form:"-"` -} - -type TweetCommentsResp base.PageResp - type GetCaptchaResp struct { Id string `json:"id"` Content string `json:"b64s"` diff --git a/internal/servants/web/loose.go b/internal/servants/web/loose.go index f5d186e6..76d3a49c 100644 --- a/internal/servants/web/loose.go +++ b/internal/servants/web/loose.go @@ -9,6 +9,8 @@ import ( "github.com/gin-gonic/gin" api "github.com/rocboss/paopao-ce/auto/api/v1" "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" + "github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr" "github.com/rocboss/paopao-ce/internal/model/web" "github.com/rocboss/paopao-ce/internal/servants/base" "github.com/rocboss/paopao-ce/internal/servants/chain" @@ -184,6 +186,93 @@ func (s *looseSrv) TopicList(req *web.TopicListReq) (*web.TopicListResp, mir.Err }, nil } +func (s *looseSrv) TweetComments(req *web.TweetCommentsReq) (*web.TweetCommentsResp, mir.Error) { + sort := "id ASC" + if req.SortStrategy == "newest" { + sort = "id DESC" + } + conditions := &core.ConditionsT{ + "post_id": req.TweetId, + "ORDER": sort, + } + + comments, err := s.Ds.GetComments(conditions, (req.Page-1)*req.PageSize, req.PageSize) + if err != nil { + return nil, _errGetCommentsFailed + } + + userIDs := []int64{} + commentIDs := []int64{} + for _, comment := range comments { + userIDs = append(userIDs, comment.UserID) + commentIDs = append(commentIDs, comment.ID) + } + + users, err := s.Ds.GetUsersByIDs(userIDs) + if err != nil { + return nil, _errGetCommentsFailed + } + + contents, err := s.Ds.GetCommentContentsByIDs(commentIDs) + if err != nil { + return nil, _errGetCommentsFailed + } + + replies, err := s.Ds.GetCommentRepliesByID(commentIDs) + if err != nil { + return nil, _errGetCommentsFailed + } + + var commentThumbs, replyThumbs cs.CommentThumbsMap + if req.Uid > 0 { + commentThumbs, replyThumbs, err = s.Ds.GetCommentThumbsMap(req.Uid, req.TweetId) + if err != nil { + return nil, _errGetCommentsFailed + } + } + + replyMap := make(map[int64][]*dbr.CommentReplyFormated) + if len(replyThumbs) > 0 { + for _, reply := range replies { + if thumbs, exist := replyThumbs[reply.ID]; exist { + reply.IsThumbsUp, reply.IsThumbsDown = thumbs.IsThumbsUp, thumbs.IsThumbsDown + } + replyMap[reply.CommentID] = append(replyMap[reply.CommentID], reply) + } + } else { + for _, reply := range replies { + replyMap[reply.CommentID] = append(replyMap[reply.CommentID], reply) + } + } + + commentsFormated := []*core.CommentFormated{} + for _, comment := range comments { + commentFormated := comment.Format() + if thumbs, exist := commentThumbs[comment.ID]; exist { + commentFormated.IsThumbsUp, commentFormated.IsThumbsDown = thumbs.IsThumbsUp, thumbs.IsThumbsDown + } + for _, content := range contents { + if content.CommentID == comment.ID { + commentFormated.Contents = append(commentFormated.Contents, content) + } + } + if replySlice, exist := replyMap[commentFormated.ID]; exist { + commentFormated.Replies = replySlice + } + for _, user := range users { + if user.ID == comment.UserID { + commentFormated.User = user.Format() + } + } + commentsFormated = append(commentsFormated, commentFormated) + } + + // 获取总量 + totalRows, _ := s.Ds.GetCommentCount(conditions) + resp := base.PageRespFrom(commentsFormated, req.Page, req.PageSize, totalRows) + return (*web.TweetCommentsResp)(resp), nil +} + func newLooseSrv(s *base.DaoServant) api.Loose { return &looseSrv{ DaoServant: s, diff --git a/internal/servants/web/pub.go b/internal/servants/web/pub.go index 8c87099a..6c58de3c 100644 --- a/internal/servants/web/pub.go +++ b/internal/servants/web/pub.go @@ -15,7 +15,6 @@ import ( "github.com/afocus/captcha" "github.com/alimy/mir/v3" - "github.com/gin-gonic/gin" "github.com/gofrs/uuid" api "github.com/rocboss/paopao-ce/auto/api/v1" "github.com/rocboss/paopao-ce/internal/core" @@ -23,7 +22,6 @@ import ( "github.com/rocboss/paopao-ce/internal/servants/base" "github.com/rocboss/paopao-ce/internal/servants/web/assets" "github.com/rocboss/paopao-ce/pkg/app" - "github.com/rocboss/paopao-ce/pkg/convert" "github.com/rocboss/paopao-ce/pkg/utils" "github.com/rocboss/paopao-ce/pkg/version" "github.com/rocboss/paopao-ce/pkg/xerror" @@ -54,92 +52,6 @@ type pubRender struct { *api.UnimplementedPubRender } -func (b *pubBinding) BindTweetComments(c *gin.Context) (*web.TweetCommentsReq, mir.Error) { - tweetId := convert.StrTo(c.Query("id")).MustInt64() - page, pageSize := app.GetPageInfo(c) - return &web.TweetCommentsReq{ - TweetId: tweetId, - SortStrategy: c.Query("sort_strategy"), - Page: page, - PageSize: pageSize, - }, nil -} - -func (s *pubSrv) TweetComments(req *web.TweetCommentsReq) (*web.TweetCommentsResp, mir.Error) { - sort := "id ASC" - if req.SortStrategy == "newest" { - sort = "id DESC" - } - conditions := &core.ConditionsT{ - "post_id": req.TweetId, - "ORDER": sort, - } - - comments, err := s.Ds.GetComments(conditions, (req.Page-1)*req.PageSize, req.PageSize) - if err != nil { - return nil, _errGetCommentsFailed - } - - userIDs := []int64{} - commentIDs := []int64{} - for _, comment := range comments { - userIDs = append(userIDs, comment.UserID) - commentIDs = append(commentIDs, comment.ID) - } - - users, err := s.Ds.GetUsersByIDs(userIDs) - if err != nil { - return nil, _errGetCommentsFailed - } - - contents, err := s.Ds.GetCommentContentsByIDs(commentIDs) - if err != nil { - return nil, _errGetCommentsFailed - } - - replies, err := s.Ds.GetCommentRepliesByID(commentIDs) - if err != nil { - return nil, _errGetCommentsFailed - } - - commentThumbs, replyThumbs, err := s.Ds.GetCommentThumbsMap(req.TweetId) - if err != nil { - return nil, _errGetCommentsFailed - } - - commentsFormated := []*core.CommentFormated{} - for _, comment := range comments { - commentFormated := comment.Format() - if thumbs, exist := commentThumbs[comment.ID]; exist { - commentFormated.IsThumbsUp, commentFormated.IsThumbsDown = thumbs.IsThumbsUp, thumbs.IsThumbsDown - } - for _, content := range contents { - if content.CommentID == comment.ID { - commentFormated.Contents = append(commentFormated.Contents, content) - } - } - for _, reply := range replies { - if thumbs, exist := replyThumbs[reply.ID]; exist { - reply.IsThumbsUp, reply.IsThumbsDown = thumbs.IsThumbsUp, thumbs.IsThumbsDown - } - if reply.CommentID == commentFormated.ID { - commentFormated.Replies = append(commentFormated.Replies, reply) - } - } - for _, user := range users { - if user.ID == comment.UserID { - commentFormated.User = user.Format() - } - } - commentsFormated = append(commentsFormated, commentFormated) - } - - // 获取总量 - totalRows, _ := s.Ds.GetCommentCount(conditions) - resp := base.PageRespFrom(commentsFormated, req.Page, req.PageSize, totalRows) - return (*web.TweetCommentsResp)(resp), nil -} - func (s *pubSrv) TweetDetail(req *web.TweetDetailReq) (*web.TweetDetailResp, mir.Error) { post, err := s.Ds.GetPostByID(req.TweetId) if err != nil { diff --git a/mirc/web/v1/loose.go b/mirc/web/v1/loose.go index 4fb9ecdf..810b2f87 100644 --- a/mirc/web/v1/loose.go +++ b/mirc/web/v1/loose.go @@ -26,4 +26,7 @@ type Loose struct { // TopicList 获取话题列表 TopicList func(Get, web.TopicListReq) web.TopicListResp `mir:"/tags"` + + // TweetComments 获取动态评论 + TweetComments func(Get, web.TweetCommentsReq) web.TweetCommentsResp `mir:"/post/comments"` } diff --git a/mirc/web/v1/pub.go b/mirc/web/v1/pub.go index 33c1544a..5320c537 100644 --- a/mirc/web/v1/pub.go +++ b/mirc/web/v1/pub.go @@ -31,7 +31,4 @@ type Pub struct { // TweetDetail 获取动态详情 TweetDetail func(Get, web.TweetDetailReq) web.TweetDetailResp `mir:"/post"` - - // TweetComments 获取动态评论 - TweetComments func(Get, web.TweetCommentsReq) web.TweetCommentsResp `mir:"/post/comments"` }