From 2df9390993d99bf11089250edfb88086ec5ee994 Mon Sep 17 00:00:00 2001 From: Asaf Gartner Date: Tue, 22 Jun 2021 15:02:47 +0300 Subject: [PATCH] Showcase xml feed --- src/templates/src/atom.xml | 22 ++++++++++++++++ src/templates/types.go | 1 + src/website/feed.go | 51 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/templates/src/atom.xml b/src/templates/src/atom.xml index e437b78e..460e774e 100644 --- a/src/templates/src/atom.xml +++ b/src/templates/src/atom.xml @@ -43,6 +43,28 @@ {{ end }} {{ else if .Snippets }} {{ range .Snippets }} + + New showcase item by {{ .OwnerName }} + + {{ .UUID }} + {{ rfc3339 .Date }} + + {{ .OwnerName }} + {{ .OwnerUrl }} + + +
+ {{ .Description }} + {{ if snippetimage . }} + + {{ else if snippetvideo . }} +
+
+
{{ end }} {{ end }} diff --git a/src/templates/types.go b/src/templates/types.go index b38af0af..d3209d13 100644 --- a/src/templates/types.go +++ b/src/templates/types.go @@ -234,6 +234,7 @@ type TimelineItem struct { Class string Date time.Time Url string + UUID string OwnerAvatarUrl string OwnerName string diff --git a/src/website/feed.go b/src/website/feed.go index 30e765a3..0b36cff3 100644 --- a/src/website/feed.go +++ b/src/website/feed.go @@ -140,7 +140,7 @@ type AtomFeedData struct { FeedType FeedType Posts []templates.PostListItem Projects []templates.Project - Snippets []int // TODO(asaf): Actually do this + Snippets []templates.TimelineItem } func AtomFeed(c *RequestContext) ResponseData { @@ -248,8 +248,55 @@ func AtomFeed(c *RequestContext) ResponseData { templateProject.Owners = append(templateProject.Owners, templates.UserToTemplate(&owner.User, "")) } c.Perf.EndBlock() + updated := time.Now() + if len(feedData.Projects) > 0 { + updated = feedData.Projects[0].DateApproved + } + feedData.Updated = updated case "showcase": - // TODO(asaf): Implement this + feedData.Title = "Showcase | Site-wide | Handmade.Network" + feedData.Subtitle = feedData.Title + feedData.FeedType = FeedTypeShowcase + feedData.FeedID = FeedIDShowcase + feedData.AtomFeedUrl = hmnurl.BuildAtomFeedForShowcase() + feedData.FeedUrl = hmnurl.BuildShowcase() + + c.Perf.StartBlock("SQL", "Fetch showcase snippets") + type snippetQuery struct { + Owner models.User `db:"owner"` + Snippet models.Snippet `db:"snippet"` + Asset *models.Asset `db:"asset"` + DiscordMessage *models.DiscordMessage `db:"discord_message"` + } + snippetQueryResult, err := db.Query(c.Context(), c.Conn, snippetQuery{}, + ` + SELECT $columns + FROM + handmade_snippet AS snippet + INNER JOIN auth_user AS owner ON owner.id = snippet.owner_id + LEFT JOIN handmade_asset AS asset ON asset.id = snippet.asset_id + LEFT JOIN handmade_discordmessage AS discord_message ON discord_message.id = snippet.discord_message_id + ORDER BY snippet.when DESC + LIMIT $1 + `, + itemsPerFeed, + ) + if err != nil { + return ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch snippets")) + } + snippetQuerySlice := snippetQueryResult.ToSlice() + for _, s := range snippetQuerySlice { + row := s.(*snippetQuery) + timelineItem := SnippetToTimelineItem(&row.Snippet, row.Asset, row.DiscordMessage, &row.Owner, c.Theme) + timelineItem.UUID = uuid.NewSHA1(uuid.NameSpaceURL, []byte(timelineItem.Url)).URN() + feedData.Snippets = append(feedData.Snippets, timelineItem) + } + c.Perf.EndBlock() + updated := time.Now() + if len(feedData.Snippets) > 0 { + updated = feedData.Snippets[0].Date + } + feedData.Updated = updated default: return FourOhFour(c) }