-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
273 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package avatars | ||
|
||
import "github.com/mayswind/ezbookkeeping/pkg/models" | ||
|
||
// AvatarProvider is user avatar provider interface | ||
type AvatarProvider interface { | ||
GetAvatarUrl(user *models.User) string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package avatars | ||
|
||
import ( | ||
"github.com/mayswind/ezbookkeeping/pkg/core" | ||
"github.com/mayswind/ezbookkeeping/pkg/errs" | ||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
"github.com/mayswind/ezbookkeeping/pkg/settings" | ||
) | ||
|
||
// AvatarProviderContainer contains the current user avatar provider | ||
type AvatarProviderContainer struct { | ||
Current AvatarProvider | ||
} | ||
|
||
// Initialize a user avatar provider container singleton instance | ||
var ( | ||
Container = &AvatarProviderContainer{} | ||
) | ||
|
||
// InitializeAvatarProvider initializes the current user avatar provider according to the config | ||
func InitializeAvatarProvider(config *settings.Config) error { | ||
if config.AvatarProvider == core.USER_AVATAR_PROVIDER_INTERNAL { | ||
Container.Current = NewInternalStorageAvatarProvider(config) | ||
return nil | ||
} else if config.AvatarProvider == core.USER_AVATAR_PROVIDER_GRAVATAR { | ||
Container.Current = NewGravatarAvatarProvider() | ||
return nil | ||
} else if config.AvatarProvider == "" { | ||
Container.Current = NewNullAvatarProvider() | ||
return nil | ||
} | ||
|
||
return errs.ErrInvalidAvatarProvider | ||
} | ||
|
||
// GetAvatarUrl returns the avatar url by the current user avatar provider | ||
func (p *AvatarProviderContainer) GetAvatarUrl(user *models.User) string { | ||
return p.Current.GetAvatarUrl(user) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package avatars | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
"github.com/mayswind/ezbookkeeping/pkg/utils" | ||
) | ||
|
||
// Reference: https://en.gravatar.com/site/implement/hash/ | ||
const gravatarUrlFormat = "https://www.gravatar.com/avatar/%s" | ||
|
||
// GravatarAvatarProvider represents the gravatar avatar provider | ||
type GravatarAvatarProvider struct { | ||
} | ||
|
||
// NewGravatarAvatarProvider returns a new gravatar avatar provider | ||
func NewGravatarAvatarProvider() *GravatarAvatarProvider { | ||
return &GravatarAvatarProvider{} | ||
} | ||
|
||
// GetAvatarUrl returns the gravatar url | ||
func (p *GravatarAvatarProvider) GetAvatarUrl(user *models.User) string { | ||
email := user.Email | ||
email = strings.TrimSpace(email) | ||
email = strings.ToLower(email) | ||
emailMd5 := utils.MD5EncodeToString([]byte(email)) | ||
|
||
return fmt.Sprintf(gravatarUrlFormat, emailMd5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package avatars | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
) | ||
|
||
func TestGravatarAvatarProvider_GetGravatarUrl(t *testing.T) { | ||
avatarProvider := NewGravatarAvatarProvider() | ||
|
||
expectedValue := "https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346" | ||
actualValue := avatarProvider.GetAvatarUrl(&models.User{ | ||
Email: "MyEmailAddress@example.com", | ||
}) | ||
|
||
assert.Equal(t, expectedValue, actualValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package avatars | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
"github.com/mayswind/ezbookkeeping/pkg/settings" | ||
) | ||
|
||
const internalAvatarUrlFormat = "%savatar/%d.%s" | ||
|
||
// InternalStorageAvatarProvider represents the internal storage avatar provider | ||
type InternalStorageAvatarProvider struct { | ||
webRootUrl string | ||
} | ||
|
||
// NewInternalStorageAvatarProvider returns a new internal storage avatar provider | ||
func NewInternalStorageAvatarProvider(config *settings.Config) *InternalStorageAvatarProvider { | ||
return &InternalStorageAvatarProvider{ | ||
webRootUrl: config.RootUrl, | ||
} | ||
} | ||
|
||
// GetAvatarUrl returns the built-in avatar url | ||
func (p *InternalStorageAvatarProvider) GetAvatarUrl(user *models.User) string { | ||
if user.CustomAvatarType == "" { | ||
return "" | ||
} | ||
|
||
return fmt.Sprintf(internalAvatarUrlFormat, p.webRootUrl, user.Uid, user.CustomAvatarType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package avatars | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
"github.com/mayswind/ezbookkeeping/pkg/settings" | ||
) | ||
|
||
func TestInternalStorageAvatarProvider_GetAvatarUrl(t *testing.T) { | ||
avatarProvider := NewInternalStorageAvatarProvider(&settings.Config{ | ||
RootUrl: "https://foo.bar/", | ||
}) | ||
|
||
expectedValue := "https://foo.bar/avatar/1234567890.jpg" | ||
actualValue := avatarProvider.GetAvatarUrl(&models.User{ | ||
Uid: 1234567890, | ||
CustomAvatarType: "jpg", | ||
}) | ||
|
||
assert.Equal(t, expectedValue, actualValue) | ||
} | ||
|
||
func TestInternalStorageAvatarProvider_GetAvatarUrl_EmptyCustomAvatarType(t *testing.T) { | ||
avatarProvider := NewInternalStorageAvatarProvider(&settings.Config{ | ||
RootUrl: "https://foo.bar/", | ||
}) | ||
|
||
expectedValue := "" | ||
actualValue := avatarProvider.GetAvatarUrl(&models.User{ | ||
Uid: 1234567890, | ||
CustomAvatarType: "", | ||
}) | ||
|
||
assert.Equal(t, expectedValue, actualValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package avatars | ||
|
||
import ( | ||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
) | ||
|
||
// NullAvatarProvider represents the null avatar provider | ||
type NullAvatarProvider struct { | ||
} | ||
|
||
// NewNullAvatarProvider returns a new null avatar provider | ||
func NewNullAvatarProvider() *NullAvatarProvider { | ||
return &NullAvatarProvider{} | ||
} | ||
|
||
// GetAvatarUrl returns an empty url | ||
func (p *NullAvatarProvider) GetAvatarUrl(user *models.User) string { | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package avatars | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/mayswind/ezbookkeeping/pkg/models" | ||
) | ||
|
||
func TestNullAvatarProvider_GetGravatarUrl(t *testing.T) { | ||
avatarProvider := NewNullAvatarProvider() | ||
|
||
expectedValue := "" | ||
actualValue := avatarProvider.GetAvatarUrl(&models.User{ | ||
Email: "MyEmailAddress@example.com", | ||
}) | ||
|
||
assert.Equal(t, expectedValue, actualValue) | ||
} |
Oops, something went wrong.