From 2470c4ac53ce705346c2c220ed39d2f8fd74534b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 17 Aug 2024 15:40:07 +0800 Subject: [PATCH] :sparkles: Better alias with region alias --- pkg/internal/models/posts.go | 1 + pkg/internal/server/api/articles_api.go | 3 +++ pkg/internal/server/api/posts_api.go | 21 +++++++++++++++++-- pkg/internal/server/api/stories_api.go | 3 +++ pkg/internal/services/posts.go | 28 +++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/internal/models/posts.go b/pkg/internal/models/posts.go index 7192dc3..e19ad0c 100644 --- a/pkg/internal/models/posts.go +++ b/pkg/internal/models/posts.go @@ -28,6 +28,7 @@ type Post struct { Body datatypes.JSONMap `json:"body"` Language string `json:"language"` Alias *string `json:"alias"` + AreaAlias *string `json:"area_alias"` Tags []Tag `json:"tags" gorm:"many2many:post_tags"` Categories []Category `json:"categories" gorm:"many2many:post_categories"` Reactions []Reaction `json:"reactions"` diff --git a/pkg/internal/server/api/articles_api.go b/pkg/internal/server/api/articles_api.go index 8a4a638..b23556e 100644 --- a/pkg/internal/server/api/articles_api.go +++ b/pkg/internal/server/api/articles_api.go @@ -86,6 +86,7 @@ func createArticle(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err)) } else { item.RealmID = &realm.ID + item.Realm = &realm } } @@ -169,6 +170,7 @@ func editArticle(c *fiber.Ctx) error { item.PublishedUntil = data.PublishedUntil item.VisibleUsers = data.VisibleUsers item.InvisibleUsers = data.InvisibleUsers + item.Author = user if data.Visibility != nil { item.Visibility = *data.Visibility @@ -181,6 +183,7 @@ func editArticle(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err)) } else { item.RealmID = &realm.ID + item.Realm = &realm } } diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index f5f62a1..2eac566 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -2,6 +2,8 @@ package api import ( "fmt" + "strconv" + "strings" "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" "git.solsynth.dev/hydrogen/interactive/pkg/internal/gap" @@ -13,9 +15,24 @@ import ( ) func getPost(c *fiber.Ctx) error { - id, _ := c.ParamsInt("postId") + id := c.Params("postId") + + var item models.Post + var err error + + tx := services.FilterPostDraft(database.C) + if numericId, paramErr := strconv.Atoi(id); paramErr == nil { + item, err = services.GetPost(tx, uint(numericId)) + } else { + segments := strings.Split(id, ":") + if len(segments) != 2 { + return fiber.NewError(fiber.StatusBadRequest, "invalid post id, must be a number or a string with two segment divided by a colon") + } + area := segments[0] + alias := segments[1] + item, err = services.GetPostByAlias(tx, area, alias) + } - item, err := services.GetPost(services.FilterPostDraft(database.C), uint(id)) if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } diff --git a/pkg/internal/server/api/stories_api.go b/pkg/internal/server/api/stories_api.go index c4dddc6..18ef3e0 100644 --- a/pkg/internal/server/api/stories_api.go +++ b/pkg/internal/server/api/stories_api.go @@ -105,6 +105,7 @@ func createStory(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err)) } else { item.RealmID = &realm.ID + item.Realm = &realm } } @@ -188,6 +189,7 @@ func editStory(c *fiber.Ctx) error { item.IsDraft = data.IsDraft item.VisibleUsers = data.VisibleUsers item.InvisibleUsers = data.InvisibleUsers + item.Author = user if data.Visibility != nil { item.Visibility = *data.Visibility @@ -200,6 +202,7 @@ func editStory(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err)) } else { item.RealmID = &realm.ID + item.Realm = &realm } } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 272b3e0..34b4069 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -116,6 +116,22 @@ func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error return item, nil } +func GetPostByAlias(tx *gorm.DB, alias, area string, ignoreLimitation ...bool) (models.Post, error) { + if len(ignoreLimitation) == 0 || !ignoreLimitation[0] { + tx = FilterPostWithPublishedAt(tx, time.Now()) + } + + var item models.Post + if err := PreloadGeneral(tx). + Where("alias = ?", alias). + Where("area_alias = ?", area). + First(&item).Error; err != nil { + return item, err + } + + return item, nil +} + func CountPost(tx *gorm.DB) (int64, error) { var count int64 if err := tx.Model(&models.Post{}).Count(&count).Error; err != nil { @@ -254,6 +270,12 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) { } func NewPost(user models.Account, item models.Post) (models.Post, error) { + if item.Realm != nil { + item.AreaAlias = &item.Realm.Alias + } else { + item.AreaAlias = &user.Name + } + log.Debug().Any("body", item.Body).Msg("Posting a post...") start := time.Now() @@ -303,6 +325,12 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) { } func EditPost(item models.Post) (models.Post, error) { + if item.Realm != nil { + item.AreaAlias = &item.Realm.Alias + } else { + item.AreaAlias = &item.Author.Name + } + item, err := EnsurePostCategoriesAndTags(item) if err != nil { return item, err