WIP: fix: webfinger before loading search results for /explore/users/ #10

Draft
realaravinth wants to merge 11 commits from task-600 into forgejo
2 changed files with 59 additions and 53 deletions
Showing only changes of commit 19887dea19 - Show all commits

View file

@ -54,23 +54,70 @@ type FederatedUser struct {
FederationHostID int64 `xorm:"INDEX"`
}
func CreateFederatedUser(ctx context.Context, u *user.User, host *FederatedHost) error {
func CreateFederatedUser(ctx context.Context, alias string, website string, hostname string) (*user.User, error) {
engine := db.GetEngine(ctx)
// _, err := engine.
// Insert(u)
// if err != nil {
// return err
// }
// create FederatedHost
exists, err := FederatedHostExists(ctx, hostname)
if err != nil {
return nil, err
}
var federatedHost FederatedHost
if exists {
x, err := GetFederatdHost(ctx, hostname)
federatedHost = *x
if err != nil {
return nil, err
}
} else {
federatedHost := new(FederatedHost)
federatedHost.HostFqdn = hostname
if err = federatedHost.Save(ctx); err != nil {
return nil, err
}
}
// create user.User
u := new(user.User)
u.Name = "@" + alias + "@" + hostname
//panic(u.Name)
u.Email = alias + "@" + hostname
u.Website = website
u.KeepEmailPrivate = true
exist, err := user.GetUser(ctx, u)
if err != nil {
return nil, err
}
if exist {
return u, nil // TODO: must also check for federatedUser existence
}
if err = createUser(ctx, u); err != nil {
return nil, err
}
federatedUser := new(FederatedUser)
federatedUser.ExternalID = u.Name
federatedUser.UserID = u.ID
federatedUser.FederationHostID = host.ID
_, err := engine.Insert(federatedUser)
return err
federatedUser.FederationHostID = federatedHost.ID
exist, err = engine.Get(federatedUser)
if err != nil {
return nil, err
}
func CreatUser(ctx context.Context, u *user.User) error {
if !exist {
_, err = engine.Insert(federatedUser)
if err != nil {
return nil, err
}
}
return u, nil
}
func createUser(ctx context.Context, u *user.User) error {
// set system defaults
u.Visibility = setting.Service.DefaultUserVisibilityMode
u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization && !setting.Admin.DisableRegularOrgCreation

View file

@ -63,46 +63,9 @@ func SavePerson(ctx context.Context, person *ap.Person) (*user.User, error) {
fmt.Println(person.ID.String())
hostname, err := GetHostnameFromResource(person.ID.String())
exists, err := federation.FederatedHostExists(ctx, hostname)
u, err := federation.CreateFederatedUser(ctx, person.PreferredUsername.String(), person.URL.GetID().String(), hostname)
if err != nil {
return nil, err
}
var federatedHost federation.FederatedHost
if exists {
x, err := federation.GetFederatdHost(ctx, hostname)
federatedHost = *x
if err != nil {
return nil, err
}
} else {
federatedHost := new(federation.FederatedHost)
federatedHost.HostFqdn = hostname
if err = federatedHost.Save(ctx); err != nil {
return nil, err
}
}
if err != nil {
return nil, err
}
u := new(user.User)
u.Name = "@" + person.PreferredUsername.String() + "@" + hostname
//panic(u.Name)
u.Email = person.PreferredUsername.String() + "@" + hostname
u.Website = person.URL.GetID().String()
u.KeepEmailPrivate = true
exist, err := user.GetUser(ctx, u)
if err != nil {
return nil, err
}
if exist {
return u, nil // TODO: must also check for federatedUser existence
}
if err = federation.CreatUser(ctx, u); err != nil {
log.Error("Got error while saving person: %w", err)
return nil, err
}
@ -116,10 +79,6 @@ func SavePerson(ctx context.Context, person *ap.Person) (*user.User, error) {
_ = user_service.UploadAvatar(ctx, u, avatar)
}
if err = federation.CreateFederatedUser(ctx, u, &federatedHost); err != nil {
return nil, err
}
return u, nil
}