This repository has been archived on 2024-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
forgejo/services/activitypub/iri.go

114 lines
3.3 KiB
Go
Raw Normal View History

// Copyright 2022 The Gitea Authors. All rights reserved.
2022-12-31 17:58:01 +00:00
// SPDX-License-Identifier: MIT
package activitypub
import (
"context"
"errors"
2022-11-11 03:58:09 +00:00
"strconv"
"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
func PersonIRIToName(personIRI ap.IRI) (string, error) {
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")
}
instance := personIRISplit[2]
name := personIRISplit[len(personIRISplit)-1]
if instance == setting.Domain {
// Local user
return name, nil
}
// Remote user
// Get name in username@instance.com format
return name + "@" + instance, nil
}
// Returns the user corresponding to a Person actor IRI
func PersonIRIToUser(ctx context.Context, personIRI ap.IRI) (*user_model.User, error) {
name, err := PersonIRIToName(personIRI)
if err != nil {
return nil, err
}
return user_model.GetUserByName(ctx, name)
}
// Returns the owner and name corresponding to a Repository actor IRI
func RepositoryIRIToName(repoIRI ap.IRI) (string, string, error) {
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")
}
instance := repoIRISplit[2]
username := repoIRISplit[len(repoIRISplit)-2]
reponame := repoIRISplit[len(repoIRISplit)-1]
if instance == setting.Domain {
// Local repo
return username, reponame, nil
}
// Remote repo
return username + "@" + instance, reponame, nil
}
// Returns the repository corresponding to a Repository actor IRI
func RepositoryIRIToRepository(ctx context.Context, repoIRI ap.IRI) (*repo_model.Repository, error) {
username, reponame, err := RepositoryIRIToName(repoIRI)
if err != nil {
return nil, err
}
return repo_model.GetRepositoryByOwnerAndName(ctx, username, reponame)
}
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 {
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
}
// 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
}