mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-24 08:57:03 -05:00
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,38 +188,36 @@ 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)
|
||||||
|
username := getUserName(person)
|
||||||
|
|
||||||
/*
|
user := &user_model.User{
|
||||||
email := generateUUIDMail(person)
|
LowerName: username.ToLower(),
|
||||||
username := getUserName(person)
|
Name: username,
|
||||||
|
Email: email,
|
||||||
|
EmailNotificationsPreference: "disabled",
|
||||||
|
Passwd: generateRandomPassword(),
|
||||||
|
MustChangePassword: false,
|
||||||
|
LoginName: target,
|
||||||
|
Type: UserType.UserTypeRemoteUser,
|
||||||
|
IsAdmin: false,
|
||||||
|
}
|
||||||
|
|
||||||
user := &user_model.User{
|
overwriteDefault := &user_model.CreateUserOverwriteOptions{
|
||||||
LowerName: username.ToLower(),
|
IsActive: util.OptionalBoolFalse,
|
||||||
Name: username,
|
IsRestricted: util.OptionalBoolFalse,
|
||||||
Email: email,
|
}
|
||||||
EmailNotificationsPreference: "disabled",
|
|
||||||
Passwd: generateRandomPassword(),
|
|
||||||
MustChangePassword: false,
|
|
||||||
Type: UserType.UserTypeRemoteUser,
|
|
||||||
Location: getUserLocation(person),
|
|
||||||
Website: getAPUserID(person),
|
|
||||||
IsAdmin: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
overwriteDefault := &user_model.CreateUserOverwriteOptions{
|
if err := user_model.CreateUser(ctx, user, overwriteDefault); err != nil {
|
||||||
IsActive: util.OptionalBoolFalse,
|
panic(fmt.Errorf("CreateUser: %w", err))
|
||||||
IsRestricted: util.OptionalBoolFalse,
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if err := user_model.CreateUser(ctx, user, overwriteDefault); err != nil {
|
|
||||||
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