🐛 Bug fixes

This commit is contained in:
LittleSheep 2024-06-01 10:43:21 +08:00
parent d03ad71064
commit 23450c0690
3 changed files with 16 additions and 17 deletions

View File

@ -26,22 +26,22 @@ func getChannel(c *fiber.Ctx) error {
return c.JSON(channel) return c.JSON(channel)
} }
func getChannelAvailability(c *fiber.Ctx) error { func getChannelIdentity(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
alias := c.Params("channel") alias := c.Params("channel")
var err error var err error
var channel models.Channel var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
channel, _, err = services.GetAvailableChannelWithAlias(alias, user, val.ID) _, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID)
} else { } else {
channel, _, err = services.GetAvailableChannelWithAlias(alias, user) _, member, err = services.GetAvailableChannelWithAlias(alias, user)
} }
if err != nil { if err != nil {
return c.Status(fiber.StatusForbidden).JSON(channel) return c.SendStatus(fiber.StatusForbidden)
} }
return c.JSON(channel) return c.JSON(member)
} }
func listChannel(c *fiber.Ctx) error { func listChannel(c *fiber.Ctx) error {

View File

@ -72,7 +72,7 @@ func NewServer() {
channels.Get("/me", authMiddleware, listOwnedChannel) channels.Get("/me", authMiddleware, listOwnedChannel)
channels.Get("/me/available", authMiddleware, listAvailableChannel) channels.Get("/me/available", authMiddleware, listAvailableChannel)
channels.Get("/:channel", getChannel) channels.Get("/:channel", getChannel)
channels.Get("/:channel/availability", authMiddleware, getChannelAvailability) channels.Get("/:channel/me", authMiddleware, getChannelIdentity)
channels.Post("/", authMiddleware, createChannel) channels.Post("/", authMiddleware, createChannel)
channels.Post("/dm", authMiddleware, createDirectChannel) channels.Post("/dm", authMiddleware, createDirectChannel)

View File

@ -20,9 +20,11 @@ func GetChannelAliasAvailability(alias string) error {
func GetChannel(id uint) (models.Channel, error) { func GetChannel(id uint) (models.Channel, error) {
var channel models.Channel var channel models.Channel
if err := database.C.Where(models.Channel{ tx := database.C.Where(models.Channel{
BaseModel: models.BaseModel{ID: id}, BaseModel: models.BaseModel{ID: id},
}).Preload("Account").First(&channel).Error; err != nil { }).Preload("Account").Preload("Realm")
tx = PreloadDirectChannelMembers(tx)
if err := tx.First(&channel).Error; err != nil {
return channel, err return channel, err
} }
@ -31,12 +33,13 @@ func GetChannel(id uint) (models.Channel, error) {
func GetChannelWithAlias(alias string, realmId ...uint) (models.Channel, error) { func GetChannelWithAlias(alias string, realmId ...uint) (models.Channel, error) {
var channel models.Channel var channel models.Channel
tx := database.C.Where(models.Channel{Alias: alias}).Preload("Account") tx := database.C.Where(models.Channel{Alias: alias}).Preload("Account").Preload("Realm")
if len(realmId) > 0 { if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId) tx = tx.Where("realm_id = ?", realmId)
} else { } else {
tx = tx.Where("realm_id IS NULL") tx = tx.Where("realm_id IS NULL")
} }
tx = PreloadDirectChannelMembers(tx)
if err := tx.First(&channel).Error; err != nil { if err := tx.First(&channel).Error; err != nil {
return channel, err return channel, err
} }
@ -69,11 +72,11 @@ func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.C
if channel, err = GetChannel(id); err != nil { if channel, err = GetChannel(id); err != nil {
return channel, member, err return channel, member, err
} }
tx := database.C.Where(models.ChannelMember{
if err := database.C.Where(models.ChannelMember{
AccountID: user.ID, AccountID: user.ID,
ChannelID: channel.ID, ChannelID: channel.ID,
}).First(&member).Error; err != nil { })
if err := tx.First(&member).Error; err != nil {
return channel, member, fmt.Errorf("channel principal not found: %v", err.Error()) return channel, member, fmt.Errorf("channel principal not found: %v", err.Error())
} }
@ -113,8 +116,6 @@ func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel
tx := database.C.Where(&models.Channel{AccountID: user.ID}).Preload("Realm") tx := database.C.Where(&models.Channel{AccountID: user.ID}).Preload("Realm")
if len(realmId) > 0 { if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId) tx = tx.Where("realm_id = ?", realmId)
} else {
tx = tx.Where("realm_id IS NULL")
} }
tx = PreloadDirectChannelMembers(tx) tx = PreloadDirectChannelMembers(tx)
@ -142,8 +143,6 @@ func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channe
tx := database.C.Preload("Realm").Where("id IN ?", idx) tx := database.C.Preload("Realm").Where("id IN ?", idx)
if len(realmId) > 0 { if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId) tx = tx.Where("realm_id = ?", realmId)
} else {
tx = tx.Where("realm_id IS NULL")
} }
tx = PreloadDirectChannelMembers(tx) tx = PreloadDirectChannelMembers(tx)