✨ Language's post
This commit is contained in:
@ -13,6 +13,7 @@ type Article struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Content string `json:"content"`
|
||||
Language string `json:"language"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:article_tags"`
|
||||
Categories []Category `json:"categories" gorm:"many2many:article_categories"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
@ -26,7 +27,5 @@ type Article struct {
|
||||
AuthorID uint `json:"author_id"`
|
||||
Author Account `json:"author"`
|
||||
|
||||
// Dynamic Calculated Values
|
||||
ReactionCount int64 `json:"reaction_count"`
|
||||
ReactionList map[string]int64 `json:"reaction_list" gorm:"-"`
|
||||
Metric PostMetric `json:"metric" gorm:"-"`
|
||||
}
|
||||
|
7
pkg/internal/models/metrics.go
Normal file
7
pkg/internal/models/metrics.go
Normal file
@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
type PostMetric struct {
|
||||
ReplyCount int64 `json:"reply_count,omitempty"`
|
||||
ReactionCount int64 `json:"reaction_count,omitempty"`
|
||||
ReactionList map[string]int64 `json:"reaction_list,omitempty"`
|
||||
}
|
@ -10,7 +10,8 @@ type Post struct {
|
||||
BaseModel
|
||||
|
||||
Alias string `json:"alias" gorm:"uniqueIndex"`
|
||||
Content string `json:"content"`
|
||||
Content *string `json:"content"`
|
||||
Language string `json:"language"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
|
||||
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
|
||||
Reactions []Reaction `json:"reactions"`
|
||||
@ -29,8 +30,5 @@ type Post struct {
|
||||
AuthorID uint `json:"author_id"`
|
||||
Author Account `json:"author"`
|
||||
|
||||
// Dynamic Calculated Values
|
||||
ReplyCount int64 `json:"reply_count"`
|
||||
ReactionCount int64 `json:"reaction_count"`
|
||||
ReactionList map[string]int64 `json:"reaction_list" gorm:"-"`
|
||||
Metric PostMetric `json:"metric" gorm:"-"`
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ func getArticle(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
item.ReactionCount = services.CountArticleReactions(item.ID)
|
||||
item.ReactionList, err = services.ListResourceReactions(database.C.Where("article_id = ?", item.ID))
|
||||
item.Metric.ReactionCount = services.CountArticleReactions(item.ID)
|
||||
item.Metric.ReactionList, err = services.ListResourceReactions(database.C.Where("article_id = ?", item.ID))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
@ -23,9 +23,11 @@ func getPost(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
item.ReplyCount = services.CountPostReply(item.ID)
|
||||
item.ReactionCount = services.CountPostReactions(item.ID)
|
||||
item.ReactionList, err = services.ListResourceReactions(database.C.Where("post_id = ?", item.ID))
|
||||
item.Metric = models.PostMetric{
|
||||
ReplyCount: services.CountPostReply(item.ID),
|
||||
ReactionCount: services.CountPostReactions(item.ID),
|
||||
}
|
||||
item.Metric.ReactionList, err = services.ListResourceReactions(database.C.Where("post_id = ?", item.ID))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
@ -139,7 +141,7 @@ func createPost(c *fiber.Ctx) error {
|
||||
|
||||
item := models.Post{
|
||||
Alias: data.Alias,
|
||||
Content: data.Content,
|
||||
Content: &data.Content,
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
Attachments: data.Attachments,
|
||||
@ -218,8 +220,8 @@ func editPost(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
item.Content = &data.Content
|
||||
item.Alias = data.Alias
|
||||
item.Content = data.Content
|
||||
item.IsDraft = data.IsDraft
|
||||
item.PublishedAt = data.PublishedAt
|
||||
item.Tags = data.Tags
|
||||
|
@ -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
|
||||
|
18
pkg/internal/services/languages.go
Normal file
18
pkg/internal/services/languages.go
Normal 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"
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user