2022-06-19 15:39:22 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-12-31 17:58:01 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-19 15:39:22 +00:00
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-07-27 15:25:40 +00:00
|
|
|
"code.gitea.io/gitea/modules/forgefed"
|
2022-06-19 15:39:22 +00:00
|
|
|
"code.gitea.io/gitea/modules/httplib"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2022-07-11 22:27:57 +00:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2022-07-14 03:10:03 +00:00
|
|
|
"github.com/go-ap/jsonld"
|
2022-06-19 15:39:22 +00:00
|
|
|
)
|
|
|
|
|
2022-07-14 03:10:03 +00:00
|
|
|
// Fetch a remote ActivityStreams object
|
2022-06-19 15:39:22 +00:00
|
|
|
func Fetch(iri *url.URL) (b []byte, err error) {
|
|
|
|
req := httplib.NewRequest(iri.String(), http.MethodGet)
|
|
|
|
req.Header("Accept", ActivityStreamsContentType)
|
|
|
|
req.Header("User-Agent", "Gitea/"+setting.AppVer)
|
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err = fmt.Errorf("url IRI fetch [%s] failed with status (%d): %s", iri, resp.StatusCode, resp.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, err = io.ReadAll(io.LimitReader(resp.Body, setting.Federation.MaxSize))
|
2022-06-26 02:40:28 +00:00
|
|
|
return b, err
|
2022-06-19 15:39:22 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 03:10:03 +00:00
|
|
|
// Send an activity
|
2022-07-24 03:12:09 +00:00
|
|
|
func Send(user *user_model.User, activity *ap.Activity) error {
|
2022-07-14 03:10:03 +00:00
|
|
|
binary, err := jsonld.WithContext(
|
|
|
|
jsonld.IRI(ap.ActivityBaseURI),
|
|
|
|
jsonld.IRI(ap.SecurityContextURI),
|
|
|
|
jsonld.IRI(forgefed.ForgeFedNamespaceURI),
|
|
|
|
).Marshal(activity)
|
2022-06-19 15:39:22 +00:00
|
|
|
if err != nil {
|
2022-07-24 03:12:09 +00:00
|
|
|
return err
|
2022-06-19 15:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, to := range activity.To {
|
2022-11-28 19:22:23 +00:00
|
|
|
client, _ := NewClient(user, user.GetIRI()+"#main-key")
|
2022-07-25 20:45:15 +00:00
|
|
|
resp, _ := client.Post(binary, to.GetLink().String())
|
2022-06-19 15:39:22 +00:00
|
|
|
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, setting.Federation.MaxSize))
|
2022-07-17 16:19:48 +00:00
|
|
|
log.Trace("Response from sending activity", string(respBody))
|
2022-06-19 15:39:22 +00:00
|
|
|
}
|
2022-07-24 03:12:09 +00:00
|
|
|
return err
|
2022-06-19 15:39:22 +00:00
|
|
|
}
|