From 6b73c097ed1caa4b8d8a334b89afadb23958f938 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Mon, 15 Aug 2022 12:00:14 -0500 Subject: [PATCH] Download avatar from URL and set it with user_service.UploadAvatar --- custom/conf/app.example.ini | 2 +- models/user/avatar.go | 9 +++++++++ modules/activitypub/user.go | 29 ++++++++++++++++++---------- modules/setting/federation.go | 2 +- routers/api/v1/activitypub/person.go | 2 +- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index dd4d5a4cbb..8ca57ea7ac 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2273,7 +2273,7 @@ ROUTER = console ;SHARE_USER_STATISTICS = true ;; ;; Maximum federation request and response size (MB) -;MAX_SIZE = 4 +;MAX_SIZE = 8 ;; ;; WARNING: Changing the settings below can break federation. ;; diff --git a/models/user/avatar.go b/models/user/avatar.go index 6a44a3bcb3..d50bd3ec81 100644 --- a/models/user/avatar.go +++ b/models/user/avatar.go @@ -98,6 +98,15 @@ func (u *User) AvatarLink() string { return link } +// AvatarFullLinkWithSize returns the full avatar link with size and http host +func (u *User) AvatarFullLinkWithSize(size int) string { + link := u.AvatarLinkWithSize(size) + if !strings.HasPrefix(link, "//") && !strings.Contains(link, "://") { + return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL+"/") + } + return link +} + // IsUploadAvatarChanged returns true if the current user's avatar would be changed with the provided data func (u *User) IsUploadAvatarChanged(data []byte) bool { if !u.UseCustomAvatar || len(u.Avatar) == 0 { diff --git a/modules/activitypub/user.go b/modules/activitypub/user.go index 94d87f6b4d..89732044ca 100644 --- a/modules/activitypub/user.go +++ b/modules/activitypub/user.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/auth" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" + user_service "code.gitea.io/gitea/services/user" ap "github.com/go-ap/activitypub" ) @@ -39,15 +40,6 @@ func FederatedUserNew(ctx context.Context, person *ap.Person) error { email = strings.ReplaceAll(name, "@", "+") + "@" + setting.Service.NoReplyAddress } - var avatar string - if person.Icon != nil { - icon := person.Icon.(*ap.Image) - // Currently doesn't work - avatar = icon.URL.GetLink().String() - } else { - avatar = "" - } - if person.PublicKey.PublicKeyPem == "" { return errors.New("person public key not found") } @@ -56,7 +48,6 @@ func FederatedUserNew(ctx context.Context, person *ap.Person) error { Name: name, FullName: person.Name.String(), // May not exist!! Email: email, - Avatar: avatar, LoginType: auth.Federated, LoginName: person.GetLink().String(), } @@ -65,6 +56,24 @@ func FederatedUserNew(ctx context.Context, person *ap.Person) error { return err } + if person.Icon != nil { + icon := person.Icon.(*ap.Image) + iconURL, err := icon.URL.GetLink().URL() + if err != nil { + return err + } + + body, err := Fetch(iconURL) + if err != nil { + return err + } + + err = user_service.UploadAvatar(user, body) + if err != nil { + return err + } + } + err = user_model.SetUserSetting(user.ID, user_model.UserActivityPubPrivPem, "") if err != nil { return err diff --git a/modules/setting/federation.go b/modules/setting/federation.go index 583d9a6e2b..89bbc1cf0b 100644 --- a/modules/setting/federation.go +++ b/modules/setting/federation.go @@ -23,7 +23,7 @@ var ( }{ Enabled: false, ShareUserStatistics: true, - MaxSize: 4, + MaxSize: 8, Algorithms: []string{"rsa-sha256", "rsa-sha512", "ed25519"}, DigestAlgorithm: "SHA-256", GetHeaders: []string{"(request-target)", "Date"}, diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go index 0280a0376b..4f0df2d605 100644 --- a/routers/api/v1/activitypub/person.go +++ b/routers/api/v1/activitypub/person.go @@ -63,7 +63,7 @@ func Person(ctx *context.APIContext) { person.Icon = ap.Image{ Type: ap.ImageType, MediaType: "image/png", - URL: ap.IRI(ctx.ContextUser.AvatarLink()), + URL: ap.IRI(ctx.ContextUser.AvatarFullLinkWithSize(2048)), } person.Inbox = ap.IRI(link + "/inbox")