🐛 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/models"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services" "git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"gorm.io/gorm"
) )
func listRecommendationFeatured(c *fiber.Ctx) error { 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 countTx := tx
count, err := services.CountPost(countTx) count, err := services.CountPost(countTx)
if err != nil { if err != nil {

View File

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