Compare commits
No commits in common. "d4dfa810d1152cc0d67bba21286acaa72247badd" and "045744aa18aea252dea941c8d1be9e076cfc2b0c" have entirely different histories.
d4dfa810d1
...
045744aa18
@ -28,9 +28,8 @@ type Post struct {
|
||||
RepostTo *Post `json:"repost_to" gorm:"foreignKey:RepostID"`
|
||||
Realm *Realm `json:"realm"`
|
||||
|
||||
IsDraft bool `json:"is_draft"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
PublishedUntil *time.Time `json:"published_until"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
|
||||
AuthorID uint `json:"author_id"`
|
||||
Author Account `json:"author"`
|
||||
|
@ -19,16 +19,15 @@ func createArticle(c *fiber.Ctx) error {
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description *string `json:"description" validate:"max=2048"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
PublishedUntil *time.Time `json:"published_until"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description *string `json:"description" validate:"max=2048"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -47,15 +46,13 @@ func createArticle(c *fiber.Ctx) error {
|
||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||
|
||||
item := models.Post{
|
||||
Type: models.PostTypeArticle,
|
||||
Body: bodyMapping,
|
||||
Language: services.DetectLanguage(data.Content),
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
IsDraft: data.IsDraft,
|
||||
PublishedAt: data.PublishedAt,
|
||||
PublishedUntil: data.PublishedUntil,
|
||||
AuthorID: user.ID,
|
||||
Type: models.PostTypeArticle,
|
||||
Body: bodyMapping,
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
IsDraft: data.IsDraft,
|
||||
PublishedAt: data.PublishedAt,
|
||||
AuthorID: user.ID,
|
||||
}
|
||||
|
||||
if data.RealmAlias != nil {
|
||||
@ -84,15 +81,14 @@ func editArticle(c *fiber.Ctx) error {
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description *string `json:"description" validate:"max=2048"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
PublishedUntil *time.Time `json:"published_until"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description *string `json:"description" validate:"max=2048"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -118,12 +114,10 @@ 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
|
||||
item.PublishedAt = data.PublishedAt
|
||||
item.PublishedUntil = data.PublishedUntil
|
||||
item.Tags = data.Tags
|
||||
item.Categories = data.Categories
|
||||
|
||||
if item, err := services.EditPost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
|
@ -19,18 +19,17 @@ func createStory(c *fiber.Ctx) error {
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Title *string `json:"title" validate:"max=1024"`
|
||||
Content string `json:"content" validate:"required,max=4096"`
|
||||
Location *string `json:"location" validate:"max=2048"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
PublishedUntil *time.Time `json:"published_until"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
RepostTo *uint `json:"repost_to"`
|
||||
Title *string `json:"title" validate:"max=1024"`
|
||||
Content string `json:"content" validate:"required,max=4096"`
|
||||
Location *string `json:"location" validate:"max=2048"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
RealmAlias *string `json:"realm"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
RepostTo *uint `json:"repost_to"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -49,15 +48,13 @@ func createStory(c *fiber.Ctx) error {
|
||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||
|
||||
item := models.Post{
|
||||
Type: models.PostTypeStory,
|
||||
Body: bodyMapping,
|
||||
Language: services.DetectLanguage(data.Content),
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
PublishedAt: data.PublishedAt,
|
||||
PublishedUntil: data.PublishedUntil,
|
||||
IsDraft: data.IsDraft,
|
||||
AuthorID: user.ID,
|
||||
Type: models.PostTypeStory,
|
||||
Body: bodyMapping,
|
||||
Tags: data.Tags,
|
||||
Categories: data.Categories,
|
||||
IsDraft: data.IsDraft,
|
||||
PublishedAt: data.PublishedAt,
|
||||
AuthorID: user.ID,
|
||||
}
|
||||
|
||||
if data.ReplyTo != nil {
|
||||
@ -103,15 +100,14 @@ func editStory(c *fiber.Ctx) error {
|
||||
user := c.Locals("user").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Title *string `json:"title" validate:"max=1024"`
|
||||
Content string `json:"content" validate:"required,max=4096"`
|
||||
Location *string `json:"location" validate:"max=2048"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
PublishedUntil *time.Time `json:"published_until"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
Title *string `json:"title" validate:"max=1024"`
|
||||
Content string `json:"content" validate:"required,max=4096"`
|
||||
Location *string `json:"location" validate:"max=2048"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
Tags []models.Tag `json:"tags"`
|
||||
Categories []models.Category `json:"categories"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
@ -138,12 +134,10 @@ func editStory(c *fiber.Ctx) error {
|
||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||
|
||||
item.Body = bodyMapping
|
||||
item.Language = services.DetectLanguage(data.Content)
|
||||
item.IsDraft = data.IsDraft
|
||||
item.PublishedAt = data.PublishedAt
|
||||
item.Tags = data.Tags
|
||||
item.Categories = data.Categories
|
||||
item.PublishedAt = data.PublishedAt
|
||||
item.PublishedUntil = data.PublishedUntil
|
||||
item.IsDraft = data.IsDraft
|
||||
|
||||
if item, err := services.EditPost(item); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
|
@ -5,12 +5,14 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func DetectLanguage(content string) string {
|
||||
detector := lingua.NewLanguageDetectorBuilder().
|
||||
FromLanguages(lingua.AllLanguages()...).
|
||||
Build()
|
||||
if lang, ok := detector.DetectLanguageOf(content); ok {
|
||||
return strings.ToLower(lang.String())
|
||||
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"
|
||||
}
|
||||
|
@ -44,9 +44,7 @@ func FilterPostReply(tx *gorm.DB, replyTo ...uint) *gorm.DB {
|
||||
}
|
||||
|
||||
func FilterPostWithPublishedAt(tx *gorm.DB, date time.Time) *gorm.DB {
|
||||
return tx.
|
||||
Where("published_at <= ? OR published_at IS NULL", date).
|
||||
Where("published_until > ? OR published_until IS NULL", date)
|
||||
return tx.Where("published_at <= ? OR published_at IS NULL", date)
|
||||
}
|
||||
|
||||
func FilterPostWithAuthorDraft(tx *gorm.DB, uid uint) *gorm.DB {
|
||||
@ -123,7 +121,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos
|
||||
var items []*models.Post
|
||||
if err := tx.
|
||||
Limit(take).Offset(offset).
|
||||
Order("published_at DESC").
|
||||
Order("created_at DESC").
|
||||
Preload("Tags").
|
||||
Preload("Categories").
|
||||
Preload("Realm").
|
||||
@ -218,10 +216,6 @@ 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user