symetric handling for value/reference

This commit is contained in:
Michael Jerger 2024-02-08 15:16:37 +01:00
parent d2d5e84977
commit e733809ef2
2 changed files with 25 additions and 31 deletions

View file

@ -44,7 +44,7 @@ func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost
return host, nil return host, nil
} }
func CreateFederationHost(ctx context.Context, host FederationHost) error { func CreateFederationHost(ctx context.Context, host *FederationHost) error {
if res, err := validation.IsValid(host); !res { if res, err := validation.IsValid(host); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err) return fmt.Errorf("FederationInfo is not valid: %v", err)
} }
@ -52,7 +52,7 @@ func CreateFederationHost(ctx context.Context, host FederationHost) error {
return err return err
} }
func UpdateFederationHost(ctx context.Context, host FederationHost) error { func UpdateFederationHost(ctx context.Context, host *FederationHost) error {
if res, err := validation.IsValid(host); !res { if res, err := validation.IsValid(host); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err) return fmt.Errorf("FederationInfo is not valid: %v", err)
} }

View file

@ -26,78 +26,74 @@ import (
func LikeActivity(ctx *context.APIContext, form any, repositoryId int64) (error, int, string) { func LikeActivity(ctx *context.APIContext, form any, repositoryId int64) (error, int, string) {
activity := form.(*forgefed_model.ForgeLike) activity := form.(*forgefed_model.ForgeLike)
if res, err := validation.IsValid(activity); !res { if res, err := validation.IsValid(activity); !res {
return err, http.StatusNotAcceptable, "RepositoryInbox: Validate activity" return err, http.StatusNotAcceptable, "Invalid activity"
} }
log.Info("RepositoryInbox: activity validated:%v", activity) log.Info("Activity validated:%v", activity)
// parse actorID (person) // parse actorID (person)
actorURI := activity.Actor.GetID().String() actorURI := activity.Actor.GetID().String()
rawActorID, err := forgefed_model.NewActorID(actorURI) rawActorID, err := forgefed_model.NewActorID(actorURI)
if err != nil { if err != nil {
return err, http.StatusInternalServerError, "RepositoryInbox: Validating ActorID" return err, http.StatusInternalServerError, "Invalid ActorID"
} }
federationHost, err := forgefed_model.FindFederationHostByFqdn(ctx, rawActorID.Host) federationHost, err := forgefed_model.FindFederationHostByFqdn(ctx, rawActorID.Host)
if err != nil { if err != nil {
return err, http.StatusInternalServerError, "RepositoryInbox: Error while loading FederationInfo" return err, http.StatusInternalServerError, "Could not loading FederationHost"
} }
if federationHost == nil { if federationHost == nil {
result, err := CreateFederationHostFromAP(ctx, rawActorID) result, err := CreateFederationHostFromAP(ctx, rawActorID)
if err != nil { if err != nil {
return err, http.StatusNotAcceptable, "RepositoryInbox: Validate actorId" return err, http.StatusNotAcceptable, "Invalid FederationHost"
} }
federationHost = result federationHost = result
log.Info("RepositoryInbox: federationInfo validated: %v", federationHost)
} }
if !activity.IsNewer(federationHost.LatestActivity) { if !activity.IsNewer(federationHost.LatestActivity) {
return fmt.Errorf("Activity already processed"), http.StatusNotAcceptable, "RepositoryInbox: Validate Activity" return fmt.Errorf("Activity already processed"), http.StatusNotAcceptable, "Activity out of order."
} }
actorID, err := forgefed_model.NewPersonID(actorURI, string(federationHost.NodeInfo.Source)) actorID, err := forgefed_model.NewPersonID(actorURI, string(federationHost.NodeInfo.Source))
if err != nil { if err != nil {
return err, http.StatusNotAcceptable, "RepositoryInbox: Validate actorId" return err, http.StatusNotAcceptable, "Invalid PersonID"
} }
log.Info("RepositoryInbox: actorId validated: %v", actorID) log.Info("Actor accepted:%v", actorID)
// parse objectID (repository) // parse objectID (repository)
objectID, err := forgefed_model.NewRepositoryID(activity.Object.GetID().String(), string(forgefed_model.ForgejoSourceType)) objectID, err := forgefed_model.NewRepositoryID(activity.Object.GetID().String(), string(forgefed_model.ForgejoSourceType))
if err != nil { if err != nil {
return err, http.StatusNotAcceptable, "RepositoryInbox: Validate objectId" return err, http.StatusNotAcceptable, "Invalid objectId"
} }
if objectID.ID != fmt.Sprint(repositoryId) { if objectID.ID != fmt.Sprint(repositoryId) {
return err, http.StatusNotAcceptable, "RepositoryInbox: Validate objectId" return err, http.StatusNotAcceptable, "Invalid objectId"
} }
log.Info("RepositoryInbox: objectId validated: %v", objectID) log.Info("Object accepted:%v", objectID)
actorAsLoginID := actorID.AsLoginName() // used as LoginName in newly created user
log.Info("RepositoryInbox: remoteStargazer: %v", actorAsLoginID)
// Check if user already exists // Check if user already exists
user, _, err := user_model.FindFederatedUser(ctx, actorID.ID, federationHost.ID) user, _, err := user_model.FindFederatedUser(ctx, actorID.ID, federationHost.ID)
if err != nil { if err != nil {
return err, http.StatusInternalServerError, "RepositoryInbox: Searching for user failed" return err, http.StatusInternalServerError, "Searching for user failed"
} }
if user != nil { if user != nil {
log.Info("RepositoryInbox: found user: %v", user) log.Info("Found local federatedUser: %v", user)
} else { } else {
user, _, err = CreateUserFromAP(ctx, actorID, federationHost.ID) user, _, err = CreateUserFromAP(ctx, actorID, federationHost.ID)
if err != nil { if err != nil {
return err, http.StatusInternalServerError, return err, http.StatusInternalServerError, "Error creating federatedUser"
"RepositoryInbox: Creating federated user failed"
} }
log.Info("RepositoryInbox: created user from ap: %v", user) log.Info("Created federatedUser from ap: %v", user)
} }
log.Info("Got user:%v", user.Name)
// execute the activity if the repo was not stared already // execute the activity if the repo was not stared already
alreadyStared := repo.IsStaring(ctx, user.ID, repositoryId) alreadyStared := repo.IsStaring(ctx, user.ID, repositoryId)
if !alreadyStared { if !alreadyStared {
err = repo.StarRepo(ctx, user.ID, repositoryId, true) err = repo.StarRepo(ctx, user.ID, repositoryId, true)
if err != nil { if err != nil {
return err, http.StatusNotAcceptable, "RepositoryInbox: Star operation" return err, http.StatusNotAcceptable, "Error staring"
} }
} }
federationHost.LatestActivity = activity.StartTime federationHost.LatestActivity = activity.StartTime
err = forgefed_model.UpdateFederationHost(ctx, *federationHost) err = forgefed_model.UpdateFederationHost(ctx, federationHost)
if err != nil { if err != nil {
return err, http.StatusNotAcceptable, "RepositoryInbox: error updateing federateionInfo" return err, http.StatusNotAcceptable, "Error updating federatedHost"
} }
return nil, 0, "" return nil, 0, ""
@ -129,7 +125,7 @@ func CreateFederationHostFromAP(ctx *context.APIContext, actorID forgefed_model.
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = forgefed_model.CreateFederationHost(ctx, result) err = forgefed_model.CreateFederationHost(ctx, &result)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -157,7 +153,7 @@ func CreateUserFromAP(ctx *context.APIContext, personID forgefed_model.PersonID,
if res, err := validation.IsValid(person); !res { if res, err := validation.IsValid(person); !res {
return nil, nil, err return nil, nil, err
} }
log.Info("RepositoryInbox: validated person: %q", person) log.Info("Fetched valid person:%q", person)
localFqdn, err := url.ParseRequestURI(setting.AppURL) localFqdn, err := url.ParseRequestURI(setting.AppURL)
if err != nil { if err != nil {
@ -166,7 +162,6 @@ func CreateUserFromAP(ctx *context.APIContext, personID forgefed_model.PersonID,
email := fmt.Sprintf("f%v@%v", uuid.New().String(), localFqdn.Hostname()) email := fmt.Sprintf("f%v@%v", uuid.New().String(), localFqdn.Hostname())
loginName := personID.AsLoginName() loginName := personID.AsLoginName()
name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix()) name := fmt.Sprintf("%v%v", person.PreferredUsername.String(), personID.HostSuffix())
log.Info("RepositoryInbox: person.Name: %v", person.Name)
fullName := person.Name.String() fullName := person.Name.String()
if len(person.Name) == 0 { if len(person.Name) == 0 {
fullName = name fullName = name
@ -187,16 +182,15 @@ func CreateUserFromAP(ctx *context.APIContext, personID forgefed_model.PersonID,
Type: user_model.UserTypeRemoteUser, Type: user_model.UserTypeRemoteUser,
IsAdmin: false, IsAdmin: false,
} }
federatedUser := user_model.FederatedUser{ federatedUser := user_model.FederatedUser{
ExternalID: personID.ID, ExternalID: personID.ID,
FederationHostID: federationHostID, FederationHostID: federationHostID,
} }
err = user_model.CreateFederatedUser(ctx, &user, &federatedUser) err = user_model.CreateFederatedUser(ctx, &user, &federatedUser)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
log.Info("Created federatedUser:%q", federatedUser)
return &user, &federatedUser, nil return &user, &federatedUser, nil
} }