2022-07-14 03:10:03 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-12-31 17:58:01 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-07-14 03:10:03 +00:00
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-11-11 03:58:09 +00:00
|
|
|
"strconv"
|
2022-07-14 03:10:03 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
ap "github.com/go-ap/activitypub"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Returns the username corresponding to a Person actor IRI
|
2022-10-21 21:06:59 +00:00
|
|
|
func PersonIRIToName(personIRI ap.IRI) (string, error) {
|
2022-07-14 03:10:03 +00:00
|
|
|
personIRISplit := strings.Split(personIRI.String(), "/")
|
2022-11-11 03:58:09 +00:00
|
|
|
if len(personIRISplit) < 4 {
|
|
|
|
return "", errors.New("not a Person actor IRI")
|
2022-07-14 03:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instance := personIRISplit[2]
|
|
|
|
name := personIRISplit[len(personIRISplit)-1]
|
|
|
|
if instance == setting.Domain {
|
|
|
|
// Local user
|
|
|
|
return name, nil
|
|
|
|
}
|
2022-07-24 03:12:09 +00:00
|
|
|
// Remote user
|
|
|
|
// Get name in username@instance.com format
|
|
|
|
return name + "@" + instance, nil
|
2022-07-14 03:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the user corresponding to a Person actor IRI
|
2022-10-21 21:06:59 +00:00
|
|
|
func PersonIRIToUser(ctx context.Context, personIRI ap.IRI) (*user_model.User, error) {
|
|
|
|
name, err := PersonIRIToName(personIRI)
|
2022-07-14 03:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_model.GetUserByName(ctx, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the owner and name corresponding to a Repository actor IRI
|
2022-10-21 21:06:59 +00:00
|
|
|
func RepositoryIRIToName(repoIRI ap.IRI) (string, string, error) {
|
2022-07-14 03:10:03 +00:00
|
|
|
repoIRISplit := strings.Split(repoIRI.String(), "/")
|
|
|
|
if len(repoIRISplit) < 5 {
|
2022-11-11 03:58:09 +00:00
|
|
|
return "", "", errors.New("not a Repository actor IRI")
|
2022-07-14 03:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instance := repoIRISplit[2]
|
|
|
|
username := repoIRISplit[len(repoIRISplit)-2]
|
|
|
|
reponame := repoIRISplit[len(repoIRISplit)-1]
|
|
|
|
if instance == setting.Domain {
|
|
|
|
// Local repo
|
|
|
|
return username, reponame, nil
|
|
|
|
}
|
2022-07-24 03:12:09 +00:00
|
|
|
// Remote repo
|
|
|
|
return username + "@" + instance, reponame, nil
|
2022-07-14 03:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the repository corresponding to a Repository actor IRI
|
2022-10-21 21:06:59 +00:00
|
|
|
func RepositoryIRIToRepository(ctx context.Context, repoIRI ap.IRI) (*repo_model.Repository, error) {
|
|
|
|
username, reponame, err := RepositoryIRIToName(repoIRI)
|
2022-07-14 03:10:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-22 21:12:50 +00:00
|
|
|
return repo_model.GetRepositoryByOwnerAndName(ctx, username, reponame)
|
2022-07-14 03:10:03 +00:00
|
|
|
}
|
2022-11-11 03:58:09 +00:00
|
|
|
|
|
|
|
// 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 {
|
2022-11-27 02:05:36 +00:00
|
|
|
return "", "", 0, errors.New("not a Ticket object IRI")
|
2022-11-11 03:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-11-27 02:05:36 +00:00
|
|
|
|
|
|
|
// Returns the owner, repo name, and idx of a Branch object IRI
|
|
|
|
func BranchIRIToName(ticketIRI ap.IRI) (string, string, string, error) {
|
|
|
|
ticketIRISplit := strings.Split(ticketIRI.String(), "/")
|
|
|
|
if len(ticketIRISplit) < 5 {
|
|
|
|
return "", "", "", errors.New("not a Branch object IRI")
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := ticketIRISplit[2]
|
|
|
|
username := ticketIRISplit[len(ticketIRISplit)-3]
|
|
|
|
reponame := ticketIRISplit[len(ticketIRISplit)-2]
|
|
|
|
branch := ticketIRISplit[len(ticketIRISplit)-1]
|
|
|
|
if instance == setting.Domain {
|
|
|
|
// Local repo
|
|
|
|
return username, reponame, branch, nil
|
|
|
|
}
|
|
|
|
// Remote repo
|
|
|
|
return username + "@" + instance, reponame, branch, nil
|
|
|
|
}
|