Fix the superfluous header errors.

Shorten the query parameter for phrases.
Move the cache control header in the phrase route down to avoid caching errors improperly.

Fixes #53
This commit is contained in:
Azareal 2019-07-12 07:38:06 +10:00
parent 101b1c51df
commit f7720575d5
5 changed files with 13 additions and 17 deletions

View File

@ -185,7 +185,7 @@ function initPhrases(loggedIn, panel = false) {
}
function fetchPhrases(plist) {
fetch("/api/phrases/?query="+plist, {cache: "no-cache"})
fetch("/api/phrases/?q="+plist, {cache: "no-cache"})
.then((resp) => resp.json())
.then((data) => {
console.log("loaded phrase endpoint data");

View File

@ -193,22 +193,18 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
// TODO: Don't make this too JSON dependent so that we can swap in newer more efficient formats
h := w.Header()
h.Set("Content-Type", "application/json")
h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000
err := r.ParseForm()
if err != nil {
return c.PreErrorJS("Bad Form", w, r)
}
query := r.FormValue("query")
query := r.FormValue("q")
if query == "" {
return c.PreErrorJS("No query provided", w, r)
}
var negations []string
var positives []string
queryBits := strings.Split(query, ",")
for _, queryBit := range queryBits {
var negations, positives []string
for _, queryBit := range strings.Split(query, ",") {
queryBit = strings.TrimSpace(queryBit)
if queryBit[0] == '!' && len(queryBit) > 1 {
queryBit = strings.TrimPrefix(queryBit, "!")
@ -230,6 +226,7 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
if len(positives) == 0 {
return c.PreErrorJS("You haven't requested any phrases", w, r)
}
h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000
var etag string
_, ok := w.(c.GzipResponseWriter)
@ -240,6 +237,7 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
}
var plist map[string]string
var doneHead = false
var posLoop = func(positive string) c.RouteError {
// ! Constrain it to a subset of phrases for now
for _, item := range phraseWhitelist {
@ -251,13 +249,13 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
} else {
ok = true
w.Header().Set("ETag", etag)
if match := r.Header.Get("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
match := r.Header.Get("If-None-Match")
if match != "" && !doneHead && strings.Contains(match, etag) {
w.WriteHeader(http.StatusNotModified)
doneHead = true
return nil
}
}
}
break
}
}
@ -309,7 +307,6 @@ func routeAPIPhrases(w http.ResponseWriter, r *http.Request, user c.User) c.Rout
return c.InternalError(err, w, r)
}
w.Write(jsonBytes)
return nil
}

View File

@ -48,7 +48,6 @@ func UsersEdit(w http.ResponseWriter, r *http.Request, user c.User, suid string)
} else if err != nil {
return c.InternalError(err, w, r)
}
if targetUser.IsAdmin && !user.IsAdmin {
return c.LocalError("Only administrators can edit the account of an administrator.", w, r, user)
}

View File

@ -7,7 +7,7 @@
{{range .Header.PreScriptsAsync}}
<script async type="text/javascript" src="/static/{{.}}"></script>{{end}}
<meta property="x-loggedin" content="{{.CurrentUser.Loggedin}}" />
<script type="text/javascript" src="/static/init.js?i=7"></script>
<script type="text/javascript" src="/static/init.js?i=8"></script>
{{range .Header.ScriptsAsync}}
<script async type="text/javascript" src="/static/{{.}}"></script>{{end}}
<script type="text/javascript" src="/static/jquery-3.1.1.min.js"></script>