Compare commits
No commits in common. "2470c4ac53ce705346c2c220ed39d2f8fd74534b" and "6e146845399f99705064e3da1e90a473facf710b" have entirely different histories.
2470c4ac53
...
6e14684539
@ -27,8 +27,6 @@ type Post struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Body datatypes.JSONMap `json:"body"`
|
Body datatypes.JSONMap `json:"body"`
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
Alias *string `json:"alias"`
|
|
||||||
AreaAlias *string `json:"area_alias"`
|
|
||||||
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
|
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
|
||||||
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
|
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
|
||||||
Reactions []Reaction `json:"reactions"`
|
Reactions []Reaction `json:"reactions"`
|
||||||
|
@ -21,7 +21,6 @@ func createArticle(c *fiber.Ctx) error {
|
|||||||
user := c.Locals("user").(models.Account)
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Alias *string `json:"alias"`
|
|
||||||
Title string `json:"title" validate:"required,max=1024"`
|
Title string `json:"title" validate:"required,max=1024"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
Content string `json:"content" validate:"required"`
|
Content string `json:"content" validate:"required"`
|
||||||
@ -55,7 +54,6 @@ func createArticle(c *fiber.Ctx) error {
|
|||||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||||
|
|
||||||
item := models.Post{
|
item := models.Post{
|
||||||
Alias: data.Alias,
|
|
||||||
Type: models.PostTypeArticle,
|
Type: models.PostTypeArticle,
|
||||||
Body: bodyMapping,
|
Body: bodyMapping,
|
||||||
Language: services.DetectLanguage(data.Content),
|
Language: services.DetectLanguage(data.Content),
|
||||||
@ -86,7 +84,6 @@ func createArticle(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||||
} else {
|
} else {
|
||||||
item.RealmID = &realm.ID
|
item.RealmID = &realm.ID
|
||||||
item.Realm = &realm
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +103,6 @@ func editArticle(c *fiber.Ctx) error {
|
|||||||
user := c.Locals("user").(models.Account)
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Alias *string `json:"alias"`
|
|
||||||
Title string `json:"title" validate:"required,max=1024"`
|
Title string `json:"title" validate:"required,max=1024"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
Content string `json:"content" validate:"required"`
|
Content string `json:"content" validate:"required"`
|
||||||
@ -161,7 +157,6 @@ func editArticle(c *fiber.Ctx) error {
|
|||||||
rawBody, _ := jsoniter.Marshal(body)
|
rawBody, _ := jsoniter.Marshal(body)
|
||||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||||
|
|
||||||
item.Alias = data.Alias
|
|
||||||
item.Body = bodyMapping
|
item.Body = bodyMapping
|
||||||
item.Language = services.DetectLanguage(data.Content)
|
item.Language = services.DetectLanguage(data.Content)
|
||||||
item.Tags = data.Tags
|
item.Tags = data.Tags
|
||||||
@ -170,7 +165,6 @@ func editArticle(c *fiber.Ctx) error {
|
|||||||
item.PublishedUntil = data.PublishedUntil
|
item.PublishedUntil = data.PublishedUntil
|
||||||
item.VisibleUsers = data.VisibleUsers
|
item.VisibleUsers = data.VisibleUsers
|
||||||
item.InvisibleUsers = data.InvisibleUsers
|
item.InvisibleUsers = data.InvisibleUsers
|
||||||
item.Author = user
|
|
||||||
|
|
||||||
if data.Visibility != nil {
|
if data.Visibility != nil {
|
||||||
item.Visibility = *data.Visibility
|
item.Visibility = *data.Visibility
|
||||||
@ -183,7 +177,6 @@ func editArticle(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||||
} else {
|
} else {
|
||||||
item.RealmID = &realm.ID
|
item.RealmID = &realm.ID
|
||||||
item.Realm = &realm
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
||||||
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
||||||
@ -15,24 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func getPost(c *fiber.Ctx) error {
|
func getPost(c *fiber.Ctx) error {
|
||||||
id := c.Params("postId")
|
id, _ := c.ParamsInt("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 {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ func createStory(c *fiber.Ctx) error {
|
|||||||
user := c.Locals("user").(models.Account)
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Alias *string `json:"alias"`
|
|
||||||
Title *string `json:"title"`
|
Title *string `json:"title"`
|
||||||
Content string `json:"content" validate:"required,max=4096"`
|
Content string `json:"content" validate:"required,max=4096"`
|
||||||
Location *string `json:"location"`
|
Location *string `json:"location"`
|
||||||
@ -57,7 +56,6 @@ func createStory(c *fiber.Ctx) error {
|
|||||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||||
|
|
||||||
item := models.Post{
|
item := models.Post{
|
||||||
Alias: data.Alias,
|
|
||||||
Type: models.PostTypeStory,
|
Type: models.PostTypeStory,
|
||||||
Body: bodyMapping,
|
Body: bodyMapping,
|
||||||
Language: services.DetectLanguage(data.Content),
|
Language: services.DetectLanguage(data.Content),
|
||||||
@ -105,7 +103,6 @@ func createStory(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||||
} else {
|
} else {
|
||||||
item.RealmID = &realm.ID
|
item.RealmID = &realm.ID
|
||||||
item.Realm = &realm
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +122,6 @@ func editStory(c *fiber.Ctx) error {
|
|||||||
user := c.Locals("user").(models.Account)
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Alias *string `json:"alias"`
|
|
||||||
Title *string `json:"title"`
|
Title *string `json:"title"`
|
||||||
Content string `json:"content" validate:"required,max=4096"`
|
Content string `json:"content" validate:"required,max=4096"`
|
||||||
Thumbnail *uint `json:"thumbnail"`
|
Thumbnail *uint `json:"thumbnail"`
|
||||||
@ -180,7 +176,6 @@ func editStory(c *fiber.Ctx) error {
|
|||||||
rawBody, _ := jsoniter.Marshal(body)
|
rawBody, _ := jsoniter.Marshal(body)
|
||||||
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
_ = jsoniter.Unmarshal(rawBody, &bodyMapping)
|
||||||
|
|
||||||
item.Alias = data.Alias
|
|
||||||
item.Body = bodyMapping
|
item.Body = bodyMapping
|
||||||
item.Language = services.DetectLanguage(data.Content)
|
item.Language = services.DetectLanguage(data.Content)
|
||||||
item.Tags = data.Tags
|
item.Tags = data.Tags
|
||||||
@ -189,7 +184,6 @@ func editStory(c *fiber.Ctx) error {
|
|||||||
item.IsDraft = data.IsDraft
|
item.IsDraft = data.IsDraft
|
||||||
item.VisibleUsers = data.VisibleUsers
|
item.VisibleUsers = data.VisibleUsers
|
||||||
item.InvisibleUsers = data.InvisibleUsers
|
item.InvisibleUsers = data.InvisibleUsers
|
||||||
item.Author = user
|
|
||||||
|
|
||||||
if data.Visibility != nil {
|
if data.Visibility != nil {
|
||||||
item.Visibility = *data.Visibility
|
item.Visibility = *data.Visibility
|
||||||
@ -202,7 +196,6 @@ func editStory(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
|
||||||
} else {
|
} else {
|
||||||
item.RealmID = &realm.ID
|
item.RealmID = &realm.ID
|
||||||
item.Realm = &realm
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,22 +116,6 @@ func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error
|
|||||||
return item, nil
|
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) {
|
func CountPost(tx *gorm.DB) (int64, error) {
|
||||||
var count int64
|
var count int64
|
||||||
if err := tx.Model(&models.Post{}).Count(&count).Error; err != nil {
|
if err := tx.Model(&models.Post{}).Count(&count).Error; err != nil {
|
||||||
@ -270,12 +254,6 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewPost(user models.Account, 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...")
|
log.Debug().Any("body", item.Body).Msg("Posting a post...")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
@ -325,12 +303,6 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EditPost(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)
|
item, err := EnsurePostCategoriesAndTags(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return item, err
|
return item, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user