Refactor RepoInbox to use On functions instead of type assertions

This commit is contained in:
Anthony Wang 2022-08-21 10:22:42 -05:00
parent 27cda2fcd4
commit b3c065ce80
Signed by: a
GPG key ID: BC96B00AEC5F2D76
4 changed files with 31 additions and 24 deletions

View file

@ -16,7 +16,7 @@ import (
)
// Create a comment
func Comment(ctx context.Context, note ap.Note) error {
func Comment(ctx context.Context, note *ap.Note) error {
actorUser, err := personIRIToUser(ctx, note.AttributedTo.GetLink())
if err != nil {
return err

View file

@ -11,6 +11,7 @@ import (
)
// Create an issue
func Issue(ctx context.Context, ticket forgefed.Ticket) {
func Issue(ctx context.Context, ticket *forgefed.Ticket) error {
// TODO
return nil
}

View file

@ -6,7 +6,6 @@ package activitypub
import (
"context"
"fmt"
"strings"
issues_model "code.gitea.io/gitea/models/issues"
@ -16,7 +15,7 @@ import (
pull_service "code.gitea.io/gitea/services/pull"
)
func PullRequest(ctx context.Context, ticket forgefed.Ticket) {
func PullRequest(ctx context.Context, ticket *forgefed.Ticket) error {
// TODO: Clean this up
actorUser, err := personIRIToUser(ctx, ticket.AttributedTo.GetLink())
@ -62,6 +61,5 @@ func PullRequest(ctx context.Context, ticket forgefed.Ticket) {
Type: issues_model.PullRequestGitea,
}
err = pull_service.NewPullRequest(ctx, targetRepo, prIssue, []int64{}, []string{}, pr, []int64{})
fmt.Println(err)
return pull_service.NewPullRequest(ctx, targetRepo, prIssue, []int64{}, []string{}, pr, []int64{})
}

View file

@ -96,6 +96,7 @@ func RepoInbox(ctx *context.APIContext) {
}
ap.ItemTyperFunc = forgefed.GetItemByType
ap.JSONItemUnmarshal = forgefed.JSONUnmarshalerFn
var activity ap.Activity
err = activity.UnmarshalJSON(body)
if err != nil {
@ -122,31 +123,38 @@ func RepoInbox(ctx *context.APIContext) {
// Process activity
switch activity.Type {
case ap.CreateType:
switch activity.Object.(ap.Object).Type {
case forgefed.RepositoryType:
// Fork created by remote instance
activitypub.ReceiveFork(ctx, activity)
case forgefed.TicketType:
// New issue or pull request
ticket := activity.Object.(forgefed.Ticket)
if ticket.Origin != nil {
// New pull request
activitypub.PullRequest(ctx, ticket)
} else {
// New issue
activitypub.Issue(ctx, ticket)
err = ap.OnObject(activity.Object, func(o *ap.Object) error {
switch o.Type {
case forgefed.RepositoryType:
// Fork created by remote instance
return activitypub.ReceiveFork(ctx, activity)
case forgefed.TicketType:
// New issue or pull request
return forgefed.OnTicket(o, func(t *forgefed.Ticket) error {
if t.Origin != nil {
// New pull request
return activitypub.PullRequest(ctx, t)
} else {
// New issue
return activitypub.Issue(ctx, t)
}
})
case ap.NoteType:
// New comment
return activitypub.Comment(ctx, o)
}
case ap.NoteType:
// New comment
activitypub.Comment(ctx, activity.Object.(ap.Note))
}
return nil
})
case ap.LikeType:
activitypub.ReceiveStar(ctx, activity)
err = activitypub.ReceiveStar(ctx, activity)
default:
log.Info("Incoming unsupported ActivityStreams type: %s", activity.Type)
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")
return
}
if err != nil {
ctx.ServerError("Error when processing: %s", err)
}
ctx.Status(http.StatusNoContent)
}