Add Atom feed (not yet linked)

This commit is contained in:
Ben Visness 2023-06-09 13:01:51 -07:00
parent 8be575875d
commit c8808e21bf
8 changed files with 103 additions and 0 deletions

View File

@ -422,9 +422,15 @@ func TestTimeMachine(t *testing.T) {
func TestTimeMachineSubmissions(t *testing.T) {
AssertRegexMatch(t, BuildTimeMachineSubmissions(), RegexTimeMachineSubmissions, nil)
AssertRegexMatch(t, BuildTimeMachineSubmission(123), RegexTimeMachineSubmissions, nil)
AssertSubdomain(t, BuildTimeMachineSubmissions(), "")
}
func TestTimeMachineAtomFeed(t *testing.T) {
AssertRegexMatch(t, BuildTimeMachineAtomFeed(), RegexTimeMachineAtomFeed, nil)
AssertSubdomain(t, BuildTimeMachineAtomFeed(), "")
}
func TestTimeMachineForm(t *testing.T) {
AssertRegexMatch(t, BuildTimeMachineForm(), RegexTimeMachineForm, nil)
AssertSubdomain(t, BuildTimeMachineForm(), "")

View File

@ -126,6 +126,18 @@ func BuildTimeMachineSubmissions() string {
return Url("/timemachine/submissions", nil)
}
func BuildTimeMachineSubmission(id int) string {
defer CatchPanic()
return UrlWithFragment("/timemachine/submissions", nil, strconv.Itoa(id))
}
var RegexTimeMachineAtomFeed = regexp.MustCompile("^/timemachine/submissions/atom$")
func BuildTimeMachineAtomFeed() string {
defer CatchPanic()
return Url("/timemachine/submissions/atom", nil)
}
var RegexTimeMachineForm = regexp.MustCompile("^/timemachine/submit$")
func BuildTimeMachineForm() string {

View File

@ -177,6 +177,8 @@
margin: 0.2rem 0;
}
</style>
{{ block "extrahead" . }}{{ end }}
</head>
<body>

View File

@ -0,0 +1,36 @@
{{ noescape "<?xml version=\"1.0\" encoding=\"utf-8\"?>" }}
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://handmade.network/timemachine/submissions</id>
<title type="text">Time Machine | Handmade Network</title>
<subtitle>This summer, dig out your old devices and see what they were actually like to use.</subtitle>
<updated>{{ rfc3339 .Updated }}</updated>
<link href="{{ .TimeMachineUrl }}"/>
<link rel="self" type="application/atom+xml" href="{{ .AtomFeedUrl }}"/>
<link rel="alternate" type="text/html" hreflang="en" href="{{ .SubmissionsUrl }}"/>
<author>
<name>Handmade Network</name>
<email>team@handmade.network</email>
<uri>https://handmade.network/</uri>
</author>
<logo>{{ .LogoUrl }}</logo>
<rights>© 2023 Handmade Network</rights>
{{ range .Submissions }}
<entry>
<id>{{ .Permalink }}</id>
<title>{{ .Title }}</title>
<link rel="alternate" type="text/html" href="{{ .Permalink }}" />
<published>{{ rfc3339 .Date }}</published>
<updated>{{ rfc3339 .Date }}</updated>
<content type="html">
{{ with .Details }}
&lt;ul&gt;
{{ range . }}
&lt;li&gt;&lt;b&gt;{{ .Name }}:&lt;/b&gt; {{ yesescape .Content }}&lt;/li&gt;
{{ end }}
&lt;/ul&gt;
{{ end }}
{{ yesescape .Description }}
</content>
</entry>
{{ end }}
</feed>

View File

@ -1,5 +1,9 @@
{{ template "timemachine_base.html" . }}
{{ define "extrahead" }}
<link rel="alternate" type="application/atom+xml" title="Submission Feed" href="{{ .AtomFeedUrl }}">
{{ end }}
{{ define "content" }}
<div class="center-layout content mw7 ph3 flex flex-column g3">
<div>

View File

@ -255,6 +255,9 @@ var HMNTemplateFuncs = template.FuncMap{
"noescape": func(str string) template.HTML {
return template.HTML(str)
},
"yesescape": func(html template.HTML) string {
return string(html)
},
"filesize": func(numBytes int) string {
scales := []string{
" bytes",

View File

@ -72,6 +72,7 @@ func NewWebsiteRoutes(conn *pgxpool.Pool) http.Handler {
hmnOnly.GET(hmnurl.RegexTimeMachine, TimeMachine)
hmnOnly.GET(hmnurl.RegexTimeMachineSubmissions, TimeMachineSubmissions)
hmnOnly.GET(hmnurl.RegexTimeMachineAtomFeed, TimeMachineAtomFeed)
hmnOnly.GET(hmnurl.RegexTimeMachineForm, needsAuth(TimeMachineForm))
hmnOnly.GET(hmnurl.RegexTimeMachineFormDone, needsAuth(TimeMachineFormDone))
hmnOnly.POST(hmnurl.RegexTimeMachineForm, needsAuth(csrfMiddleware(TimeMachineFormSubmit)))

View File

@ -4,6 +4,7 @@ import (
"html/template"
"net/http"
"strings"
"time"
"git.handmade.network/hmn/hmn/src/email"
"git.handmade.network/hmn/hmn/src/hmnurl"
@ -67,12 +68,14 @@ func TimeMachineSubmissions(c *RequestContext) ResponseData {
templates.BaseData
MainUrl string
SubmitUrl string
AtomFeedUrl string
Submissions []TimeMachineSubmission
}
tmpl := TemplateData{
BaseData: baseData,
MainUrl: hmnurl.BuildTimeMachine(),
SubmitUrl: hmnurl.BuildTimeMachineForm(),
AtomFeedUrl: hmnurl.BuildTimeMachineAtomFeed(),
Submissions: tmSubmissions,
}
@ -139,10 +142,39 @@ func TimeMachineFormDone(c *RequestContext) ResponseData {
return res
}
func TimeMachineAtomFeed(c *RequestContext) ResponseData {
type TemplateData struct {
Updated time.Time
TimeMachineUrl string
SubmissionsUrl string
AtomFeedUrl string
LogoUrl string
Submissions []TimeMachineSubmission
}
tmpl := TemplateData{
Updated: tmSubmissions[0].Date,
TimeMachineUrl: hmnurl.BuildTimeMachine(),
SubmissionsUrl: hmnurl.BuildTimeMachineSubmissions(),
AtomFeedUrl: hmnurl.BuildTimeMachineAtomFeed(),
LogoUrl: hmnurl.BuildPublic("timemachine/twittercard.png", true),
Submissions: tmSubmissions,
}
var res ResponseData
res.MustWriteTemplate(
"timemachine_atom.xml",
tmpl,
c.Perf,
)
return res
}
type TimeMachineSubmission struct {
Date time.Time
Title string
Url string
Thumbnail string
Permalink string // generated for feed
Details []TimeMachineSubmissionDetail
Description template.HTML
}
@ -154,6 +186,7 @@ type TimeMachineSubmissionDetail struct {
var tmSubmissions = []TimeMachineSubmission{
{
Date: time.Date(2023, 6, 6, 0, 0, 0, 0, time.UTC),
Title: "2009 iPod Touch",
Url: "https://youtu.be/2eBFk1yV6mE",
Thumbnail: "timemachine/ipodtouch-dither.gif",
@ -188,3 +221,9 @@ var tmSubmissions = []TimeMachineSubmission{
</p>`,
},
}
func init() {
for i := range tmSubmissions {
tmSubmissions[i].Permalink = hmnurl.BuildTimeMachineSubmission(len(tmSubmissions) - i)
}
}