@@ -27,42 +27,50 @@ type campArchive struct {
27
27
func handleGetCampaignArchives (c echo.Context ) error {
28
28
var (
29
29
app = c .Get ("app" ).(* App )
30
- pg = app .paginator .NewFromURL (c .Request ().URL .Query ())
31
30
)
32
31
32
+ // Get archives from the DB.
33
+ pg := app .paginator .NewFromURL (c .Request ().URL .Query ())
33
34
camps , total , err := getCampaignArchives (pg .Offset , pg .Limit , false , app )
34
35
if err != nil {
35
36
return err
36
37
}
37
38
38
- var out models.PageResults
39
39
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
+ }})
42
43
}
43
44
44
45
// 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
+ }
49
52
50
53
return c .JSON (200 , okResp {out })
51
54
}
52
55
53
56
// handleGetCampaignArchivesFeed renders the public campaign archives RSS feed.
54
57
func handleGetCampaignArchivesFeed (c echo.Context ) error {
55
58
var (
56
- app = c .Get ("app" ).(* App )
59
+ app = c .Get ("app" ).(* App )
60
+ )
61
+
62
+ var (
57
63
pg = app .paginator .NewFromURL (c .Request ().URL .Query ())
58
64
showFullContent = app .constants .EnablePublicArchiveRSSContent
59
65
)
60
66
67
+ // Get archives from the DB.
61
68
camps , _ , err := getCampaignArchives (pg .Offset , pg .Limit , showFullContent , app )
62
69
if err != nil {
63
70
return err
64
71
}
65
72
73
+ // Format output for the feed.
66
74
out := make ([]* feeds.Item , 0 , len (camps ))
67
75
for _ , c := range camps {
68
76
pubDate := c .CreatedAt .Time
@@ -79,6 +87,7 @@ func handleGetCampaignArchivesFeed(c echo.Context) error {
79
87
})
80
88
}
81
89
90
+ // Generate the feed.
82
91
feed := & feeds.Feed {
83
92
Title : app .constants .SiteName ,
84
93
Link : & feeds.Link {Href : app .constants .RootURL },
@@ -98,9 +107,10 @@ func handleGetCampaignArchivesFeed(c echo.Context) error {
98
107
func handleCampaignArchivesPage (c echo.Context ) error {
99
108
var (
100
109
app = c .Get ("app" ).(* App )
101
- pg = app .paginator .NewFromURL (c .Request ().URL .Query ())
102
110
)
103
111
112
+ // Get archives from the DB.
113
+ pg := app .paginator .NewFromURL (c .Request ().URL .Query ())
104
114
out , total , err := getCampaignArchives (pg .Offset , pg .Limit , false , app )
105
115
if err != nil {
106
116
return err
@@ -120,50 +130,58 @@ func handleCampaignArchivesPage(c echo.Context) error {
120
130
// handleCampaignArchivePage renders the public campaign archives page.
121
131
func handleCampaignArchivePage (c echo.Context ) error {
122
132
var (
123
- app = c .Get ("app" ).(* App )
124
- id = c .Param ("id" )
125
- uuid = ""
126
- slug = ""
133
+ app = c .Get ("app" ).(* App )
127
134
)
128
135
129
136
// 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
132
143
} else {
133
- slug = id
144
+ slug = idStr
134
145
}
135
146
147
+ // Get the campaign from the DB.
136
148
pubCamp , err := app .core .GetArchivedCampaign (0 , uuid , slug )
137
149
if err != nil || pubCamp .Type != models .CampaignTypeRegular {
138
150
notFound := false
151
+
152
+ // Camppaig doesn't exist.
139
153
if er , ok := err .(* echo.HTTPError ); ok {
140
154
if er .Code == http .StatusBadRequest {
141
155
notFound = true
142
156
}
143
157
} else if pubCamp .Type != models .CampaignTypeRegular {
158
+ // Campaign isn't of regular type.
144
159
notFound = true
145
160
}
146
161
162
+ // 404.
147
163
if notFound {
148
164
return c .Render (http .StatusNotFound , tplMessage ,
149
165
makeMsgTpl (app .i18n .T ("public.notFoundTitle" ), "" , app .i18n .T ("public.campaignNotFound" )))
150
166
}
151
167
168
+ // Some other internal error.
152
169
return c .Render (http .StatusInternalServerError , tplMessage ,
153
170
makeMsgTpl (app .i18n .T ("public.errorTitle" ), "" , app .i18n .Ts ("public.errorFetchingCampaign" )))
154
171
}
155
172
173
+ // "Compile" the campaign template with appropriate data.
156
174
out , err := compileArchiveCampaigns ([]models.Campaign {pubCamp }, app )
157
175
if err != nil {
158
176
return c .Render (http .StatusInternalServerError , tplMessage ,
159
177
makeMsgTpl (app .i18n .T ("public.errorTitle" ), "" , app .i18n .Ts ("public.errorFetchingCampaign" )))
160
178
}
161
179
162
- // Render the message body.
180
+ // Render the campaign body.
163
181
camp := out [0 ].Campaign
164
182
msg , err := app .manager .NewCampaignMessage (camp , out [0 ].Subscriber )
165
183
if err != nil {
166
- app .log .Printf ("error rendering message : %v" , err )
184
+ app .log .Printf ("error rendering campaign : %v" , err )
167
185
return c .Render (http .StatusInternalServerError , tplMessage ,
168
186
makeMsgTpl (app .i18n .T ("public.errorTitle" ), "" , app .i18n .Ts ("public.errorFetchingCampaign" )))
169
187
}
@@ -177,6 +195,7 @@ func handleCampaignArchivePageLatest(c echo.Context) error {
177
195
app = c .Get ("app" ).(* App )
178
196
)
179
197
198
+ // Get the latest campaign from the DB.
180
199
camps , _ , err := getCampaignArchives (0 , 1 , true , app )
181
200
if err != nil {
182
201
return err
@@ -186,12 +205,12 @@ func handleCampaignArchivePageLatest(c echo.Context) error {
186
205
return c .Render (http .StatusNotFound , tplMessage ,
187
206
makeMsgTpl (app .i18n .T ("public.notFoundTitle" ), "" , app .i18n .T ("public.campaignNotFound" )))
188
207
}
189
-
190
208
camp := camps [0 ]
191
209
192
210
return c .HTML (http .StatusOK , camp .Content )
193
211
}
194
212
213
+ // getCampaignArchives fetches the public campaign archives from the DB.
195
214
func getCampaignArchives (offset , limit int , renderBody bool , app * App ) ([]campArchive , int , error ) {
196
215
pubCamps , total , err := app .core .GetArchivedCampaigns (offset , limit )
197
216
if err != nil {
@@ -214,12 +233,14 @@ func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campAr
214
233
SendAt : camp .SendAt ,
215
234
}
216
235
236
+ // The campaign may have a custom slug.
217
237
if camp .ArchiveSlug .Valid {
218
238
archive .URL , _ = url .JoinPath (app .constants .ArchiveURL , camp .ArchiveSlug .String )
219
239
} else {
220
240
archive .URL , _ = url .JoinPath (app .constants .ArchiveURL , camp .UUID )
221
241
}
222
242
243
+ // Render the full template body if requested.
223
244
if renderBody {
224
245
msg , err := app .manager .NewCampaignMessage (camp , m .Subscriber )
225
246
if err != nil {
@@ -234,12 +255,13 @@ func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campAr
234
255
return out , total , nil
235
256
}
236
257
258
+ // compileArchiveCampaigns compiles the campaign template with the subscriber data.
237
259
func compileArchiveCampaigns (camps []models.Campaign , app * App ) ([]manager.CampaignMessage , error ) {
260
+
238
261
var (
239
- b = bytes.Buffer {}
262
+ b = bytes.Buffer {}
263
+ out = make ([]manager.CampaignMessage , 0 , len (camps ))
240
264
)
241
-
242
- out := make ([]manager.CampaignMessage , 0 , len (camps ))
243
265
for _ , c := range camps {
244
266
camp := c
245
267
if err := camp .CompileTemplate (app .manager .TemplateFuncs (& camp )); err != nil {
@@ -266,7 +288,6 @@ func compileArchiveCampaigns(camps []models.Campaign, app *App) ([]manager.Campa
266
288
}
267
289
camp .Subject = b .String ()
268
290
b .Reset ()
269
-
270
291
}
271
292
272
293
out = append (out , m )
0 commit comments