Preload channel members in DM

This commit is contained in:
LittleSheep 2024-05-29 00:01:46 +08:00
parent 8567ce7d17
commit d03ad71064
2 changed files with 24 additions and 1 deletions

View File

@ -7,6 +7,8 @@ import (
"git.solsynth.dev/hydrogen/messaging/pkg/database" "git.solsynth.dev/hydrogen/messaging/pkg/database"
"git.solsynth.dev/hydrogen/messaging/pkg/models" "git.solsynth.dev/hydrogen/messaging/pkg/models"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/spf13/viper"
"gorm.io/gorm"
) )
func GetChannelAliasAvailability(alias string) error { func GetChannelAliasAvailability(alias string) error {
@ -78,12 +80,27 @@ func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.C
return channel, member, nil return channel, member, nil
} }
func PreloadDirectChannelMembers(tx *gorm.DB) *gorm.DB {
return tx.Preload("Members", func(db *gorm.DB) *gorm.DB {
return db.Joins(
fmt.Sprintf(
"JOIN %schannels AS c ON c.type = ?",
viper.GetString("database.prefix"),
),
models.ChannelTypeDirect,
)
}).Preload("Members.Account")
}
func ListChannel(realmId ...uint) ([]models.Channel, error) { func ListChannel(realmId ...uint) ([]models.Channel, error) {
var channels []models.Channel var channels []models.Channel
tx := database.C.Preload("Account").Preload("Realm") tx := database.C.Preload("Account").Preload("Realm")
if len(realmId) > 0 { if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId) tx = tx.Where("realm_id = ?", realmId)
} }
tx = PreloadDirectChannelMembers(tx)
if err := tx.Find(&channels).Error; err != nil { if err := tx.Find(&channels).Error; err != nil {
return channels, err return channels, err
} }
@ -99,6 +116,9 @@ func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel
} else { } else {
tx = tx.Where("realm_id IS NULL") tx = tx.Where("realm_id IS NULL")
} }
tx = PreloadDirectChannelMembers(tx)
if err := tx.Find(&channels).Error; err != nil { if err := tx.Find(&channels).Error; err != nil {
return channels, err return channels, err
} }
@ -125,6 +145,9 @@ func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channe
} else { } else {
tx = tx.Where("realm_id IS NULL") tx = tx.Where("realm_id IS NULL")
} }
tx = PreloadDirectChannelMembers(tx)
if err := tx.Find(&channels).Error; err != nil { if err := tx.Find(&channels).Error; err != nil {
return channels, err return channels, err
} }

View File

@ -10,7 +10,7 @@ secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi"
content = "uploads" content = "uploads"
[debug] [debug]
database = false database = true
print_routes = false print_routes = false
[paperclip] [paperclip]