Files
pages/internal/server/periodical_share.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

74 lines
1.9 KiB
Go

package server
import (
"context"
"net/http"
"sort"
"time"
)
type periodicalShareTarget struct {
Title string `json:"title"`
URL string `json:"url"`
}
// Share URLs come from controller-configured local projections, not request
// headers or caller supplied URLs. Publication is checked separately.
func (s *Server) periodicalShareTargets(r *http.Request, kind, path string) []periodicalShareTarget {
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
r = r.WithContext(ctx)
source := s.domainUUIDForRequest(r)
rows, err := s.store.DB.QueryContext(r.Context(), `SELECT domain_uuid FROM periodical_projections WHERE source_uuid=? AND kind=? ORDER BY domain_uuid LIMIT 65`, source, kind)
if err != nil {
return nil
}
ids := []string{}
for rows.Next() {
var id string
if err = rows.Scan(&id); err != nil {
rows.Close()
return nil
}
ids = append(ids, id)
}
err = rows.Err()
rows.Close()
if err != nil || len(ids) > 64 {
return nil
}
out := []periodicalShareTarget{}
for _, id := range ids {
projection, err := s.store.PeriodicalProjection(r.Context(), id, kind)
if err != nil || projection == nil || s.homeLayout(id) != kind {
continue
}
href := projectedArticleHref(projection.Prefix, path)
if href == "" {
continue
}
on, err := s.store.PeriodicalEnabled(r.Context(), id, kind)
if err != nil || !on {
continue
}
host, err := s.store.GetDomainEffectiveHost(id)
if err != nil || host == "" {
continue
}
target := r.Clone(r.Context())
target.Host = host
origin := s.socialOrigin(target)
if origin == "" {
continue
}
out = append(out, periodicalShareTarget{Title: s.getWikiName(id), URL: origin + href})
}
sort.Slice(out, func(i, j int) bool { return out[i].URL < out[j].URL })
if len(ids) == 0 {
if origin := s.socialOrigin(r); origin != "" {
out = append(out, periodicalShareTarget{Title: s.getWikiName(source), URL: origin + cityPageHref(path)})
}
}
return out
}