Skip to content

Commit

Permalink
Add RSS feed to the public mailing list archive.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Nov 10, 2022
1 parent 438568e commit f958f3d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
37 changes: 37 additions & 0 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
"net/http"

"github.com/gorilla/feeds"
"github.com/knadh/listmonk/internal/manager"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -46,6 +47,42 @@ func handleGetCampaignArchives(c echo.Context) error {
return c.JSON(200, okResp{out})
}

// handleGetCampaignArchivesFeed renders the public campaign archives RSS feed.
func handleGetCampaignArchivesFeed(c echo.Context) error {
var (
app = c.Get("app").(*App)
pg = app.paginator.NewFromURL(c.Request().URL.Query())
)

camps, _, err := getCampaignArchives(pg.Offset, pg.Limit, app)
if err != nil {
return err
}

out := make([]*feeds.Item, 0, len(camps))
for _, c := range camps {
out = append(out, &feeds.Item{
Title: c.Subject,
Link: &feeds.Link{Href: c.URL},
Created: c.CreatedAt.Time,
})
}

feed := &feeds.Feed{
Title: app.constants.SiteName,
Link: &feeds.Link{Href: app.constants.RootURL},
Description: app.i18n.T("public.archiveTitle"),
Items: out,
}

if err := feed.WriteRss(c.Response().Writer); err != nil {
app.log.Printf("error generating archive RSS feed: %v", err)
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("public.errorProcessingRequest"))
}

return nil
}

// handleCampaignArchivesPage renders the public campaign archives page.
func handleCampaignArchivesPage(c echo.Context) error {
var (
Expand Down
2 changes: 2 additions & 0 deletions cmd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func initHTTPHandlers(e *echo.Echo, app *App) {
"campUUID", "subUUID")))
e.GET("/campaign/:campUUID/:subUUID/px.png", noIndex(validateUUID(handleRegisterCampaignView,
"campUUID", "subUUID")))

e.GET("/archive", handleCampaignArchivesPage)
e.GET("/archive.xml", handleGetCampaignArchivesFeed)
e.GET("/archive/:uuid", handleCampaignArchivePage)

e.GET("/public/custom.css", serveCustomApperance("public.custom_css"))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gofrs/uuid v4.0.0+incompatible
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/feeds v1.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jmoiron/sqlx v1.3.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
Expand Down
12 changes: 11 additions & 1 deletion static/public/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ input[disabled] {

.archive {
list-style-type: none;
margin: 0;
margin: 25px 0 0 0;
padding: 0;
}
.archive .date {
Expand All @@ -151,6 +151,16 @@ input[disabled] {
.archive li {
margin-bottom: 15px;
}
.feed {
margin-right: 15px;
}

.home-options {
margin-top: 30px;
}
.home-options a {
margin: 0 7px;
}

.pagination {
margin-top: 30px;
Expand Down
10 changes: 7 additions & 3 deletions static/public/templates/archive.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ <h2>{{ L.T "public.archiveTitle" }}</h2>
{{ end }}

{{ if .EnablePublicSubPage }}
<p class="right">
<a href="{{ .RootURL }}/subscription/form">Subscribe</a>
</p>
<div class="right">
<a href="{{ .RootURL }}/archive.xml">
<img src="{{ .RootURL }}/public/static/rss.svg" alt="RSS" class="feed"
width="16" height="16" />
</a>
<a href="{{ .RootURL }}/subscription/form">{{ L.T "public.sub" }}</a>
</div>
{{ end }}

{{ if gt .Data.TotalPages 1 }}
Expand Down
9 changes: 9 additions & 0 deletions static/public/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

<section class="center">
<a href="admin" class="button">{{ L.T "users.login" }}</a>

<div class="home-options">
{{ if .EnablePublicSubPage }}
<a href="{{ .RootURL }}/subscription/form">{{ L.T "public.sub" }}</a>
{{ end }}
{{ if .EnablePublicSubPage }}
<a href="{{ .RootURL }}/archive">{{ L.T "public.archiveTitle" }}</a>
{{ end }}
</div>
</section>

{{ template "footer" .}}
Expand Down
3 changes: 3 additions & 0 deletions static/public/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<title>{{ .Data.Title }} - {{ .SiteName }}</title>
<meta name="description" content="{{ .Data.Description }}" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />

<link rel="alternate" type="application/rss+xml" title="{{ L.T "public.archiveTitle" }} - {{ .SiteName }}" href="{{ .RootURL }}/archive.xml" />

<link href="/public/static/style.css" rel="stylesheet" type="text/css" />
<link href="/public/custom.css" rel="stylesheet" type="text/css">
<script src="/public/custom.js" async defer></script>
Expand Down

0 comments on commit f958f3d

Please sign in to comment.