🐛 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)
}
func getChannelAvailability(c *fiber.Ctx) error {
func getChannelIdentity(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
alias := c.Params("channel")
var err error
var channel models.Channel
var member models.ChannelMember
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 {
channel, _, err = services.GetAvailableChannelWithAlias(alias, user)
_, member, err = services.GetAvailableChannelWithAlias(alias, user)
}
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 {

View File

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

View File

@ -20,9 +20,11 @@ func GetChannelAliasAvailability(alias string) error {
func GetChannel(id uint) (models.Channel, error) {
var channel models.Channel
if err := database.C.Where(models.Channel{
tx := database.C.Where(models.Channel{
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
}
@ -31,12 +33,13 @@ func GetChannel(id uint) (models.Channel, error) {
func GetChannelWithAlias(alias string, realmId ...uint) (models.Channel, error) {
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 {
tx = tx.Where("realm_id = ?", realmId)
} else {
tx = tx.Where("realm_id IS NULL")
}
tx = PreloadDirectChannelMembers(tx)
if err := tx.First(&channel).Error; err != nil {
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 {
return channel, member, err
}
if err := database.C.Where(models.ChannelMember{
tx := database.C.Where(models.ChannelMember{
AccountID: user.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())
}
@ -113,8 +116,6 @@ func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel
tx := database.C.Where(&models.Channel{AccountID: user.ID}).Preload("Realm")
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
} else {
tx = tx.Where("realm_id IS NULL")
}
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)
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
} else {
tx = tx.Where("realm_id IS NULL")
}
tx = PreloadDirectChannelMembers(tx)