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.
paopao-ce/internal/servants/web/broker/post.go

550 lines
13 KiB

// 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 (
"errors"
3 years ago
"fmt"
"math"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/core/cs"
"github.com/rocboss/paopao-ce/pkg/errcode"
"github.com/rocboss/paopao-ce/pkg/util"
"github.com/sirupsen/logrus"
3 years ago
)
type TagType = cs.TagType
3 years ago
type PostListReq struct {
Conditions *core.ConditionsT
3 years ago
Offset int
Limit int
}
type PostTagsReq struct {
Type TagType `json:"type" form:"type" binding:"required"`
Num int `json:"num" form:"num" binding:"required"`
}
type PostCreationReq struct {
Contents []*PostContentItem `json:"contents" binding:"required"`
Tags []string `json:"tags" binding:"required"`
Users []string `json:"users" binding:"required"`
AttachmentPrice int64 `json:"attachment_price"`
Visibility core.PostVisibleT `json:"visibility"`
3 years ago
}
type PostDelReq struct {
ID int64 `json:"id" binding:"required"`
}
3 years ago
type PostLockReq struct {
ID int64 `json:"id" binding:"required"`
}
type PostStickReq struct {
ID int64 `json:"id" binding:"required"`
}
type PostVisibilityReq struct {
ID int64 `json:"id" binding:"required"`
Visibility core.PostVisibleT `json:"visibility"`
}
3 years ago
type PostStarReq struct {
ID int64 `json:"id" binding:"required"`
}
3 years ago
type PostCollectionReq struct {
ID int64 `json:"id" binding:"required"`
}
3 years ago
type PostContentItem struct {
Content string `json:"content" binding:"required"`
Type core.PostContentT `json:"type" binding:"required"`
Sort int64 `json:"sort" binding:"required"`
3 years ago
}
// Check 检查PostContentItem属性
func (p *PostContentItem) Check() error {
// 检查附件是否是本站资源
if p.Type == core.ContentTypeImage || p.Type == core.ContentTypeVideo || p.Type == core.ContentTypeAttachment {
if err := ds.CheckAttachment(p.Content); err != nil {
return err
}
}
// 检查链接是否合法
if p.Type == core.ContentTypeLink {
if strings.Index(p.Content, "http://") != 0 && strings.Index(p.Content, "https://") != 0 {
return fmt.Errorf("链接不合法")
}
}
return nil
}
func tagsFrom(originTags []string) []string {
tags := make([]string, 0, len(originTags))
for _, tag := range originTags {
// TODO: 优化tag有效性检测
if tag = strings.TrimSpace(tag); len(tag) > 0 {
tags = append(tags, tag)
}
}
return tags
}
// CreatePost 创建文章
// TODO: 推文+推文内容需要在一个事务中添加,后续优化
func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (_ *core.PostFormated, err error) {
var mediaContents []string
defer func() {
if err != nil {
deleteOssObjects(mediaContents)
}
}()
if mediaContents, err = persistMediaContents(param.Contents); err != nil {
return
}
ip := c.ClientIP()
tags := tagsFrom(param.Tags)
post := &core.Post{
3 years ago
UserID: userID,
Tags: strings.Join(tags, ","),
3 years ago
IP: ip,
IPLoc: util.GetIPLoc(ip),
AttachmentPrice: param.AttachmentPrice,
Visibility: param.Visibility,
3 years ago
}
post, err = ds.CreatePost(post)
3 years ago
if err != nil {
return nil, err
}
for _, item := range param.Contents {
if err := item.Check(); err != nil {
// 属性非法
logrus.Infof("contents check err: %v", err)
continue
3 years ago
}
if item.Type == core.ContentTypeAttachment && param.AttachmentPrice > 0 {
item.Type = core.ContentTypeChargeAttachment
3 years ago
}
postContent := &core.PostContent{
3 years ago
PostID: post.ID,
UserID: userID,
Content: item.Content,
Type: item.Type,
Sort: item.Sort,
}
if _, err = ds.CreatePostContent(postContent); err != nil {
return nil, err
}
3 years ago
}
// 私密推文不创建标签与用户提醒
if post.Visibility != core.PostVisitPrivate {
// 创建标签
ds.UpsertTags(userID, tags)
// 创建用户消息提醒
for _, u := range param.Users {
user, err := ds.GetUserByUsername(u)
if err != nil || user.ID == userID {
continue
}
// 创建消息提醒
// TODO: 优化消息提醒处理机制
go ds.CreateMessage(&core.Message{
SenderUserID: userID,
ReceiverUserID: user.ID,
Type: core.MsgTypePost,
Brief: "在新发布的泡泡动态中@了你",
PostID: post.ID,
})
}
3 years ago
}
// 推送Search
PushPostToSearch(post)
formatedPosts, err := ds.RevampPosts([]*core.PostFormated{post.Format()})
if err != nil {
return nil, err
}
return formatedPosts[0], nil
3 years ago
}
func DeletePost(user *core.User, id int64) *errcode.Error {
if user == nil {
return errcode.NoPermission
3 years ago
}
post, err := ds.GetPostByID(id)
if err != nil {
return errcode.GetPostFailed
}
if post.UserID != user.ID && !user.IsAdmin {
return errcode.NoPermission
}
3 years ago
mediaContents, err := ds.DeletePost(post)
3 years ago
if err != nil {
logrus.Errorf("service.DeletePost delete post failed: %s", err)
return errcode.DeletePostFailed
}
// 删除推文的媒体内容
deleteOssObjects(mediaContents)
// 删除索引
DeleteSearchPost(post)
return nil
}
// deleteOssObjects 删除推文的媒体内容, 宽松处理错误(就是不处理), 后续完善
func deleteOssObjects(mediaContents []string) {
mediaContentsSize := len(mediaContents)
if mediaContentsSize > 1 {
objectKeys := make([]string, 0, mediaContentsSize)
for _, cUrl := range mediaContents {
objectKeys = append(objectKeys, oss.ObjectKey(cUrl))
}
// TODO: 优化处理尽量使用channel传递objectKeys使用可控数量的Goroutine集中处理object删除动作后续完善
go oss.DeleteObjects(objectKeys)
} else if mediaContentsSize == 1 {
oss.DeleteObject(oss.ObjectKey(mediaContents[0]))
3 years ago
}
}
func LockPost(id int64) error {
post, _ := ds.GetPostByID(id)
3 years ago
err := ds.LockPost(post)
3 years ago
if err != nil {
return err
}
return nil
}
func StickPost(id int64) error {
post, _ := ds.GetPostByID(id)
err := ds.StickPost(post)
if err != nil {
return err
}
return nil
}
func VisiblePost(user *core.User, postId int64, visibility core.PostVisibleT) *errcode.Error {
if visibility >= core.PostVisitInvalid {
return errcode.InvalidParams
}
post, err := ds.GetPostByID(postId)
if err != nil {
return errcode.GetPostFailed
}
if err := checkPermision(user, post.UserID); err != nil {
return err
}
if err = ds.VisiblePost(post, visibility); err != nil {
logrus.Warnf("update post failure: %v", err)
return errcode.VisblePostFailed
}
// 推送Search
post.Visibility = visibility
PushPostToSearch(post)
return nil
}
func GetPostStar(postID, userID int64) (*core.PostStar, error) {
return ds.GetUserPostStar(postID, userID)
3 years ago
}
func CreatePostStar(postID, userID int64) (*core.PostStar, error) {
3 years ago
// 加载Post
post, err := ds.GetPostByID(postID)
3 years ago
if err != nil {
return nil, err
}
// 私密post不可操作
if post.Visibility == core.PostVisitPrivate {
return nil, errors.New("no permision")
}
star, err := ds.CreatePostStar(postID, userID)
3 years ago
if err != nil {
return nil, err
}
// 更新Post点赞数
post.UpvoteCount++
ds.UpdatePost(post)
3 years ago
// 更新索引
PushPostToSearch(post)
3 years ago
return star, nil
}
func DeletePostStar(star *core.PostStar) error {
err := ds.DeletePostStar(star)
3 years ago
if err != nil {
return err
}
// 加载Post
post, err := ds.GetPostByID(star.PostID)
3 years ago
if err != nil {
return err
}
// 私密post不可操作
if post.Visibility == core.PostVisitPrivate {
return errors.New("no permision")
}
3 years ago
// 更新Post点赞数
post.UpvoteCount--
ds.UpdatePost(post)
3 years ago
// 更新索引
PushPostToSearch(post)
3 years ago
return nil
}
func GetPostCollection(postID, userID int64) (*core.PostCollection, error) {
return ds.GetUserPostCollection(postID, userID)
3 years ago
}
func CreatePostCollection(postID, userID int64) (*core.PostCollection, error) {
3 years ago
// 加载Post
post, err := ds.GetPostByID(postID)
3 years ago
if err != nil {
return nil, err
}
// 私密post不可操作
if post.Visibility == core.PostVisitPrivate {
return nil, errors.New("no permision")
}
collection, err := ds.CreatePostCollection(postID, userID)
3 years ago
if err != nil {
return nil, err
}
// 更新Post点赞数
post.CollectionCount++
ds.UpdatePost(post)
3 years ago
// 更新索引
PushPostToSearch(post)
3 years ago
return collection, nil
}
func DeletePostCollection(collection *core.PostCollection) error {
err := ds.DeletePostCollection(collection)
3 years ago
if err != nil {
return err
}
// 加载Post
post, err := ds.GetPostByID(collection.PostID)
3 years ago
if err != nil {
return err
}
// 私密post不可操作
if post.Visibility == core.PostVisitPrivate {
return errors.New("no permision")
}
3 years ago
// 更新Post点赞数
post.CollectionCount--
ds.UpdatePost(post)
3 years ago
// 更新索引
PushPostToSearch(post)
3 years ago
return nil
}
func GetPost(id int64) (*core.PostFormated, error) {
post, err := ds.GetPostByID(id)
3 years ago
if err != nil {
return nil, err
}
postContents, err := ds.GetPostContentsByIDs([]int64{post.ID})
3 years ago
if err != nil {
return nil, err
}
users, err := ds.GetUsersByIDs([]int64{post.UserID})
3 years ago
if err != nil {
return nil, err
}
// 数据整合
postFormated := post.Format()
for _, user := range users {
postFormated.User = user.Format()
}
for _, content := range postContents {
if content.PostID == post.ID {
postFormated.Contents = append(postFormated.Contents, content.Format())
}
}
return postFormated, nil
}
func GetPostContentByID(id int64) (*core.PostContent, error) {
return ds.GetPostContentByID(id)
3 years ago
}
func GetIndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) {
return ds.IndexPosts(user, offset, limit)
}
func GetPostList(req *PostListReq) ([]*core.Post, []*core.PostFormated, error) {
posts, err := ds.GetPosts(req.Conditions, req.Offset, req.Limit)
3 years ago
if err != nil {
return nil, nil, err
3 years ago
}
postFormated, err := ds.MergePosts(posts)
return posts, postFormated, err
3 years ago
}
func GetPostCount(conditions *core.ConditionsT) (int64, error) {
return ds.GetPostCount(conditions)
3 years ago
}
func GetPostListFromSearch(user *core.User, q *core.QueryReq, offset, limit int) ([]*core.PostFormated, int64, error) {
resp, err := ts.Search(user, q, offset, limit)
3 years ago
if err != nil {
return nil, 0, err
}
posts, err := ds.RevampPosts(resp.Items)
3 years ago
if err != nil {
return nil, 0, err
}
return posts, resp.Total, nil
3 years ago
}
func GetPostListFromSearchByQuery(user *core.User, query string, offset, limit int) ([]*core.PostFormated, int64, error) {
q := &core.QueryReq{
Query: query,
Type: "search",
3 years ago
}
return GetPostListFromSearch(user, q, offset, limit)
3 years ago
}
func PushPostToSearch(post *core.Post) {
3 years ago
postFormated := post.Format()
postFormated.User = &core.UserFormated{
3 years ago
ID: post.UserID,
}
contents, _ := ds.GetPostContentsByIDs([]int64{post.ID})
3 years ago
for _, content := range contents {
postFormated.Contents = append(postFormated.Contents, content.Format())
}
contentFormated := ""
for _, content := range postFormated.Contents {
if content.Type == core.ContentTypeText || content.Type == core.ContentTypeTitle {
3 years ago
contentFormated = contentFormated + content.Content + "\n"
}
}
docs := []core.TsDocItem{{
Post: post,
Content: contentFormated,
}}
ts.AddDocuments(docs, fmt.Sprintf("%d", post.ID))
3 years ago
}
func DeleteSearchPost(post *core.Post) error {
return ts.DeleteDocuments([]string{fmt.Sprintf("%d", post.ID)})
3 years ago
}
func PushPostsToSearch(c *gin.Context) {
if ok, _ := redisClient.SetNX(c, "JOB_PUSH_TO_SEARCH", 1, time.Hour).Result(); ok {
defer redisClient.Del(c, "JOB_PUSH_TO_SEARCH")
3 years ago
splitNum := 1000
totalRows, _ := GetPostCount(&core.ConditionsT{
"visibility IN ?": []core.PostVisibleT{core.PostVisitPublic, core.PostVisitFriend},
})
3 years ago
pages := math.Ceil(float64(totalRows) / float64(splitNum))
nums := int(pages)
for i := 0; i < nums; i++ {
posts, postsFormated, err := GetPostList(&PostListReq{
Conditions: &core.ConditionsT{},
Offset: i * splitNum,
Limit: splitNum,
3 years ago
})
if err != nil || len(posts) != len(postsFormated) {
continue
}
for i, pf := range postsFormated {
3 years ago
contentFormated := ""
for _, content := range pf.Contents {
if content.Type == core.ContentTypeText || content.Type == core.ContentTypeTitle {
3 years ago
contentFormated = contentFormated + content.Content + "\n"
}
}
docs := []core.TsDocItem{{
Post: posts[i],
Content: contentFormated,
}}
ts.AddDocuments(docs, fmt.Sprintf("%d", posts[i].ID))
3 years ago
}
}
}
}
func GetPostTags(param *PostTagsReq) (cs.TagList, error) {
3 years ago
num := param.Num
if num > conf.AppSetting.MaxPageSize {
num = conf.AppSetting.MaxPageSize
3 years ago
}
tags, err := ds.ListTags(param.Type, num, 0)
3 years ago
if err != nil {
return nil, err
}
return tags, nil
3 years ago
}
func CheckPostAttachmentIsPaid(postID, userID int64) bool {
bill, err := ds.GetPostAttatchmentBill(postID, userID)
3 years ago
return err == nil && bill.Model != nil && bill.ID > 0
}