From d4dfa810d1152cc0d67bba21286acaa72247badd Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 22 Jul 2024 01:46:38 +0800 Subject: [PATCH] :sparkles: Detect language in controller layer --- pkg/internal/server/api/articles_api.go | 2 ++ pkg/internal/server/api/stories_api.go | 2 ++ pkg/internal/services/languages.go | 14 ++++++-------- pkg/internal/services/posts.go | 4 ++++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pkg/internal/server/api/articles_api.go b/pkg/internal/server/api/articles_api.go index abc03f2..6b9982d 100644 --- a/pkg/internal/server/api/articles_api.go +++ b/pkg/internal/server/api/articles_api.go @@ -49,6 +49,7 @@ func createArticle(c *fiber.Ctx) error { item := models.Post{ Type: models.PostTypeArticle, Body: bodyMapping, + Language: services.DetectLanguage(data.Content), Tags: data.Tags, Categories: data.Categories, IsDraft: data.IsDraft, @@ -117,6 +118,7 @@ func editArticle(c *fiber.Ctx) error { _ = jsoniter.Unmarshal(rawBody, &bodyMapping) item.Body = bodyMapping + item.Language = services.DetectLanguage(data.Content) item.Tags = data.Tags item.Categories = data.Categories item.IsDraft = data.IsDraft diff --git a/pkg/internal/server/api/stories_api.go b/pkg/internal/server/api/stories_api.go index e6ae11b..b38f2e8 100644 --- a/pkg/internal/server/api/stories_api.go +++ b/pkg/internal/server/api/stories_api.go @@ -51,6 +51,7 @@ func createStory(c *fiber.Ctx) error { item := models.Post{ Type: models.PostTypeStory, Body: bodyMapping, + Language: services.DetectLanguage(data.Content), Tags: data.Tags, Categories: data.Categories, PublishedAt: data.PublishedAt, @@ -137,6 +138,7 @@ func editStory(c *fiber.Ctx) error { _ = jsoniter.Unmarshal(rawBody, &bodyMapping) item.Body = bodyMapping + item.Language = services.DetectLanguage(data.Content) item.Tags = data.Tags item.Categories = data.Categories item.PublishedAt = data.PublishedAt diff --git a/pkg/internal/services/languages.go b/pkg/internal/services/languages.go index f244290..36e9503 100644 --- a/pkg/internal/services/languages.go +++ b/pkg/internal/services/languages.go @@ -5,14 +5,12 @@ import ( "strings" ) -func DetectLanguage(content *string) string { - if content != nil { - detector := lingua.NewLanguageDetectorBuilder(). - FromLanguages(lingua.AllLanguages()...). - Build() - if lang, ok := detector.DetectLanguageOf(*content); ok { - return strings.ToLower(lang.String()) - } +func DetectLanguage(content string) string { + detector := lingua.NewLanguageDetectorBuilder(). + FromLanguages(lingua.AllLanguages()...). + Build() + if lang, ok := detector.DetectLanguageOf(content); ok { + return strings.ToLower(lang.String()) } return "unknown" } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 52303fb..ca9a52c 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -218,6 +218,10 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) { } func NewPost(user models.Account, item models.Post) (models.Post, error) { + if !item.IsDraft && item.PublishedAt == nil { + item.PublishedAt = lo.ToPtr(time.Now()) + } + item, err := EnsurePostCategoriesAndTags(item) if err != nil { return item, err