From 20c05637d9fd2209b7b8ce98dc5705fdbe767e29 Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Tue, 17 Aug 2021 22:48:44 +0300 Subject: [PATCH] Added whenisit and default opengraph items --- src/hmnurl/hmnurl_test.go | 4 + src/hmnurl/urls.go | 7 ++ src/templates/src/whenisit.html | 138 ++++++++++++++++++++++++++ src/templates/src/whenisit_setup.html | 65 ++++++++++++ src/website/routes.go | 12 +++ src/website/snippet.go | 2 +- src/website/whenisit.go | 68 +++++++++++++ 7 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 src/templates/src/whenisit.html create mode 100644 src/templates/src/whenisit_setup.html create mode 100644 src/website/whenisit.go diff --git a/src/hmnurl/hmnurl_test.go b/src/hmnurl/hmnurl_test.go index 42713444..711fe170 100644 --- a/src/hmnurl/hmnurl_test.go +++ b/src/hmnurl/hmnurl_test.go @@ -44,6 +44,10 @@ func TestSiteMap(t *testing.T) { AssertRegexMatch(t, BuildSiteMap(), RegexSiteMap, nil) } +func TestWhenIsIt(t *testing.T) { + AssertRegexMatch(t, BuildWhenIsIt(), RegexWhenIsIt, nil) +} + func TestAtomFeed(t *testing.T) { AssertRegexMatch(t, BuildAtomFeed(), RegexAtomFeed, nil) AssertRegexMatch(t, BuildAtomFeedForProjects(), RegexAtomFeed, map[string]string{"feedtype": "projects"}) diff --git a/src/hmnurl/urls.go b/src/hmnurl/urls.go index 9e0a0724..f71960c0 100644 --- a/src/hmnurl/urls.go +++ b/src/hmnurl/urls.go @@ -49,6 +49,13 @@ func BuildSiteMap() string { return Url("/sitemap", nil) } +var RegexWhenIsIt = regexp.MustCompile("^/whenisit$") + +func BuildWhenIsIt() string { + defer CatchPanic() + return Url("/whenisit", nil) +} + // QUESTION(ben): Can we change these routes? var RegexLoginAction = regexp.MustCompile("^/login$") diff --git a/src/templates/src/whenisit.html b/src/templates/src/whenisit.html new file mode 100644 index 00000000..b93be77e --- /dev/null +++ b/src/templates/src/whenisit.html @@ -0,0 +1,138 @@ +{{ template "base.html" . }} + +{{ define "extrahead" }} + +{{ end }} + +{{ define "content" }} +
+

+ +

+

+

+

ago

+
+
+ +{{ end }} diff --git a/src/templates/src/whenisit_setup.html b/src/templates/src/whenisit_setup.html new file mode 100644 index 00000000..f4d8b05c --- /dev/null +++ b/src/templates/src/whenisit_setup.html @@ -0,0 +1,65 @@ +{{ template "base.html" . }} + +{{ define "extrahead" }} + +{{ end }} + +{{ define "content" }} +
+

Make a timer

+
+
+
+
+ +
+ +{{ end }} diff --git a/src/website/routes.go b/src/website/routes.go index ba8ff09b..1f4d385b 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -160,6 +160,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe staticPages.GET(hmnurl.RegexContactPage, ContactPage) staticPages.GET(hmnurl.RegexMonthlyUpdatePolicy, MonthlyUpdatePolicy) staticPages.GET(hmnurl.RegexProjectSubmissionGuidelines, ProjectSubmissionGuidelines) + staticPages.GET(hmnurl.RegexWhenIsIt, WhenIsIt) // TODO(asaf): Have separate middleware for HMN-only routes and any-project routes // NOTE(asaf): HMN-only routes: @@ -269,6 +270,8 @@ func getBaseData(c *RequestContext) templates.BaseData { Session: templateSession, Notices: notices, + OpenGraphItems: buildDefaultOpenGraphItems(c.CurrentProject), + IsProjectPage: !c.CurrentProject.IsHMN(), Header: templates.Header{ AdminUrl: hmnurl.BuildHomepage(), // TODO(asaf) @@ -301,6 +304,15 @@ func getBaseData(c *RequestContext) templates.BaseData { } } +func buildDefaultOpenGraphItems(project *models.Project) []templates.OpenGraphItem { + return []templates.OpenGraphItem{ + {Property: "og:site_name", Value: "Handmade.Network"}, + {Property: "og:type", Value: "website"}, + {Property: "og:image", Value: hmnurl.BuildUserFile(project.LogoLight)}, + {Property: "og:image:secure_url", Value: hmnurl.BuildUserFile(project.LogoLight)}, + } +} + func FetchProjectBySlug(ctx context.Context, conn *pgxpool.Pool, slug string) (*models.Project, error) { if len(slug) > 0 && slug != models.HMNProjectSlug { subdomainProjectRow, err := db.QueryOne(ctx, conn, models.Project{}, "SELECT $columns FROM handmade_project WHERE slug = $1", slug) diff --git a/src/website/snippet.go b/src/website/snippet.go index fa384fb7..0a279331 100644 --- a/src/website/snippet.go +++ b/src/website/snippet.go @@ -104,7 +104,7 @@ func Snippet(c *RequestContext) ResponseData { } baseData := getBaseData(c) - baseData.OpenGraphItems = opengraph + baseData.OpenGraphItems = opengraph // NOTE(asaf): We're overriding the defaults on purpose. var res ResponseData err = res.WriteTemplate("snippet.html", SnippetData{ BaseData: baseData, diff --git a/src/website/whenisit.go b/src/website/whenisit.go new file mode 100644 index 00000000..871d6912 --- /dev/null +++ b/src/website/whenisit.go @@ -0,0 +1,68 @@ +package website + +import ( + "fmt" + "strconv" + + "git.handmade.network/hmn/hmn/src/templates" +) + +type WhenIsItData struct { + templates.BaseData + Timestamp int + Name string + Url string +} + +func WhenIsIt(c *RequestContext) ResponseData { + timestampStr := c.Req.URL.Query().Get("t") + timestamp := 0 + hasTimestamp := false + + if timestampStr != "" { + var err error + timestamp, err = strconv.Atoi(timestampStr) + hasTimestamp = (err == nil) + } + + baseData := getBaseData(c) + baseData.Title = "When is it?" + + baseData.OpenGraphItems = append(baseData.OpenGraphItems, templates.OpenGraphItem{ + Property: "og:title", + Value: baseData.Title, + }) + baseData.OpenGraphItems = append(baseData.OpenGraphItems, templates.OpenGraphItem{ + Property: "og:url", + Value: c.FullUrl(), + }) + + if hasTimestamp { + name := c.Req.URL.Query().Get("n") + url := c.Req.URL.Query().Get("u") + + if name != "" { + baseData.OpenGraphItems = append(baseData.OpenGraphItems, templates.OpenGraphItem{ + Property: "og:description", + Value: fmt.Sprintf("Find out when %s starts.", name), + }) + } + + var res ResponseData + res.MustWriteTemplate("whenisit.html", WhenIsItData{ + BaseData: baseData, + Timestamp: timestamp, + Name: name, + Url: url, + }, c.Perf) + return res + } else { + baseData.OpenGraphItems = append(baseData.OpenGraphItems, templates.OpenGraphItem{ + Property: "og:description", + Value: "A countdown timer", + }) + var res ResponseData + res.MustWriteTemplate("whenisit_setup.html", baseData, c.Perf) + return res + } +}