@@ -27,42 +27,50 @@ type campArchive struct {
2727func handleGetCampaignArchives (c echo.Context ) error {
2828 var (
2929 app = c .Get ("app" ).(* App )
30- pg = app .paginator .NewFromURL (c .Request ().URL .Query ())
3130 )
3231
32+ // Get archives from the DB.
33+ pg := app .paginator .NewFromURL (c .Request ().URL .Query ())
3334 camps , total , err := getCampaignArchives (pg .Offset , pg .Limit , false , app )
3435 if err != nil {
3536 return err
3637 }
3738
38- var out models.PageResults
3939 if len (camps ) == 0 {
40- out .Results = []campArchive {}
41- return c .JSON (http .StatusOK , okResp {out })
40+ return c .JSON (http .StatusOK , okResp {models.PageResults {
41+ Results : []campArchive {},
42+ }})
4243 }
4344
4445 // Meta.
45- out .Results = camps
46- out .Total = total
47- out .Page = pg .Page
48- out .PerPage = pg .PerPage
46+ out := models.PageResults {
47+ Results : camps ,
48+ Total : total ,
49+ Page : pg .Page ,
50+ PerPage : pg .PerPage ,
51+ }
4952
5053 return c .JSON (200 , okResp {out })
5154}
5255
5356// handleGetCampaignArchivesFeed renders the public campaign archives RSS feed.
5457func handleGetCampaignArchivesFeed (c echo.Context ) error {
5558 var (
56- app = c .Get ("app" ).(* App )
59+ app = c .Get ("app" ).(* App )
60+ )
61+
62+ var (
5763 pg = app .paginator .NewFromURL (c .Request ().URL .Query ())
5864 showFullContent = app .constants .EnablePublicArchiveRSSContent
5965 )
6066
67+ // Get archives from the DB.
6168 camps , _ , err := getCampaignArchives (pg .Offset , pg .Limit , showFullContent , app )
6269 if err != nil {
6370 return err
6471 }
6572
73+ // Format output for the feed.
6674 out := make ([]* feeds.Item , 0 , len (camps ))
6775 for _ , c := range camps {
6876 pubDate := c .CreatedAt .Time
@@ -79,6 +87,7 @@ func handleGetCampaignArchivesFeed(c echo.Context) error {
7987 })
8088 }
8189
90+ // Generate the feed.
8291 feed := & feeds.Feed {
8392 Title : app .constants .SiteName ,
8493 Link : & feeds.Link {Href : app .constants .RootURL },
@@ -98,9 +107,10 @@ func handleGetCampaignArchivesFeed(c echo.Context) error {
98107func handleCampaignArchivesPage (c echo.Context ) error {
99108 var (
100109 app = c .Get ("app" ).(* App )
101- pg = app .paginator .NewFromURL (c .Request ().URL .Query ())
102110 )
103111
112+ // Get archives from the DB.
113+ pg := app .paginator .NewFromURL (c .Request ().URL .Query ())
104114 out , total , err := getCampaignArchives (pg .Offset , pg .Limit , false , app )
105115 if err != nil {
106116 return err
@@ -120,50 +130,58 @@ func handleCampaignArchivesPage(c echo.Context) error {
120130// handleCampaignArchivePage renders the public campaign archives page.
121131func handleCampaignArchivePage (c echo.Context ) error {
122132 var (
123- app = c .Get ("app" ).(* App )
124- id = c .Param ("id" )
125- uuid = ""
126- slug = ""
133+ app = c .Get ("app" ).(* App )
127134 )
128135
129136 // ID can be the UUID or slug.
130- if reUUID .MatchString (id ) {
131- uuid = id
137+ var (
138+ idStr = c .Param ("id" )
139+ uuid , slug string
140+ )
141+ if reUUID .MatchString (idStr ) {
142+ uuid = idStr
132143 } else {
133- slug = id
144+ slug = idStr
134145 }
135146
147+ // Get the campaign from the DB.
136148 pubCamp , err := app .core .GetArchivedCampaign (0 , uuid , slug )
137149 if err != nil || pubCamp .Type != models .CampaignTypeRegular {
138150 notFound := false
151+
152+ // Camppaig doesn't exist.
139153 if er , ok := err .(* echo.HTTPError ); ok {
140154 if er .Code == http .StatusBadRequest {
141155 notFound = true
142156 }
143157 } else if pubCamp .Type != models .CampaignTypeRegular {
158+ // Campaign isn't of regular type.
144159 notFound = true
145160 }
146161
162+ // 404.
147163 if notFound {
148164 return c .Render (http .StatusNotFound , tplMessage ,
149165 makeMsgTpl (app .i18n .T ("public.notFoundTitle" ), "" , app .i18n .T ("public.campaignNotFound" )))
150166 }
151167
168+ // Some other internal error.
152169 return c .Render (http .StatusInternalServerError , tplMessage ,
153170 makeMsgTpl (app .i18n .T ("public.errorTitle" ), "" , app .i18n .Ts ("public.errorFetchingCampaign" )))
154171 }
155172
173+ // "Compile" the campaign template with appropriate data.
156174 out , err := compileArchiveCampaigns ([]models.Campaign {pubCamp }, app )
157175 if err != nil {
158176 return c .Render (http .StatusInternalServerError , tplMessage ,
159177 makeMsgTpl (app .i18n .T ("public.errorTitle" ), "" , app .i18n .Ts ("public.errorFetchingCampaign" )))
160178 }
161179
162- // Render the message body.
180+ // Render the campaign body.
163181 camp := out [0 ].Campaign
164182 msg , err := app .manager .NewCampaignMessage (camp , out [0 ].Subscriber )
165183 if err != nil {
166- app .log .Printf ("error rendering message : %v" , err )
184+ app .log .Printf ("error rendering campaign : %v" , err )
167185 return c .Render (http .StatusInternalServerError , tplMessage ,
168186 makeMsgTpl (app .i18n .T ("public.errorTitle" ), "" , app .i18n .Ts ("public.errorFetchingCampaign" )))
169187 }
@@ -177,6 +195,7 @@ func handleCampaignArchivePageLatest(c echo.Context) error {
177195 app = c .Get ("app" ).(* App )
178196 )
179197
198+ // Get the latest campaign from the DB.
180199 camps , _ , err := getCampaignArchives (0 , 1 , true , app )
181200 if err != nil {
182201 return err
@@ -186,12 +205,12 @@ func handleCampaignArchivePageLatest(c echo.Context) error {
186205 return c .Render (http .StatusNotFound , tplMessage ,
187206 makeMsgTpl (app .i18n .T ("public.notFoundTitle" ), "" , app .i18n .T ("public.campaignNotFound" )))
188207 }
189-
190208 camp := camps [0 ]
191209
192210 return c .HTML (http .StatusOK , camp .Content )
193211}
194212
213+ // getCampaignArchives fetches the public campaign archives from the DB.
195214func getCampaignArchives (offset , limit int , renderBody bool , app * App ) ([]campArchive , int , error ) {
196215 pubCamps , total , err := app .core .GetArchivedCampaigns (offset , limit )
197216 if err != nil {
@@ -214,12 +233,14 @@ func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campAr
214233 SendAt : camp .SendAt ,
215234 }
216235
236+ // The campaign may have a custom slug.
217237 if camp .ArchiveSlug .Valid {
218238 archive .URL , _ = url .JoinPath (app .constants .ArchiveURL , camp .ArchiveSlug .String )
219239 } else {
220240 archive .URL , _ = url .JoinPath (app .constants .ArchiveURL , camp .UUID )
221241 }
222242
243+ // Render the full template body if requested.
223244 if renderBody {
224245 msg , err := app .manager .NewCampaignMessage (camp , m .Subscriber )
225246 if err != nil {
@@ -234,12 +255,13 @@ func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campAr
234255 return out , total , nil
235256}
236257
258+ // compileArchiveCampaigns compiles the campaign template with the subscriber data.
237259func compileArchiveCampaigns (camps []models.Campaign , app * App ) ([]manager.CampaignMessage , error ) {
260+
238261 var (
239- b = bytes.Buffer {}
262+ b = bytes.Buffer {}
263+ out = make ([]manager.CampaignMessage , 0 , len (camps ))
240264 )
241-
242- out := make ([]manager.CampaignMessage , 0 , len (camps ))
243265 for _ , c := range camps {
244266 camp := c
245267 if err := camp .CompileTemplate (app .manager .TemplateFuncs (& camp )); err != nil {
@@ -266,7 +288,6 @@ func compileArchiveCampaigns(camps []models.Campaign, app *App) ([]manager.Campa
266288 }
267289 camp .Subject = b .String ()
268290 b .Reset ()
269-
270291 }
271292
272293 out = append (out , m )
0 commit comments