feat: slug on the post author object, for author links that do not expose the login

This commit is contained in:
Jakub Zych
2026-10-31 13:11:44 +01:00
parent 08e3d82ecd
commit f7bfb3313b
3 changed files with 26 additions and 6 deletions

View File

@@ -620,7 +620,7 @@ func serializePost(p *models.Post, db *gorm.DB) map[string]any {
"is_pinned": p.IsPinned,
"metadata": jsonField(p.Metadata),
"sources": jsonField(p.Sources),
"author": serializeAuthor(p.User),
"author": serializeAuthor(db, p.User),
"categories": cats,
"tags": tags,
"featured_images": featured,
@@ -683,15 +683,27 @@ func firstImageURL(featured []map[string]any) any {
return featured[0]["url"]
}
func serializeAuthor(user *cabana.BackendUser) any {
// serializeAuthor is a post's author object. slug is the author's canonical
// identifier, as on GET /authors/{author}: the author slug when set, otherwise
// the login, so a consumer links to the author page by slug.
func serializeAuthor(db *gorm.DB, user *cabana.BackendUser) any {
if user == nil {
return nil
}
slug := user.Login
if db != nil {
var authorSlug string
if err := db.Table("backend_users").Select("COALESCE(golem15_bloghub_author_slug, '')").
Where("id = ?", user.ID).Scan(&authorSlug).Error; err == nil && authorSlug != "" {
slug = authorSlug
}
}
return map[string]any{
"id": user.ID,
"first_name": user.FirstName,
"last_name": user.LastName,
"login": user.Login,
"slug": slug,
}
}