Download avatar from URL and set it with user_service.UploadAvatar
This commit is contained in:
		
							parent
							
								
									d945e6ac72
								
							
						
					
					
						commit
						6b73c097ed
					
				
					 5 changed files with 31 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -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.
 | 
			
		||||
;;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue