Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed comments thumbs up/down state incorrect error #283

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ All notable changes to paopao-ce are documented in this file.
- fixed cache index not expire in delete/add tweet error [#266](https://github.com/rocboss/paopao-ce/pull/266)
- fixed tweet's owner not allow star/collection action when tweet is private error [#274](https://github.com/rocboss/paopao-ce/pull/274)
- fixed user not list owner's collectioned private tweet error [#274](https://github.com/rocboss/paopao-ce/pull/274)
- fixed comments thumbs up/down state incorrect error [#283](https://github.com/rocboss/paopao-ce/pull/283)

### Changed

Expand Down
33 changes: 33 additions & 0 deletions auto/api/v1/loose.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions auto/api/v1/pub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/core/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 评论管理服务
Expand Down
8 changes: 6 additions & 2 deletions internal/dao/jinzhu/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions internal/model/web/loose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
10 changes: 0 additions & 10 deletions internal/model/web/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"`
Expand Down
89 changes: 89 additions & 0 deletions internal/servants/web/loose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
Loading