feat(media): unify uploads onto modules/album, add batched srcset enrichment and Media Library picker
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"git.golem15.com/golem15/sm-journal-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-journal-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/album"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||
@@ -128,11 +129,21 @@ func Index(app *backpack.App) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
rows := make([]map[string]any, 0, len(posts))
|
||||
for i := range posts {
|
||||
if suggest {
|
||||
rows = append(rows, serializeSuggestion(&posts[i], db.WithContext(r.Context())))
|
||||
} else {
|
||||
rows = append(rows, serializePost(&posts[i], db.WithContext(r.Context())))
|
||||
dbCtx := db.WithContext(r.Context())
|
||||
if suggest {
|
||||
for i := range posts {
|
||||
rows = append(rows, serializeSuggestion(&posts[i], dbCtx))
|
||||
}
|
||||
} else {
|
||||
postPtrs := make([]*models.Post, len(posts))
|
||||
trs := make([]any, len(posts))
|
||||
for i := range posts {
|
||||
postPtrs[i] = &posts[i]
|
||||
trs[i] = translationsOf(dbCtx, &posts[i])
|
||||
}
|
||||
media := batchLoadPostMedia(r.Context(), dbCtx, app, postPtrs, trs)
|
||||
for i := range posts {
|
||||
rows = append(rows, serializePostWithMedia(&posts[i], dbCtx, trs[i], media))
|
||||
}
|
||||
}
|
||||
wire.WriteJSON(w, http.StatusOK, lagoon.Paginate(rows, page, perPage, total))
|
||||
@@ -166,29 +177,47 @@ func Show(app *backpack.App) http.HandlerFunc {
|
||||
writeAPIError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
payload := serializePost(post, db)
|
||||
payload["previous_post"] = serializeSibling(previousPost(db, post))
|
||||
payload["next_post"] = serializeSibling(nextPost(db, post))
|
||||
// golem15.journal.related_mode: the default category mode answers as
|
||||
// it always has; scored (opt-in, P33-44) ranks the related posts by
|
||||
// shared tags and categories and adds the neighbours within the
|
||||
// post's first category (additive keys, scored mode only).
|
||||
relatedCfg := classes.ReadRelatedSettings(appConfig(app))
|
||||
var related []models.Post
|
||||
var prevInCat, nextInCat any
|
||||
if relatedCfg.Scored() {
|
||||
related, err = classes.ScoredRelatedPosts(r.Context(), db, post, relatedCfg.Count, isEditor(pr))
|
||||
if err != nil {
|
||||
writeAPIError(w, http.StatusInternalServerError, "Internal server error")
|
||||
return
|
||||
}
|
||||
payload["previous_in_category"] = serializeSibling(classes.CategoryNeighbour(r.Context(), db, post, true))
|
||||
payload["next_in_category"] = serializeSibling(classes.CategoryNeighbour(r.Context(), db, post, false))
|
||||
prevInCat = serializeSibling(classes.CategoryNeighbour(r.Context(), db, post, true))
|
||||
nextInCat = serializeSibling(classes.CategoryNeighbour(r.Context(), db, post, false))
|
||||
} else {
|
||||
related = relatedPosts(db, post, isEditor(pr))
|
||||
}
|
||||
// Batch-load all inline /media/<short-id>/... items across the post,
|
||||
// its translations, and all related posts in a single DB query (D-14).
|
||||
allPosts := make([]*models.Post, 0, 1+len(related))
|
||||
allPosts = append(allPosts, post)
|
||||
for i := range related {
|
||||
allPosts = append(allPosts, &related[i])
|
||||
}
|
||||
allTrs := make([]any, len(allPosts))
|
||||
for i, p := range allPosts {
|
||||
allTrs[i] = translationsOf(db, p)
|
||||
}
|
||||
media := batchLoadPostMedia(r.Context(), db, app, allPosts, allTrs)
|
||||
|
||||
payload := serializePostWithMedia(post, db, allTrs[0], media)
|
||||
payload["previous_post"] = serializeSibling(previousPost(db, post))
|
||||
payload["next_post"] = serializeSibling(nextPost(db, post))
|
||||
if relatedCfg.Scored() {
|
||||
payload["previous_in_category"] = prevInCat
|
||||
payload["next_in_category"] = nextInCat
|
||||
}
|
||||
rows := make([]map[string]any, 0, len(related))
|
||||
for i := range related {
|
||||
rows = append(rows, serializePost(&related[i], db))
|
||||
rows = append(rows, serializePostWithMedia(&related[i], db, allTrs[i+1], media))
|
||||
}
|
||||
payload["related_posts"] = rows
|
||||
// Additive (2026-10-28 Journal H1 setting): whether H1 in post
|
||||
@@ -294,7 +323,10 @@ func Store(app *backpack.App) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
purgeSiteCache(r, app, post)
|
||||
wire.WriteJSON(w, http.StatusCreated, h1.answer(r, app, map[string]any{"data": serializePost(post, db.WithContext(r.Context()))}))
|
||||
dbCtx := db.WithContext(r.Context())
|
||||
tr := translationsOf(dbCtx, post)
|
||||
media := batchLoadPostMedia(r.Context(), dbCtx, app, []*models.Post{post}, []any{tr})
|
||||
wire.WriteJSON(w, http.StatusCreated, h1.answer(r, app, map[string]any{"data": serializePostWithMedia(post, dbCtx, tr, media)}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,7 +401,10 @@ func Update(app *backpack.App) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
purgeSiteCache(r, app, post)
|
||||
wire.WriteJSON(w, http.StatusOK, h1.answer(r, app, map[string]any{"data": serializePost(post, db.WithContext(r.Context()))}))
|
||||
dbCtx := db.WithContext(r.Context())
|
||||
tr := translationsOf(dbCtx, post)
|
||||
media := batchLoadPostMedia(r.Context(), dbCtx, app, []*models.Post{post}, []any{tr})
|
||||
wire.WriteJSON(w, http.StatusOK, h1.answer(r, app, map[string]any{"data": serializePostWithMedia(post, dbCtx, tr, media)}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -642,7 +677,97 @@ func applyTagFilter(q, db *gorm.DB, input string) *gorm.DB {
|
||||
return q.Where("id IN (SELECT post_id FROM golem15_journal_posts_tags WHERE tag_id = ?)", tag.ID)
|
||||
}
|
||||
|
||||
type postMediaContext struct {
|
||||
items map[string]album.Item
|
||||
cfg *compass.Config
|
||||
}
|
||||
|
||||
func batchLoadPostMedia(ctx context.Context, db *gorm.DB, app *backpack.App, posts []*models.Post, translations []any) postMediaContext {
|
||||
cfg := appConfig(app)
|
||||
if db == nil || len(posts) == 0 {
|
||||
return postMediaContext{cfg: cfg}
|
||||
}
|
||||
var allIDs []string
|
||||
seen := make(map[string]struct{})
|
||||
collect := func(htmlDoc string) {
|
||||
for _, id := range classes.ExtractMediaShortIDsWithConfig(htmlDoc, cfg) {
|
||||
if _, ok := seen[id]; !ok {
|
||||
seen[id] = struct{}{}
|
||||
allIDs = append(allIDs, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range posts {
|
||||
if p == nil {
|
||||
continue
|
||||
}
|
||||
collect(p.ContentHTML)
|
||||
}
|
||||
for _, tr := range translations {
|
||||
m, ok := tr.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, rawAttrs := range m {
|
||||
attrs, ok := rawAttrs.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if s, ok := attrs["content_html"].(string); ok {
|
||||
collect(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(allIDs) == 0 {
|
||||
return postMediaContext{cfg: cfg}
|
||||
}
|
||||
svc, ok := album.ServiceFrom(app)
|
||||
if !ok {
|
||||
svc = album.NewService(cfg)
|
||||
}
|
||||
items, err := svc.GetItemsByShortIDs(ctx, db, allIDs)
|
||||
if err != nil {
|
||||
pluginLog.Warn("journal: load inline media items", "error", err)
|
||||
return postMediaContext{cfg: cfg}
|
||||
}
|
||||
return postMediaContext{items: items, cfg: cfg}
|
||||
}
|
||||
|
||||
func enrichTranslationsMedia(tr any, media postMediaContext) any {
|
||||
if len(media.items) == 0 {
|
||||
return tr
|
||||
}
|
||||
m, ok := tr.(map[string]any)
|
||||
if !ok || len(m) == 0 {
|
||||
return tr
|
||||
}
|
||||
out := make(map[string]any, len(m))
|
||||
for loc, rawAttrs := range m {
|
||||
attrs, ok := rawAttrs.(map[string]any)
|
||||
if !ok {
|
||||
out[loc] = rawAttrs
|
||||
continue
|
||||
}
|
||||
cloned := make(map[string]any, len(attrs))
|
||||
for k, v := range attrs {
|
||||
if k == "content_html" {
|
||||
if s, isStr := v.(string); isStr {
|
||||
cloned[k] = classes.EnrichMediaHTML(s, media.items, media.cfg)
|
||||
continue
|
||||
}
|
||||
}
|
||||
cloned[k] = v
|
||||
}
|
||||
out[loc] = cloned
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func serializePost(p *models.Post, db *gorm.DB) map[string]any {
|
||||
return serializePostWithMedia(p, db, nil, postMediaContext{})
|
||||
}
|
||||
|
||||
func serializePostWithMedia(p *models.Post, db *gorm.DB, preloadedTranslations any, media postMediaContext) map[string]any {
|
||||
if p == nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
@@ -656,13 +781,22 @@ func serializePost(p *models.Post, db *gorm.DB) map[string]any {
|
||||
}
|
||||
featured := featuredImages(db, p)
|
||||
featuredURL := firstImageURL(featured)
|
||||
tr := preloadedTranslations
|
||||
if tr == nil {
|
||||
tr = translationsOf(db, p)
|
||||
}
|
||||
contentHTML := p.ContentHTML
|
||||
if len(media.items) > 0 {
|
||||
contentHTML = classes.EnrichMediaHTML(contentHTML, media.items, media.cfg)
|
||||
tr = enrichTranslationsMedia(tr, media)
|
||||
}
|
||||
row := map[string]any{
|
||||
"id": p.ID,
|
||||
"title": p.Title,
|
||||
"slug": p.Slug,
|
||||
"excerpt": p.Excerpt,
|
||||
"content": p.Content,
|
||||
"content_html": p.ContentHTML,
|
||||
"content_html": contentHTML,
|
||||
"reading_time": readingTime(p.ContentHTML),
|
||||
"published": p.Published,
|
||||
"published_at": carbonTime(p.PublishedAt),
|
||||
@@ -674,7 +808,7 @@ func serializePost(p *models.Post, db *gorm.DB) map[string]any {
|
||||
"tags": tags,
|
||||
"featured_images": featured,
|
||||
"featured_image_url": featuredURL,
|
||||
"translations": translationsOf(db, p),
|
||||
"translations": tr,
|
||||
"created_at": carbonTime(p.CreatedAt),
|
||||
"updated_at": carbonTime(p.UpdatedAt),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user