From d03ad71064930ed90502f26cc75de0db56deff5e Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 29 May 2024 00:01:46 +0800 Subject: [PATCH] :sparkles: Preload channel members in DM --- pkg/services/channels.go | 23 +++++++++++++++++++++++ settings.toml | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/services/channels.go b/pkg/services/channels.go index 17ba455..e682be8 100644 --- a/pkg/services/channels.go +++ b/pkg/services/channels.go @@ -7,6 +7,8 @@ import ( "git.solsynth.dev/hydrogen/messaging/pkg/database" "git.solsynth.dev/hydrogen/messaging/pkg/models" "github.com/samber/lo" + "github.com/spf13/viper" + "gorm.io/gorm" ) func GetChannelAliasAvailability(alias string) error { @@ -78,12 +80,27 @@ func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.C 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) { var channels []models.Channel tx := database.C.Preload("Account").Preload("Realm") if len(realmId) > 0 { tx = tx.Where("realm_id = ?", realmId) } + + tx = PreloadDirectChannelMembers(tx) + if err := tx.Find(&channels).Error; err != nil { return channels, err } @@ -99,6 +116,9 @@ func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel } else { tx = tx.Where("realm_id IS NULL") } + + tx = PreloadDirectChannelMembers(tx) + if err := tx.Find(&channels).Error; err != nil { return channels, err } @@ -125,6 +145,9 @@ func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channe } else { tx = tx.Where("realm_id IS NULL") } + + tx = PreloadDirectChannelMembers(tx) + if err := tx.Find(&channels).Error; err != nil { return channels, err } diff --git a/settings.toml b/settings.toml index a474756..cfcf6c9 100644 --- a/settings.toml +++ b/settings.toml @@ -10,7 +10,7 @@ secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi" content = "uploads" [debug] -database = false +database = true print_routes = false [paperclip]