/
idiomDetail.go
344 lines (297 loc) · 10.6 KB
/
idiomDetail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"bytes"
"io"
"net/http"
"strings"
"time"
. "github.com/Deleplace/programming-idioms/idioms"
"context"
"github.com/gorilla/mux"
"google.golang.org/appengine/log"
)
// IdiomDetailFacade is the Facade for the Idiom Detail page.
type IdiomDetailFacade struct {
PageMeta PageMeta
UserProfile UserProfile
Idiom *Idiom
SelectedImplID int
SelectedImplLang string
}
func idiomDetail(w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
ctx := r.Context()
userProfile := readUserProfile(r)
favlangs := userProfile.FavoriteLanguages
pushResources := func() {
if _, err := r.Cookie("v"); err == nil {
log.Infof(ctx, "Returning visitor: no resource server push needed.")
} else {
// TODO enable when page-idiom-detail-minimal is default, and cookie "v" is in use
//
// log.Infof(ctx, "New visitor: server push resources!")
// prefix := hostPrefix() + themeDirectory()
// var links bytes.Buffer
// fmt.Fprintf(&links, "<%s>; rel=preload; as=%s, ", prefix+"/js/pages/idiom-detail-minimal.js", "script")
// fmt.Fprintf(&links, "<%s>; rel=preload; as=%s, ", prefix+"/css/pages/idiom-detail-minimal.css", "style")
// fmt.Fprintf(&links, "<%s>; rel=preload; as=%s", "/default_20200205_/img/wheel.svg", "image")
// w.Header().Set("Link", links.String())
}
}
userProfileEmpty := userProfile.Empty()
// #185 Take "Language of current impl" into account for the right bar Cheatsheet links,
// even it this language is "not yet" in the user's favorites.
implLangInURL := vars["implLang"]
if implLangInURL != "" {
if !StringSliceContainsCaseInsensitive(userProfile.FavoriteLanguages, implLangInURL) {
// userProfile.FavoriteLanguages = append(userProfile.FavoriteLanguages, implLangInURL)
// But that would mess up a bit with the favlanag bar, so... let's do this in the frontend instead
}
// #112 Auto add favorite languages by setting cookie
// Nah, let's do this in the frontend instead
}
if userProfileEmpty {
//
// Zero-preference ≡ anonymous visit ≡ server cache enabled
//
path := r.URL.RequestURI()
if cachedPage := htmlCacheRead(ctx, path); cachedPage != nil {
// Using the whole HTML block from Memcache
log.Debugf(ctx, "%s from memcache!", path)
_, err := w.Write(cachedPage)
return err
}
log.Debugf(ctx, "%s not in memcache.", path)
var buffer bytes.Buffer
err := generateIdiomDetailPage(ctx, &buffer, vars)
if err != nil {
if properURL, ok := err.(needRedirectError); ok {
http.Redirect(w, r, string(properURL), http.StatusFound) // 302
return nil
}
return err
}
pushResources()
_, err = w.Write(buffer.Bytes())
if err != nil {
return err
}
htmlCacheWrite(ctx, path, buffer.Bytes(), 24*time.Hour)
// Note that this cache entry must be later invalidated in case
// of any modification in this idiom.
// Here we just cached 1 HTML page for 1 day.
// We tried previously to agressively trigger htmlRecacheNowAndTomorrow,
// but it didn't lead to great results in shared memcache.
return nil
}
//
// Below: for users having custom profile
//
// WARNING the code below is currently very redundant with the second part of generateIdiomDetail.
// Please try to not diverge.
//
idiomIDStr := vars["idiomId"]
idiomID := String2Int(idiomIDStr)
var canonicalURL string
_, idiom, err := dao.getIdiom(ctx, idiomID)
if err != nil {
return PiErrorf(http.StatusNotFound, "Could not find idiom %q", idiomIDStr)
}
var selectedImplID int
var selectedImplLang string
if selectedImplIDStr := vars["implId"]; selectedImplIDStr != "" {
selectedImplID = String2Int(selectedImplIDStr)
for _, impl := range idiom.Implementations {
if selectedImplID == impl.Id {
selectedImplLang = impl.LanguageName
break
}
}
if selectedImplLang == "" {
// The requested implementation was not found.
properURL := NiceIdiomURL(idiom)
http.Redirect(w, r, properURL, 302)
return nil
}
canonicalURL = host() + NiceImplRelativeURL(idiom, selectedImplID, selectedImplLang)
} else {
// Just the idiom, no specific impl
canonicalURL = host() + NiceIdiomRelativeURL(idiom)
}
idiomTitleInURL := vars["idiomTitle"]
if idiomTitleInURL != "" && uriNormalize(idiom.Title) != idiomTitleInURL {
// Maybe the title has changed recently,
// or someone is attempting a practical joke forging a funny URL ?
properURL := canonicalURL
http.Redirect(w, r, properURL, http.StatusMovedPermanently) // 301
return nil
}
includeNonFav := seeNonFavorite(r)
log.Debugf(ctx, "Reorder impls start...")
implFavoriteLanguagesFirstWithOrder(idiom, favlangs, selectedImplLang, includeNonFav)
// Selected impl as very first element
for i := range idiom.Implementations {
if idiom.Implementations[i].LanguageName != selectedImplLang {
break
}
if idiom.Implementations[i].Id == selectedImplID {
idiom.Implementations[0], idiom.Implementations[i] = idiom.Implementations[i], idiom.Implementations[0]
break
}
}
log.Debugf(ctx, "Reorder impls end.")
if implLangInURL != "" && strings.ToLower(selectedImplLang) != strings.ToLower(implLangInURL) {
// Maybe an accident,
// or someone is attempting a practical joke forging a funny URL ?
properURL := NiceImplURL(idiom, selectedImplID, selectedImplLang)
http.Redirect(w, r, properURL, http.StatusMovedPermanently) // 301
return nil
}
if toggles.Any("idiomVotingUp", "implVotingUp") {
log.Debugf(ctx, "Decorate with votes start...")
daoVotes.decorateIdiom(ctx, idiom, userProfile.Nickname)
log.Debugf(ctx, "Decorate with votes end.")
}
pageTitle := idiom.Title
if selectedImplLang != "" {
// SEO: specify the language in the HTML title
pageTitle += ", in " + PrintNiceLang(selectedImplLang)
}
pageKeywords := preparePageKeywords(idiom, selectedImplLang)
extraJS := []string{}
if IsAdmin(r) {
extraJS = append(extraJS, hostPrefix()+themeDirectory()+"/js/programming-idioms-admin.js")
}
myToggles := copyToggles(toggles)
myToggles["actionEditIdiom"] = !idiom.Protected || IsAdmin(r)
myToggles["actionIdiomHistory"] = true
myToggles["actionAddImpl"] = !idiom.Protected || IsAdmin(r)
data := &IdiomDetailFacade{
PageMeta: PageMeta{
PageTitle: pageTitle,
PageKeywords: pageKeywords,
CanonicalURL: canonicalURL,
Toggles: myToggles,
ExtraJs: extraJS,
},
UserProfile: userProfile,
Idiom: idiom,
SelectedImplID: selectedImplID,
SelectedImplLang: selectedImplLang,
}
pushResources()
log.Debugf(ctx, "ExecuteTemplate start...")
err = templates.ExecuteTemplate(w, "page-idiom-detail", data)
// err = templates.ExecuteTemplate(w, "page-idiom-detail-minimal", data)
log.Debugf(ctx, "ExecuteTemplate end.")
return err
}
func generateIdiomDetailPage(ctx context.Context, w io.Writer, vars map[string]string) error {
//
// WARNING this code is currently very redundant with the second part of idiomDetail.
// Please try to not diverge.
//
idiomIDStr := vars["idiomId"]
idiomID := String2Int(idiomIDStr)
var canonicalURL string
_, idiom, err := dao.getIdiom(ctx, idiomID)
if err != nil {
return PiErrorf(http.StatusNotFound, "Could not find idiom %q", idiomIDStr)
}
idiomTitleInURL := vars["idiomTitle"]
if idiomTitleInURL != "" && uriNormalize(idiom.Title) != idiomTitleInURL {
// Maybe the title has changed recently,
// or someone is attempting a practical joke forging a funny URL ?
properURL := NiceIdiomURL(idiom)
return needRedirectError(properURL)
}
var selectedImplID int
var selectedImplLang string
if selectedImplIDStr := vars["implId"]; selectedImplIDStr != "" {
selectedImplID = String2Int(selectedImplIDStr)
for _, impl := range idiom.Implementations {
if selectedImplID == impl.Id {
selectedImplLang = impl.LanguageName
break
}
}
if selectedImplLang == "" {
// The requested implementation was not found.
properURL := NiceIdiomURL(idiom)
return needRedirectError(properURL)
}
canonicalURL = host() + NiceImplRelativeURL(idiom, selectedImplID, selectedImplLang)
} else {
// Just the idiom, no specific impl
canonicalURL = host() + NiceIdiomRelativeURL(idiom)
}
implFavoriteLanguagesFirstWithOrder(idiom, nil, selectedImplLang, true)
// Selected impl as very first element
for i := range idiom.Implementations {
if idiom.Implementations[i].LanguageName != selectedImplLang {
break
}
if idiom.Implementations[i].Id == selectedImplID {
idiom.Implementations[0], idiom.Implementations[i] = idiom.Implementations[i], idiom.Implementations[0]
break
}
}
implLangInURL := vars["implLang"]
if implLangInURL != "" && strings.ToLower(selectedImplLang) != strings.ToLower(implLangInURL) {
// Maybe an accident,
// or someone is attempting a practical joke forging a funny URL ?
properURL := NiceImplURL(idiom, selectedImplID, selectedImplLang)
return needRedirectError(properURL)
}
pageTitle := idiom.Title
if selectedImplLang != "" {
// SEO: specify the language in the HTML title
pageTitle += ", in " + PrintNiceLang(selectedImplLang)
}
pageKeywords := preparePageKeywords(idiom, selectedImplLang)
myToggles := copyToggles(toggles)
myToggles["actionEditIdiom"] = !idiom.Protected
myToggles["actionIdiomHistory"] = true
myToggles["actionAddImpl"] = !idiom.Protected
data := &IdiomDetailFacade{
PageMeta: PageMeta{
PageTitle: pageTitle,
PageKeywords: pageKeywords,
CanonicalURL: canonicalURL,
Toggles: myToggles,
},
UserProfile: EmptyUserProfile(),
Idiom: idiom,
SelectedImplID: selectedImplID,
SelectedImplLang: selectedImplLang,
}
log.Debugf(ctx, "ExecuteTemplate start...")
err = templates.ExecuteTemplate(w, "page-idiom-detail", data)
// err = templates.ExecuteTemplate(w, "page-idiom-detail-minimal", data)
log.Debugf(ctx, "ExecuteTemplate end.")
return err
}
type needRedirectError string
func (err needRedirectError) Error() string {
return string(err)
}
// SEO meta keywords
func preparePageKeywords(idiom *Idiom, selectedImplLang string) string {
// Keywords from the Idiom itself
keywords := idiom.ExtraKeywords
// #144 most idiom.ExtraKeywords have space separators, but wee need commas.
// Let's replace with commas. Drawback: a multiple word keyword don't survive.
keywords = strings.Replace(keywords, " ", ", ", -1)
keywords = strings.Replace(keywords, ",,", ",", -1)
// Extra keywords for the languqge name of the selected impl
if selectedImplLang != "" {
langAliases := selectedImplLang
niceLang := PrintNiceLang(selectedImplLang)
if selectedImplLang != niceLang {
langAliases = niceLang + ", " + langAliases
}
langAliases += ", " + strings.Join(LanguageExtraKeywords(selectedImplLang), ", ")
keywords = langAliases + ", " + keywords
}
return keywords
}