From 727b259ca76c5c5c10b78cd11a10a1696990e250 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 22 May 2024 23:45:43 +0800 Subject: [PATCH] :bug: Bug fixes in unable get accurate avatar & banner url --- pkg/grpc/auth.go | 6 ++---- pkg/models/accounts.go | 16 ++++++++++++++++ pkg/server/accounts_api.go | 5 +++-- pkg/server/ui/accounts.go | 12 ++++++------ pkg/server/ui/directory.go | 12 ++++++------ 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/pkg/grpc/auth.go b/pkg/grpc/auth.go index 66de9ff..9ee0ad1 100644 --- a/pkg/grpc/auth.go +++ b/pkg/grpc/auth.go @@ -2,13 +2,11 @@ package grpc import ( "context" - "fmt" "git.solsynth.dev/hydrogen/passport/pkg/grpc/proto" "git.solsynth.dev/hydrogen/passport/pkg/services" jsoniter "github.com/json-iterator/go" "github.com/samber/lo" - "github.com/spf13/viper" ) func (v *Server) Authenticate(_ context.Context, in *proto.AuthRequest) (*proto.AuthReply, error) { @@ -30,10 +28,10 @@ func (v *Server) Authenticate(_ context.Context, in *proto.AuthRequest) (*proto. } if user.Avatar != nil { - userinfo.Avatar = fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), *user.Avatar) + userinfo.Avatar = *user.GetAvatar() } if user.Banner != nil { - userinfo.Banner = fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), *user.Banner) + userinfo.Banner = *user.GetBanner() } return &proto.AuthReply{ diff --git a/pkg/models/accounts.go b/pkg/models/accounts.go index 8162c2c..308ff7b 100644 --- a/pkg/models/accounts.go +++ b/pkg/models/accounts.go @@ -1,9 +1,11 @@ package models import ( + "fmt" "time" "github.com/samber/lo" + "github.com/spf13/viper" "gorm.io/datatypes" ) @@ -38,6 +40,20 @@ type Account struct { RelatedFriendships []AccountFriendship `json:"related_friendships" gorm:"foreignKey:RelatedID"` } +func (v Account) GetAvatar() *string { + if v.Avatar != nil { + return lo.ToPtr(fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), *v.Avatar)) + } + return nil +} + +func (v Account) GetBanner() *string { + if v.Banner != nil { + return lo.ToPtr(fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), *v.Banner)) + } + return nil +} + func (v Account) GetPrimaryEmail() AccountContact { val, _ := lo.Find(v.Contacts, func(item AccountContact) bool { return item.Type == EmailAccountContact && item.IsPrimary diff --git a/pkg/server/accounts_api.go b/pkg/server/accounts_api.go index d584716..d4d2aec 100644 --- a/pkg/server/accounts_api.go +++ b/pkg/server/accounts_api.go @@ -2,10 +2,11 @@ package server import ( "fmt" - "git.solsynth.dev/hydrogen/passport/pkg/utils" "strconv" "time" + "git.solsynth.dev/hydrogen/passport/pkg/utils" + "git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/services" @@ -38,7 +39,7 @@ func getUserinfo(c *fiber.Ctx) error { resp["preferred_username"] = data.Nick if data.Avatar != nil { - resp["picture"] = fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Avatar) + resp["picture"] = *data.GetAvatar() } return c.JSON(resp) diff --git a/pkg/server/ui/accounts.go b/pkg/server/ui/accounts.go index 2467f8d..b1a394a 100644 --- a/pkg/server/ui/accounts.go +++ b/pkg/server/ui/accounts.go @@ -2,16 +2,16 @@ package ui import ( "fmt" + "html/template" + "time" + "git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/models" "github.com/gofiber/fiber/v2" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" - "github.com/spf13/viper" "github.com/sujit-baniya/flash" - "html/template" - "time" ) func selfUserinfoPage(c *fiber.Ctx) error { @@ -27,7 +27,7 @@ func selfUserinfoPage(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - var birthday = "Unknown" + birthday := "Unknown" if data.Profile.Birthday != nil { birthday = data.Profile.Birthday.Format(time.RFC822) } @@ -45,7 +45,7 @@ func selfUserinfoPage(c *fiber.Ctx) error { "birthday_at": birthday, "personal_page": template.HTML(markdown.Render(doc, renderer)), "userinfo": data, - "avatar": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Avatar), - "banner": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Banner), + "avatar": data.GetAvatar(), + "banner": data.GetBanner(), }, "views/layouts/user-center") } diff --git a/pkg/server/ui/directory.go b/pkg/server/ui/directory.go index 6f20300..dc64950 100644 --- a/pkg/server/ui/directory.go +++ b/pkg/server/ui/directory.go @@ -2,16 +2,16 @@ package ui import ( "fmt" + "html/template" + "time" + "git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/models" "github.com/gofiber/fiber/v2" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" - "github.com/spf13/viper" "github.com/sujit-baniya/flash" - "html/template" - "time" ) func otherUserinfoPage(c *fiber.Ctx) error { @@ -27,7 +27,7 @@ func otherUserinfoPage(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - var birthday = "Unknown" + birthday := "Unknown" if data.Profile.Birthday != nil { birthday = data.Profile.Birthday.Format(time.RFC822) } @@ -45,7 +45,7 @@ func otherUserinfoPage(c *fiber.Ctx) error { "birthday_at": birthday, "personal_page": template.HTML(markdown.Render(doc, renderer)), "userinfo": data, - "avatar": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Avatar), - "banner": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Banner), + "avatar": data.GetAvatar(), + "banner": data.GetBanner(), }, "views/layouts/user-center") }