From 2c0a4c88d50845c7947ec20cf2ba8e213f7a2703 Mon Sep 17 00:00:00 2001 From: mStar Date: Sat, 10 May 2025 12:03:25 +0200 Subject: [PATCH] Make the code slightly better --- web/public/api/activitypub/inbox.go | 39 ++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/web/public/api/activitypub/inbox.go b/web/public/api/activitypub/inbox.go index c304450..aa6519a 100644 --- a/web/public/api/activitypub/inbox.go +++ b/web/public/api/activitypub/inbox.go @@ -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,