From bbef9cead82b3abd5080fa79dd40cb0c8e6d2d24 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 20 Oct 2019 11:42:19 +1000 Subject: [PATCH] Optimise away blocked user queries on profiles for guests. Shorten some things. --- router_gen/main.go | 27 +++++++++++++-------------- router_gen/route_impl.go | 40 ++++++++++++++++++++-------------------- routes/profile.go | 18 ++++++++++-------- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/router_gen/main.go b/router_gen/main.go index 13f0be1f..556efd63 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -35,26 +35,26 @@ func main() { r := &Router{} routes(r) - var tmplVars = TmplVars{ + tmplVars := TmplVars{ RouteList: r.routeList, RouteGroups: r.routeGroups, } var allRouteNames []RouteName - var allRouteMap = make(map[string]int) + allRouteMap := make(map[string]int) var out string - var mapIt = func(name string) { + mapIt := func(name string) { allRouteNames = append(allRouteNames, RouteName{name, strings.Replace(name, "common.", "c.", -1)}) allRouteMap[name] = len(allRouteNames) - 1 } - var countToIndents = func(indent int) (indentor string) { + countToIndents := func(indent int) (indentor string) { for i := 0; i < indent; i++ { indentor += "\t" } return indentor } - var runBefore = func(runnables []Runnable, indent int) (out string) { - var indentor = countToIndents(indent) + runBefore := func(runnables []Runnable, indent int) (out string) { + indentor := countToIndents(indent) if len(runnables) > 0 { for _, runnable := range runnables { if runnable.Literal { @@ -72,7 +72,7 @@ func main() { for _, route := range r.routeList { mapIt(route.Name) - var end = len(route.Path) - 1 + end := len(route.Path) - 1 out += "\n\t\tcase \"" + route.Path[0:end] + "\":" out += runBefore(route.RunBefore, 4) out += "\n\t\t\tcounters.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")" @@ -91,12 +91,12 @@ func main() { } for _, group := range r.routeGroups { - var end = len(group.Path) - 1 + end := len(group.Path) - 1 out += "\n\t\tcase \"" + group.Path[0:end] + "\":" out += runBefore(group.RunBefore, 3) out += "\n\t\t\tswitch(req.URL.Path) {" - var defaultRoute = blankRoute() + defaultRoute := blankRoute() for _, route := range group.RouteList { if group.Path == route.Path { defaultRoute = route @@ -301,7 +301,7 @@ func main() { "Twitterbot": "twitter", "facebookexternalhit": "facebook", "Facebot": "facebook", - "Applebot":"apple", + "Applebot": "apple", "Discourse": "discourse", "SemrushBot": "semrush", @@ -309,7 +309,7 @@ func main() { "zgrab": "zgrab", } - var fileData = `// Code generated by Gosora's Router Generator. DO NOT EDIT. + fileData := `// Code generated by Gosora's Router Generator. DO NOT EDIT. /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main @@ -882,10 +882,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c return err } ` - var tmpl = template.Must(template.New("router").Parse(fileData)) + tmpl := template.Must(template.New("router").Parse(fileData)) var b bytes.Buffer - err := tmpl.Execute(&b, tmplVars) - if err != nil { + if err := tmpl.Execute(&b, tmplVars); err != nil { log.Fatal(err) } diff --git a/router_gen/route_impl.go b/router_gen/route_impl.go index c263a489..c01ff9ba 100644 --- a/router_gen/route_impl.go +++ b/router_gen/route_impl.go @@ -18,40 +18,40 @@ type Runnable struct { Literal bool } -func (route *RouteImpl) Before(items ...string) *RouteImpl { +func (r *RouteImpl) Before(items ...string) *RouteImpl { for _, item := range items { - route.RunBefore = append(route.RunBefore, Runnable{item, false}) + r.RunBefore = append(r.RunBefore, Runnable{item, false}) } - return route + return r } -func (route *RouteImpl) LitBefore(items ...string) *RouteImpl { +func (r *RouteImpl) LitBefore(items ...string) *RouteImpl { for _, item := range items { - route.RunBefore = append(route.RunBefore, Runnable{item, true}) + r.RunBefore = append(r.RunBefore, Runnable{item, true}) } - return route + return r } -func (route *RouteImpl) LitBeforeMultiline(items ...string) *RouteImpl { +func (r *RouteImpl) LitBeforeMultiline(items ...string) *RouteImpl { for _, item := range items { for _, line := range strings.Split(item, "\n") { - route.LitBefore(strings.TrimSpace(line)) + r.LitBefore(strings.TrimSpace(line)) } } - return route + return r } -func (route *RouteImpl) hasBefore(items ...string) bool { +func (r *RouteImpl) hasBefore(items ...string) bool { for _, item := range items { - if route.hasBeforeItem(item) { + if r.hasBeforeItem(item) { return true } } return false } -func (route *RouteImpl) hasBeforeItem(item string) bool { - for _, before := range route.RunBefore { +func (r *RouteImpl) hasBeforeItem(item string) bool { + for _, before := range r.RunBefore { if before.Contents == item { return true } @@ -59,8 +59,8 @@ func (route *RouteImpl) hasBeforeItem(item string) bool { return false } -func (route *RouteImpl) NoGzip() *RouteImpl { - return route.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter) +func (r *RouteImpl) NoGzip() *RouteImpl { + return r.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter) if ok { w = gzw.ResponseWriter w.Header().Del("Content-Type") @@ -68,9 +68,9 @@ func (route *RouteImpl) NoGzip() *RouteImpl { }`) } -func (route *RouteImpl) NoHeader() *RouteImpl { - route.NoHead = true - return route +func (r *RouteImpl) NoHeader() *RouteImpl { + r.NoHead = true + return r } func blankRoute() *RouteImpl { @@ -149,8 +149,8 @@ func (action *uploadAction) MaxSizeVar(varName string) *RouteImpl { } type RouteSet struct { - Name string - Path string + Name string + Path string Items []*RouteImpl } diff --git a/routes/profile.go b/routes/profile.go index 81607966..8a2505d9 100644 --- a/routes/profile.go +++ b/routes/profile.go @@ -102,14 +102,16 @@ func ViewProfile(w http.ResponseWriter, r *http.Request, user c.User, header *c. prevScore := c.GetLevelScore(puser.Level) currentScore := puser.Score - prevScore nextScore := c.GetLevelScore(puser.Level+1) - prevScore - blocked, err := c.UserBlocks.IsBlockedBy(user.ID, puser.ID) - if err != nil { - return c.InternalError(err, w, r) - } - - blockedInv, err := c.UserBlocks.IsBlockedBy(puser.ID, user.ID) - if err != nil { - return c.InternalError(err, w, r) + var blocked, blockedInv bool + if user.Loggedin { + blocked, err = c.UserBlocks.IsBlockedBy(user.ID, puser.ID) + if err != nil { + return c.InternalError(err, w, r) + } + blockedInv, err = c.UserBlocks.IsBlockedBy(puser.ID, user.ID) + if err != nil { + return c.InternalError(err, w, r) + } } canMessage := (!blockedInv && user.Perms.UseConvos) || user.IsSuperMod canComment := !blockedInv && user.Perms.ViewTopic && user.Perms.CreateReply