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{}
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
@ -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)
}

View File

@ -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 {

View File

@ -102,15 +102,17 @@ 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)
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)
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