From 0cacdc37fbad8f0e8f8e6a381080c0b3b6b2cc95 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Fri, 11 Nov 2022 03:58:09 +0000 Subject: [PATCH] More Ticket IRI processing to iri.go --- modules/activitypub/comment.go | 23 ++++++++++++----------- modules/activitypub/iri.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/modules/activitypub/comment.go b/modules/activitypub/comment.go index 7e36bed764..9052fa3600 100644 --- a/modules/activitypub/comment.go +++ b/modules/activitypub/comment.go @@ -6,8 +6,6 @@ package activitypub import ( "context" - "strconv" - "strings" "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" @@ -22,15 +20,18 @@ func Comment(ctx context.Context, note *ap.Note) error { return err } - // TODO: Move IRI processing stuff to iri.go - context := note.Context.GetLink() - contextSplit := strings.Split(context.String(), "/") - username := contextSplit[3] - reponame := contextSplit[4] - repo, _ := repo_model.GetRepositoryByOwnerAndNameCtx(ctx, username, reponame) - - idx, _ := strconv.ParseInt(contextSplit[len(contextSplit)-1], 10, 64) - issue, _ := issues.GetIssueByIndex(repo.ID, idx) + username, reponame, idx, err := TicketIRIToName(note.Context.GetLink()) + if err != nil { + return err + } + repo, err := repo_model.GetRepositoryByOwnerAndNameCtx(ctx, username, reponame) + if err != nil { + return err + } + issue, err := issues.GetIssueByIndex(repo.ID, idx) + if err != nil { + return err + } _, err = issues.CreateCommentCtx(ctx, &issues.CreateCommentOptions{ Doer: actorUser, Repo: repo, diff --git a/modules/activitypub/iri.go b/modules/activitypub/iri.go index b546202c27..d7d098f4a9 100644 --- a/modules/activitypub/iri.go +++ b/modules/activitypub/iri.go @@ -7,6 +7,7 @@ package activitypub import ( "context" "errors" + "strconv" "strings" repo_model "code.gitea.io/gitea/models/repo" @@ -19,8 +20,8 @@ import ( // Returns the username corresponding to a Person actor IRI func PersonIRIToName(personIRI ap.IRI) (string, error) { personIRISplit := strings.Split(personIRI.String(), "/") - if len(personIRISplit) < 3 { - return "", errors.New("Not a Person actor IRI") + if len(personIRISplit) < 4 { + return "", errors.New("not a Person actor IRI") } instance := personIRISplit[2] @@ -53,7 +54,7 @@ func PersonIRIToUser(ctx context.Context, personIRI ap.IRI) (*user_model.User, e func RepositoryIRIToName(repoIRI ap.IRI) (string, string, error) { repoIRISplit := strings.Split(repoIRI.String(), "/") if len(repoIRISplit) < 5 { - return "", "", errors.New("Not a Repository actor IRI") + return "", "", errors.New("not a Repository actor IRI") } instance := repoIRISplit[2] @@ -77,3 +78,25 @@ func RepositoryIRIToRepository(ctx context.Context, repoIRI ap.IRI) (*repo_model // TODO: create remote repo if not exists return repo_model.GetRepositoryByOwnerAndName(username, reponame) } + +// Returns the owner, repo name, and idx of a Ticket object IRI +func TicketIRIToName(ticketIRI ap.IRI) (string, string, int64, error) { + ticketIRISplit := strings.Split(ticketIRI.String(), "/") + if len(ticketIRISplit) < 5 { + return "", "", 0, errors.New("not a Ticket actor IRI") + } + + instance := ticketIRISplit[2] + username := ticketIRISplit[len(ticketIRISplit)-3] + reponame := ticketIRISplit[len(ticketIRISplit)-2] + idx, err := strconv.ParseInt(ticketIRISplit[len(ticketIRISplit)-1], 10, 64) + if err != nil { + return "", "", 0, err + } + if instance == setting.Domain { + // Local repo + return username, reponame, idx, nil + } + // Remote repo + return username + "@" + instance, reponame, idx, nil +}