MCPcopy
hub / github.com/writefreely/writefreely / makeActivityPost

Function makeActivityPost

activitypub.go:699–751  ·  view source on GitHub ↗
(hostName string, p *activitystreams.Person, url string, m interface{})

Source from the content-addressed store, hash-verified

697}
698
699func makeActivityPost(hostName string, p *activitystreams.Person, url string, m interface{}) error {
700 log.Info("POST %s", url)
701 b, err := json.Marshal(m)
702 if err != nil {
703 return err
704 }
705
706 r, _ := http.NewRequest("POST", url, bytes.NewBuffer(b))
707 r.Header.Add("Content-Type", "application/activity+json")
708 r.Header.Set("User-Agent", ServerUserAgent(hostName))
709 h := sha256.New()
710 h.Write(b)
711 r.Header.Add("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(h.Sum(nil)))
712
713 // Sign using the 'Signature' header
714 privKey, err := activitypub.DecodePrivateKey(p.GetPrivKey())
715 if err != nil {
716 return err
717 }
718 signer := httpsig.NewSigner(p.PublicKey.ID, privKey, httpsig.RSASHA256, []string{"(request-target)", "date", "host", "digest"})
719 err = signer.SignSigHeader(r)
720 if err != nil {
721 log.Error("Can't sign: %v", err)
722 }
723
724 if debugging {
725 dump, err := httputil.DumpRequestOut(r, true)
726 if err != nil {
727 log.Error("Can't dump: %v", err)
728 } else {
729 log.Info("%s", dump)
730 }
731 }
732
733 resp, err := activityPubClient().Do(r)
734 if err != nil {
735 return err
736 }
737 if resp != nil && resp.Body != nil {
738 defer resp.Body.Close()
739 }
740
741 body, err := io.ReadAll(resp.Body)
742 if err != nil {
743 return err
744 }
745 if debugging {
746 log.Info("Status : %s", resp.Status)
747 log.Info("Response: %s", body)
748 }
749
750 return nil
751}
752
753func resolveIRI(hostName, url string) ([]byte, error) {
754 log.Info("GET %s", url)

Callers 3

deleteFederatedPostFunction · 0.85
federatePostFunction · 0.85

Calls 4

ServerUserAgentFunction · 0.85
activityPubClientFunction · 0.85
CloseMethod · 0.80
DoMethod · 0.65

Tested by

no test coverage detected