diff --git a/dal/comment.gen.go b/dal/comment.gen.go index 84e5ef7..9a837e5 100644 --- a/dal/comment.gen.go +++ b/dal/comment.gen.go @@ -42,6 +42,7 @@ func newComment(db *gorm.DB, opts ...gen.DOOption) comment { _comment.Status = field.NewField(tableName, "status") _comment.TopPriority = field.NewInt32(tableName, "top_priority") _comment.UserAgent = field.NewString(tableName, "user_agent") + _comment.Likes = field.NewInt32(tableName, "likes") _comment.fillFieldMap() @@ -69,6 +70,7 @@ type comment struct { Status field.Field TopPriority field.Int32 UserAgent field.String + Likes field.Int32 fieldMap map[string]field.Expr } @@ -102,6 +104,7 @@ func (c *comment) updateTableName(table string) *comment { c.Status = field.NewField(table, "status") c.TopPriority = field.NewInt32(table, "top_priority") c.UserAgent = field.NewString(table, "user_agent") + c.Likes = field.NewInt32(table, "likes") c.fillFieldMap() @@ -144,6 +147,7 @@ func (c *comment) fillFieldMap() { c.fieldMap["status"] = c.Status c.fieldMap["top_priority"] = c.TopPriority c.fieldMap["user_agent"] = c.UserAgent + c.fieldMap["likes"] = c.Likes } func (c comment) clone(db *gorm.DB) comment { diff --git a/handler/content/api/comment.go b/handler/content/api/comment.go new file mode 100644 index 0000000..f3fa16f --- /dev/null +++ b/handler/content/api/comment.go @@ -0,0 +1,26 @@ +package api + +import ( + "github.com/gin-gonic/gin" + + "github.com/go-sonic/sonic/service" + "github.com/go-sonic/sonic/util" +) + +type CommentHandler struct { + BaseCommentService service.BaseCommentService +} + +func NewCommentHandler(baseCommentService service.BaseCommentService) *CommentHandler { + return &CommentHandler{ + BaseCommentService: baseCommentService, + } +} + +func (c *CommentHandler) Like(ctx *gin.Context) (interface{}, error) { + commentID, err := util.ParamInt32(ctx, "commentID") + if err != nil { + return nil, err + } + return nil, c.BaseCommentService.IncreaseLike(ctx, commentID) +} diff --git a/handler/content/api/init.go b/handler/content/api/init.go index 1890ecc..40af7b7 100644 --- a/handler/content/api/init.go +++ b/handler/content/api/init.go @@ -12,5 +12,6 @@ func init() { NewSheetHandler, NewOptionHandler, NewPhotoHandler, + NewCommentHandler, ) } diff --git a/handler/router.go b/handler/router.go index 4b00aa4..5e92c14 100644 --- a/handler/router.go +++ b/handler/router.go @@ -346,6 +346,8 @@ func (s *Server) RegisterRouters() { contentAPIRouter.GET("/links/team_view", s.wrapHandler(s.ContentAPILinkHandler.LinkTeamVO)) contentAPIRouter.GET("/options/comment", s.wrapHandler(s.ContentAPIOptionHandler.Comment)) + + contentAPIRouter.POST("/comments/:commentID/likes", s.wrapHandler(s.ContentAPICommentHandler.Like)) } } } diff --git a/handler/server.go b/handler/server.go index 24e2dfc..afd2744 100644 --- a/handler/server.go +++ b/handler/server.go @@ -77,6 +77,7 @@ type Server struct { ContentAPISheetHandler *api.SheetHandler ContentAPIOptionHandler *api.OptionHandler ContentAPIPhotoHandler *api.PhotoHandler + ContentAPICommentHandler *api.CommentHandler } type ServerParams struct { @@ -132,6 +133,7 @@ type ServerParams struct { ContentAPISheetHandler *api.SheetHandler ContentAPIOptionHandler *api.OptionHandler ContentAPIPhotoHandler *api.PhotoHandler + ContentAPICommentHandler *api.CommentHandler } func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server { @@ -197,6 +199,7 @@ func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server { ContentAPIOptionHandler: param.ContentAPIOptionHandler, ContentSearchHandler: param.ContentSearchHandler, ContentAPIPhotoHandler: param.ContentAPIPhotoHandler, + ContentAPICommentHandler: param.ContentAPICommentHandler, } lifecycle.Append(fx.Hook{ OnStop: httpServer.Shutdown, diff --git a/model/dto/comment.go b/model/dto/comment.go index b46daa2..bb7aba0 100644 --- a/model/dto/comment.go +++ b/model/dto/comment.go @@ -17,4 +17,5 @@ type Comment struct { AllowNotification bool `json:"allowNotification"` CreateTime int64 `json:"createTime"` Avatar string `json:"avatar"` + Likes int32 `json:"likes"` } diff --git a/model/entity/comment.gen.go b/model/entity/comment.gen.go index 5bed316..b6a40bf 100644 --- a/model/entity/comment.gen.go +++ b/model/entity/comment.gen.go @@ -31,6 +31,7 @@ type Comment struct { Status consts.CommentStatus `gorm:"column:status;type:bigint;not null;index:comment_type_status,priority:2" json:"status"` TopPriority int32 `gorm:"column:top_priority;type:int;not null" json:"top_priority"` UserAgent string `gorm:"column:user_agent;type:varchar(511);not null" json:"user_agent"` + Likes int32 `gorm:"column:likes;type:int;not null;default: 0" json:"likes"` } // TableName Comment's table name diff --git a/model/property/comment.go b/model/property/comment.go index 010569b..725a832 100644 --- a/model/property/comment.go +++ b/model/property/comment.go @@ -5,7 +5,7 @@ import "reflect" var ( CommentGravatarDefault = Property{ KeyValue: "comment_gravatar_default", - DefaultValue: "mm", + DefaultValue: "identicon", Kind: reflect.String, } CommentNewNeedCheck = Property{ @@ -40,12 +40,12 @@ var ( } CommentInternalPluginJs = Property{ KeyValue: "comment_internal_plugin_js", - DefaultValue: "//cdn.jsdelivr.net/npm/halo-comment@latest/dist/halo-comment.min.js", + DefaultValue: "https://cdn.jsdelivr.net/npm/halo-comment@latest/dist/halo-comment.min.js", Kind: reflect.String, } CommentGravatarSource = Property{ KeyValue: "gravatar_source", - DefaultValue: "//gravatar.com/avatar/", + DefaultValue: "https://gravatar.com/avatar/", Kind: reflect.String, } CommentBanTime = Property{ diff --git a/model/vo/comment.go b/model/vo/comment.go index d007bf5..f4be555 100644 --- a/model/vo/comment.go +++ b/model/vo/comment.go @@ -28,5 +28,6 @@ type JournalCommentWithJournal struct { type CommentWithHasChildren struct { *dto.Comment - HasChildren bool `json:"hasChildren"` + HasChildren bool `json:"hasChildren"` + ChildrenCount int64 `json:"childrenCount"` } diff --git a/resources/template/common/macro/global_macro.tmpl b/resources/template/common/macro/global_macro.tmpl index 9f151f6..a805dce 100644 --- a/resources/template/common/macro/global_macro.tmpl +++ b/resources/template/common/macro/global_macro.tmpl @@ -12,4 +12,19 @@ {{end}} +{{end}} + +{{define "global.sonic_comment"}} + {{if or (eq .type "journal") (not .target.DisallowComment)}} +
+ + + + {{end}} {{end}} \ No newline at end of file diff --git a/scripts/table.sql b/scripts/table.sql index 5044942..b4cde32 100644 --- a/scripts/table.sql +++ b/scripts/table.sql @@ -67,6 +67,7 @@ create table if not exists comment status int default 0 not null, top_priority int default 0 not null, user_agent varchar(511) default '' not null, + likes int default 0 not null , index comment_parent_id (parent_id), index comment_post_id (post_id), index comment_type_status (type, status) diff --git a/service/assembler/comment_base.go b/service/assembler/comment_base.go index 9df3cb8..7621429 100644 --- a/service/assembler/comment_base.go +++ b/service/assembler/comment_base.go @@ -67,8 +67,10 @@ func (b *baseCommentAssembler) ConvertToWithHasChildren(ctx context.Context, com } if count, ok := countMap[commentDTO.ID]; ok && count > 0 { commentWithHasChildren.HasChildren = true + commentWithHasChildren.ChildrenCount = count } else { commentWithHasChildren.HasChildren = false + commentWithHasChildren.ChildrenCount = 0 } result = append(result, commentWithHasChildren) } @@ -90,6 +92,7 @@ func (b *baseCommentAssembler) ConvertToDTO(ctx context.Context, comment *entity IsAdmin: comment.IsAdmin, AllowNotification: comment.AllowNotification, CreateTime: comment.CreateTime.UnixMilli(), + Likes: comment.Likes, } avatarURL, err := b.BaseCommentService.BuildAvatarURL(ctx, comment.GravatarMd5, nil, nil) if err != nil { @@ -124,6 +127,7 @@ func (b *baseCommentAssembler) ConvertToDTOList(ctx context.Context, comments [] IsAdmin: comment.IsAdmin, AllowNotification: comment.AllowNotification, CreateTime: comment.CreateTime.UnixMilli(), + Likes: comment.Likes, } avatarURL, err := b.BaseCommentService.BuildAvatarURL(ctx, comment.GravatarMd5, util.StringPtr(gravatarSource.(string)), util.StringPtr(gravatarDefault.(string))) if err != nil { diff --git a/service/comment_base.go b/service/comment_base.go index 3015272..b8c3cb0 100644 --- a/service/comment_base.go +++ b/service/comment_base.go @@ -26,4 +26,5 @@ type BaseCommentService interface { CountByStatusAndContentIDs(ctx context.Context, status consts.CommentStatus, contentIDs []int32) (map[int32]int64, error) CountChildren(ctx context.Context, parentCommentIDs []int32) (map[int32]int64, error) GetChildren(ctx context.Context, parentCommentID int32, contentID int32, commentType consts.CommentType) ([]*entity.Comment, error) + IncreaseLike(ctx context.Context, commentID int32) error } diff --git a/service/impl/comment_base.go b/service/impl/comment_base.go index 14d3412..8472a82 100644 --- a/service/impl/comment_base.go +++ b/service/impl/comment_base.go @@ -341,3 +341,15 @@ func (b *baseCommentServiceImpl) GetChildren(ctx context.Context, parentCommentI } return children, nil } + +func (b baseCommentServiceImpl) IncreaseLike(ctx context.Context, commentID int32) error { + commentDAL := dal.GetQueryByCtx(ctx).Comment + info, err := commentDAL.WithContext(ctx).Where(commentDAL.ID.Eq(commentID)).UpdateSimple(commentDAL.Likes.Add(1)) + if err != nil { + return WrapDBErr(err) + } + if info.RowsAffected != 1 { + return xerr.NoType.New("increase comment like failed postID=%v", commentID).WithStatus(xerr.StatusBadRequest).WithMsg("failed to like comment") + } + return nil +}