Compare commits

...

2 Commits

Author SHA1 Message Date
2e18b2455b Separate list public channel 2025-02-15 15:55:06 +08:00
406eb9c93b 🐛 Fix no perm check on DM 2025-02-15 15:49:07 +08:00
4 changed files with 37 additions and 0 deletions

View File

@@ -76,6 +76,21 @@ func listChannel(c *fiber.Ctx) error {
return c.JSON(channels)
}
func listPublicChannel(c *fiber.Ctx) error {
var err error
var channels []models.Channel
if val, ok := c.Locals("realm").(authm.Realm); ok {
channels, err = services.ListChannelPublic(val.ID)
} else {
channels, err = services.ListChannelPublic()
}
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(channels)
}
func listOwnedChannel(c *fiber.Ctx) error {
if err := sec.EnsureAuthenticated(c); err != nil {
return err

View File

@@ -53,6 +53,10 @@ func createDirectChannel(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you already have a direct with that user #%d", ch.ID))
}
if err := authkit.EnsureUserPermGranted(gap.Nx, user.ID, relatedUser.ID, "ChannelAdd", true); err != nil {
return fmt.Errorf("unable to add user into your channel due to access denied: %v", err)
}
channel := models.Channel{
Alias: data.Alias,
Name: data.Name,

View File

@@ -15,6 +15,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
channels := api.Group("/channels/:realm").Use(realmMiddleware).Name("Channels API")
{
channels.Get("/", listChannel)
channels.Get("/public", listPublicChannel)
channels.Get("/me", listOwnedChannel)
channels.Get("/me/available", listAvailableChannel)
channels.Get("/:channel", getChannel)

View File

@@ -207,6 +207,23 @@ func ListChannel(user *authm.Account, realmId ...uint) ([]models.Channel, error)
return channels, nil
}
func ListChannelPublic(realmId ...uint) ([]models.Channel, error) {
var channels []models.Channel
tx := database.C
tx = tx.Where("is_public = true")
if len(realmId) > 0 {
tx = tx.Where("realm_id = ?", realmId)
}
tx = PreloadDirectChannelMembers(tx)
if err := tx.Find(&channels).Error; err != nil {
return channels, err
}
return channels, nil
}
func ListChannelWithUser(user authm.Account, realmId ...uint) ([]models.Channel, error) {
var channels []models.Channel
tx := database.C.Where(&models.Channel{AccountID: user.ID})