From e6d09ab41bb4363d29aa90f1a86851fa3d23cb63 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 17 Sep 2024 11:45:44 +0800 Subject: [PATCH] :sparkles: Channel isPublic and isCommunity --- pkg/internal/models/channels.go | 3 ++- pkg/internal/server/api/channels_api.go | 11 +++++++---- pkg/internal/services/channels.go | 21 ++++++++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/pkg/internal/models/channels.go b/pkg/internal/models/channels.go index 65874c7..426cee7 100644 --- a/pkg/internal/models/channels.go +++ b/pkg/internal/models/channels.go @@ -25,7 +25,8 @@ type Channel struct { Type ChannelType `json:"type"` Account Account `json:"account"` AccountID uint `json:"account_id"` - IsEncrypted bool `json:"is_encrypted"` + IsPublic bool `json:"is_public"` + IsCommunity bool `json:"is_community"` Realm Realm `json:"realm"` RealmID *uint `json:"realm_id"` diff --git a/pkg/internal/server/api/channels_api.go b/pkg/internal/server/api/channels_api.go index 039c79c..6936360 100644 --- a/pkg/internal/server/api/channels_api.go +++ b/pkg/internal/server/api/channels_api.go @@ -116,7 +116,8 @@ func createChannel(c *fiber.Ctx) error { Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` Name string `json:"name" validate:"required"` Description string `json:"description"` - IsEncrypted bool `json:"is_encrypted"` + IsPublic bool `json:"is_public"` + IsCommunity bool `json:"is_community"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -140,9 +141,10 @@ func createChannel(c *fiber.Ctx) error { Alias: data.Alias, Name: data.Name, Description: data.Description, - IsEncrypted: data.IsEncrypted, AccountID: user.ID, Type: models.ChannelTypeCommon, + IsPublic: data.IsPublic, + IsCommunity: data.IsCommunity, Members: []models.ChannelMember{ {AccountID: user.ID, PowerLevel: 100}, }, @@ -171,7 +173,8 @@ func editChannel(c *fiber.Ctx) error { Alias string `json:"alias" validate:"required,min=4,max=32"` Name string `json:"name" validate:"required"` Description string `json:"description"` - IsEncrypted bool `json:"is_encrypted"` + IsPublic bool `json:"is_public"` + IsCommunity bool `json:"is_community"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -205,7 +208,7 @@ func editChannel(c *fiber.Ctx) error { } } - channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description, data.IsEncrypted) + channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description, data.IsPublic, data.IsCommunity) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/internal/services/channels.go b/pkg/internal/services/channels.go index 28450af..be944d4 100644 --- a/pkg/internal/services/channels.go +++ b/pkg/internal/services/channels.go @@ -96,9 +96,21 @@ func PreloadDirectChannelMembers(tx *gorm.DB) *gorm.DB { }).Preload("Members.Account") } -func ListChannel(realmId ...uint) ([]models.Channel, error) { +func ListChannel(user *models.Account, realmId ...uint) ([]models.Channel, error) { + var identities []models.ChannelMember + var idRange []uint + if user != nil { + if err := database.C.Where("account_id = ?", user.ID).Find(&identities).Error; err != nil { + return nil, fmt.Errorf("unabkle to get identities: %v", err) + } + for _, identity := range identities { + idRange = append(idRange, identity.ChannelID) + } + } + var channels []models.Channel tx := database.C.Preload("Account").Preload("Realm") + tx = tx.Where("id IN ? OR is_public = true", idRange) if len(realmId) > 0 { tx = tx.Where("realm_id = ?", realmId) } @@ -160,13 +172,12 @@ func NewChannel(channel models.Channel) (models.Channel, error) { return channel, err } -func EditChannel(channel models.Channel, alias, name, description string, isEncrypted bool) (models.Channel, error) { +func EditChannel(channel models.Channel, alias, name, description string, isPublic, isCommunity bool) (models.Channel, error) { channel.Alias = alias channel.Name = name channel.Description = description - if !channel.IsEncrypted { - channel.IsEncrypted = isEncrypted - } + channel.IsPublic = isPublic + channel.IsCommunity = isCommunity err := database.C.Save(&channel).Error