Better pulling available channel api by dividing dm and non-dm

This commit is contained in:
2024-10-05 17:46:39 +08:00
parent f958201097
commit a3ef3d52d7
4 changed files with 16 additions and 15 deletions

View File

@ -98,12 +98,20 @@ func listAvailableChannel(c *fiber.Ctx) error {
}
user := c.Locals("user").(models.Account)
tx := database.C
isDirect := c.QueryBool("direct", false)
if isDirect {
tx = tx.Where("type = ?", models.ChannelTypeDirect)
} else {
tx = tx.Where("type = ?", models.ChannelTypeCommon)
}
var err error
var channels []models.Channel
if val, ok := c.Locals("realm").(models.Realm); ok {
channels, err = services.ListAvailableChannel(user, val.ID)
channels, err = services.ListAvailableChannel(tx, user, val.ID)
} else {
channels, err = services.ListAvailableChannel(user)
channels, err = services.ListAvailableChannel(tx, user)
}
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())

View File

@ -140,7 +140,7 @@ func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel
return channels, nil
}
func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channel, error) {
func ListAvailableChannel(tx *gorm.DB, user models.Account, realmId ...uint) ([]models.Channel, error) {
var channels []models.Channel
var members []models.ChannelMember
if err := database.C.Where(&models.ChannelMember{
@ -153,7 +153,7 @@ func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channe
return item.ChannelID
})
tx := database.C.Preload("Realm").Where("id IN ?", idx)
tx = tx.Preload("Realm").Where("id IN ?", idx)
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
}