Extend the publishing pipeline to generate category index pages, per-category archive pages, per-category RSS/JSON feeds, and display categories on individual post pages and post lists. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package publisher
|
|
|
|
import (
|
|
"context"
|
|
"iter"
|
|
|
|
"lmika.dev/lmika/weiro/models"
|
|
"lmika.dev/lmika/weiro/providers/db"
|
|
)
|
|
|
|
// postIter returns a post iterator which returns posts in reverse chronological order.
|
|
func (s *Publisher) postIter(ctx context.Context, site int64) iter.Seq[models.Maybe[*models.Post]] {
|
|
return func(yield func(models.Maybe[*models.Post]) bool) {
|
|
paging := db.PagingParams{Offset: 0, Limit: 50}
|
|
page, err := s.db.SelectPostsOfSite(ctx, site, false, paging)
|
|
if err != nil {
|
|
yield(models.Maybe[*models.Post]{Err: err})
|
|
return
|
|
}
|
|
|
|
for {
|
|
for _, post := range page {
|
|
if post.State != models.StatePublished {
|
|
continue
|
|
}
|
|
|
|
if !yield(models.Maybe[*models.Post]{Value: post}) {
|
|
return
|
|
}
|
|
}
|
|
paging.Offset += paging.Limit
|
|
page, err = s.db.SelectPostsOfSite(ctx, site, false, paging)
|
|
if err != nil {
|
|
yield(models.Maybe[*models.Post]{Err: err})
|
|
return
|
|
} else if len(page) == 0 {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// postIterByCategory returns a post iterator for posts in a specific category.
|
|
func (s *Publisher) postIterByCategory(ctx context.Context, categoryID int64) iter.Seq[models.Maybe[*models.Post]] {
|
|
return func(yield func(models.Maybe[*models.Post]) bool) {
|
|
paging := db.PagingParams{Offset: 0, Limit: 50}
|
|
for {
|
|
page, err := s.db.SelectPostsOfCategory(ctx, categoryID, paging)
|
|
if err != nil {
|
|
yield(models.Maybe[*models.Post]{Err: err})
|
|
return
|
|
}
|
|
if len(page) == 0 {
|
|
return
|
|
}
|
|
for _, post := range page {
|
|
if !yield(models.Maybe[*models.Post]{Value: post}) {
|
|
return
|
|
}
|
|
}
|
|
paging.Offset += paging.Limit
|
|
}
|
|
}
|
|
}
|