chore: refactor saving remote actor in DB; move db stuff from services/ to models/

This commit is contained in:
Aravinth Manivannan 2024-03-29 14:07:10 +05:30
parent 5d5c7af983
commit 19887dea19
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 59 additions and 53 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
}
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 // 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

@ -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
} }