2024-02-02 15:42:42 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-02-02 16:50:23 +00:00
|
|
|
"errors"
|
2024-02-03 07:20:32 +00:00
|
|
|
"fmt"
|
2024-02-03 17:08:31 +00:00
|
|
|
"time"
|
2024-02-03 16:24:04 +00:00
|
|
|
|
|
|
|
"code.smartsheep.studio/hydrogen/interactive/pkg/database"
|
|
|
|
"code.smartsheep.studio/hydrogen/interactive/pkg/models"
|
2024-02-03 07:20:32 +00:00
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/spf13/viper"
|
2024-02-02 16:50:23 +00:00
|
|
|
"gorm.io/gorm"
|
2024-02-02 15:42:42 +00:00
|
|
|
)
|
|
|
|
|
2024-02-05 07:51:31 +00:00
|
|
|
func PreloadRelatedPost(tx *gorm.DB) *gorm.DB {
|
|
|
|
return tx.
|
2024-02-03 07:20:32 +00:00
|
|
|
Preload("Author").
|
2024-02-04 10:40:20 +00:00
|
|
|
Preload("Attachments").
|
2024-02-04 12:13:06 +00:00
|
|
|
Preload("Categories").
|
|
|
|
Preload("Tags").
|
2024-02-03 16:24:04 +00:00
|
|
|
Preload("RepostTo").
|
|
|
|
Preload("ReplyTo").
|
|
|
|
Preload("RepostTo.Author").
|
|
|
|
Preload("ReplyTo.Author").
|
2024-02-04 10:40:20 +00:00
|
|
|
Preload("RepostTo.Attachments").
|
|
|
|
Preload("ReplyTo.Attachments").
|
2024-02-04 12:13:06 +00:00
|
|
|
Preload("RepostTo.Categories").
|
|
|
|
Preload("ReplyTo.Categories").
|
|
|
|
Preload("RepostTo.Tags").
|
2024-02-05 07:51:31 +00:00
|
|
|
Preload("ReplyTo.Tags")
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:41:35 +00:00
|
|
|
func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB {
|
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
return tx.Joins(fmt.Sprintf("JOIN %spost_categories ON %sposts.id = %spost_categories.post_id", prefix, prefix, prefix)).
|
|
|
|
Joins(fmt.Sprintf("JOIN %scategories ON %scategories.id = %spost_categories.category_id", prefix, prefix, prefix)).
|
|
|
|
Where(fmt.Sprintf("%scategories.alias = ?", prefix), alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB {
|
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
return tx.Joins(fmt.Sprintf("JOIN %spost_tags ON %sposts.id = %spost_tags.post_id", prefix, prefix, prefix)).
|
|
|
|
Joins(fmt.Sprintf("JOIN %stags ON %stags.id = %spost_tags.tag_id", prefix, prefix, prefix)).
|
|
|
|
Where(fmt.Sprintf("%stags.alias = ?", prefix), alias)
|
|
|
|
}
|
|
|
|
|
2024-02-06 02:57:05 +00:00
|
|
|
func GetPost(tx *gorm.DB) (*models.Post, error) {
|
|
|
|
var post *models.Post
|
|
|
|
if err := PreloadRelatedPost(tx).
|
|
|
|
First(&post).Error; err != nil {
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var reactInfo struct {
|
|
|
|
PostID uint
|
|
|
|
LikeCount int64
|
|
|
|
DislikeCount int64
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
database.C.Raw(fmt.Sprintf(`SELECT t.id as post_id,
|
|
|
|
COALESCE(l.like_count, 0) AS like_count,
|
|
|
|
COALESCE(d.dislike_count, 0) AS dislike_count
|
|
|
|
FROM %sposts t
|
|
|
|
LEFT JOIN (SELECT post_id, COUNT(*) AS like_count
|
|
|
|
FROM %spost_likes
|
|
|
|
GROUP BY post_id) l ON t.id = l.post_id
|
|
|
|
LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count
|
|
|
|
FROM %spost_dislikes
|
|
|
|
GROUP BY post_id) d ON t.id = d.post_id
|
|
|
|
WHERE t.id = ?`, prefix, prefix, prefix), post.ID).Scan(&reactInfo)
|
|
|
|
|
|
|
|
post.LikeCount = reactInfo.LikeCount
|
|
|
|
post.DislikeCount = reactInfo.DislikeCount
|
|
|
|
|
|
|
|
return post, nil
|
|
|
|
}
|
|
|
|
|
2024-02-05 07:51:31 +00:00
|
|
|
func ListPost(tx *gorm.DB, take int, offset int) ([]*models.Post, error) {
|
|
|
|
var posts []*models.Post
|
|
|
|
if err := PreloadRelatedPost(tx).
|
|
|
|
Limit(take).
|
|
|
|
Offset(offset).
|
2024-02-03 07:20:32 +00:00
|
|
|
Find(&posts).Error; err != nil {
|
|
|
|
return posts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
postIds := lo.Map(posts, func(item *models.Post, _ int) uint {
|
|
|
|
return item.ID
|
|
|
|
})
|
|
|
|
|
|
|
|
var reactInfo []struct {
|
|
|
|
PostID uint
|
|
|
|
LikeCount int64
|
|
|
|
DislikeCount int64
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
database.C.Raw(fmt.Sprintf(`SELECT t.id as post_id,
|
|
|
|
COALESCE(l.like_count, 0) AS like_count,
|
|
|
|
COALESCE(d.dislike_count, 0) AS dislike_count
|
|
|
|
FROM %sposts t
|
|
|
|
LEFT JOIN (SELECT post_id, COUNT(*) AS like_count
|
|
|
|
FROM %spost_likes
|
|
|
|
GROUP BY post_id) l ON t.id = l.post_id
|
|
|
|
LEFT JOIN (SELECT post_id, COUNT(*) AS dislike_count
|
|
|
|
FROM %spost_dislikes
|
|
|
|
GROUP BY post_id) d ON t.id = d.post_id
|
|
|
|
WHERE t.id IN (?)`, prefix, prefix, prefix), postIds).Scan(&reactInfo)
|
|
|
|
|
|
|
|
postMap := lo.SliceToMap(posts, func(item *models.Post) (uint, *models.Post) {
|
|
|
|
return item.ID, item
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, info := range reactInfo {
|
|
|
|
if post, ok := postMap[info.PostID]; ok {
|
|
|
|
post.LikeCount = info.LikeCount
|
|
|
|
post.DislikeCount = info.DislikeCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts, nil
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:42:42 +00:00
|
|
|
func NewPost(
|
|
|
|
user models.Account,
|
|
|
|
realm *models.Realm,
|
|
|
|
alias, title, content string,
|
2024-02-04 10:40:20 +00:00
|
|
|
attachments []models.Attachment,
|
2024-02-02 15:42:42 +00:00
|
|
|
categories []models.Category,
|
|
|
|
tags []models.Tag,
|
2024-02-03 17:08:31 +00:00
|
|
|
publishedAt *time.Time,
|
2024-02-03 16:24:04 +00:00
|
|
|
replyTo, repostTo *uint,
|
2024-02-02 15:42:42 +00:00
|
|
|
) (models.Post, error) {
|
|
|
|
var err error
|
|
|
|
var post models.Post
|
|
|
|
for idx, category := range categories {
|
|
|
|
categories[idx], err = GetCategory(category.Alias, category.Name)
|
|
|
|
if err != nil {
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx, tag := range tags {
|
|
|
|
tags[idx], err = GetTag(tag.Alias, tag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var realmId *uint
|
|
|
|
if realm != nil {
|
|
|
|
realmId = &realm.ID
|
|
|
|
}
|
|
|
|
|
2024-02-03 17:08:31 +00:00
|
|
|
if publishedAt == nil {
|
|
|
|
publishedAt = lo.ToPtr(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:42:42 +00:00
|
|
|
post = models.Post{
|
2024-02-03 17:08:31 +00:00
|
|
|
Alias: alias,
|
|
|
|
Title: title,
|
|
|
|
Content: content,
|
2024-02-04 10:40:20 +00:00
|
|
|
Attachments: attachments,
|
2024-02-03 17:08:31 +00:00
|
|
|
Tags: tags,
|
|
|
|
Categories: categories,
|
|
|
|
AuthorID: user.ID,
|
|
|
|
RealmID: realmId,
|
|
|
|
PublishedAt: *publishedAt,
|
|
|
|
RepostID: repostTo,
|
|
|
|
ReplyID: replyTo,
|
2024-02-02 15:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Save(&post).Error; err != nil {
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return post, nil
|
|
|
|
}
|
2024-02-02 16:50:23 +00:00
|
|
|
|
2024-02-03 17:08:31 +00:00
|
|
|
func EditPost(
|
|
|
|
post models.Post,
|
|
|
|
alias, title, content string,
|
|
|
|
publishedAt *time.Time,
|
|
|
|
categories []models.Category,
|
|
|
|
tags []models.Tag,
|
|
|
|
) (models.Post, error) {
|
|
|
|
var err error
|
|
|
|
for idx, category := range categories {
|
|
|
|
categories[idx], err = GetCategory(category.Alias, category.Name)
|
|
|
|
if err != nil {
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for idx, tag := range tags {
|
|
|
|
tags[idx], err = GetTag(tag.Alias, tag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if publishedAt == nil {
|
|
|
|
publishedAt = lo.ToPtr(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
post.Alias = alias
|
|
|
|
post.Title = title
|
|
|
|
post.Content = content
|
|
|
|
post.PublishedAt = *publishedAt
|
|
|
|
|
|
|
|
err = database.C.Save(&post).Error
|
|
|
|
|
|
|
|
return post, err
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:50:23 +00:00
|
|
|
func LikePost(user models.Account, post models.Post) (bool, error) {
|
|
|
|
var like models.PostLike
|
|
|
|
if err := database.C.Where(&models.PostLike{
|
|
|
|
AccountID: user.ID,
|
|
|
|
PostID: post.ID,
|
|
|
|
}).First(&like).Error; err != nil {
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
like = models.PostLike{
|
|
|
|
AccountID: user.ID,
|
|
|
|
PostID: post.ID,
|
|
|
|
}
|
|
|
|
return true, database.C.Save(&like).Error
|
|
|
|
} else {
|
|
|
|
return false, database.C.Delete(&like).Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DislikePost(user models.Account, post models.Post) (bool, error) {
|
|
|
|
var dislike models.PostDislike
|
|
|
|
if err := database.C.Where(&models.PostDislike{
|
|
|
|
AccountID: user.ID,
|
|
|
|
PostID: post.ID,
|
|
|
|
}).First(&dislike).Error; err != nil {
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
dislike = models.PostDislike{
|
|
|
|
AccountID: user.ID,
|
|
|
|
PostID: post.ID,
|
|
|
|
}
|
|
|
|
return true, database.C.Save(&dislike).Error
|
|
|
|
} else {
|
|
|
|
return false, database.C.Delete(&dislike).Error
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 17:08:31 +00:00
|
|
|
|
|
|
|
func DeletePost(post models.Post) error {
|
|
|
|
return database.C.Delete(&post).Error
|
|
|
|
}
|