fix: webfinger before loading search results for /explore/users/ #7
3 changed files with 67 additions and 60 deletions
|
@ -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
|
||||
}
|
||||
|
||||
if !exist {
|
||||
_, err = engine.Insert(federatedUser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func CreatUser(ctx context.Context, u *user.User) error {
|
||||
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
|
||||
|
|
|
@ -93,13 +93,6 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
|
|||
|
||||
opts.Keyword = ctx.FormTrim("q")
|
||||
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) {
|
||||
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 {
|
||||
m := sitemap.NewSitemap()
|
||||
for _, item := range users {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue