From d65e5aa70e148a8df3f0e2c95a7c8ec36a874569 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 5 Dec 2023 14:49:27 +0100 Subject: [PATCH] Move helper functions to top --- routers/api/v1/activitypub/repository.go | 97 ++++++++++++++++-------- 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go index fff1b9dff3..d88f554d13 100644 --- a/routers/api/v1/activitypub/repository.go +++ b/routers/api/v1/activitypub/repository.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "net/http" + "net/url" "strings" "code.gitea.io/gitea/models/activitypub" @@ -26,6 +27,71 @@ import ( //f3 "lab.forgefriends.org/friendlyforgeformat/gof3" ) +var actionsUser = user_model.NewActionsUser() + +func generateUUIDMail(person ap.Actor) (string, error) { + // UUID@remote.host + id := uuid.New().String() + + //url, err := url.Parse(person.URL.GetID().String()) + + //host := url.Host + + return strings.Join([]string{id, "example.com"}, "@"), nil +} + +func generateRemoteUserName(person ap.Actor) (string, error) { + u, err := url.Parse(person.URL.GetID().String()) + if err != nil { + return "", err + } + + host := strings.Split(u.Host, ":")[0] // no port in username + + if name := person.PreferredUsername.String(); name != "" { + return strings.Join([]string{name, host}, "_"), nil + } + if name := person.Name.String(); name != "" { + return strings.Join([]string{name, host}, "_"), nil + } + + return "", fmt.Errorf("empty name, preferredUsername field") +} + +func generateRandomPassword() (string, error) { + // Generate a password that is 64 characters long with 10 digits, 10 symbols, + // allowing upper and lower case letters, disallowing repeat characters. + res, err := pwd_gen.Generate(32, 10, 10, false, false) + if err != nil { + return "", err + } + return res, err +} + +func searchUsersByPerson(remoteStargazer string, person ap.Actor) ([]*user_model.User, error) { + + actionsUser.IsAdmin = true + + options := &user_model.SearchUserOptions{ + LoginName: remoteStargazer, + Actor: actionsUser, + Type: user_model.UserTypeRemoteUser, + OrderBy: db.SearchOrderByAlphabetically, + ListOptions: db.ListOptions{PageSize: 1}, + IsActive: util.OptionalBoolFalse, + IncludeReserved: true, + } + users, _, err := user_model.SearchUsers(db.DefaultContext, options) + if err != nil { + return []*user_model.User{}, fmt.Errorf("search failed: %v", err) + } + + log.Info("local found users: %v", len(users)) + + return users, nil + +} + // Repository function returns the Repository actor for a repo func Repository(ctx *context.APIContext) { // swagger:operation GET /activitypub/repository-id/{repository-id} activitypub activitypubRepository @@ -238,34 +304,3 @@ func RepositoryInbox(ctx *context.APIContext) { ctx.Status(http.StatusNoContent) } - -func generateUUIDMail(person ap.Actor) (string, error) { - // UUID@remote.host - id := uuid.New().String() - - //url, err := url.Parse(person.URL.GetID().String()) - - //host := url.Host - - return strings.Join([]string{id, "example.com"}, "@"), nil -} - -func getUserName(person ap.Actor) (string, error) { - if name := person.PreferredUsername.String(); name != "" { - return name, nil - } - if name := person.Name.String(); name != "" { - return name, nil - } - return "", fmt.Errorf("Empty name, preferredUsername field") -} - -func generateRandomPassword() (string, error) { - // Generate a password that is 64 characters long with 10 digits, 10 symbols, - // allowing upper and lower case letters, disallowing repeat characters. - res, err := pwd_gen.Generate(32, 10, 10, false, false) - if err != nil { - return "", err - } - return res, err -}