From 9db3c05ed63b5257da3048858e89f0ff7d3b0dfc Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 23 Dec 2020 08:20:03 +0000 Subject: [PATCH] few bugs --- instance.go | 4 ++++ poll.go | 5 +++-- retrieve.go | 33 ++++++++++++++++++++++++++++++--- stream.go | 3 ++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/instance.go b/instance.go index 9be7382..4c3d616 100644 --- a/instance.go +++ b/instance.go @@ -74,10 +74,12 @@ func StartInstance(endpoint string) { http_client, nodeinfo := GetNodeInfo(endpoint) ri_mutex.Lock() m := runninginstances[endpoint] + ri_mutex.Unlock() if nodeinfo.Software.Name == "" { m.Software = "" m.LastRun = time.Now().Format(time.RFC3339) m.Status = UNSUPPORTED_INSTANCE + ri_mutex.Lock() runninginstances[endpoint] = m ri_mutex.Unlock() return @@ -87,12 +89,14 @@ func StartInstance(endpoint string) { if nodeinfo.Software.Name == "pleroma" { log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name) m.CaptureType = "Poll" + ri_mutex.Lock() runninginstances[endpoint] = m ri_mutex.Unlock() PollMastodonPleroma(endpoint, http_client) } else if nodeinfo.Software.Name == "mastodon" { log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name) m.CaptureType = "Stream" + ri_mutex.Lock() runninginstances[endpoint] = m ri_mutex.Unlock() StreamMastodon(endpoint) diff --git a/poll.go b/poll.go index 76dc151..be8df90 100644 --- a/poll.go +++ b/poll.go @@ -70,7 +70,8 @@ func PollMastodonPleroma(endpoint string, http_client http.Client) { use_auth = true client_id, client_secret, err = get_client(endpoint, &http_client) if err != nil { - log.Fatal("Unable to register client: ", err) + log.Print("Unable to register client: ", err) + return } oauthData, err = oauth_login(endpoint, extaccount.Username, extaccount.Password, client_id, client_secret) @@ -114,7 +115,7 @@ func PollMastodonPleroma(endpoint string, http_client http.Client) { ri_mutex.Lock() runninginstances[endpoint] = m ri_mutex.Unlock() - log.Fatal("Failure here", err.Error()) + log.Print("Failure here", err.Error()) return } diff --git a/retrieve.go b/retrieve.go index 5fbd805..a805e91 100644 --- a/retrieve.go +++ b/retrieve.go @@ -7,6 +7,7 @@ import ( "encoding/json" "github.com/jackc/pgx/pgxpool" "time" + "io/ioutil" "net/http" ) @@ -110,6 +111,10 @@ func check_post(uri string) (PostJson, error) { return postjson, nil } endslash := strings.Index(uri[8:], "/") + if endslash == -1 { + return postjson, nil + } + log.Print("Was: " + uri, " ", endslash) postjson.instance = uri[8:endslash+8] @@ -118,17 +123,37 @@ func check_post(uri string) (PostJson, error) { req.Header.Add("Accept", "application/ld+json") resp, err := o.client.Do(req) + if err != nil { + return postjson, nil + } +/* err = json.NewDecoder(resp.Body).Decode(&postjson) if err != nil { return postjson, err } +*/ + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return postjson, nil + } + err = json.Unmarshal(body, &postjson) + if err != nil { + return postjson, nil + } if postjson.InReplyTo != "" && postjson.InReplyTo != uri { log.Print("GOING INTO NEW POST: " + postjson.InReplyTo + " " + uri) go check_post(postjson.InReplyTo) } + // If AttributedTo is blank, this is likely an authentication failure + // For now, skip it... + + if postjson.AttributedTo == "" { + return postjson, nil + } check_user(postjson.AttributedTo) // This must be done BEFORE the `INSERT INTO posts` below connrequest = ConnRequest{} @@ -141,12 +166,12 @@ func check_post(uri string) (PostJson, error) { _, err = myconn.Exec(context.Background(), "INSERT INTO posts (id, inreplyto, published, summary, content, normalized, attributedto, posthash, instance) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)", postjson.ID, postjson.InReplyTo, postjson.Published, postjson.Summary, postjson.Content, postjson.normalized, postjson.AttributedTo, postjson.posthash, postjson.instance) close(connrequest.b) if err != nil { - log.Print("INSERT posts error: ", err) + log.Print("INSERT posts error of " + uri + ": ", err) return postjson, err } - for _, to := range postjson.To { - if to != "https://www.w3.org/ns/activitystreams#Public" { + for _, to := range postjson.To{ + if to != "https://www.w3.org/ns/activitystreams#Public" && to != "" { go check_user(to) } } @@ -171,7 +196,9 @@ func check_user(uri string) (UserJson, error) { if err == nil { return userjson, nil } + log.Print("The URI: " + uri) endslash := strings.Index(uri[8:], "/") + log.Print("on " + uri + " ", endslash) userjson.instance = uri[8:endslash+8] o := GetHTTPSession(userjson.instance) diff --git a/stream.go b/stream.go index d57d48d..c3a6dda 100644 --- a/stream.go +++ b/stream.go @@ -46,7 +46,8 @@ func StreamMastodon(endpoint string) { resp, err := http_client.Do(req) if err != nil { - log.Fatal("Error occured for " + api_timeline) + log.Print("Unable to stream " + api_timeline + ": ", err) + return } defer resp.Body.Close()