Optimise away blocked user queries on profiles for guests.

Shorten some things.
This commit is contained in:
Azareal 2019-10-20 11:42:19 +10:00
parent 47aa6faa11
commit bbef9cead8
3 changed files with 43 additions and 42 deletions

View File

@ -35,26 +35,26 @@ func main() {
r := &Router{} r := &Router{}
routes(r) routes(r)
var tmplVars = TmplVars{ tmplVars := TmplVars{
RouteList: r.routeList, RouteList: r.routeList,
RouteGroups: r.routeGroups, RouteGroups: r.routeGroups,
} }
var allRouteNames []RouteName var allRouteNames []RouteName
var allRouteMap = make(map[string]int) allRouteMap := make(map[string]int)
var out string var out string
var mapIt = func(name string) { mapIt := func(name string) {
allRouteNames = append(allRouteNames, RouteName{name, strings.Replace(name, "common.", "c.", -1)}) allRouteNames = append(allRouteNames, RouteName{name, strings.Replace(name, "common.", "c.", -1)})
allRouteMap[name] = len(allRouteNames) - 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++ { for i := 0; i < indent; i++ {
indentor += "\t" indentor += "\t"
} }
return indentor return indentor
} }
var runBefore = func(runnables []Runnable, indent int) (out string) { runBefore := func(runnables []Runnable, indent int) (out string) {
var indentor = countToIndents(indent) indentor := countToIndents(indent)
if len(runnables) > 0 { if len(runnables) > 0 {
for _, runnable := range runnables { for _, runnable := range runnables {
if runnable.Literal { if runnable.Literal {
@ -72,7 +72,7 @@ func main() {
for _, route := range r.routeList { for _, route := range r.routeList {
mapIt(route.Name) mapIt(route.Name)
var end = len(route.Path) - 1 end := len(route.Path) - 1
out += "\n\t\tcase \"" + route.Path[0:end] + "\":" out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
out += runBefore(route.RunBefore, 4) out += runBefore(route.RunBefore, 4)
out += "\n\t\t\tcounters.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")" out += "\n\t\t\tcounters.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
@ -91,12 +91,12 @@ func main() {
} }
for _, group := range r.routeGroups { 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 += "\n\t\tcase \"" + group.Path[0:end] + "\":"
out += runBefore(group.RunBefore, 3) out += runBefore(group.RunBefore, 3)
out += "\n\t\t\tswitch(req.URL.Path) {" out += "\n\t\t\tswitch(req.URL.Path) {"
var defaultRoute = blankRoute() defaultRoute := blankRoute()
for _, route := range group.RouteList { for _, route := range group.RouteList {
if group.Path == route.Path { if group.Path == route.Path {
defaultRoute = route defaultRoute = route
@ -301,7 +301,7 @@ func main() {
"Twitterbot": "twitter", "Twitterbot": "twitter",
"facebookexternalhit": "facebook", "facebookexternalhit": "facebook",
"Facebot": "facebook", "Facebot": "facebook",
"Applebot":"apple", "Applebot": "apple",
"Discourse": "discourse", "Discourse": "discourse",
"SemrushBot": "semrush", "SemrushBot": "semrush",
@ -309,7 +309,7 @@ func main() {
"zgrab": "zgrab", "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. */ /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package main package main
@ -882,10 +882,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
return err return err
} }
` `
var tmpl = template.Must(template.New("router").Parse(fileData)) tmpl := template.Must(template.New("router").Parse(fileData))
var b bytes.Buffer var b bytes.Buffer
err := tmpl.Execute(&b, tmplVars) if err := tmpl.Execute(&b, tmplVars); err != nil {
if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -18,40 +18,40 @@ type Runnable struct {
Literal bool Literal bool
} }
func (route *RouteImpl) Before(items ...string) *RouteImpl { func (r *RouteImpl) Before(items ...string) *RouteImpl {
for _, item := range items { 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 { 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 _, item := range items {
for _, line := range strings.Split(item, "\n") { 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 { for _, item := range items {
if route.hasBeforeItem(item) { if r.hasBeforeItem(item) {
return true return true
} }
} }
return false return false
} }
func (route *RouteImpl) hasBeforeItem(item string) bool { func (r *RouteImpl) hasBeforeItem(item string) bool {
for _, before := range route.RunBefore { for _, before := range r.RunBefore {
if before.Contents == item { if before.Contents == item {
return true return true
} }
@ -59,8 +59,8 @@ func (route *RouteImpl) hasBeforeItem(item string) bool {
return false return false
} }
func (route *RouteImpl) NoGzip() *RouteImpl { func (r *RouteImpl) NoGzip() *RouteImpl {
return route.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter) return r.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
w.Header().Del("Content-Type") w.Header().Del("Content-Type")
@ -68,9 +68,9 @@ func (route *RouteImpl) NoGzip() *RouteImpl {
}`) }`)
} }
func (route *RouteImpl) NoHeader() *RouteImpl { func (r *RouteImpl) NoHeader() *RouteImpl {
route.NoHead = true r.NoHead = true
return route return r
} }
func blankRoute() *RouteImpl { func blankRoute() *RouteImpl {
@ -149,8 +149,8 @@ func (action *uploadAction) MaxSizeVar(varName string) *RouteImpl {
} }
type RouteSet struct { type RouteSet struct {
Name string Name string
Path string Path string
Items []*RouteImpl Items []*RouteImpl
} }

View File

@ -102,14 +102,16 @@ func ViewProfile(w http.ResponseWriter, r *http.Request, user c.User, header *c.
prevScore := c.GetLevelScore(puser.Level) prevScore := c.GetLevelScore(puser.Level)
currentScore := puser.Score - prevScore currentScore := puser.Score - prevScore
nextScore := c.GetLevelScore(puser.Level+1) - prevScore nextScore := c.GetLevelScore(puser.Level+1) - prevScore
blocked, err := c.UserBlocks.IsBlockedBy(user.ID, puser.ID) var blocked, blockedInv bool
if err != nil { if user.Loggedin {
return c.InternalError(err, w, r) 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 { blockedInv, err = c.UserBlocks.IsBlockedBy(puser.ID, user.ID)
return c.InternalError(err, w, r) if err != nil {
return c.InternalError(err, w, r)
}
} }
canMessage := (!blockedInv && user.Perms.UseConvos) || user.IsSuperMod canMessage := (!blockedInv && user.Perms.UseConvos) || user.IsSuperMod
canComment := !blockedInv && user.Perms.ViewTopic && user.Perms.CreateReply canComment := !blockedInv && user.Perms.ViewTopic && user.Perms.CreateReply