Language's post

This commit is contained in:
2024-07-13 23:16:40 +08:00
parent ad3257dabf
commit c67a38f4bb
10 changed files with 58 additions and 19 deletions

View File

@ -138,7 +138,7 @@ func ListArticle(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.
for k, v := range mapping {
if post, ok := itemMap[k]; ok {
post.ReactionList = v
post.Metric.ReactionList = v
}
}
}
@ -165,6 +165,8 @@ func EnsureArticleCategoriesAndTags(item models.Article) (models.Article, error)
}
func NewArticle(user models.Account, item models.Article) (models.Article, error) {
item.Language = DetectLanguage(&item.Content)
item, err := EnsureArticleCategoriesAndTags(item)
if err != nil {
return item, err
@ -185,6 +187,7 @@ func NewArticle(user models.Account, item models.Article) (models.Article, error
}
func EditArticle(item models.Article) (models.Article, error) {
item.Language = DetectLanguage(&item.Content)
item, err := EnsureArticleCategoriesAndTags(item)
if err != nil {
return item, err

View File

@ -0,0 +1,18 @@
package services
import (
"github.com/pemistahl/lingua-go"
"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())
}
}
return "unknown"
}

View File

@ -167,6 +167,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos
}
idx := lo.Map(items, func(item *models.Post, index int) uint {
item.Metric = models.PostMetric{}
return item.ID
})
@ -181,7 +182,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos
for k, v := range mapping {
if post, ok := itemMap[k]; ok {
post.ReactionList = v
post.Metric.ReactionList = v
}
}
}
@ -213,7 +214,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos
for k, v := range list {
if post, ok := itemMap[k]; ok {
post.ReplyCount = v
post.Metric.ReplyCount = v
}
}
}
@ -239,6 +240,8 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
}
func NewPost(user models.Account, item models.Post) (models.Post, error) {
item.Language = DetectLanguage(item.Content)
item, err := EnsurePostCategoriesAndTags(item)
if err != nil {
return item, err
@ -281,6 +284,7 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
}
func EditPost(item models.Post) (models.Post, error) {
item.Language = DetectLanguage(item.Content)
item, err := EnsurePostCategoriesAndTags(item)
if err != nil {
return item, err