Skip to content

Commit 7d8a4f6

Browse files
committed
post comment
1 parent 99c21a9 commit 7d8a4f6

File tree

8 files changed

+286
-17
lines changed

8 files changed

+286
-17
lines changed

apimodels/post.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ type Posts struct {
66
}
77

88
type Post struct {
9-
PostId string `json:"post_id"`
10-
Content *PostContent `json:"content"`
11-
UserInfo *UserObj `json:"user_info"`
12-
CreatedTime int64 `json:"created_time"`
13-
UpdatedTime int64 `json:"updated_time"`
14-
Reactions map[string]*Reaction `json:"reactions"`
9+
PostId string `json:"post_id"`
10+
Content *PostContent `json:"content"`
11+
UserInfo *UserObj `json:"user_info"`
12+
CreatedTime int64 `json:"created_time"`
13+
UpdatedTime int64 `json:"updated_time"`
14+
Reactions map[string][]*Reaction `json:"reactions"`
15+
TopComments []*PostComment `json:"top_comments"`
1516
}
1617

1718
type Reaction struct {
@@ -34,12 +35,18 @@ type PostContentVideo struct {
3435
}
3536

3637
type PostComment struct {
37-
PostId string `json:"post_id"`
3838
CommentId string `json:"comment_id"`
39+
PostId string `json:"post_id"`
3940
ParentCommentId string `json:"parent_comment_id"`
40-
Content string `json:"content"`
41+
Text string `json:"text"`
42+
ParentUserId string `json:"parent_user_id,omitempty"`
4143
ParentUserInfo *UserObj `json:"parent_user_info"`
4244
UserInfo *UserObj `json:"user_info"`
4345
CreatedTime int64 `json:"created_time"`
4446
UpdatedTime int64 `json:"updated_time"`
4547
}
48+
49+
type PostComments struct {
50+
Items []*PostComment `json:"items"`
51+
IsFinished bool `json:"is_finished"`
52+
}

apis/post.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func PostAdd(ctx *gin.Context) {
2323
SuccessHttpResp(ctx, resp)
2424
}
2525

26-
func PostList(ctx *gin.Context) {
26+
func QryPosts(ctx *gin.Context) {
2727
var limit int64 = 20
2828
limitStr := ctx.Query("limit")
2929
var err error
@@ -54,3 +54,68 @@ func PostList(ctx *gin.Context) {
5454
}
5555
SuccessHttpResp(ctx, resp)
5656
}
57+
58+
func PostInfo(ctx *gin.Context) {
59+
postId := ctx.Query("post_id")
60+
if postId == "" {
61+
ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
62+
return
63+
}
64+
code, resp := services.QryPostInfo(services.ToCtx(ctx), postId)
65+
if code != errs.IMErrorCode_SUCCESS {
66+
ErrorHttpResp(ctx, code)
67+
return
68+
}
69+
SuccessHttpResp(ctx, resp)
70+
}
71+
72+
func QryPostComments(ctx *gin.Context) {
73+
postId := ctx.Query("post_id")
74+
if postId == "" {
75+
ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
76+
return
77+
}
78+
var limit int64 = 20
79+
limitStr := ctx.Query("limit")
80+
var err error
81+
if limitStr != "" {
82+
limit, err = utils.String2Int64(limitStr)
83+
if err != nil {
84+
limit = 20
85+
}
86+
}
87+
var start int64
88+
startTimeStr := ctx.Query("start")
89+
start, err = utils.String2Int64(startTimeStr)
90+
if err != nil {
91+
start = 0
92+
}
93+
var isPositive bool = false
94+
orderStr := ctx.Query("order")
95+
order, err := utils.String2Int64(orderStr)
96+
if err == nil {
97+
if order == 1 {
98+
isPositive = true
99+
}
100+
}
101+
code, resp := services.QryPostComments(services.ToCtx(ctx), postId, start, limit, isPositive)
102+
if code != errs.IMErrorCode_SUCCESS {
103+
ErrorHttpResp(ctx, code)
104+
return
105+
}
106+
SuccessHttpResp(ctx, resp)
107+
}
108+
109+
func PostCommentAdd(ctx *gin.Context) {
110+
req := apimodels.PostComment{}
111+
if err := ctx.BindJSON(&req); err != nil || req.PostId == "" {
112+
ErrorHttpResp(ctx, errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
113+
return
114+
}
115+
code, resp := services.PostCommentAdd(services.ToCtx(ctx), &req)
116+
if code != errs.IMErrorCode_SUCCESS {
117+
ErrorHttpResp(ctx, code)
118+
return
119+
}
120+
SuccessHttpResp(ctx, resp)
121+
}

docs/appbusiness.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ CREATE TABLE `postcomments` (
269269
`parent_comment_id` varchar(32) DEFAULT NULL,
270270
`parent_user_id` varchar(32) DEFAULT NULL,
271271
`user_id` varchar(32) DEFAULT NULL,
272-
`content` varchar(5000) DEFAULT NULL,
272+
`text` varchar(5000) DEFAULT NULL,
273273
`created_time` bigint DEFAULT NULL,
274274
`updated_time` datetime(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
275275
`is_delete` tinyint DEFAULT '0',

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ func main() {
106106
group.GET("/jim/friends/mypendingapplications", apis.MyPendingFriendApplications)
107107

108108
//post
109-
group.GET("/jim/posts/list", apis.PostList)
110-
group.GET("/jim/posts/info")
109+
group.GET("/jim/posts/list", apis.QryPosts)
110+
group.GET("/jim/posts/info", apis.PostInfo)
111111
group.POST("/jim/posts/add", apis.PostAdd)
112112
group.POST("/jim/posts/update")
113113
group.POST("/jim/posts/del")
114114
group.POST("/jim/posts/reactions/add")
115115
group.GET("/jim/posts/reactions/list")
116116

117-
group.GET("/jim/postcomments/list")
118-
group.POST("/jim/postcomments/add")
117+
group.GET("/jim/postcomments/list", apis.QryPostComments)
118+
group.POST("/jim/postcomments/add", apis.PostCommentAdd)
119119
group.POST("/jim/postcomments/update")
120120
group.POST("/jim/postcomments/del")
121121

services/postservice.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,90 @@ func QryPosts(ctx context.Context, startTime int64, limit int64, isPositive bool
4949
CreatedTime: p.CreatedTime,
5050
UpdatedTime: p.UpdatedTime.UnixMilli(),
5151
}
52+
//top comments
53+
code, topComments := QryPostComments(ctx, p.PostId, 0, 10, false)
54+
if code == errs.IMErrorCode_SUCCESS && topComments != nil {
55+
post.TopComments = topComments.Items
56+
}
5257
ret.Items = append(ret.Items, post)
5358
}
59+
if len(ret.Items) > int(limit) {
60+
ret.Items = ret.Items[:limit]
61+
ret.IsFinished = false
62+
}
63+
}
64+
return errs.IMErrorCode_SUCCESS, ret
65+
}
66+
67+
func QryPostInfo(ctx context.Context, postId string) (errs.IMErrorCode, *apimodels.Post) {
68+
appkey := GetAppKeyFromCtx(ctx)
69+
ret := &apimodels.Post{}
70+
storage := storages.NewPostStorage()
71+
post, err := storage.FindById(appkey, postId)
72+
if err == nil {
73+
postContent := &apimodels.PostContent{}
74+
err = utils.JsonUnMarshal(post.Content, postContent)
75+
if err == nil {
76+
ret.Content = postContent
77+
}
78+
ret.PostId = post.PostId
79+
ret.CreatedTime = post.CreatedTime
80+
ret.UpdatedTime = post.UpdatedTime.UnixMilli()
81+
ret.UserInfo = GetUser(ctx, post.UserId)
82+
}
83+
return errs.IMErrorCode_SUCCESS, ret
84+
}
85+
86+
func PostCommentAdd(ctx context.Context, req *apimodels.PostComment) (errs.IMErrorCode, *apimodels.PostComment) {
87+
appkey := GetRequesterIdFromCtx(ctx)
88+
userId := GetRequesterIdFromCtx(ctx)
89+
postId := req.PostId
90+
storage := storages.NewPostCommentStorage()
91+
commentId := utils.GenerateUUIDString()
92+
currTime := time.Now()
93+
parentUserId := req.ParentUserId
94+
if parentUserId == "" && req.ParentUserInfo != nil && req.ParentUserInfo.UserId != "" {
95+
parentUserId = req.ParentUserInfo.UserId
96+
}
97+
storage.Create(models.PostComment{
98+
CommentId: commentId,
99+
PostId: postId,
100+
ParentCommentId: req.ParentCommentId,
101+
ParentUserId: parentUserId,
102+
Text: req.Text,
103+
UserId: userId,
104+
CreatedTime: currTime.UnixMilli(),
105+
UpdatedTime: currTime,
106+
AppKey: appkey,
107+
})
108+
return errs.IMErrorCode_SUCCESS, nil
109+
}
110+
111+
func QryPostComments(ctx context.Context, postId string, startTime int64, limit int64, isPositive bool) (errs.IMErrorCode, *apimodels.PostComments) {
112+
appkey := GetAppKeyFromCtx(ctx)
113+
ret := &apimodels.PostComments{
114+
Items: []*apimodels.PostComment{},
115+
IsFinished: true,
116+
}
117+
storage := storages.NewPostCommentStorage()
118+
comments, err := storage.QryPostComments(appkey, postId, startTime, limit+1, isPositive)
119+
if err == nil {
120+
for _, c := range comments {
121+
ret.Items = append(ret.Items, &apimodels.PostComment{
122+
PostId: c.PostId,
123+
CommentId: c.CommentId,
124+
Text: c.Text,
125+
ParentCommentId: c.ParentCommentId,
126+
ParentUserInfo: GetUser(ctx, c.ParentUserId),
127+
UserInfo: GetUser(ctx, c.UserId),
128+
CreatedTime: c.CreatedTime,
129+
UpdatedTime: c.UpdatedTime.UnixMilli(),
130+
})
131+
}
132+
if len(ret.Items) > int(limit) {
133+
ret.Items = ret.Items[:limit]
134+
ret.IsFinished = false
135+
}
54136
}
55137
return errs.IMErrorCode_SUCCESS, ret
56138
}

storages/dbs/postcommentdao.go

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package dbs
22

3-
import "time"
3+
import (
4+
"bytes"
5+
"jugglechat-server/storages/dbs/dbcommons"
6+
"jugglechat-server/storages/models"
7+
"time"
8+
)
49

510
type PostCommentDao struct {
611
ID int64 `gorm:"primary_key"`
712
CommentId string `gorm:"comment_id"`
813
PostId string `gorm:"post_id"`
914
ParentCommentId string `gorm:"parent_comment_id"`
1015
ParentUserId string `gorm:"parent_user_id"`
11-
Content string `gorm:"content"`
16+
Text string `gorm:"text"`
1217
IsDelete int `gorm:"is_delete"`
1318
UserId string `gorm:"user_id"`
14-
CreatedTime time.Time `gorm:"created_time"`
19+
CreatedTime int64 `gorm:"created_time"`
1520
UpdatedTime time.Time `gorm:"updated_time"`
1621
Status int `gorm:"status"`
1722
AppKey string `gorm:"app_key"`
@@ -20,3 +25,88 @@ type PostCommentDao struct {
2025
func (comment PostCommentDao) TableName() string {
2126
return "postcomments"
2227
}
28+
29+
func (comment PostCommentDao) Create(item models.PostComment) error {
30+
return dbcommons.GetDb().Create(&PostCommentDao{
31+
CommentId: item.CommentId,
32+
PostId: item.PostId,
33+
ParentCommentId: item.ParentCommentId,
34+
ParentUserId: item.ParentUserId,
35+
Text: item.Text,
36+
IsDelete: item.IsDelete,
37+
UserId: item.UserId,
38+
CreatedTime: item.CreatedTime,
39+
UpdatedTime: time.Now(),
40+
Status: item.Status,
41+
AppKey: item.AppKey,
42+
}).Error
43+
}
44+
45+
func (comment PostCommentDao) FindByIds(appkey string, commentIds []string) (map[string]*models.PostComment, error) {
46+
var items []*PostCommentDao
47+
err := dbcommons.GetDb().Where("app_key=? and comment_id in (?)", appkey, commentIds).Find(&items).Error
48+
if err != nil {
49+
return nil, err
50+
}
51+
ret := map[string]*models.PostComment{}
52+
for _, item := range items {
53+
ret[item.CommentId] = &models.PostComment{
54+
ID: item.ID,
55+
CommentId: item.CommentId,
56+
PostId: item.PostId,
57+
ParentCommentId: item.ParentCommentId,
58+
ParentUserId: item.ParentUserId,
59+
Text: item.Text,
60+
IsDelete: item.IsDelete,
61+
UserId: item.UserId,
62+
CreatedTime: item.CreatedTime,
63+
UpdatedTime: time.Now(),
64+
Status: item.Status,
65+
AppKey: item.AppKey,
66+
}
67+
}
68+
return ret, nil
69+
}
70+
71+
func (comment PostCommentDao) QryPostComments(appkey, postId string, startTime, limit int64, isPosttive bool) ([]*models.PostComment, error) {
72+
var items []*PostCommentDao
73+
conditionBuf := bytes.Buffer{}
74+
params := []interface{}{}
75+
conditionBuf.WriteString("app_key=? and post_id=?")
76+
params = append(params, appkey)
77+
params = append(params, postId)
78+
orderStr := "created_time desc"
79+
if isPosttive {
80+
orderStr = "created_time asc"
81+
conditionBuf.WriteString(" and created_time>?")
82+
params = append(params, startTime)
83+
} else {
84+
if startTime <= 0 {
85+
startTime = time.Now().UnixMilli()
86+
}
87+
conditionBuf.WriteString(" and created_time<?")
88+
params = append(params, startTime)
89+
}
90+
err := dbcommons.GetDb().Where(conditionBuf.String(), params...).Order(orderStr).Limit(limit).Find(&items).Error
91+
if err != nil {
92+
return nil, err
93+
}
94+
ret := []*models.PostComment{}
95+
for _, item := range items {
96+
ret = append(ret, &models.PostComment{
97+
ID: item.ID,
98+
CommentId: item.CommentId,
99+
PostId: item.PostId,
100+
ParentCommentId: item.ParentCommentId,
101+
ParentUserId: item.ParentUserId,
102+
Text: item.Text,
103+
IsDelete: item.IsDelete,
104+
UserId: item.UserId,
105+
CreatedTime: item.CreatedTime,
106+
UpdatedTime: time.Now(),
107+
Status: item.Status,
108+
AppKey: item.AppKey,
109+
})
110+
}
111+
return ret, nil
112+
}

storages/models/post.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,24 @@ type IPostStorage interface {
2323
FindByIds(appkey string, postIds []string) (map[string]*Post, error)
2424
QryPosts(appkey string, startTime, limit int64, isPositive bool) ([]*Post, error)
2525
}
26+
27+
type PostComment struct {
28+
ID int64
29+
CommentId string
30+
PostId string
31+
ParentCommentId string
32+
ParentUserId string
33+
Text string
34+
IsDelete int
35+
UserId string
36+
CreatedTime int64
37+
UpdatedTime time.Time
38+
Status int
39+
AppKey string
40+
}
41+
42+
type IPostCommentStorage interface {
43+
Create(item PostComment) error
44+
FindByIds(appkey string, commentIds []string) (map[string]*PostComment, error)
45+
QryPostComments(appkey, postId string, startTime, limit int64, isPosttive bool) ([]*PostComment, error)
46+
}

storages/storage.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ func NewGroupAdminStorage() models.IGroupAdminStorage {
7676
func NewPostStorage() models.IPostStorage {
7777
return &dbs.PostDao{}
7878
}
79+
80+
func NewPostCommentStorage() models.IPostCommentStorage {
81+
return &dbs.PostCommentDao{}
82+
}

0 commit comments

Comments
 (0)