diff --git a/pkg/modules/avatar/gravatar/gravatar.go b/pkg/modules/avatar/gravatar/gravatar.go index c7b8d444b..f20184b52 100644 --- a/pkg/modules/avatar/gravatar/gravatar.go +++ b/pkg/modules/avatar/gravatar/gravatar.go @@ -26,37 +26,39 @@ import ( "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/modules/keyvalue" "code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/utils" ) type avatar struct { - content []byte - mimeType string - loadedAt time.Time + Content []byte `json:"content"` + MimeType string `json:"mime_type"` + LoadedAt time.Time `json:"loaded_at"` } // Provider is the gravatar provider type Provider struct { } -// avatars is a global map which contains cached avatars of the users -var avatars map[string]*avatar - -func init() { - avatars = make(map[string]*avatar) -} +const keyPrefix = "gravatar_avatar_" // GetAvatar implements getting the avatar for the user func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error) { sizeString := strconv.FormatInt(size, 10) - cacheKey := user.Username + "_" + sizeString - a, exists := avatars[cacheKey] + cacheKey := keyPrefix + user.Username + "_" + sizeString + + var av avatar + exists, err := keyvalue.GetWithValue(cacheKey, &av) + if err != nil { + log.Errorf("Error retrieving gravatar from keyvalue store: %s", err) + } + var needsRefetch bool if exists { - // elaped is alway < 0 so the next check would always succeed. + // elapsed is always < 0 so the next check would always succeed. // To have it make sense, we flip that. - elapsed := time.Until(a.loadedAt) * -1 + elapsed := time.Until(av.LoadedAt) * -1 needsRefetch = elapsed > time.Duration(config.AvatarGravaterExpiration.GetInt64())*time.Second if needsRefetch { log.Debugf("Refetching avatar for user %d after %v", user.ID, elapsed) @@ -64,6 +66,7 @@ func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error log.Debugf("Serving avatar for user %d from cache", user.ID) } } + if !exists || needsRefetch { log.Debugf("Gravatar for user %d with size %d not cached, requesting from gravatar...", user.ID, size) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://www.gravatar.com/avatar/"+utils.Md5String(strings.ToLower(user.Email))+"?s="+sizeString+"&d=mp", nil) @@ -86,11 +89,17 @@ func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error mimeType = contentType } - avatars[cacheKey] = &avatar{ - content: avatarContent, - mimeType: mimeType, - loadedAt: time.Now(), + av = avatar{ + Content: avatarContent, + MimeType: mimeType, + LoadedAt: time.Now(), + } + + // Store in keyvalue cache + if err := keyvalue.Put(cacheKey, av); err != nil { + log.Errorf("Error storing gravatar in keyvalue store: %s", err) } } - return avatars[cacheKey].content, avatars[cacheKey].mimeType, nil + + return av.Content, av.MimeType, nil }