WIP: Generate User if not exists
This commit is contained in:
parent
94880d64f4
commit
85e09a7ada
1 changed files with 41 additions and 26 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/activitypub"
|
"code.gitea.io/gitea/models/activitypub"
|
||||||
|
@ -16,7 +17,9 @@ import (
|
||||||
"code.gitea.io/gitea/modules/forgefed"
|
"code.gitea.io/gitea/modules/forgefed"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/modules/web"
|
"code.gitea.io/gitea/modules/web"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
|
@ -107,7 +110,7 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
// make http client
|
// make http client
|
||||||
// TODO: this should also work without autorizing the api call // doer might be empty
|
// TODO: this should also work without autorizing the api call // doer might be empty
|
||||||
host := activity.To.GetID().String()
|
host := activity.To.GetID().String()
|
||||||
client, err := api.NewClient(ctx, ctx.ContextUser, host) // ToDo: This is hacky, we need a hostname from somewhere
|
client, err := api.NewClient(ctx, ctx.Doer, host) // ToDo: This is hacky, we need a hostname from somewhere
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -141,8 +144,9 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
|
|
||||||
// Check if user already exists
|
// Check if user already exists
|
||||||
// TODO: If we where able to search for federated id there would be no need to get the remote person.
|
// TODO: If we where able to search for federated id there would be no need to get the remote person.
|
||||||
|
// Search for login_name
|
||||||
options := &user_model.SearchUserOptions{
|
options := &user_model.SearchUserOptions{
|
||||||
Keyword: person.PreferredUsername.Get("en").String(),
|
Keyword: target,
|
||||||
Actor: ctx.Doer,
|
Actor: ctx.Doer,
|
||||||
Type: user_model.UserTypeRemoteUser,
|
Type: user_model.UserTypeRemoteUser,
|
||||||
OrderBy: db.SearchOrderByAlphabetically,
|
OrderBy: db.SearchOrderByAlphabetically,
|
||||||
|
@ -153,6 +157,9 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
users, usersCount, err := user_model.SearchUsers(db.DefaultContext, options)
|
users, usersCount, err := user_model.SearchUsers(db.DefaultContext, options)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Errorf("Search failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
log.Info("local found users: %v", usersCount)
|
log.Info("local found users: %v", usersCount)
|
||||||
|
|
||||||
|
@ -162,7 +169,6 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
/*
|
/*
|
||||||
ToDo: Make user
|
ToDo: Make user
|
||||||
|
|
||||||
|
|
||||||
Fill in user There is a usertype remote in models/user/user.go
|
Fill in user There is a usertype remote in models/user/user.go
|
||||||
In Location maybe the federated user ID
|
In Location maybe the federated user ID
|
||||||
isActive to false
|
isActive to false
|
||||||
|
@ -182,9 +188,7 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
SearchUsers is defined in models/user/search.go
|
SearchUsers is defined in models/user/search.go
|
||||||
And depending on implementation check if the person already exists in federated user db.
|
And depending on implementation check if the person already exists in federated user db.
|
||||||
*/
|
*/
|
||||||
|
email, err := generateUUIDMail(person)
|
||||||
/*
|
|
||||||
email := generateUUIDMail(person)
|
|
||||||
username := getUserName(person)
|
username := getUserName(person)
|
||||||
|
|
||||||
user := &user_model.User{
|
user := &user_model.User{
|
||||||
|
@ -194,9 +198,8 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
EmailNotificationsPreference: "disabled",
|
EmailNotificationsPreference: "disabled",
|
||||||
Passwd: generateRandomPassword(),
|
Passwd: generateRandomPassword(),
|
||||||
MustChangePassword: false,
|
MustChangePassword: false,
|
||||||
|
LoginName: target,
|
||||||
Type: UserType.UserTypeRemoteUser,
|
Type: UserType.UserTypeRemoteUser,
|
||||||
Location: getUserLocation(person),
|
|
||||||
Website: getAPUserID(person),
|
|
||||||
IsAdmin: false,
|
IsAdmin: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,12 +211,13 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
if err := user_model.CreateUser(ctx, user, overwriteDefault); err != nil {
|
if err := user_model.CreateUser(ctx, user, overwriteDefault); err != nil {
|
||||||
panic(fmt.Errorf("CreateUser: %w", err))
|
panic(fmt.Errorf("CreateUser: %w", err))
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
} else {
|
} else {
|
||||||
// use first user
|
// use first user
|
||||||
user := users[0]
|
user := users[0]
|
||||||
log.Info("%v", user)
|
log.Info("%v", user)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle case of count > 1
|
// TODO: handle case of count > 1
|
||||||
|
|
||||||
// execute star action
|
// execute star action
|
||||||
|
@ -223,3 +227,14 @@ func RepositoryInbox(ctx *context.APIContext) {
|
||||||
ctx.Status(http.StatusNoContent)
|
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, host}, "@"), err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue