Better alias with region alias

This commit is contained in:
LittleSheep 2024-08-17 15:40:07 +08:00
parent 008d531855
commit 2470c4ac53
5 changed files with 54 additions and 2 deletions

View File

@ -28,6 +28,7 @@ type Post struct {
Body datatypes.JSONMap `json:"body"`
Language string `json:"language"`
Alias *string `json:"alias"`
AreaAlias *string `json:"area_alias"`
Tags []Tag `json:"tags" gorm:"many2many:post_tags"`
Categories []Category `json:"categories" gorm:"many2many:post_categories"`
Reactions []Reaction `json:"reactions"`

View File

@ -86,6 +86,7 @@ func createArticle(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
} else {
item.RealmID = &realm.ID
item.Realm = &realm
}
}
@ -169,6 +170,7 @@ func editArticle(c *fiber.Ctx) error {
item.PublishedUntil = data.PublishedUntil
item.VisibleUsers = data.VisibleUsers
item.InvisibleUsers = data.InvisibleUsers
item.Author = user
if data.Visibility != nil {
item.Visibility = *data.Visibility
@ -181,6 +183,7 @@ func editArticle(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
} else {
item.RealmID = &realm.ID
item.Realm = &realm
}
}

View File

@ -2,6 +2,8 @@ package api
import (
"fmt"
"strconv"
"strings"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
@ -13,9 +15,24 @@ import (
)
func getPost(c *fiber.Ctx) error {
id, _ := c.ParamsInt("postId")
id := c.Params("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 {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}

View File

@ -105,6 +105,7 @@ func createStory(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
} else {
item.RealmID = &realm.ID
item.Realm = &realm
}
}
@ -188,6 +189,7 @@ func editStory(c *fiber.Ctx) error {
item.IsDraft = data.IsDraft
item.VisibleUsers = data.VisibleUsers
item.InvisibleUsers = data.InvisibleUsers
item.Author = user
if data.Visibility != nil {
item.Visibility = *data.Visibility
@ -200,6 +202,7 @@ func editStory(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to post in the realm, access denied: %v", err))
} else {
item.RealmID = &realm.ID
item.Realm = &realm
}
}

View File

@ -116,6 +116,22 @@ func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error
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) {
var count int64
if err := tx.Model(&models.Post{}).Count(&count).Error; err != nil {
@ -254,6 +270,12 @@ func EnsurePostCategoriesAndTags(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...")
start := time.Now()
@ -303,6 +325,12 @@ func NewPost(user models.Account, 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)
if err != nil {
return item, err