Interactive/pkg/services/posts.go

291 lines
6.7 KiB
Go
Raw Normal View History

2024-02-02 15:42:42 +00:00
package services
import (
"errors"
"fmt"
2024-03-20 12:57:21 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models"
2024-04-13 11:26:44 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
2024-03-02 17:23:11 +00:00
"github.com/rs/zerolog/log"
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-05-15 11:45:49 +00:00
"time"
2024-03-02 17:23:11 +00:00
)
2024-05-15 11:45:49 +00:00
func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB {
return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id").
Joins("JOIN post_categories ON post_categories.id = post_categories.category_id").
Where("post_categories.alias = ?", alias)
}
2024-05-15 11:45:49 +00:00
func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB {
return tx.Joins("JOIN post_tags ON posts.id = post_tags.post_id").
Joins("JOIN post_tags ON post_tags.id = post_tags.category_id").
Where("post_tags.alias = ?", alias)
}
2024-05-15 11:45:49 +00:00
func FilterWithRealm(tx *gorm.DB, id uint) *gorm.DB {
2024-03-02 17:23:11 +00:00
if id > 0 {
2024-05-15 11:45:49 +00:00
return tx.Where("realm_id = ?", id)
2024-03-02 17:23:11 +00:00
} else {
2024-05-15 11:45:49 +00:00
return tx.Where("realm_id IS NULL")
}
2024-03-02 17:23:11 +00:00
}
2024-05-15 11:45:49 +00:00
func FilterPostReply(tx *gorm.DB, replyTo ...uint) *gorm.DB {
if len(replyTo) > 0 && replyTo[0] > 0 {
return tx.Where("reply_id = ?", replyTo[0])
2024-03-02 17:23:11 +00:00
} else {
2024-05-15 11:45:49 +00:00
return tx.Where("reply_id IS NULL")
}
2024-03-02 17:23:11 +00:00
}
2024-05-15 11:45:49 +00:00
func FilterPostWithPublishedAt(tx *gorm.DB, date time.Time) *gorm.DB {
return tx.Where("published_at <= ? OR published_at IS NULL", date)
2024-03-02 17:23:11 +00:00
}
2024-05-15 11:45:49 +00:00
func GetPostWithAlias(alias string, ignoreLimitation ...bool) (models.Post, error) {
tx := database.C
if len(ignoreLimitation) == 0 || !ignoreLimitation[0] {
tx = FilterPostWithPublishedAt(tx, time.Now())
2024-03-03 13:24:08 +00:00
}
2024-05-15 11:45:49 +00:00
var item models.Post
if err := tx.
Where("alias = ?", alias).
Preload("Author").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("RepostTo").
Preload("RepostTo.Author").
2024-05-15 11:45:49 +00:00
First(&item).Error; err != nil {
2024-03-10 15:35:38 +00:00
return item, err
}
2024-03-03 13:24:08 +00:00
return item, nil
}
2024-05-15 11:45:49 +00:00
func GetPost(id uint, ignoreLimitation ...bool) (models.Post, error) {
tx := database.C
if len(ignoreLimitation) == 0 || !ignoreLimitation[0] {
tx = FilterPostWithPublishedAt(tx, time.Now())
2024-03-02 17:23:11 +00:00
}
2024-05-15 11:45:49 +00:00
var item models.Post
if err := tx.
Where("id = ?", id).
Preload("Author").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("RepostTo").
Preload("RepostTo.Author").
2024-05-15 11:45:49 +00:00
First(&item).Error; err != nil {
2024-03-10 15:35:38 +00:00
return item, err
}
2024-03-02 17:23:11 +00:00
return item, nil
}
2024-05-15 11:45:49 +00:00
func CountPost(tx *gorm.DB) (int64, error) {
2024-03-02 17:23:11 +00:00
var count int64
2024-05-15 11:45:49 +00:00
if err := tx.Model(&models.Post{}).Count(&count).Error; err != nil {
2024-03-02 17:23:11 +00:00
return count, err
}
return count, nil
}
2024-05-15 11:45:49 +00:00
func CountPostReply(id uint) int64 {
var count int64
2024-05-15 11:45:49 +00:00
if err := database.C.Model(&models.Post{}).
Where("reply_id = ?", id).
Count(&count).Error; err != nil {
return 0
}
return count
}
2024-05-15 11:45:49 +00:00
func CountPostReactions(id uint) int64 {
var count int64
if err := database.C.Model(&models.Reaction{}).
2024-05-15 11:45:49 +00:00
Where("post_id = ?", id).
Count(&count).Error; err != nil {
return 0
}
return count
}
2024-05-15 11:45:49 +00:00
func ListPostReactions(id uint) (map[string]int64, error) {
2024-03-03 14:57:17 +00:00
var reactions []struct {
Symbol string
Count int64
}
if err := database.C.Model(&models.Reaction{}).
Select("symbol, COUNT(id) as count").
2024-05-15 11:45:49 +00:00
Where("post_id = ?", id).
2024-03-03 14:57:17 +00:00
Group("symbol").
Scan(&reactions).Error; err != nil {
return map[string]int64{}, err
}
return lo.SliceToMap(reactions, func(item struct {
Symbol string
Count int64
},
) (string, int64) {
2024-03-03 14:57:17 +00:00
return item.Symbol, item.Count
}), nil
}
func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Post, error) {
2024-02-12 04:32:37 +00:00
if take > 20 {
take = 20
}
var items []*models.Post
2024-05-15 11:45:49 +00:00
if err := tx.
Limit(take).Offset(offset).
Order("created_at DESC").
Preload("Author").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("RepostTo").
Preload("RepostTo.Author").
2024-05-15 11:45:49 +00:00
Find(&items).Error; err != nil {
2024-03-02 17:23:11 +00:00
return items, err
2024-02-03 07:20:32 +00:00
}
idx := lo.Map(items, func(item *models.Post, index int) uint {
return item.ID
2024-03-03 14:57:17 +00:00
})
if len(noReact) <= 0 || !noReact[0] {
var reactions []struct {
PostID uint
Symbol string
Count int64
}
if err := database.C.Model(&models.Reaction{}).
Select("post_id, symbol, COUNT(id) as count").
2024-05-15 11:45:49 +00:00
Where("post_id IN (?)", idx).
2024-03-03 14:57:17 +00:00
Group("post_id, symbol").
Scan(&reactions).Error; err != nil {
return items, err
}
itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) {
return item.ID, item
2024-03-03 14:57:17 +00:00
})
list := map[uint]map[string]int64{}
for _, info := range reactions {
if _, ok := list[info.PostID]; !ok {
list[info.PostID] = make(map[string]int64)
}
list[info.PostID][info.Symbol] = info.Count
}
for k, v := range list {
if post, ok := itemMap[k]; ok {
post.ReactionList = v
2024-03-03 14:57:17 +00:00
}
}
}
2024-03-02 17:23:11 +00:00
return items, nil
2024-02-03 07:20:32 +00:00
}
2024-05-15 11:45:49 +00:00
func InitPostCategoriesAndTags(item models.Post) (models.Post, error) {
2024-02-02 15:42:42 +00:00
var err error
2024-05-15 11:45:49 +00:00
for idx, category := range item.Categories {
item.Categories[idx], err = GetCategory(category.Alias)
2024-02-02 15:42:42 +00:00
if err != nil {
2024-03-02 17:23:11 +00:00
return item, err
2024-02-02 15:42:42 +00:00
}
}
2024-05-15 11:45:49 +00:00
for idx, tag := range item.Tags {
item.Tags[idx], err = GetTagOrCreate(tag.Alias, tag.Name)
2024-02-02 15:42:42 +00:00
if err != nil {
2024-03-02 17:23:11 +00:00
return item, err
2024-02-02 15:42:42 +00:00
}
}
2024-03-02 17:23:11 +00:00
return item, nil
}
2024-05-15 11:45:49 +00:00
func NewPost(user models.Account, item models.Post) (models.Post, error) {
item, err := InitPostCategoriesAndTags(item)
2024-03-02 17:23:11 +00:00
if err != nil {
return item, err
}
2024-02-02 15:42:42 +00:00
2024-05-15 11:45:49 +00:00
if item.RealmID != nil {
_, err := GetRealmMember(*item.RealmID, user.ExternalID)
if err != nil {
return item, fmt.Errorf("you aren't a part of that realm: %v", err)
2024-02-09 04:36:39 +00:00
}
2024-02-02 15:42:42 +00:00
}
2024-03-02 17:23:11 +00:00
if err := database.C.Save(&item).Error; err != nil {
return item, err
2024-02-03 17:08:31 +00:00
}
2024-05-15 11:45:49 +00:00
// Notify the original poster its post has been replied
if item.ReplyID != nil {
2024-03-02 17:23:11 +00:00
go func() {
2024-05-15 11:45:49 +00:00
var op models.Post
2024-04-26 16:13:47 +00:00
if err := database.C.
2024-05-15 11:45:49 +00:00
Where("id = ?", item.ReplyID).
2024-04-26 16:13:47 +00:00
Preload("Author").
First(&op).Error; err == nil {
2024-05-15 11:45:49 +00:00
if op.Author.ID != user.ID {
postUrl := fmt.Sprintf("https://%s/posts/%s", viper.GetString("domain"), item.Alias)
2024-03-02 17:23:11 +00:00
err := NotifyAccount(
op.Author,
2024-05-15 11:45:49 +00:00
fmt.Sprintf("%s replied you", user.Nick),
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
2024-03-31 13:46:59 +00:00
false,
2024-03-02 17:23:11 +00:00
&proto.NotifyLink{Label: "Related post", Url: postUrl},
)
if err != nil {
log.Error().Err(err).Msg("An error occurred when notifying user...")
}
}
}
}()
2024-02-02 15:42:42 +00:00
}
2024-03-02 17:23:11 +00:00
return item, nil
2024-02-02 15:42:42 +00:00
}
2024-02-02 16:50:23 +00:00
2024-05-15 11:45:49 +00:00
func EditPost(item models.Post) (models.Post, error) {
item, err := InitPostCategoriesAndTags(item)
2024-03-02 17:23:11 +00:00
if err != nil {
return item, err
2024-02-03 17:08:31 +00:00
}
2024-03-02 17:23:11 +00:00
err = database.C.Save(&item).Error
2024-02-03 17:08:31 +00:00
2024-03-02 17:23:11 +00:00
return item, err
}
2024-02-03 17:08:31 +00:00
2024-05-15 11:45:49 +00:00
func DeletePost(item models.Post) error {
2024-03-02 17:23:11 +00:00
return database.C.Delete(&item).Error
2024-02-03 17:08:31 +00:00
}
2024-05-15 11:45:49 +00:00
func ReactPost(reaction models.Reaction) (bool, models.Reaction, error) {
if err := database.C.Where(reaction).First(&reaction).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return true, reaction, database.C.Save(&reaction).Error
} else {
return true, reaction, err
}
2024-02-02 16:50:23 +00:00
} else {
return false, reaction, database.C.Delete(&reaction).Error
2024-02-02 16:50:23 +00:00
}
}