🐛 Fix posts query

This commit is contained in:
LittleSheep 2024-07-30 00:06:58 +08:00
parent 2d66b8acc0
commit ec0a2c3ac6
2 changed files with 25 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
func listRecommendationFeatured(c *fiber.Ctx) error {
@ -68,6 +69,19 @@ func listRecommendationNews(c *fiber.Ctx) error {
}
}
fmt.Println(database.C.ToSQL(func(tx *gorm.DB) *gorm.DB {
tx = services.FilterPostDraft(tx)
if user, authenticated := c.Locals("user").(models.Account); authenticated {
tx = services.FilterPostWithUserContext(tx, &user)
} else {
tx = services.FilterPostWithUserContext(tx, nil)
}
services.CountPost(tx)
return tx
}))
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {

View File

@ -3,6 +3,7 @@ package services
import (
"errors"
"fmt"
"strconv"
"time"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
@ -10,6 +11,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/viper"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@ -18,17 +20,17 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB {
return tx.Where("visibility = ?", models.PostVisibilityAll)
}
// TODO Blocked by dealer, need support get friend list
tx = tx.Where(
"visibility != ? OR (visibility = ? AND visible_users @> '[?]'::jsonb) OR (visibility = ? AND NOT invisible_users @> '[?]'::jsonb) OR visibility != ?",
models.PostVisibilityFriends,
models.PostVisibilitySelected,
user.ID,
models.PostVisibilitySelected,
user.ID,
models.PostVisibilityNone,
const (
FriendsVisibility = models.PostVisibilityFriends
SelectedVisibility = models.PostVisibilitySelected
NoneVisibility = models.PostVisibilityNone
)
// TODO Blocked by dealer, need support get friend list
tx = tx.Where("visibility != ? OR visibility != ?", FriendsVisibility, NoneVisibility).
Or("visibility = ? AND ?", SelectedVisibility, datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID)))).
Or("visibility = ? AND NOT ?", SelectedVisibility, datatypes.JSONQuery("invisible_users").HasKey(strconv.Itoa(int(user.ID))))
return tx
}