From 96316a8f31d0204a1d8e6bd05db93408cd1cb09b Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 28 Mar 2024 19:44:37 +0530 Subject: [PATCH] feat: get remote person's avatar and assign to user --- services/forgefed/actor.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/services/forgefed/actor.go b/services/forgefed/actor.go index ab10a41e3..a709260b9 100644 --- a/services/forgefed/actor.go +++ b/services/forgefed/actor.go @@ -43,6 +43,21 @@ func GetActor(id string) (*ap.Actor, error) { return actorObj, nil } +func GetPersonAvatar(ctx context.Context, person *ap.Person) ([]byte, error) { + + avatarObj := new(ap.Image) + ap.CopyItemProperties(avatarObj, person.Icon) + log.Info("Getting avatar from link : %s", avatarObj.URL.GetLink().String()) + + r, err := http.Get(avatarObj.URL.GetLink().String()) + if err != nil { + log.Error("Got error while fetching avatar fn: %w", err) + return nil, err + } + defer r.Body.Close() + return io.ReadAll(r.Body) +} + func SavePerson(ctx context.Context, person *ap.Person) (*user.User, error) { fmt.Println(person.ID.String()) @@ -91,6 +106,16 @@ func SavePerson(ctx context.Context, person *ap.Person) (*user.User, error) { return nil, err } + avatar, err := GetPersonAvatar(ctx, person) + if err != nil { + log.Error("Got error while fetching avatar: %w", err) + return nil, err + } + + if u.IsUploadAvatarChanged(avatar) { + _ = user_service.UploadAvatar(ctx, u, avatar) + } + if err = federation.CreateFederatedUser(ctx, u, &federatedHost); err != nil { return nil, err }