From bef846b77490b6f4eb06dcec70aa684490ea0fd7 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Thu, 22 Jul 2021 15:49:34 -0700 Subject: added actor information requests to cache resets on server restart --- main.go | 82 ++++++++++++++++++++++++++++++++++++++++----------------- static/faq.html | 2 +- 2 files changed, 59 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 601394f..60f537f 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,8 @@ var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/acti var MediaHashs = make(map[string]string) +var ActorCache = make(map[string]Actor) + func main() { CreatedNeededDirectories() @@ -1197,7 +1199,13 @@ func main() { return } - if !IsActorLocal(db, TP + "" + actorDomain[1] + "/" + actorDomain[0]) { + if actorDomain[0] == "main" { + actorDomain[0] = "" + } else { + actorDomain[0] = "/" + actorDomain[0] + } + + if !IsActorLocal(db, TP + "" + actorDomain[1] + "" + actorDomain[0]) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("actor not local")) return @@ -1209,7 +1217,7 @@ func main() { finger.Subject = "acct:" + actorDomain[0] + "@" + actorDomain[1] link.Rel = "self" link.Type = "application/activity+json" - link.Href = TP + "" + actorDomain[1] + "/" + actorDomain[0] + link.Href = TP + "" + actorDomain[1] + "" + actorDomain[0] finger.Links = append(finger.Links, link) @@ -1721,26 +1729,34 @@ func GetActor(id string) Actor { return respActor } - req, err := http.NewRequest("GET", id, nil) + actor, instance := GetActorInstance(id) - CheckError(err, "error with getting actor req") + if ActorCache[actor + "@" + instance].Id != "" { + respActor = ActorCache[actor + "@" + instance] + } else { + req, err := http.NewRequest("GET", id, nil) - req.Header.Set("Accept", activitystreams) + CheckError(err, "error with getting actor req") - resp, err := RouteProxy(req) + req.Header.Set("Accept", activitystreams) - if err != nil { - fmt.Println("error with getting actor resp " + id) - return respActor - } + resp, err := RouteProxy(req) - defer resp.Body.Close() + if err != nil { + fmt.Println("error with getting actor resp " + id) + return respActor + } - body, _ := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) - err = json.Unmarshal(body, &respActor) + err = json.Unmarshal(body, &respActor) - CheckError(err, "error getting actor from body") + CheckError(err, "error getting actor from body") + + ActorCache[actor + "@" + instance] = respActor + } return respActor } @@ -2489,21 +2505,30 @@ func CreatedNeededDirectories() { //looks for actor with pattern of board@instance func FingerActor(path string) Actor{ + var nActor Actor + actor, instance := GetActorInstance(path) - r := FingerRequest(actor, instance) + if actor == "" && instance == "" { + return nActor + } - var nActor Actor + if ActorCache[actor + "@" + instance].Id != "" { + nActor = ActorCache[actor + "@" + instance] + } else { + r := FingerRequest(actor, instance) + if r != nil && r.StatusCode == 200 { + defer r.Body.Close() + + body, _ := ioutil.ReadAll(r.Body) - if r != nil && r.StatusCode == 200 { - defer r.Body.Close() - - body, _ := ioutil.ReadAll(r.Body) + err := json.Unmarshal(body, &nActor) - err := json.Unmarshal(body, &nActor) + CheckError(err, "error getting fingerrequet resp from json body") - CheckError(err, "error getting fingerrequet resp from json body") - } + ActorCache[actor + "@" + instance] = nActor + } + } return nActor } @@ -2561,7 +2586,16 @@ func GetActorInstance(path string) (string, string) { } } - re = regexp.MustCompile(`(https?:\\)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)(\/([\w\d-_.]+))?`) + re = regexp.MustCompile(`(https?://)(www)?([\w\d-_.:]+)(/|\s+|\r|\r\n)?$`) + mainActor := re.MatchString(path) + if(mainActor) { + match := re.FindStringSubmatch(path) + if(len(match) > 2) { + return "main", match[3] + } + } + + re = regexp.MustCompile(`(https?://)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)(\/([\w\d-_.]+))?`) httpFormat := re.MatchString(path) if(httpFormat) { diff --git a/static/faq.html b/static/faq.html index 9baf8ca..6f60d62 100644 --- a/static/faq.html +++ b/static/faq.html @@ -56,7 +56,7 @@

Soon™.

What version is this FChannel instance?

-

v0.0.11-dev

+

v0.0.12-dev

[Home][Rules][FAQ] -- cgit v1.2.3