|
|
@ -16,6 +16,8 @@ import ( |
|
|
|
"math" |
|
|
|
"math" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"net/url" |
|
|
|
|
|
|
|
"path" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
"time" |
|
|
@ -197,24 +199,59 @@ func DefaultAvatarLink() string { |
|
|
|
return setting.AppSubURL + "/img/avatar_default.png" |
|
|
|
return setting.AppSubURL + "/img/avatar_default.png" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AvatarLink returns relative avatar link to the site domain by given email,
|
|
|
|
// DefaultAvatarSize is a sentinel value for the default avatar size, as
|
|
|
|
// which includes app sub-url as prefix. However, it is possible
|
|
|
|
// determined by the avatar-hosting service.
|
|
|
|
// to return full URL if user enables Gravatar-like service.
|
|
|
|
const DefaultAvatarSize = -1 |
|
|
|
func AvatarLink(email string) string { |
|
|
|
|
|
|
|
if setting.EnableFederatedAvatar && setting.LibravatarService != nil { |
|
|
|
// libravatarURL returns the URL for the given email. This function should only
|
|
|
|
url, err := setting.LibravatarService.FromEmail(email) |
|
|
|
// be called if a federated avatar service is enabled.
|
|
|
|
|
|
|
|
func libravatarURL(email string) (*url.URL, error) { |
|
|
|
|
|
|
|
urlStr, err := setting.LibravatarService.FromEmail(email) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error(4, "LibravatarService.FromEmail(email=%s): error %v", email, err) |
|
|
|
log.Error(4, "LibravatarService.FromEmail(email=%s): error %v", email, err) |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
u, err := url.Parse(urlStr) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Error(4, "Failed to parse libravatar url(%s): error %v", urlStr, err) |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return u, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SizedAvatarLink returns a sized link to the avatar for the given email
|
|
|
|
|
|
|
|
// address.
|
|
|
|
|
|
|
|
func SizedAvatarLink(email string, size int) string { |
|
|
|
|
|
|
|
var avatarURL *url.URL |
|
|
|
|
|
|
|
if setting.EnableFederatedAvatar && setting.LibravatarService != nil { |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
avatarURL, err = libravatarURL(email) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return DefaultAvatarLink() |
|
|
|
return DefaultAvatarLink() |
|
|
|
} |
|
|
|
} |
|
|
|
return url |
|
|
|
} else if !setting.DisableGravatar { |
|
|
|
|
|
|
|
// copy GravatarSourceURL, because we will modify its Path.
|
|
|
|
|
|
|
|
copyOfGravatarSourceURL := *setting.GravatarSourceURL |
|
|
|
|
|
|
|
avatarURL = ©OfGravatarSourceURL |
|
|
|
|
|
|
|
avatarURL.Path = path.Join(avatarURL.Path, HashEmail(email)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return DefaultAvatarLink() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !setting.DisableGravatar { |
|
|
|
vals := avatarURL.Query() |
|
|
|
return setting.GravatarSource + HashEmail(email) + "?d=identicon" |
|
|
|
vals.Set("d", "identicon") |
|
|
|
|
|
|
|
if size != DefaultAvatarSize { |
|
|
|
|
|
|
|
vals.Set("s", strconv.Itoa(size)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
avatarURL.RawQuery = vals.Encode() |
|
|
|
|
|
|
|
return avatarURL.String() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return DefaultAvatarLink() |
|
|
|
// AvatarLink returns relative avatar link to the site domain by given email,
|
|
|
|
|
|
|
|
// which includes app sub-url as prefix. However, it is possible
|
|
|
|
|
|
|
|
// to return full URL if user enables Gravatar-like service.
|
|
|
|
|
|
|
|
func AvatarLink(email string) string { |
|
|
|
|
|
|
|
return SizedAvatarLink(email, DefaultAvatarSize) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Seconds-based time units
|
|
|
|
// Seconds-based time units
|
|
|
|