From e1f1cd513044bdc3ac3599f0dac77d99851f2908 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 20 Feb 2025 21:30:21 +0800 Subject: [PATCH] :sparkles: Create post with realm --- pkg/internal/http/api/articles_api.go | 9 +++++++++ pkg/internal/http/api/questions_api.go | 8 ++++++++ pkg/internal/http/api/stories_api.go | 8 ++++++++ pkg/internal/http/api/videos_api.go | 9 +++++++++ pkg/internal/models/posts.go | 4 ++-- 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pkg/internal/http/api/articles_api.go b/pkg/internal/http/api/articles_api.go index c272552..82b1bd3 100644 --- a/pkg/internal/http/api/articles_api.go +++ b/pkg/internal/http/api/articles_api.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "strconv" "time" @@ -41,6 +42,7 @@ func createArticle(c *fiber.Ctx) error { InvisibleUsers []uint `json:"invisible_users_list"` Visibility *int8 `json:"visibility"` IsDraft bool `json:"is_draft"` + Realm *uint `json:"realm"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -83,6 +85,13 @@ func createArticle(c *fiber.Ctx) error { item.PublishedAt = lo.ToPtr(time.Now()) } + if data.Realm != nil { + if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm)) + } + item.RealmID = data.Realm + } + if data.Visibility != nil { item.Visibility = *data.Visibility } else { diff --git a/pkg/internal/http/api/questions_api.go b/pkg/internal/http/api/questions_api.go index 021ddea..f557343 100644 --- a/pkg/internal/http/api/questions_api.go +++ b/pkg/internal/http/api/questions_api.go @@ -43,6 +43,7 @@ func createQuestion(c *fiber.Ctx) error { InvisibleUsers []uint `json:"invisible_users_list"` Visibility *int8 `json:"visibility"` IsDraft bool `json:"is_draft"` + Realm *uint `json:"realm"` Reward float64 `json:"reward"` } @@ -109,6 +110,13 @@ func createQuestion(c *fiber.Ctx) error { item.PublishedAt = lo.ToPtr(time.Now()) } + if data.Realm != nil { + if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm)) + } + item.RealmID = data.Realm + } + if data.Visibility != nil { item.Visibility = *data.Visibility } else { diff --git a/pkg/internal/http/api/stories_api.go b/pkg/internal/http/api/stories_api.go index b9a3fcd..71cbc80 100644 --- a/pkg/internal/http/api/stories_api.go +++ b/pkg/internal/http/api/stories_api.go @@ -45,6 +45,7 @@ func createStory(c *fiber.Ctx) error { ReplyTo *uint `json:"reply_to"` RepostTo *uint `json:"repost_to"` Poll *uint `json:"poll"` + Realm *uint `json:"realm"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -96,6 +97,13 @@ func createStory(c *fiber.Ctx) error { item.Visibility = models.PostVisibilityAll } + if data.Realm != nil { + if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm)) + } + item.RealmID = data.Realm + } + if data.ReplyTo != nil { var replyTo models.Post if err := database.C.Where("id = ?", data.ReplyTo).First(&replyTo).Error; err != nil { diff --git a/pkg/internal/http/api/videos_api.go b/pkg/internal/http/api/videos_api.go index ed1ef91..db54d0d 100644 --- a/pkg/internal/http/api/videos_api.go +++ b/pkg/internal/http/api/videos_api.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "strconv" "time" @@ -41,6 +42,7 @@ func createVideo(c *fiber.Ctx) error { InvisibleUsers []uint `json:"invisible_users_list"` Visibility *int8 `json:"visibility"` IsDraft bool `json:"is_draft"` + Realm *uint `json:"realm"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -84,6 +86,13 @@ func createVideo(c *fiber.Ctx) error { item.PublishedAt = lo.ToPtr(time.Now()) } + if data.Realm != nil { + if _, err := authkit.GetRealmMember(gap.Nx, *data.Realm, user.ID); err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("you are not a member of realm #%d", *data.Realm)) + } + item.RealmID = data.Realm + } + if data.Visibility != nil { item.Visibility = *data.Visibility } else { diff --git a/pkg/internal/models/posts.go b/pkg/internal/models/posts.go index c374f4e..76fa0da 100644 --- a/pkg/internal/models/posts.go +++ b/pkg/internal/models/posts.go @@ -32,8 +32,8 @@ type Post struct { Type string `json:"type"` Body datatypes.JSONMap `json:"body" gorm:"index:,type:gin"` Language string `json:"language"` - Alias *string `json:"alias"` - AliasPrefix *string `json:"alias_prefix"` + Alias *string `json:"alias" gorm:"index"` + AliasPrefix *string `json:"alias_prefix" gorm:"index"` Tags []Tag `json:"tags" gorm:"many2many:post_tags"` Categories []Category `json:"categories" gorm:"many2many:post_categories"` Reactions []Reaction `json:"reactions"`