Landwelle Gitea CI / Go tests and vet (push) Successful in 1m33s
Landwelle Gitea source ZIP / source-zip (push) Failing after 11m45s
Landwelle Gitea CI / JavaScript and deployment-script checks (push) Failing after 14m47s
Landwelle Gitea CI / Linux x64 production build (push) Failing after 14m53s
Replace the static excursions promo with eight published editorial links, category and date; share ordered query with existing story cards. Add template and PostgreSQL regression tests.
562 lines
22 KiB
Go
562 lines
22 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"regexp"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
"unicode"
|
||
)
|
||
|
||
type EditorialEntry struct {
|
||
ID int64 `json:"id"`
|
||
ContentType string `json:"content_type"`
|
||
ContentTypeLabel string `json:"content_type_label"`
|
||
Title string `json:"title"`
|
||
Slug string `json:"slug"`
|
||
Teaser string `json:"teaser"`
|
||
Body string `json:"body"`
|
||
Location string `json:"location"`
|
||
Status string `json:"status"`
|
||
AIGenerated bool `json:"ai_generated"`
|
||
AIUsage string `json:"ai_usage"`
|
||
AIHumanReviewed bool `json:"ai_human_reviewed"`
|
||
AIEditorialResponsible string `json:"ai_editorial_responsible"`
|
||
AIDisclosure string `json:"ai_disclosure"`
|
||
MediaAIUsage string `json:"media_ai_usage"`
|
||
MediaAIDisclosure string `json:"media_ai_disclosure"`
|
||
MediaWatermarkEnabled bool `json:"media_watermark_enabled"`
|
||
MediaWatermarkText string `json:"media_watermark_text"`
|
||
MediaAssetID int64 `json:"media_asset_id"`
|
||
MediaURL string `json:"media_url"`
|
||
MediaAlt string `json:"media_alt"`
|
||
PostToFacebook bool `json:"post_to_facebook"`
|
||
FacebookStatus string `json:"facebook_status"`
|
||
HomepageSection bool `json:"homepage_section"`
|
||
PublishedAt time.Time `json:"published_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
}
|
||
|
||
func (e EditorialEntry) EffectiveAIUsage() string {
|
||
usage := normalizeAIUsage(e.AIUsage)
|
||
if usage == "none" && e.AIGenerated {
|
||
return "assisted"
|
||
}
|
||
return usage
|
||
}
|
||
|
||
func (e EditorialEntry) HasAITransparency() bool { return e.EffectiveAIUsage() != "none" }
|
||
func (e EditorialEntry) AIShortLabel() string { return aiUsageShortLabel(e.EffectiveAIUsage()) }
|
||
func (e EditorialEntry) AITransparencyLabel() string { return aiUsageLabel(e.EffectiveAIUsage()) }
|
||
func (e EditorialEntry) AITransparencyText() string {
|
||
if text := strings.TrimSpace(e.AIDisclosure); text != "" {
|
||
return text
|
||
}
|
||
return defaultAIDisclosure(e.EffectiveAIUsage())
|
||
}
|
||
func (e EditorialEntry) AIReviewLabel() string {
|
||
if !e.HasAITransparency() {
|
||
return ""
|
||
}
|
||
if e.AIHumanReviewed {
|
||
if responsible := strings.TrimSpace(e.AIEditorialResponsible); responsible != "" {
|
||
return "Redaktionell geprüft · Verantwortlich: " + responsible
|
||
}
|
||
return "Redaktionell geprüft"
|
||
}
|
||
return "Keine redaktionelle Prüfung dokumentiert"
|
||
}
|
||
func (e EditorialEntry) HasMediaAITransparency() bool {
|
||
return normalizeMediaAIUsage(e.MediaAIUsage) != "none"
|
||
}
|
||
func (e EditorialEntry) MediaAITransparencyText() string {
|
||
if text := strings.TrimSpace(e.MediaAIDisclosure); text != "" {
|
||
return text
|
||
}
|
||
switch normalizeMediaAIUsage(e.MediaAIUsage) {
|
||
case "generated":
|
||
return "Dieses Bild wurde mithilfe künstlicher Intelligenz erstellt."
|
||
case "manipulated":
|
||
return "Dieses Bild wurde mithilfe künstlicher Intelligenz wesentlich bearbeitet."
|
||
default:
|
||
return ""
|
||
}
|
||
}
|
||
|
||
func (e EditorialEntry) HasMediaWatermark() bool {
|
||
return e.MediaWatermarkEnabled && normalizeMediaAIUsage(e.MediaAIUsage) != "none"
|
||
}
|
||
func (e EditorialEntry) MediaWatermarkLabel() string {
|
||
if !e.HasMediaWatermark() {
|
||
return ""
|
||
}
|
||
if text := strings.TrimSpace(e.MediaWatermarkText); text != "" {
|
||
return text
|
||
}
|
||
if normalizeMediaAIUsage(e.MediaAIUsage) == "manipulated" {
|
||
return "Mit KI bearbeitet"
|
||
}
|
||
return "Mit KI erstellt"
|
||
}
|
||
|
||
func (e EditorialEntry) UpdatedLabel() string {
|
||
if e.UpdatedAt.IsZero() {
|
||
return "–"
|
||
}
|
||
return e.UpdatedAt.Local().Format("02.01.2006 · 15:04")
|
||
}
|
||
|
||
func editorialLocation() *time.Location {
|
||
loc, err := time.LoadLocation("Europe/Berlin")
|
||
if err != nil {
|
||
return time.Local
|
||
}
|
||
return loc
|
||
}
|
||
func (e EditorialEntry) PublicationDate() string {
|
||
t := e.PublishedAt
|
||
if t.IsZero() {
|
||
t = time.Now()
|
||
}
|
||
return t.In(editorialLocation()).Format("2006-01-02")
|
||
}
|
||
func (e EditorialEntry) PublicationLabel() string {
|
||
return e.PublishedAt.In(editorialLocation()).Format("02.01.2006")
|
||
}
|
||
func (e EditorialEntry) IsScheduled() bool {
|
||
return e.Status == "published" && e.PublishedAt.After(time.Now())
|
||
}
|
||
func (e EditorialEntry) IsPublished() bool {
|
||
return e.Status == "published" && !e.PublishedAt.After(time.Now())
|
||
}
|
||
func (e EditorialEntry) DisplayStatus() string {
|
||
if e.IsScheduled() {
|
||
return "scheduled"
|
||
}
|
||
return e.Status
|
||
}
|
||
func parseEditorialPublicationDate(raw string, previous EditorialEntry, now time.Time) (time.Time, error) {
|
||
loc := editorialLocation()
|
||
if strings.TrimSpace(raw) == "" {
|
||
if !previous.PublishedAt.IsZero() {
|
||
return previous.PublishedAt, nil
|
||
}
|
||
return now, nil
|
||
}
|
||
t, err := time.ParseInLocation("2006-01-02", raw, loc)
|
||
if err != nil {
|
||
return time.Time{}, errors.New("Ungültiges Veröffentlichungsdatum")
|
||
}
|
||
if !previous.PublishedAt.IsZero() && previous.PublicationDate() == raw {
|
||
return previous.PublishedAt, nil
|
||
}
|
||
if raw == now.In(loc).Format("2006-01-02") {
|
||
return now, nil
|
||
}
|
||
return t, nil
|
||
}
|
||
|
||
func (e EditorialEntry) StatusLabel() string {
|
||
if e.IsScheduled() {
|
||
return "Geplant"
|
||
}
|
||
switch strings.TrimSpace(e.Status) {
|
||
case "draft":
|
||
return "Entwurf"
|
||
case "review":
|
||
return "In Prüfung"
|
||
case "published":
|
||
return "Veröffentlicht"
|
||
case "archived":
|
||
return "Archiviert"
|
||
default:
|
||
return e.Status
|
||
}
|
||
}
|
||
|
||
func (e EditorialEntry) FacebookStatusKind() string {
|
||
if !e.PostToFacebook {
|
||
return "off"
|
||
}
|
||
status := strings.TrimSpace(e.FacebookStatus)
|
||
switch {
|
||
case strings.HasPrefix(status, "published"):
|
||
return "published"
|
||
case strings.HasPrefix(status, "failed"):
|
||
return "failed"
|
||
case status == "sending":
|
||
return "sending"
|
||
default:
|
||
return "pending"
|
||
}
|
||
}
|
||
|
||
func (e EditorialEntry) FacebookStatusLabel() string {
|
||
switch e.FacebookStatusKind() {
|
||
case "off":
|
||
return "Facebook nicht ausgewählt"
|
||
case "published":
|
||
return "Auf Facebook veröffentlicht"
|
||
case "failed":
|
||
return "Facebook-Übertragung fehlgeschlagen"
|
||
case "sending":
|
||
return "Wird an Facebook übertragen"
|
||
default:
|
||
if !e.IsPublished() {
|
||
return "Wird nach Veröffentlichung gesendet"
|
||
}
|
||
return "Facebook-Versand steht aus"
|
||
}
|
||
}
|
||
|
||
func (e EditorialEntry) FacebookStatusDetail() string {
|
||
status := strings.TrimSpace(e.FacebookStatus)
|
||
if strings.HasPrefix(status, "published:") {
|
||
return strings.TrimSpace(strings.TrimPrefix(status, "published:"))
|
||
}
|
||
if strings.HasPrefix(status, "failed:") {
|
||
return strings.TrimSpace(strings.TrimPrefix(status, "failed:"))
|
||
}
|
||
return ""
|
||
}
|
||
|
||
func (e EditorialEntry) PublicPath() string {
|
||
category := strings.TrimSpace(e.ContentType)
|
||
slug := strings.TrimSpace(e.Slug)
|
||
if !e.HomepageSection || category == "" || slug == "" {
|
||
return ""
|
||
}
|
||
return "/landwelle-vor-ort/" + category + "/" + slug
|
||
}
|
||
|
||
func (e EditorialEntry) CategoryPath() string {
|
||
category := strings.TrimSpace(e.ContentType)
|
||
if !e.HomepageSection || category == "" {
|
||
return "/landwelle-vor-ort"
|
||
}
|
||
return "/landwelle-vor-ort/" + category
|
||
}
|
||
|
||
var slugInvalid = regexp.MustCompile(`[^a-z0-9]+`)
|
||
|
||
func slugify(title string) string {
|
||
title = strings.ToLower(strings.TrimSpace(title))
|
||
r := strings.NewReplacer("ä", "ae", "ö", "oe", "ü", "ue", "ß", "ss")
|
||
title = r.Replace(title)
|
||
var b strings.Builder
|
||
for _, ch := range title {
|
||
if unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == ' ' || ch == '-' {
|
||
b.WriteRune(ch)
|
||
}
|
||
}
|
||
slug := strings.Trim(slugInvalid.ReplaceAllString(b.String(), "-"), "-")
|
||
if slug == "" {
|
||
slug = "inhalt-" + strconv.FormatInt(time.Now().Unix(), 10)
|
||
}
|
||
return slug
|
||
}
|
||
|
||
type EditorialContentType struct {
|
||
Slug string `json:"slug"`
|
||
Name string `json:"name"`
|
||
Description string `json:"description"`
|
||
HomepageSection bool `json:"homepage_section"`
|
||
Active bool `json:"active"`
|
||
SortOrder int `json:"sort_order"`
|
||
}
|
||
|
||
func (c EditorialContentType) PublicPath() string {
|
||
slug := strings.TrimSpace(c.Slug)
|
||
if !c.Active || !c.HomepageSection || slug == "" {
|
||
return "/landwelle-vor-ort"
|
||
}
|
||
return "/landwelle-vor-ort/" + slug
|
||
}
|
||
|
||
type editorialStore struct{ db *postgresStore }
|
||
|
||
func newEditorialStore(db *postgresStore) *editorialStore { return &editorialStore{db: db} }
|
||
func (s *editorialStore) Enabled() bool { return s != nil && s.db != nil && s.db.Enabled() }
|
||
|
||
const editorialJSON = `'id',e.id,'content_type',e.content_type,'content_type_label',COALESCE(ct.name,e.content_type),'homepage_section',COALESCE(ct.homepage_section,false),'title',e.title,'slug',e.slug,'teaser',e.teaser,'body',e.body,'location',e.location,'status',e.status,'ai_generated',e.ai_generated,'ai_usage',COALESCE(e.ai_usage,'none'),'ai_human_reviewed',COALESCE(e.ai_human_reviewed,false),'ai_editorial_responsible',COALESCE(e.ai_editorial_responsible,''),'ai_disclosure',COALESCE(e.ai_disclosure,''),'media_asset_id',COALESCE(e.media_asset_id,0),'media_url',CASE WHEN m.storage_key IS NULL THEN '' ELSE '/media/'||m.storage_key END,'media_alt',COALESCE(m.alt_text,''),'media_ai_usage',COALESCE(m.ai_usage,'none'),'media_ai_disclosure',COALESCE(m.ai_disclosure,''),'media_watermark_enabled',COALESCE(m.ai_watermark_enabled,false),'media_watermark_text',COALESCE(m.ai_watermark_text,''),'post_to_facebook',e.post_to_facebook,'facebook_status',e.facebook_status,'published_at',COALESCE(e.published_at,e.updated_at),'updated_at',e.updated_at`
|
||
|
||
func (s *editorialStore) List(ctx context.Context) ([]EditorialEntry, error) {
|
||
if !s.Enabled() {
|
||
return nil, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
q := `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `) ORDER BY e.updated_at DESC),'[]'::json)::text FROM landwelle.editorial_entries e LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id LEFT JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type;`
|
||
return items, s.db.queryJSON(ctx, q, &items)
|
||
}
|
||
func (s *editorialStore) Get(ctx context.Context, id int64) (EditorialEntry, bool, error) {
|
||
if !s.Enabled() {
|
||
return EditorialEntry{}, false, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
q := `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `)),'[]'::json)::text FROM landwelle.editorial_entries e LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id LEFT JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type WHERE e.id=` + strconv.FormatInt(id, 10) + `;`
|
||
if err := s.db.queryJSON(ctx, q, &items); err != nil {
|
||
return EditorialEntry{}, false, err
|
||
}
|
||
if len(items) == 0 {
|
||
return EditorialEntry{}, false, nil
|
||
}
|
||
return items[0], true, nil
|
||
}
|
||
func (s *editorialStore) Save(ctx context.Context, e EditorialEntry) error {
|
||
if !s.Enabled() {
|
||
return errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
e.ContentType = strings.TrimSpace(e.ContentType)
|
||
e.Title = strings.TrimSpace(e.Title)
|
||
e.Teaser = strings.TrimSpace(e.Teaser)
|
||
e.Body = strings.TrimSpace(e.Body)
|
||
e.Location = strings.TrimSpace(e.Location)
|
||
e.Status = strings.TrimSpace(e.Status)
|
||
e.AIUsage = normalizeAIUsage(e.AIUsage)
|
||
if e.AIUsage == "none" && e.AIGenerated {
|
||
e.AIUsage = "assisted"
|
||
}
|
||
e.AIGenerated = e.AIUsage != "none"
|
||
e.AIEditorialResponsible = compactText(e.AIEditorialResponsible)
|
||
e.AIDisclosure = strings.TrimSpace(e.AIDisclosure)
|
||
if e.AIUsage != "none" && e.AIEditorialResponsible == "" {
|
||
e.AIEditorialResponsible = "Landwelle Redaktion"
|
||
}
|
||
if e.Title == "" {
|
||
return errors.New("der Titel fehlt")
|
||
}
|
||
e.Slug = slugify(e.Title)
|
||
validType, err := s.ContentTypeExists(ctx, e.ContentType)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !validType {
|
||
return errors.New("ungültiger oder inaktiver Inhaltstyp")
|
||
}
|
||
allowedStatus := map[string]bool{"draft": true, "review": true, "published": true, "archived": true}
|
||
if !allowedStatus[e.Status] {
|
||
return errors.New("ungültiger Status")
|
||
}
|
||
publicationSQL := "now()"
|
||
if !e.PublishedAt.IsZero() {
|
||
publicationSQL = sqlText(e.PublishedAt.UTC().Format(time.RFC3339Nano)) + "::timestamptz"
|
||
}
|
||
mediaSQL := "NULL"
|
||
if e.MediaAssetID > 0 {
|
||
mediaSQL = strconv.FormatInt(e.MediaAssetID, 10)
|
||
}
|
||
if e.ID == 0 {
|
||
q := `INSERT INTO landwelle.editorial_entries(content_type,title,slug,teaser,body,location,status,ai_generated,ai_usage,ai_human_reviewed,ai_editorial_responsible,ai_disclosure,media_asset_id,published_at) VALUES (` + sqlText(e.ContentType) + `,` + sqlText(e.Title) + `,` + sqlText(e.Slug) + `,` + sqlText(e.Teaser) + `,` + sqlText(e.Body) + `,` + sqlText(e.Location) + `,` + sqlText(e.Status) + `,` + sqlBool(e.AIGenerated) + `,` + sqlText(e.AIUsage) + `,` + sqlBool(e.AIHumanReviewed) + `,` + sqlText(e.AIEditorialResponsible) + `,` + sqlText(e.AIDisclosure) + `,` + mediaSQL + `,` + publicationSQL + `);`
|
||
_, err := s.db.exec(ctx, q)
|
||
return err
|
||
}
|
||
q := `UPDATE landwelle.editorial_entries SET content_type=` + sqlText(e.ContentType) + `,title=` + sqlText(e.Title) + `,slug=` + sqlText(e.Slug) + `,teaser=` + sqlText(e.Teaser) + `,body=` + sqlText(e.Body) + `,location=` + sqlText(e.Location) + `,status=` + sqlText(e.Status) + `,ai_generated=` + sqlBool(e.AIGenerated) + `,ai_usage=` + sqlText(e.AIUsage) + `,ai_human_reviewed=` + sqlBool(e.AIHumanReviewed) + `,ai_editorial_responsible=` + sqlText(e.AIEditorialResponsible) + `,ai_disclosure=` + sqlText(e.AIDisclosure) + `,media_asset_id=` + mediaSQL + `,published_at=` + publicationSQL + `,updated_at=now() WHERE id=` + strconv.FormatInt(e.ID, 10) + `;`
|
||
_, err = s.db.exec(ctx, q)
|
||
return err
|
||
}
|
||
|
||
func (s *editorialStore) SaveAndGet(ctx context.Context, e EditorialEntry) (EditorialEntry, error) {
|
||
if err := s.Save(ctx, e); err != nil {
|
||
return EditorialEntry{}, err
|
||
}
|
||
if e.ID > 0 {
|
||
item, found, err := s.Get(ctx, e.ID)
|
||
if err != nil {
|
||
return EditorialEntry{}, err
|
||
}
|
||
if !found {
|
||
return EditorialEntry{}, errors.New("gespeicherter Inhalt konnte nicht erneut geladen werden")
|
||
}
|
||
return item, nil
|
||
}
|
||
item, found, err := s.GetBySlug(ctx, slugify(e.Title))
|
||
if err != nil {
|
||
return EditorialEntry{}, err
|
||
}
|
||
if !found {
|
||
return EditorialEntry{}, errors.New("gespeicherter Inhalt konnte nicht erneut geladen werden")
|
||
}
|
||
return item, nil
|
||
}
|
||
|
||
func (s *editorialStore) Delete(ctx context.Context, id int64) error {
|
||
if !s.Enabled() {
|
||
return errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
_, err := s.db.exec(ctx, "DELETE FROM landwelle.editorial_entries WHERE id="+strconv.FormatInt(id, 10)+";")
|
||
return err
|
||
}
|
||
|
||
func (s *editorialStore) ContentTypes(ctx context.Context, includeInactive bool) ([]EditorialContentType, error) {
|
||
if !s.Enabled() {
|
||
return nil, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
where := "WHERE active=true"
|
||
if includeInactive {
|
||
where = ""
|
||
}
|
||
query := `SELECT COALESCE(json_agg(row_to_json(t) ORDER BY t.sort_order,t.name),'[]'::json)::text FROM (SELECT slug,name,description,homepage_section,active,sort_order FROM landwelle.editorial_content_types ` + where + `) t;`
|
||
var types []EditorialContentType
|
||
if err := s.db.queryJSON(ctx, query, &types); err != nil {
|
||
return nil, err
|
||
}
|
||
return types, nil
|
||
}
|
||
|
||
func (s *editorialStore) ContentTypeExists(ctx context.Context, slug string) (bool, error) {
|
||
if !s.Enabled() {
|
||
return false, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var rows []struct {
|
||
Count int `json:"count"`
|
||
}
|
||
query := `SELECT json_build_array(json_build_object('count',count(*)::int))::text FROM landwelle.editorial_content_types WHERE slug=` + sqlText(strings.TrimSpace(slug)) + ` AND active=true;`
|
||
if err := s.db.queryJSON(ctx, query, &rows); err != nil {
|
||
return false, err
|
||
}
|
||
return len(rows) > 0 && rows[0].Count > 0, nil
|
||
}
|
||
|
||
func (s *editorialStore) SaveContentType(ctx context.Context, item EditorialContentType) error {
|
||
if !s.Enabled() {
|
||
return errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
item.Name = strings.TrimSpace(item.Name)
|
||
if item.Name == "" {
|
||
return errors.New("Name der Kategorie fehlt")
|
||
}
|
||
item.Slug = slugify(item.Slug)
|
||
if item.Slug == "" || strings.HasPrefix(item.Slug, "inhalt-") {
|
||
item.Slug = slugify(item.Name)
|
||
}
|
||
_, err := s.db.exec(ctx, `INSERT INTO landwelle.editorial_content_types(slug,name,description,homepage_section,active,sort_order) VALUES(`+sqlText(item.Slug)+`,`+sqlText(item.Name)+`,`+sqlText(strings.TrimSpace(item.Description))+`,`+sqlBool(item.HomepageSection)+`,`+sqlBool(item.Active)+`,`+strconv.Itoa(item.SortOrder)+`) ON CONFLICT(slug) DO UPDATE SET name=excluded.name,description=excluded.description,homepage_section=excluded.homepage_section,active=excluded.active,sort_order=excluded.sort_order,updated_at=now();`)
|
||
return err
|
||
}
|
||
|
||
func (s *editorialStore) DeleteContentType(ctx context.Context, slug string) error {
|
||
if !s.Enabled() {
|
||
return errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
_, err := s.db.exec(ctx, `DELETE FROM landwelle.editorial_content_types WHERE slug=`+sqlText(strings.TrimSpace(slug))+`;`)
|
||
if err != nil {
|
||
return errors.New("Die Kategorie wird noch verwendet und kann nicht gelöscht werden")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func homepageStoriesQuery(limit int) string {
|
||
limitClause := ""
|
||
if limit > 0 {
|
||
limitClause = " LIMIT " + strconv.Itoa(limit)
|
||
}
|
||
return `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `) ORDER BY COALESCE(e.published_at,e.updated_at) DESC, e.id DESC),'[]'::json)::text FROM (SELECT e.* FROM landwelle.editorial_entries e JOIN landwelle.editorial_content_types type_filter ON type_filter.slug=e.content_type WHERE e.status='published' AND COALESCE(e.published_at,e.updated_at)<=now() AND type_filter.active=true AND type_filter.homepage_section=true ORDER BY COALESCE(e.published_at,e.updated_at) DESC, e.id DESC` + limitClause + `) e LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id LEFT JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type;`
|
||
}
|
||
|
||
func (s *editorialStore) HomepageStories(ctx context.Context, limit int) ([]EditorialEntry, error) {
|
||
if !s.Enabled() {
|
||
return nil, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
if limit < 1 || limit > 12 {
|
||
limit = 3
|
||
}
|
||
var items []EditorialEntry
|
||
if err := s.db.queryJSON(ctx, homepageStoriesQuery(limit), &items); err != nil {
|
||
return nil, err
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
// AllHomepageStories liefert alle veröffentlichten Beiträge aus den für
|
||
// „Landwelle vor Ort“ freigegebenen Rubriken. Die Startseite verwendet
|
||
// weiterhin HomepageStories mit einem kleinen Limit.
|
||
func (s *editorialStore) AllHomepageStories(ctx context.Context) ([]EditorialEntry, error) {
|
||
if !s.Enabled() {
|
||
return nil, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
if err := s.db.queryJSON(ctx, homepageStoriesQuery(0), &items); err != nil {
|
||
return nil, err
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
// HomepageContentTypes liefert die aktiven Kategorien, die öffentlich unter
|
||
// „Landwelle vor Ort“ erscheinen dürfen.
|
||
func (s *editorialStore) HomepageContentTypes(ctx context.Context) ([]EditorialContentType, error) {
|
||
types, err := s.ContentTypes(ctx, false)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
items := make([]EditorialContentType, 0, len(types))
|
||
for _, item := range types {
|
||
if item.Active && item.HomepageSection {
|
||
items = append(items, item)
|
||
}
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
func homepageStoriesByCategoryQuery(category string) string {
|
||
return `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `) ORDER BY COALESCE(e.published_at,e.updated_at) DESC),'[]'::json)::text FROM landwelle.editorial_entries e JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id WHERE e.status='published' AND COALESCE(e.published_at,e.updated_at)<=now() AND ct.active=true AND ct.homepage_section=true AND e.content_type=` + sqlText(strings.TrimSpace(category)) + `;`
|
||
}
|
||
|
||
// HomepageStoriesByCategory liefert alle veröffentlichten Vor-Ort-Beiträge
|
||
// einer freigegebenen Kategorie ohne künstliches Limit.
|
||
func (s *editorialStore) HomepageStoriesByCategory(ctx context.Context, category string) ([]EditorialEntry, error) {
|
||
if !s.Enabled() {
|
||
return nil, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
if err := s.db.queryJSON(ctx, homepageStoriesByCategoryQuery(category), &items); err != nil {
|
||
return nil, err
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
func (s *editorialStore) HomepageStoryBySlug(ctx context.Context, slug string) (EditorialEntry, bool, error) {
|
||
if !s.Enabled() {
|
||
return EditorialEntry{}, false, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
query := `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `)),'[]'::json)::text FROM landwelle.editorial_entries e JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id WHERE e.slug=` + sqlText(strings.TrimSpace(slug)) + ` AND e.status='published' AND COALESCE(e.published_at,e.updated_at)<=now() AND ct.active=true AND ct.homepage_section=true LIMIT 1;`
|
||
if err := s.db.queryJSON(ctx, query, &items); err != nil {
|
||
return EditorialEntry{}, false, err
|
||
}
|
||
if len(items) == 0 {
|
||
return EditorialEntry{}, false, nil
|
||
}
|
||
return items[0], true, nil
|
||
}
|
||
|
||
func (s *editorialStore) HomepageStoryByCategoryAndSlug(ctx context.Context, category, slug string) (EditorialEntry, bool, error) {
|
||
if !s.Enabled() {
|
||
return EditorialEntry{}, false, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
query := `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `)),'[]'::json)::text FROM landwelle.editorial_entries e JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id WHERE e.content_type=` + sqlText(strings.TrimSpace(category)) + ` AND e.slug=` + sqlText(strings.TrimSpace(slug)) + ` AND e.status='published' AND COALESCE(e.published_at,e.updated_at)<=now() AND ct.active=true AND ct.homepage_section=true LIMIT 1;`
|
||
if err := s.db.queryJSON(ctx, query, &items); err != nil {
|
||
return EditorialEntry{}, false, err
|
||
}
|
||
if len(items) == 0 {
|
||
return EditorialEntry{}, false, nil
|
||
}
|
||
return items[0], true, nil
|
||
}
|
||
|
||
func (s *editorialStore) GetBySlug(ctx context.Context, slug string) (EditorialEntry, bool, error) {
|
||
if !s.Enabled() {
|
||
return EditorialEntry{}, false, errors.New("PostgreSQL ist nicht konfiguriert")
|
||
}
|
||
var items []EditorialEntry
|
||
q := `SELECT COALESCE(json_agg(json_build_object(` + editorialJSON + `)),'[]'::json)::text FROM landwelle.editorial_entries e LEFT JOIN landwelle.media_assets m ON m.id=e.media_asset_id LEFT JOIN landwelle.editorial_content_types ct ON ct.slug=e.content_type WHERE e.slug=` + sqlText(slug) + ` LIMIT 1;`
|
||
if err := s.db.queryJSON(ctx, q, &items); err != nil {
|
||
return EditorialEntry{}, false, err
|
||
}
|
||
if len(items) == 0 {
|
||
return EditorialEntry{}, false, nil
|
||
}
|
||
return items[0], true, nil
|
||
}
|