1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

Refactor if conditional to switch, use split up functions

This commit is contained in:
erik 2023-12-07 13:21:26 +01:00 committed by Michael Jerger
parent 75ee273f40
commit 976256bf3d

View file

@ -229,6 +229,8 @@ func RepositoryInbox(ctx *context.APIContext) {
// "204": // "204":
// "$ref": "#/responses/empty" // "$ref": "#/responses/empty"
var user *user_model.User
log.Info("RepositoryInbox: repo %v, %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name) log.Info("RepositoryInbox: repo %v, %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name)
activity := web.GetForm(ctx).(*forgefed.Star) activity := web.GetForm(ctx).(*forgefed.Star)
log.Info("RepositoryInbox: Activity.Source %v", activity.Source) log.Info("RepositoryInbox: Activity.Source %v", activity.Source)
@ -264,24 +266,36 @@ func RepositoryInbox(ctx *context.APIContext) {
panic(fmt.Errorf("searching for user failed: %v", err)) panic(fmt.Errorf("searching for user failed: %v", err))
} }
if len(users) == 0 { switch len(users) {
// ToDo: We need a remote server with federation enabled to properly test this case 0:
{
body, err := getBody(remoteStargazer, ctx.Repo.Owner.HTMLURL(), ctx) body, err := getBody(remoteStargazer, ctx.Repo.Owner.HTMLURL(), ctx)
if err != nil { if err != nil {
panic(fmt.Errorf("http get failed: %v", err)) panic(fmt.Errorf("http get failed: %v", err))
}
person, err := unmarshallPersonJSON(body)
if err != nil {
panic(fmt.Errorf("getting user failed: %v", err))
}
user, err = createFederatedUserFromPerson(person, remoteStargazer)
if err != nil {
panic(fmt.Errorf("create federated user: %w", err))
}
err = saveFederatedUserRecord(ctx, user)
if err != nil {
panic(fmt.Errorf("save user: %w", err))
}
} }
case 1:
person, err := unmarshallPersonJSON(body) {
if err != nil { user = users[0]
panic(fmt.Errorf("getting user failed: %v", err)) log.Info("%v", user)
} }
default:
// create user {
err = createFederatedUserFromPerson(ctx, person, remoteStargazer) panic(fmt.Errorf("found more than one matches for federated users"))
if err != nil {
panic(fmt.Errorf("createUser: %w", err))
} }
}
} else { } else {
// use first user // use first user