reduce nesting with early returns

This commit is contained in:
Ted Unangst 2019-10-26 19:17:46 -04:00
parent f502872a48
commit 64c45abc74
1 changed files with 70 additions and 72 deletions

142
web.go
View File

@ -343,26 +343,26 @@ func inbox(w http.ResponseWriter, r *http.Request) {
log.Printf("pong from %s: %s", who, obj) log.Printf("pong from %s: %s", who, obj)
case "Follow": case "Follow":
obj, _ := j.GetString("object") obj, _ := j.GetString("object")
if obj == user.URL { if obj != user.URL {
log.Printf("updating honker follow: %s", who)
db := opendatabase()
row := db.QueryRow("select xid from honkers where xid = ? and userid = ? and flavor in ('dub', 'undub')", who, user.ID)
var x string
err = row.Scan(&x)
if err != sql.ErrNoRows {
log.Printf("duplicate follow request: %s", who)
_, err = stmtUpdateFlavor.Exec("dub", user.ID, who, "undub")
if err != nil {
log.Printf("error updating honker: %s", err)
}
} else {
stmtSaveDub.Exec(user.ID, who, who, "dub")
}
go rubadubdub(user, j)
} else {
log.Printf("can't follow %s", obj) log.Printf("can't follow %s", obj)
return
} }
log.Printf("updating honker follow: %s", who)
db := opendatabase()
row := db.QueryRow("select xid from honkers where xid = ? and userid = ? and flavor in ('dub', 'undub')", who, user.ID)
var x string
err = row.Scan(&x)
if err != sql.ErrNoRows {
log.Printf("duplicate follow request: %s", who)
_, err = stmtUpdateFlavor.Exec("dub", user.ID, who, "undub")
if err != nil {
log.Printf("error updating honker: %s", err)
}
} else {
stmtSaveDub.Exec(user.ID, who, who, "dub")
}
go rubadubdub(user, j)
case "Accept": case "Accept":
log.Printf("updating honker accept: %s", who) log.Printf("updating honker accept: %s", who)
_, err = stmtUpdateFlavor.Exec("sub", user.ID, who, "presub") _, err = stmtUpdateFlavor.Exec("sub", user.ID, who, "presub")
@ -394,23 +394,23 @@ func inbox(w http.ResponseWriter, r *http.Request) {
obj, ok := j.GetMap("object") obj, ok := j.GetMap("object")
if !ok { if !ok {
log.Printf("unknown undo no object") log.Printf("unknown undo no object")
} else { return
what, _ := obj.GetString("type") }
switch what { what, _ := obj.GetString("type")
case "Follow": switch what {
log.Printf("updating honker undo: %s", who) case "Follow":
_, err = stmtUpdateFlavor.Exec("undub", user.ID, who, "dub") log.Printf("updating honker undo: %s", who)
if err != nil { _, err = stmtUpdateFlavor.Exec("undub", user.ID, who, "dub")
log.Printf("error updating honker: %s", err) if err != nil {
return log.Printf("error updating honker: %s", err)
} return
case "Announce":
xid, _ := obj.GetString("object")
log.Printf("undo announce: %s", xid)
case "Like":
default:
log.Printf("unknown undo: %s", what)
} }
case "Announce":
xid, _ := obj.GetString("object")
log.Printf("undo announce: %s", xid)
case "Like":
default:
log.Printf("unknown undo: %s", what)
} }
default: default:
go xonksaver(user, j, origin) go xonksaver(user, j, origin)
@ -469,50 +469,48 @@ func serverinbox(w http.ResponseWriter, r *http.Request) {
return return
} }
m := re_ont.FindStringSubmatch(obj) m := re_ont.FindStringSubmatch(obj)
if len(m) == 2 { if len(m) != 2 {
ont := "#" + m[1]
log.Printf("%s wants to follow %s", who, ont)
db := opendatabase()
row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", ont, who, user.ID)
var x string
err = row.Scan(&x)
if err != sql.ErrNoRows {
log.Printf("duplicate follow request: %s", who)
_, err = stmtUpdateFlavor.Exec("dub", user.ID, who, ont, "undub")
if err != nil {
log.Printf("error updating honker: %s", err)
}
} else {
stmtSaveDub.Exec(user.ID, ont, who, "dub")
}
go rubadubdub(user, j)
} else {
log.Printf("not sure how to handle this") log.Printf("not sure how to handle this")
return
} }
ont := "#" + m[1]
log.Printf("%s wants to follow %s", who, ont)
db := opendatabase()
row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", ont, who, user.ID)
var x string
err = row.Scan(&x)
if err != sql.ErrNoRows {
log.Printf("duplicate follow request: %s", who)
_, err = stmtUpdateFlavor.Exec("dub", user.ID, who, ont, "undub")
if err != nil {
log.Printf("error updating honker: %s", err)
}
} else {
stmtSaveDub.Exec(user.ID, ont, who, "dub")
}
go rubadubdub(user, j)
case "Undo": case "Undo":
obj, ok := j.GetMap("object") obj, ok := j.GetMap("object")
if !ok { if !ok {
log.Printf("unknown undo no object") log.Printf("unknown undo no object")
} else { return
what, _ := obj.GetString("type") }
switch what { what, _ := obj.GetString("type")
case "Follow": if what != "Follow" {
targ, _ := obj.GetString("object") log.Printf("unknown undo: %s", what)
m := re_ont.FindStringSubmatch(targ) }
if len(m) == 2 { targ, _ := obj.GetString("object")
ont := "#" + m[1] m := re_ont.FindStringSubmatch(targ)
log.Printf("updating honker undo: %s", who, ont) if len(m) != 2 {
_, err = stmtUpdateFlavor.Exec("undub", user.ID, who, ont, "dub") log.Printf("not sure how to handle this")
if err != nil { return
log.Printf("error updating honker: %s", err) }
return ont := "#" + m[1]
} log.Printf("updating honker undo: %s", who, ont)
} else { _, err = stmtUpdateFlavor.Exec("undub", user.ID, who, ont, "dub")
log.Printf("not sure how to handle this") if err != nil {
} log.Printf("error updating honker: %s", err)
default: return
log.Printf("unknown undo: %s", what)
}
} }
} }
} }