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

Merged
realaravinth merged 2 commits from task-600 into federation 2024-03-29 14:13:08 +05:30
3 changed files with 67 additions and 60 deletions

View file

@ -54,23 +54,70 @@ type FederatedUser struct {
FederationHostID int64 `xorm:"INDEX"` 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) engine := db.GetEngine(ctx)
// _, err := engine.
// Insert(u) // create FederatedHost
// if err != nil { exists, err := FederatedHostExists(ctx, hostname)
// return err 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 := new(FederatedUser)
federatedUser.ExternalID = u.Name federatedUser.ExternalID = u.Name
federatedUser.UserID = u.ID federatedUser.UserID = u.ID
federatedUser.FederationHostID = host.ID federatedUser.FederationHostID = federatedHost.ID
_, err := engine.Insert(federatedUser) exist, err = engine.Get(federatedUser)
return err 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 // set system defaults
u.Visibility = setting.Service.DefaultUserVisibilityMode u.Visibility = setting.Service.DefaultUserVisibilityMode
u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization && !setting.Admin.DisableRegularOrgCreation u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization && !setting.Admin.DisableRegularOrgCreation

View file

@ -93,13 +93,6 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
opts.Keyword = ctx.FormTrim("q") opts.Keyword = ctx.FormTrim("q")
opts.OrderBy = orderBy opts.OrderBy = orderBy
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
users, count, err = user_model.SearchUsers(ctx, opts)
if err != nil {
ctx.ServerError("SearchUsers", err)
return
}
}
if len(opts.Keyword) > 0 && forgefed.IsFingerable(opts.Keyword) { if len(opts.Keyword) > 0 && forgefed.IsFingerable(opts.Keyword) {
webfingerRes, err := forgefed.WebFingerLookup(opts.Keyword) webfingerRes, err := forgefed.WebFingerLookup(opts.Keyword)
@ -125,6 +118,14 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
// } // }
} }
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
users, count, err = user_model.SearchUsers(ctx, opts)
if err != nil {
ctx.ServerError("SearchUsers", err)
return
}
}
if isSitemap { if isSitemap {
m := sitemap.NewSitemap() m := sitemap.NewSitemap()
for _, item := range users { for _, item := range users {

View file

@ -63,46 +63,9 @@ func SavePerson(ctx context.Context, person *ap.Person) (*user.User, error) {
fmt.Println(person.ID.String()) fmt.Println(person.ID.String())
hostname, err := GetHostnameFromResource(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 { if err != nil {
return nil, err log.Error("Got error while saving person: %w", 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 {
return nil, 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) _ = user_service.UploadAvatar(ctx, u, avatar)
} }
if err = federation.CreateFederatedUser(ctx, u, &federatedHost); err != nil {
return nil, err
}
return u, nil return u, nil
} }