63 lines
1.7 KiB
Go
63 lines
1.7 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)
|
|
ids, err := s.store.PeriodicalProjectionTargets(r.Context(), source, kind)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if 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
|
|
}
|