From 6be21e9f0b8965ea4d61e1f9c91b88f7ed0a1074 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 10 May 2019 18:18:32 -0400 Subject: [PATCH] can cache fishing results in db --- activity.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ honk.go | 41 ------------------------------------- 2 files changed, 58 insertions(+), 41 deletions(-) diff --git a/activity.go b/activity.go index 5d35213..452318a 100644 --- a/activity.go +++ b/activity.go @@ -845,3 +845,61 @@ func asjonker(user *WhatAbout) map[string]interface{} { return j } + +var handfull = make(map[string]string) +var handlock sync.Mutex + +func gofish(name string) string { + if name[0] == '@' { + name = name[1:] + } + m := strings.Split(name, "@") + if len(m) != 2 { + log.Printf("bad fish name: %s", name) + return "" + } + handlock.Lock() + ref, ok := handfull[name] + handlock.Unlock() + if ok { + return ref + } + db := opendatabase() + row := db.QueryRow("select ibox from xonkers where xid = ?", name) + var href string + err := row.Scan(&href) + if err == nil { + handlock.Lock() + handfull[name] = href + handlock.Unlock() + return href + } + log.Printf("fishing for %s", name) + j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name)) + if err != nil { + log.Printf("failed to go fish %s: %s", name, err) + handlock.Lock() + handfull[name] = "" + handlock.Unlock() + return "" + } + links, _ := jsongetarray(j, "links") + for _, l := range links { + href, _ := jsongetstring(l, "href") + rel, _ := jsongetstring(l, "rel") + t, _ := jsongetstring(l, "type") + if rel == "self" && friendorfoe(t) { + db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)", + name, href, "", "", "") + handlock.Lock() + handfull[name] = href + handlock.Unlock() + return href + } + } + handlock.Lock() + handfull[name] = "" + handlock.Unlock() + return "" +} + diff --git a/honk.go b/honk.go index 0b81110..641f501 100644 --- a/honk.go +++ b/honk.go @@ -31,7 +31,6 @@ import ( "sort" "strconv" "strings" - "sync" "time" "github.com/gorilla/mux" @@ -982,46 +981,6 @@ func showcombos(w http.ResponseWriter, r *http.Request) { } } -var handfull = make(map[string]string) -var handlock sync.Mutex - -func gofish(name string) string { - if name[0] == '@' { - name = name[1:] - } - m := strings.Split(name, "@") - if len(m) != 2 { - log.Printf("bad fish name: %s", name) - return "" - } - handlock.Lock() - ref, ok := handfull[name] - handlock.Unlock() - if ok { - return ref - } - j, err := GetJunk(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name)) - handlock.Lock() - defer handlock.Unlock() - if err != nil { - log.Printf("failed to go fish %s: %s", name, err) - handfull[name] = "" - return "" - } - links, _ := jsongetarray(j, "links") - for _, l := range links { - href, _ := jsongetstring(l, "href") - rel, _ := jsongetstring(l, "rel") - t, _ := jsongetstring(l, "type") - if rel == "self" && friendorfoe(t) { - handfull[name] = href - return href - } - } - handfull[name] = "" - return "" -} - func savehonker(w http.ResponseWriter, r *http.Request) { u := login.GetUserInfo(r) name := r.FormValue("name")