From fca4032031e5b6daef116567f2d352dc50eab59f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 10 Aug 2024 16:45:42 +0800 Subject: [PATCH] :sparkles: Able to transfer story / article between realms --- pkg/internal/server/api/articles_api.go | 11 +++++++++++ pkg/internal/server/api/stories_api.go | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/internal/server/api/articles_api.go b/pkg/internal/server/api/articles_api.go index bf222ce..7eb5161 100644 --- a/pkg/internal/server/api/articles_api.go +++ b/pkg/internal/server/api/articles_api.go @@ -113,6 +113,7 @@ func editArticle(c *fiber.Ctx) error { InvisibleUsers []uint `json:"invisible_users_list"` Visibility *int8 `json:"visibility"` IsDraft bool `json:"is_draft"` + RealmAlias *string `json:"realm"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -165,6 +166,16 @@ func editArticle(c *fiber.Ctx) error { item.Visibility = *data.Visibility } + if data.RealmAlias != nil { + if realm, err := services.GetRealmWithAlias(*data.RealmAlias); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else if _, err = services.GetRealmMember(realm.ExternalID, user.ExternalID); err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err)) + } else { + item.RealmID = &realm.ID + } + } + if item, err := services.EditPost(item); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { diff --git a/pkg/internal/server/api/stories_api.go b/pkg/internal/server/api/stories_api.go index b7378c0..d6771fb 100644 --- a/pkg/internal/server/api/stories_api.go +++ b/pkg/internal/server/api/stories_api.go @@ -132,6 +132,7 @@ func editStory(c *fiber.Ctx) error { InvisibleUsers []uint `json:"invisible_users_list"` Visibility *int8 `json:"visibility"` IsDraft bool `json:"is_draft"` + RealmAlias *string `json:"realm"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -184,6 +185,16 @@ func editStory(c *fiber.Ctx) error { item.Visibility = *data.Visibility } + if data.RealmAlias != nil { + if realm, err := services.GetRealmWithAlias(*data.RealmAlias); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else if _, err = services.GetRealmMember(realm.ExternalID, user.ExternalID); err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err)) + } else { + item.RealmID = &realm.ID + } + } + if item, err := services.EditPost(item); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else {