mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
348 lines
8.2 KiB
348 lines
8.2 KiB
2 years ago
|
// Copyright 2022 ROC. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package broker
|
||
3 years ago
|
|
||
|
import (
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
3 years ago
|
"time"
|
||
|
|
||
3 years ago
|
"github.com/gin-gonic/gin"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/conf"
|
||
3 years ago
|
"github.com/rocboss/paopao-ce/pkg/errcode"
|
||
|
"github.com/rocboss/paopao-ce/pkg/util"
|
||
3 years ago
|
)
|
||
|
|
||
|
type CommentCreationReq struct {
|
||
|
PostID int64 `json:"post_id" binding:"required"`
|
||
|
Contents []*PostContentItem `json:"contents" binding:"required"`
|
||
|
Users []string `json:"users" binding:"required"`
|
||
|
}
|
||
|
type CommentReplyCreationReq struct {
|
||
|
CommentID int64 `json:"comment_id" binding:"required"`
|
||
|
Content string `json:"content" binding:"required"`
|
||
|
AtUserID int64 `json:"at_user_id"`
|
||
|
}
|
||
|
|
||
|
type CommentDelReq struct {
|
||
|
ID int64 `json:"id" binding:"required"`
|
||
|
}
|
||
|
type ReplyDelReq struct {
|
||
|
ID int64 `json:"id" binding:"required"`
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetPostComments(postID int64, sort string, offset, limit int) ([]*core.CommentFormated, int64, error) {
|
||
|
conditions := &core.ConditionsT{
|
||
3 years ago
|
"post_id": postID,
|
||
|
"ORDER": sort,
|
||
|
}
|
||
2 years ago
|
comments, err := ds.GetComments(conditions, offset, limit)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
userIDs := []int64{}
|
||
|
commentIDs := []int64{}
|
||
|
for _, comment := range comments {
|
||
|
userIDs = append(userIDs, comment.UserID)
|
||
|
commentIDs = append(commentIDs, comment.ID)
|
||
|
}
|
||
|
|
||
2 years ago
|
users, err := ds.GetUsersByIDs(userIDs)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
2 years ago
|
contents, err := ds.GetCommentContentsByIDs(commentIDs)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
2 years ago
|
replies, err := ds.GetCommentRepliesByID(commentIDs)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
2 years ago
|
commentsFormated := []*core.CommentFormated{}
|
||
3 years ago
|
for _, comment := range comments {
|
||
|
commentFormated := comment.Format()
|
||
|
for _, content := range contents {
|
||
|
if content.CommentID == comment.ID {
|
||
|
commentFormated.Contents = append(commentFormated.Contents, content)
|
||
|
}
|
||
|
}
|
||
|
for _, reply := range replies {
|
||
|
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)
|
||
|
}
|
||
|
|
||
|
// 获取总量
|
||
2 years ago
|
totalRows, _ := ds.GetCommentCount(conditions)
|
||
3 years ago
|
|
||
|
return commentsFormated, totalRows, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) (comment *core.Comment, err error) {
|
||
2 years ago
|
var mediaContents []string
|
||
|
|
||
|
defer func() {
|
||
|
if err != nil {
|
||
|
deleteOssObjects(mediaContents)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
if mediaContents, err = persistMediaContents(param.Contents); err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
// 加载Post
|
||
2 years ago
|
post, err := ds.GetPostByID(param.PostID)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
2 years ago
|
if post.CommentCount >= conf.AppSetting.MaxCommentCount {
|
||
3 years ago
|
return nil, errcode.MaxCommentCount
|
||
|
}
|
||
3 years ago
|
ip := ctx.ClientIP()
|
||
2 years ago
|
comment = &core.Comment{
|
||
3 years ago
|
PostID: post.ID,
|
||
|
UserID: userID,
|
||
|
IP: ip,
|
||
|
IPLoc: util.GetIPLoc(ip),
|
||
|
}
|
||
2 years ago
|
comment, err = ds.CreateComment(comment)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
for _, item := range param.Contents {
|
||
|
// 检查附件是否是本站资源
|
||
2 years ago
|
if item.Type == core.ContentTypeImage || item.Type == core.ContentTypeVideo || item.Type == core.ContentTypeAttachment {
|
||
2 years ago
|
if err := ds.CheckAttachment(item.Content); err != nil {
|
||
3 years ago
|
continue
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
postContent := &core.CommentContent{
|
||
3 years ago
|
CommentID: comment.ID,
|
||
|
UserID: userID,
|
||
|
Content: item.Content,
|
||
|
Type: item.Type,
|
||
|
Sort: item.Sort,
|
||
|
}
|
||
2 years ago
|
ds.CreateCommentContent(postContent)
|
||
3 years ago
|
}
|
||
|
|
||
|
// 更新Post回复数
|
||
|
post.CommentCount++
|
||
|
post.LatestRepliedOn = time.Now().Unix()
|
||
2 years ago
|
ds.UpdatePost(post)
|
||
3 years ago
|
|
||
|
// 更新索引
|
||
2 years ago
|
PushPostToSearch(post)
|
||
3 years ago
|
|
||
|
// 创建用户消息提醒
|
||
2 years ago
|
postMaster, err := ds.GetUserByID(post.UserID)
|
||
3 years ago
|
if err == nil && postMaster.ID != userID {
|
||
2 years ago
|
go ds.CreateMessage(&core.Message{
|
||
3 years ago
|
SenderUserID: userID,
|
||
|
ReceiverUserID: postMaster.ID,
|
||
2 years ago
|
Type: core.MsgtypeComment,
|
||
3 years ago
|
Brief: "在泡泡中评论了你",
|
||
3 years ago
|
PostID: post.ID,
|
||
|
CommentID: comment.ID,
|
||
|
})
|
||
|
}
|
||
|
for _, u := range param.Users {
|
||
2 years ago
|
user, err := ds.GetUserByUsername(u)
|
||
3 years ago
|
if err != nil || user.ID == userID || user.ID == postMaster.ID {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// 创建消息提醒
|
||
2 years ago
|
go ds.CreateMessage(&core.Message{
|
||
3 years ago
|
SenderUserID: userID,
|
||
|
ReceiverUserID: user.ID,
|
||
2 years ago
|
Type: core.MsgtypeComment,
|
||
3 years ago
|
Brief: "在泡泡评论中@了你",
|
||
3 years ago
|
PostID: post.ID,
|
||
|
CommentID: comment.ID,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return comment, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetPostComment(id int64) (*core.Comment, error) {
|
||
2 years ago
|
return ds.GetCommentByID(id)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func DeletePostComment(comment *core.Comment) error {
|
||
3 years ago
|
// 加载post
|
||
2 years ago
|
post, err := ds.GetPostByID(comment.PostID)
|
||
3 years ago
|
if err == nil {
|
||
|
// 更新post回复数
|
||
|
post.CommentCount--
|
||
2 years ago
|
ds.UpdatePost(post)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
return ds.DeleteComment(comment)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func createPostPreHandler(commentID int64, userID, atUserID int64) (*core.Post, *core.Comment, int64,
|
||
3 years ago
|
error) {
|
||
3 years ago
|
// 加载Comment
|
||
2 years ago
|
comment, err := ds.GetCommentByID(commentID)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return nil, nil, atUserID, err
|
||
3 years ago
|
}
|
||
|
|
||
|
// 加载comment的post
|
||
2 years ago
|
post, err := ds.GetPostByID(comment.PostID)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
return nil, nil, atUserID, err
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
if post.CommentCount >= conf.AppSetting.MaxCommentCount {
|
||
3 years ago
|
return nil, nil, atUserID, errcode.MaxCommentCount
|
||
3 years ago
|
}
|
||
|
|
||
|
if userID == atUserID {
|
||
|
atUserID = 0
|
||
|
}
|
||
|
|
||
|
if atUserID > 0 {
|
||
|
// 检测目前用户是否存在
|
||
2 years ago
|
users, _ := ds.GetUsersByIDs([]int64{atUserID})
|
||
3 years ago
|
if len(users) == 0 {
|
||
|
atUserID = 0
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return post, comment, atUserID, nil
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, userID, atUserID int64) (*core.CommentReply, error) {
|
||
3 years ago
|
var (
|
||
2 years ago
|
post *core.Post
|
||
|
comment *core.Comment
|
||
3 years ago
|
err error
|
||
|
)
|
||
3 years ago
|
if post, comment, atUserID, err = createPostPreHandler(commentID, userID, atUserID); err != nil {
|
||
3 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
// 创建评论
|
||
3 years ago
|
ip := ctx.ClientIP()
|
||
2 years ago
|
reply := &core.CommentReply{
|
||
3 years ago
|
CommentID: commentID,
|
||
|
UserID: userID,
|
||
|
Content: content,
|
||
|
AtUserID: atUserID,
|
||
|
IP: ip,
|
||
|
IPLoc: util.GetIPLoc(ip),
|
||
|
}
|
||
|
|
||
2 years ago
|
reply, err = ds.CreateCommentReply(reply)
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// 更新Post回复数
|
||
|
post.CommentCount++
|
||
|
post.LatestRepliedOn = time.Now().Unix()
|
||
2 years ago
|
ds.UpdatePost(post)
|
||
3 years ago
|
|
||
|
// 更新索引
|
||
2 years ago
|
PushPostToSearch(post)
|
||
3 years ago
|
|
||
|
// 创建用户消息提醒
|
||
2 years ago
|
commentMaster, err := ds.GetUserByID(comment.UserID)
|
||
3 years ago
|
if err == nil && commentMaster.ID != userID {
|
||
2 years ago
|
go ds.CreateMessage(&core.Message{
|
||
3 years ago
|
SenderUserID: userID,
|
||
|
ReceiverUserID: commentMaster.ID,
|
||
2 years ago
|
Type: core.MsgTypeReply,
|
||
3 years ago
|
Brief: "在泡泡评论下回复了你",
|
||
3 years ago
|
PostID: post.ID,
|
||
|
CommentID: comment.ID,
|
||
|
ReplyID: reply.ID,
|
||
|
})
|
||
|
}
|
||
2 years ago
|
postMaster, err := ds.GetUserByID(post.UserID)
|
||
3 years ago
|
if err == nil && postMaster.ID != userID && commentMaster.ID != postMaster.ID {
|
||
2 years ago
|
go ds.CreateMessage(&core.Message{
|
||
3 years ago
|
SenderUserID: userID,
|
||
|
ReceiverUserID: postMaster.ID,
|
||
2 years ago
|
Type: core.MsgTypeReply,
|
||
3 years ago
|
Brief: "在泡泡评论下发布了新回复",
|
||
3 years ago
|
PostID: post.ID,
|
||
|
CommentID: comment.ID,
|
||
|
ReplyID: reply.ID,
|
||
|
})
|
||
|
}
|
||
|
if atUserID > 0 {
|
||
2 years ago
|
user, err := ds.GetUserByID(atUserID)
|
||
3 years ago
|
if err == nil && user.ID != userID && commentMaster.ID != user.ID && postMaster.ID != user.ID {
|
||
|
// 创建消息提醒
|
||
2 years ago
|
go ds.CreateMessage(&core.Message{
|
||
3 years ago
|
SenderUserID: userID,
|
||
|
ReceiverUserID: user.ID,
|
||
2 years ago
|
Type: core.MsgTypeReply,
|
||
3 years ago
|
Brief: "在泡泡评论的回复中@了你",
|
||
3 years ago
|
PostID: post.ID,
|
||
|
CommentID: comment.ID,
|
||
|
ReplyID: reply.ID,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return reply, nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func GetPostCommentReply(id int64) (*core.CommentReply, error) {
|
||
2 years ago
|
return ds.GetCommentReplyByID(id)
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
func DeletePostCommentReply(reply *core.CommentReply) error {
|
||
2 years ago
|
err := ds.DeleteCommentReply(reply)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 加载Comment
|
||
2 years ago
|
comment, err := ds.GetCommentByID(reply.CommentID)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 加载comment的post
|
||
2 years ago
|
post, err := ds.GetPostByID(comment.PostID)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 更新Post回复数
|
||
|
post.CommentCount--
|
||
|
post.LatestRepliedOn = time.Now().Unix()
|
||
2 years ago
|
ds.UpdatePost(post)
|
||
3 years ago
|
|
||
|
// 更新索引
|
||
2 years ago
|
PushPostToSearch(post)
|
||
3 years ago
|
|
||
|
return nil
|
||
|
}
|