summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorSandro Santilli <strk@kbt.io>2016-08-07 19:27:38 +0200
committer无闻 <u@gogs.io>2016-08-07 10:27:38 -0700
commit90dd0657b564210746c9c494c8c5b07dd8eee91f (patch)
tree228f1f1a684e355ef77c6d6798027dac76ce1d73 /modules
parentec92565f234deb75ea8df0c30a8772c4cd72bc2d (diff)
downloadgitea-90dd0657b564210746c9c494c8c5b07dd8eee91f.tar.gz
gitea-90dd0657b564210746c9c494c8c5b07dd8eee91f.zip
Add support for federated avatars (#3320)
* Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
Diffstat (limited to 'modules')
-rw-r--r--modules/auth/user_form.go27
-rw-r--r--modules/base/tool.go22
-rw-r--r--modules/setting/setting.go25
3 files changed, 57 insertions, 17 deletions
diff --git a/modules/auth/user_form.go b/modules/auth/user_form.go
index 57451d9eb6..7bd6c7b9bc 100644
--- a/modules/auth/user_form.go
+++ b/modules/auth/user_form.go
@@ -36,11 +36,12 @@ type InstallForm struct {
RegisterConfirm bool
MailNotify bool
- OfflineMode bool
- DisableGravatar bool
- DisableRegistration bool
- EnableCaptcha bool
- RequireSignInView bool
+ OfflineMode bool
+ DisableGravatar bool
+ EnableFederatedAvatar bool
+ DisableRegistration bool
+ EnableCaptcha bool
+ RequireSignInView bool
AdminName string `binding:"OmitEmpty;AlphaDashDot;MaxSize(30)" locale:"install.admin_name"`
AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"`
@@ -93,19 +94,25 @@ type UpdateProfileForm struct {
Email string `binding:"Required;Email;MaxSize(254)"`
Website string `binding:"Url;MaxSize(100)"`
Location string `binding:"MaxSize(50)"`
- Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
}
func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f, ctx.Locale)
}
-type UploadAvatarForm struct {
- Enable bool
- Avatar *multipart.FileHeader
+const (
+ AVATAR_LOCAL string = "local"
+ AVATAR_BYMAIL string = "bymail"
+)
+
+type AvatarForm struct {
+ Source string
+ Avatar *multipart.FileHeader
+ Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
+ Federavatar bool
}
-func (f *UploadAvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
+func (f *AvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f, ctx.Locale)
}
diff --git a/modules/base/tool.go b/modules/base/tool.go
index f045cb2270..b5125cab8a 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -205,12 +205,26 @@ func HashEmail(email string) string {
}
// AvatarLink returns avatar link by given email.
-func AvatarLink(email string) string {
- if setting.DisableGravatar || setting.OfflineMode {
- return setting.AppSubUrl + "/img/avatar_default.png"
+func AvatarLink(email string) (url string) {
+
+ if !setting.OfflineMode {
+ if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
+ var err error
+ url, err = setting.LibravatarService.FromEmail(email)
+ if err != nil {
+ log.Error(1, "LibravatarService.FromEmail:: %v", err)
+ }
+ }
+ if len(url) == 0 && !setting.DisableGravatar {
+ url = setting.GravatarSource + HashEmail(email)
+ }
+ }
+
+ if len(url) == 0 {
+ url = setting.AppSubUrl + "/img/avatar_default.png"
}
- return setting.GravatarSource + HashEmail(email)
+ return url
}
// Seconds-based time units
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index f59aa0884d..b8e8f65ef1 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -21,6 +21,7 @@ import (
"github.com/go-macaron/session"
_ "github.com/go-macaron/session/redis"
"gopkg.in/ini.v1"
+ "github.com/strk/go-libravatar"
"github.com/gogits/gogs/modules/bindata"
"github.com/gogits/gogs/modules/log"
@@ -140,9 +141,11 @@ var (
}
// Picture settings
- AvatarUploadPath string
- GravatarSource string
- DisableGravatar bool
+ AvatarUploadPath string
+ GravatarSource string
+ DisableGravatar bool
+ EnableFederatedAvatar bool
+ LibravatarService *libravatar.Libravatar
// Log settings
LogRootPath string
@@ -462,8 +465,24 @@ func NewContext() {
GravatarSource = source
}
DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool()
+ EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool()
if OfflineMode {
DisableGravatar = true
+ EnableFederatedAvatar = false
+ }
+
+ if !DisableGravatar && EnableFederatedAvatar {
+ LibravatarService = libravatar.New()
+ parts := strings.Split(GravatarSource, "/")
+ if len(parts) >= 3 {
+ if parts[0] == "https:" {
+ LibravatarService.SetUseHTTPS(true)
+ LibravatarService.SetSecureFallbackHost(parts[2])
+ } else {
+ LibravatarService.SetUseHTTPS(false)
+ LibravatarService.SetFallbackHost(parts[2])
+ }
+ }
}
if err = Cfg.Section("ui").MapTo(&UI); err != nil {