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 // 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()) actorUser, err := personIRIToUser(ctx, note.AttributedTo.GetLink())
if err != nil { if err != nil {
return err return err

View file

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

View file

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

View file

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