diff --git a/actor.go b/actor.go index 2f84fc9..ad7c1e0 100644 --- a/actor.go +++ b/actor.go @@ -109,7 +109,7 @@ func (a *Actor) save() error { return nil } -func (a *Actor) PostArticle(url string) { +func (a *Actor) PostArticle(url string) error { create := make(map[string]interface{}) note := make(map[string]interface{}) create["@context"] = context() @@ -120,28 +120,26 @@ func (a *Actor) PostArticle(url string) { article := make(map[string]interface{}) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - return + return errors.New("failed to create request to fetch article") } req.Header.Add("User-Agent", fmt.Sprintf("%s %s", libName, version)) req.Header.Add("Accept", ContentTypeAs2) - resp, err := client.Do(req) - if err != nil || isSuccess(resp.StatusCode) { - fmt.Println("Failed to fetch article") - return + resp, err := http.DefaultClient.Do(req) + if err != nil || !isSuccess(resp.StatusCode) { + return errors.New("failed to fetch article") } respBody, err := ioutil.ReadAll(resp.Body) defer func() { _ = resp.Body.Close() }() if err != nil { - fmt.Println("Failed to read response from fetched article") - return + return errors.New("failed to read response from fetched article") } err = json.Unmarshal(respBody, &article) if err != nil { - fmt.Println("Failed to unmarshal fetched article") - return + return errors.New("failed to unmarshal fetched article") } create["object"] = article - go func() { _ = a.sendToFollowers(create) }() + go a.sendToFollowers(create) + return nil } // signedHTTPPost performs an HTTP post on behalf of Actor with the @@ -174,7 +172,7 @@ func (a *Actor) signedHTTPPost(content map[string]interface{}, to string) (err e if err != nil { return } - resp, err := client.Do(req) + resp, err := http.DefaultClient.Do(req) if err != nil { return } @@ -205,7 +203,7 @@ func (a *Actor) batchSend(activity map[string]interface{}, recipients []string) } // send to followers sends a batch of http posts to each one of the followers -func (a *Actor) sendToFollowers(activity map[string]interface{}) (err error) { +func (a *Actor) sendToFollowers(activity map[string]interface{}) { recipients := make([]string, len(a.followers)) i := 0 for _, inbox := range a.followers { diff --git a/feed.go b/feed.go index 49865b7..e40c9fe 100644 --- a/feed.go +++ b/feed.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "io/ioutil" + "net/http" ) func allFeedItems(url string) ([]string, error) { @@ -12,7 +13,7 @@ func allFeedItems(url string) ([]string, error) { Url string `json:"url"` } `json:"items"` }{} - resp, err := client.Get(url) + resp, err := http.DefaultClient.Get(url) if err != nil { return nil, errors.New("failed to get json feed") } diff --git a/http.go b/http.go index ea1aa25..f93fb43 100644 --- a/http.go +++ b/http.go @@ -66,7 +66,7 @@ func Serve() { inReplyTo, ok := object["inReplyTo"].(string) id, ok2 := object["id"].(string) if ok && ok2 && len(inReplyTo) > 0 && len(id) > 0 { - webmentionClient := webmention.New(&client) + webmentionClient := webmention.New(nil) endpoint, err := webmentionClient.DiscoverEndpoint(inReplyTo) if err != nil || len(endpoint) < 1 { return diff --git a/main.go b/main.go index 0a25493..6372266 100644 --- a/main.go +++ b/main.go @@ -28,9 +28,13 @@ func main() { for _, article := range articles { if oldSentItems[article] == false { fmt.Println("Send", article) - go actor.PostArticle(article) + err = actor.PostArticle(article) + if err == nil { + actor.sentItems[article] = true + } + } else { + actor.sentItems[article] = true } - actor.sentItems[article] = true } _ = actor.save() } diff --git a/remoteActor.go b/remoteActor.go index 4b5b91c..a767b4e 100644 --- a/remoteActor.go +++ b/remoteActor.go @@ -43,7 +43,7 @@ func get(iri string) (info map[string]interface{}, err error) { req.Header.Add("Accept", ContentTypeAs2) req.Header.Add("User-Agent", fmt.Sprintf("%s %s", libName, version)) req.Header.Add("Accept-Charset", "utf-8") - resp, err := client.Do(req) + resp, err := http.DefaultClient.Do(req) if err != nil { return } diff --git a/setup.go b/setup.go index 0bc902a..88ee6fb 100644 --- a/setup.go +++ b/setup.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "net/http" "os" "strings" ) @@ -15,8 +14,6 @@ var actors = make(map[string]*Actor) const libName = "jsonpub" const version = "0.0.1" -var client = http.Client{} - // Setup sets our environment up func Setup() { // Load base url