diff options
author | FChannel <> | 2021-07-22 15:49:34 -0700 |
---|---|---|
committer | FChannel <> | 2021-07-22 15:49:34 -0700 |
commit | bef846b77490b6f4eb06dcec70aa684490ea0fd7 (patch) | |
tree | 931b5d8076ba2a8ebe893f6dd2e519a117b84db4 /main.go | |
parent | fa895350a8bdf2eb93495dbf569fa3fe0680d2af (diff) |
added actor information requests to cache resets on server restart
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 82 |
1 files changed, 58 insertions, 24 deletions
@@ -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) { |