Channel isPublic and isCommunity

This commit is contained in:
LittleSheep 2024-09-17 11:45:44 +08:00
parent 5b78292d1b
commit e6d09ab41b
3 changed files with 25 additions and 10 deletions

View File

@ -25,7 +25,8 @@ type Channel struct {
Type ChannelType `json:"type"` Type ChannelType `json:"type"`
Account Account `json:"account"` Account Account `json:"account"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
IsEncrypted bool `json:"is_encrypted"` IsPublic bool `json:"is_public"`
IsCommunity bool `json:"is_community"`
Realm Realm `json:"realm"` Realm Realm `json:"realm"`
RealmID *uint `json:"realm_id"` RealmID *uint `json:"realm_id"`

View File

@ -116,7 +116,8 @@ func createChannel(c *fiber.Ctx) error {
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
Name string `json:"name" validate:"required"` Name string `json:"name" validate:"required"`
Description string `json:"description"` 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 { if err := exts.BindAndValidate(c, &data); err != nil {
@ -140,9 +141,10 @@ func createChannel(c *fiber.Ctx) error {
Alias: data.Alias, Alias: data.Alias,
Name: data.Name, Name: data.Name,
Description: data.Description, Description: data.Description,
IsEncrypted: data.IsEncrypted,
AccountID: user.ID, AccountID: user.ID,
Type: models.ChannelTypeCommon, Type: models.ChannelTypeCommon,
IsPublic: data.IsPublic,
IsCommunity: data.IsCommunity,
Members: []models.ChannelMember{ Members: []models.ChannelMember{
{AccountID: user.ID, PowerLevel: 100}, {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"` Alias string `json:"alias" validate:"required,min=4,max=32"`
Name string `json:"name" validate:"required"` Name string `json:"name" validate:"required"`
Description string `json:"description"` 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 { 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 { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} }

View File

@ -96,9 +96,21 @@ func PreloadDirectChannelMembers(tx *gorm.DB) *gorm.DB {
}).Preload("Members.Account") }).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 var channels []models.Channel
tx := database.C.Preload("Account").Preload("Realm") tx := database.C.Preload("Account").Preload("Realm")
tx = tx.Where("id IN ? OR is_public = true", idRange)
if len(realmId) > 0 { if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId) tx = tx.Where("realm_id = ?", realmId)
} }
@ -160,13 +172,12 @@ func NewChannel(channel models.Channel) (models.Channel, error) {
return channel, err 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.Alias = alias
channel.Name = name channel.Name = name
channel.Description = description channel.Description = description
if !channel.IsEncrypted { channel.IsPublic = isPublic
channel.IsEncrypted = isEncrypted channel.IsCommunity = isCommunity
}
err := database.C.Save(&channel).Error err := database.C.Save(&channel).Error