Make the code slightly better
All checks were successful
/ docker (push) Successful in 4m26s

This commit is contained in:
Melody Becker 2025-05-10 12:03:25 +02:00
parent e7632c6ad4
commit 2c0a4c88d5
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8

View file

@ -30,7 +30,7 @@ func userInbox(w http.ResponseWriter, r *http.Request) {
log := hlog.FromRequest(r)
userId := r.PathValue("id")
body, err := io.ReadAll(r.Body)
log.Info().
log.Trace().
Err(err).
Str("userId", userId).
Bytes("body", body).
@ -268,6 +268,11 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any)
return
}
follower, err := activitypub.ImportRemoteAccountByAPUrl(actorApId)
if err != nil {
log.Error().Err(err).Msg("Failed to import following account")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
u2u := dbgen.UserToUserRelation
followRelations, err := u2u.Where(
u2u.UserId.Eq(follower.ID),
@ -297,7 +302,7 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any)
}
err = tx.UserToUserRelation.Create(&req)
if err != nil {
tx.Rollback()
_ = tx.Rollback()
log.Error().Err(err).Any("follow-request", req).Msg("Failed to store follow request")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
@ -310,7 +315,7 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any)
}
err = tx.Activity.Create(&activity)
if err != nil {
tx.Rollback()
_ = tx.Rollback()
log.Error().Err(err).Any("activity", activity).Msg("Failed to store follow activity")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
@ -318,11 +323,18 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any)
err = tx.Commit()
if err != nil {
log.Error().Err(err).Msg("Failed to commit follow activity transaction")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
if !followed.RestrictedFollow {
// FIXME: Handle errors
tx = dbgen.Q.Begin()
_, err = u2u.Where(u2u.ID.Eq(req.ID)).UpdateColumn(u2u.Relation, models.RelationFollow)
if err != nil {
_ = tx.Rollback()
log.Error().Err(err).Msg("Failed to update follow to confirmed")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
acceptActivity := models.Activity{
Id: shared.NewId(),
Type: string(models.ActivityAccept),
@ -330,9 +342,20 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any)
ObjectType: uint32(models.ActivitystreamsActivityTargetActivity),
}
err = tx.Activity.Create(&acceptActivity)
tx.Commit()
if err != nil {
_ = tx.Rollback()
log.Error().Err(err).Msg("Failed to store accept activity in db")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
err = tx.Commit()
if err != nil {
log.Error().Err(err).Msg("Failed to commit follow accept to db")
webutils.ProblemDetailsStatusOnly(w, http.StatusInternalServerError)
return
}
go func() {
// FIXME: Clean this entire mess up
// TODO: Maybe move this part to a separate function
time.Sleep(time.Millisecond * 20)
webAccept, err := AcceptFromStorage(r.Context(), acceptActivity.Id)
if err != nil {
@ -345,6 +368,10 @@ func handleFollow(w http.ResponseWriter, r *http.Request, object map[string]any)
log.Error().Err(err).Msg("Failed to marshal accept")
return
}
log.Debug().
Bytes("body", body).
Str("target", follower.RemoteInfo.InboxLink).
Msg("Sending follow accept out")
res, err := webshared.RequestSignedCavage(
"POST",
follower.RemoteInfo.InboxLink,