Fix a bunch of lint errors (still 10 more to fix 🙁)
This commit is contained in:
parent
ab540d07be
commit
bffb682117
12 changed files with 89 additions and 65 deletions
|
@ -50,18 +50,18 @@ func (p Push) MarshalJSON() ([]byte, error) {
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Push) UnmarshalJSON(data []byte) error {
|
func (p *Push) UnmarshalJSON(data []byte) error {
|
||||||
p := fastjson.Parser{}
|
ps := fastjson.Parser{}
|
||||||
val, err := p.ParseBytes(data)
|
val, err := ps.ParseBytes(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Target = ap.JSONGetItem(val, "target")
|
p.Target = ap.JSONGetItem(val, "target")
|
||||||
c.HashBefore = ap.JSONGetItem(val, "hashBefore")
|
p.HashBefore = ap.JSONGetItem(val, "hashBefore")
|
||||||
c.HashAfter = ap.JSONGetItem(val, "hashAfter")
|
p.HashAfter = ap.JSONGetItem(val, "hashAfter")
|
||||||
|
|
||||||
return ap.OnObject(&c.Object, func(a *ap.Object) error {
|
return ap.OnObject(&p.Object, func(a *ap.Object) error {
|
||||||
return ap.LoadObject(val, a)
|
return ap.LoadObject(val, a)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package forgefed
|
package forgefed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -22,25 +22,29 @@ func AuthorizeInteraction(c *context.Context) {
|
||||||
}
|
}
|
||||||
resp, err := Fetch(uri)
|
resp, err := Fetch(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("Could not fetch remote URI", err)
|
c.ServerError("Fetch", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var object map[string]interface{}
|
var object map[string]interface{}
|
||||||
err = json.Unmarshal(resp, &object)
|
err = json.Unmarshal(resp, &object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("Could not unmarshal response into JSON", err)
|
c.ServerError("Unmarshal", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch object["type"] {
|
switch object["type"] {
|
||||||
case "Person":
|
case "Person":
|
||||||
var person ap.Person
|
var person ap.Person
|
||||||
person.UnmarshalJSON(resp)
|
err = person.UnmarshalJSON(resp)
|
||||||
err = FederatedUserNew(c, person)
|
if err != nil {
|
||||||
/*if err != nil {
|
c.ServerError("UnmarshalJSON", err)
|
||||||
c.ServerError("Could not create new federated user", err)
|
|
||||||
return
|
return
|
||||||
}*/
|
}
|
||||||
|
err = FederatedUserNew(c, person)
|
||||||
|
if err != nil {
|
||||||
|
c.ServerError("FederatedUserNew", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
name, err := personIRIToName(person.GetLink())
|
name, err := personIRIToName(person.GetLink())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("personIRIToName", err)
|
c.ServerError("personIRIToName", err)
|
||||||
|
|
|
@ -11,17 +11,15 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/issues"
|
"code.gitea.io/gitea/models/issues"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/modules/log"
|
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create a comment
|
// Create a comment
|
||||||
func Comment(ctx context.Context, note ap.Note) {
|
func Comment(ctx context.Context, note ap.Note) error {
|
||||||
actorUser, err := personIRIToUser(ctx, note.AttributedTo.GetLink())
|
actorUser, err := personIRIToUser(ctx, note.AttributedTo.GetLink())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Couldn't find actor", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move IRI processing stuff to iri.go
|
// TODO: Move IRI processing stuff to iri.go
|
||||||
|
@ -33,10 +31,11 @@ func Comment(ctx context.Context, note ap.Note) {
|
||||||
|
|
||||||
idx, _ := strconv.ParseInt(contextSplit[len(contextSplit)-1], 10, 64)
|
idx, _ := strconv.ParseInt(contextSplit[len(contextSplit)-1], 10, 64)
|
||||||
issue, _ := issues.GetIssueByIndex(repo.ID, idx)
|
issue, _ := issues.GetIssueByIndex(repo.ID, idx)
|
||||||
issues.CreateCommentCtx(ctx, &issues.CreateCommentOptions{
|
_, err = issues.CreateCommentCtx(ctx, &issues.CreateCommentOptions{
|
||||||
Doer: actorUser,
|
Doer: actorUser,
|
||||||
Repo: repo,
|
Repo: repo,
|
||||||
Issue: issue,
|
Issue: issue,
|
||||||
Content: note.Content.String(),
|
Content: note.Content.String(),
|
||||||
})
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,19 +9,17 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/log"
|
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Process a Follow activity
|
// Process a Follow activity
|
||||||
func Follow(ctx context.Context, follow ap.Follow) {
|
func Follow(ctx context.Context, follow ap.Follow) error {
|
||||||
// Actor is the user performing the follow
|
// Actor is the user performing the follow
|
||||||
actorIRI := follow.Actor.GetID()
|
actorIRI := follow.Actor.GetID()
|
||||||
actorUser, err := personIRIToUser(ctx, actorIRI)
|
actorUser, err := personIRIToUser(ctx, actorIRI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Couldn't find actor user for follow", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Object is the user being followed
|
// Object is the user being followed
|
||||||
|
@ -29,29 +27,30 @@ func Follow(ctx context.Context, follow ap.Follow) {
|
||||||
objectUser, err := personIRIToUser(ctx, objectIRI)
|
objectUser, err := personIRIToUser(ctx, objectIRI)
|
||||||
// Must be a local user
|
// Must be a local user
|
||||||
if err != nil || strings.Contains(objectUser.Name, "@") {
|
if err != nil || strings.Contains(objectUser.Name, "@") {
|
||||||
log.Warn("Couldn't find object user for follow", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user_model.FollowUser(actorUser.ID, objectUser.ID)
|
err = user_model.FollowUser(actorUser.ID, objectUser.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Send back an Accept activity
|
// Send back an Accept activity
|
||||||
accept := ap.AcceptNew(objectIRI, follow)
|
accept := ap.AcceptNew(objectIRI, follow)
|
||||||
accept.Actor = ap.Person{ID: objectIRI}
|
accept.Actor = ap.Person{ID: objectIRI}
|
||||||
accept.To = ap.ItemCollection{ap.IRI(actorIRI.String() + "/inbox")}
|
accept.To = ap.ItemCollection{ap.IRI(actorIRI.String() + "/inbox")}
|
||||||
accept.Object = follow
|
accept.Object = follow
|
||||||
Send(objectUser, accept)
|
return Send(objectUser, accept)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process a Undo follow activity
|
// Process a Undo follow activity
|
||||||
func Unfollow(ctx context.Context, unfollow ap.Undo) {
|
func Unfollow(ctx context.Context, unfollow ap.Undo) error {
|
||||||
follow := unfollow.Object.(*ap.Follow)
|
follow := unfollow.Object.(*ap.Follow)
|
||||||
// Actor is the user performing the undo follow
|
// Actor is the user performing the undo follow
|
||||||
actorIRI := follow.Actor.GetID()
|
actorIRI := follow.Actor.GetID()
|
||||||
actorUser, err := personIRIToUser(ctx, actorIRI)
|
actorUser, err := personIRIToUser(ctx, actorIRI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Couldn't find actor user for follow", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Object is the user being unfollowed
|
// Object is the user being unfollowed
|
||||||
|
@ -59,9 +58,8 @@ func Unfollow(ctx context.Context, unfollow ap.Undo) {
|
||||||
objectUser, err := personIRIToUser(ctx, objectIRI)
|
objectUser, err := personIRIToUser(ctx, objectIRI)
|
||||||
// Must be a local user
|
// Must be a local user
|
||||||
if err != nil || strings.Contains(objectUser.Name, "@") {
|
if err != nil || strings.Contains(objectUser.Name, "@") {
|
||||||
log.Warn("Couldn't find object user for follow", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user_model.UnfollowUser(actorUser.ID, objectUser.ID)
|
return user_model.UnfollowUser(actorUser.ID, objectUser.ID)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ import (
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Fork(ctx context.Context, instance, username, reponame, destUsername string) {
|
func Fork(ctx context.Context, instance, username, reponame, destUsername string) error {
|
||||||
// TODO: Clean this up
|
// TODO: Clean this up
|
||||||
|
|
||||||
// Migrate repository code
|
// Migrate repository code
|
||||||
|
@ -42,10 +42,10 @@ func Fork(ctx context.Context, instance, username, reponame, destUsername string
|
||||||
// repo.ForkedFrom = forgefed.RepositoryNew(ap.IRI())
|
// repo.ForkedFrom = forgefed.RepositoryNew(ap.IRI())
|
||||||
create.Object = repo
|
create.Object = repo
|
||||||
|
|
||||||
Send(user, &create)
|
return Send(user, &create)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ForkFromCreate(ctx context.Context, repository forgefed.Repository) {
|
func ForkFromCreate(ctx context.Context, repository forgefed.Repository) error {
|
||||||
// TODO: Clean this up
|
// TODO: Clean this up
|
||||||
|
|
||||||
// Don't create an actual copy of the remote repo!
|
// Don't create an actual copy of the remote repo!
|
||||||
|
@ -66,5 +66,5 @@ func ForkFromCreate(ctx context.Context, repository forgefed.Repository) {
|
||||||
repo, _ := repo_model.GetRepositoryByOwnerAndName("Ta180m", reponame) // hardcoded for now :(
|
repo, _ := repo_model.GetRepositoryByOwnerAndName("Ta180m", reponame) // hardcoded for now :(
|
||||||
|
|
||||||
_, err := repo_service.ForkRepository(ctx, user, user, repo_service.ForkRepoOptions{BaseRepo: repo, Name: reponame, Description: "this is a remote fork"})
|
_, err := repo_service.ForkRepository(ctx, user, user, repo_service.ForkRepoOptions{BaseRepo: repo, Name: reponame, Description: "this is a remote fork"})
|
||||||
log.Warn("Couldn't create copy of remote fork", err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,11 +28,10 @@ func personIRIToName(personIRI ap.IRI) (string, error) {
|
||||||
if instance == setting.Domain {
|
if instance == setting.Domain {
|
||||||
// Local user
|
// Local user
|
||||||
return name, nil
|
return name, nil
|
||||||
} else {
|
}
|
||||||
// Remote user
|
// Remote user
|
||||||
// Get name in username@instance.com format
|
// Get name in username@instance.com format
|
||||||
return name + "@" + instance, nil
|
return name + "@" + instance, nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the user corresponding to a Person actor IRI
|
// Returns the user corresponding to a Person actor IRI
|
||||||
|
@ -63,10 +62,9 @@ func repositoryIRIToName(repoIRI ap.IRI) (string, string, error) {
|
||||||
if instance == setting.Domain {
|
if instance == setting.Domain {
|
||||||
// Local repo
|
// Local repo
|
||||||
return username, reponame, nil
|
return username, reponame, nil
|
||||||
} else {
|
}
|
||||||
// Remote repo
|
// Remote repo
|
||||||
return username + "@" + instance, reponame, nil
|
return username + "@" + instance, reponame, nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the repository corresponding to a Repository actor IRI
|
// Returns the repository corresponding to a Repository actor IRI
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FederatedRepoNew(user *user_model.User, name string, IRI ap.IRI) (*repo_model.Repository, error) {
|
func FederatedRepoNew(user *user_model.User, name string, iri ap.IRI) (*repo_model.Repository, error) {
|
||||||
// TODO: also handle forks
|
// TODO: also handle forks
|
||||||
return repository.CreateRepository(user, user, models.CreateRepoOptions{
|
return repository.CreateRepository(user, user, models.CreateRepoOptions{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
|
|
@ -40,15 +40,14 @@ func Fetch(iri *url.URL) (b []byte, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send an activity
|
// Send an activity
|
||||||
func Send(user *user_model.User, activity *ap.Activity) {
|
func Send(user *user_model.User, activity *ap.Activity) error {
|
||||||
binary, err := jsonld.WithContext(
|
binary, err := jsonld.WithContext(
|
||||||
jsonld.IRI(ap.ActivityBaseURI),
|
jsonld.IRI(ap.ActivityBaseURI),
|
||||||
jsonld.IRI(ap.SecurityContextURI),
|
jsonld.IRI(ap.SecurityContextURI),
|
||||||
jsonld.IRI(forgefed.ForgeFedNamespaceURI),
|
jsonld.IRI(forgefed.ForgeFedNamespaceURI),
|
||||||
).Marshal(activity)
|
).Marshal(activity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Marshal", err)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, to := range activity.To {
|
for _, to := range activity.To {
|
||||||
|
@ -57,4 +56,5 @@ func Send(user *user_model.User, activity *ap.Activity) {
|
||||||
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, setting.Federation.MaxSize))
|
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, setting.Federation.MaxSize))
|
||||||
log.Trace("Response from sending activity", string(respBody))
|
log.Trace("Response from sending activity", string(respBody))
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,14 +129,18 @@ func PersonInbox(ctx *context.APIContext) {
|
||||||
// Process activity
|
// Process activity
|
||||||
switch activity.Type {
|
switch activity.Type {
|
||||||
case ap.FollowType:
|
case ap.FollowType:
|
||||||
activitypub.Follow(ctx, activity)
|
err = activitypub.Follow(ctx, activity)
|
||||||
case ap.UndoType:
|
case ap.UndoType:
|
||||||
activitypub.Unfollow(ctx, activity)
|
err = activitypub.Unfollow(ctx, activity)
|
||||||
default:
|
default:
|
||||||
log.Info("Incoming unsupported ActivityStreams type: %s", activity.GetType())
|
log.Info("Incoming unsupported ActivityStreams type: %s", activity.GetType())
|
||||||
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")
|
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("Could not process activity", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ctx.Status(http.StatusNoContent)
|
ctx.Status(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
@ -180,7 +184,11 @@ func PersonOutbox(ctx *context.APIContext) {
|
||||||
object := ap.Note{Type: ap.NoteType, Content: ap.NaturalLanguageValuesNew()}
|
object := ap.Note{Type: ap.NoteType, Content: ap.NaturalLanguageValuesNew()}
|
||||||
object.Content.Set("en", ap.Content(action.GetRepoName()))
|
object.Content.Set("en", ap.Content(action.GetRepoName()))
|
||||||
create := ap.Create{Type: ap.CreateType, Object: object}
|
create := ap.Create{Type: ap.CreateType, Object: object}
|
||||||
outbox.OrderedItems.Append(create)
|
err := outbox.OrderedItems.Append(create)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("OrderedItems.Append", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +202,11 @@ func PersonOutbox(ctx *context.APIContext) {
|
||||||
object := ap.Note{Type: ap.NoteType, Content: ap.NaturalLanguageValuesNew()}
|
object := ap.Note{Type: ap.NoteType, Content: ap.NaturalLanguageValuesNew()}
|
||||||
object.Content.Set("en", ap.Content("Starred "+star.Name))
|
object.Content.Set("en", ap.Content("Starred "+star.Name))
|
||||||
create := ap.Create{Type: ap.CreateType, Object: object}
|
create := ap.Create{Type: ap.CreateType, Object: object}
|
||||||
outbox.OrderedItems.Append(create)
|
err := outbox.OrderedItems.Append(create)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("OrderedItems.Append", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
outbox.TotalItems = uint(len(outbox.OrderedItems))
|
outbox.TotalItems = uint(len(outbox.OrderedItems))
|
||||||
|
@ -233,7 +245,11 @@ func PersonFollowing(ctx *context.APIContext) {
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
// TODO: handle non-Federated users
|
// TODO: handle non-Federated users
|
||||||
person := ap.PersonNew(ap.IRI(user.Website))
|
person := ap.PersonNew(ap.IRI(user.Website))
|
||||||
following.OrderedItems.Append(person)
|
err := following.OrderedItems.Append(person)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("OrderedItems.Append", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response(ctx, following)
|
response(ctx, following)
|
||||||
|
@ -270,7 +286,11 @@ func PersonFollowers(ctx *context.APIContext) {
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
// TODO: handle non-Federated users
|
// TODO: handle non-Federated users
|
||||||
person := ap.PersonNew(ap.IRI(user.Website))
|
person := ap.PersonNew(ap.IRI(user.Website))
|
||||||
followers.OrderedItems.Append(person)
|
err := followers.OrderedItems.Append(person)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("OrderedItems.Append", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response(ctx, followers)
|
response(ctx, followers)
|
||||||
|
@ -311,7 +331,11 @@ func PersonLiked(ctx *context.APIContext) {
|
||||||
for _, repo := range repos {
|
for _, repo := range repos {
|
||||||
// TODO: Handle remote starred repos
|
// TODO: Handle remote starred repos
|
||||||
repo := forgefed.RepositoryNew(ap.IRI(setting.AppURL + "api/v1/activitypub/repo/" + repo.OwnerName + "/" + repo.Name))
|
repo := forgefed.RepositoryNew(ap.IRI(setting.AppURL + "api/v1/activitypub/repo/" + repo.OwnerName + "/" + repo.Name))
|
||||||
liked.OrderedItems.Append(repo)
|
err := liked.OrderedItems.Append(repo)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("OrderedItems.Append", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response(ctx, liked)
|
response(ctx, liked)
|
||||||
|
|
|
@ -81,7 +81,7 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er
|
||||||
var person ap.Person
|
var person ap.Person
|
||||||
person.UnmarshalJSON(b)
|
person.UnmarshalJSON(b)
|
||||||
err = activitypub.FederatedUserNew(ctx, person)
|
err = activitypub.FederatedUserNew(ctx, person)
|
||||||
return
|
return authenticated, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReqHTTPSignature function
|
// ReqHTTPSignature function
|
||||||
|
|
Loading…
Add table
Reference in a new issue