🐛 Bug fixes in unable get accurate avatar & banner url

This commit is contained in:
LittleSheep 2024-05-22 23:45:43 +08:00
parent 4fb4abda89
commit 727b259ca7
5 changed files with 33 additions and 18 deletions

View File

@ -2,13 +2,11 @@ package grpc
import ( import (
"context" "context"
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/grpc/proto" "git.solsynth.dev/hydrogen/passport/pkg/grpc/proto"
"git.solsynth.dev/hydrogen/passport/pkg/services" "git.solsynth.dev/hydrogen/passport/pkg/services"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/spf13/viper"
) )
func (v *Server) Authenticate(_ context.Context, in *proto.AuthRequest) (*proto.AuthReply, error) { 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 { 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 { 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{ return &proto.AuthReply{

View File

@ -1,9 +1,11 @@
package models package models
import ( import (
"fmt"
"time" "time"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/spf13/viper"
"gorm.io/datatypes" "gorm.io/datatypes"
) )
@ -38,6 +40,20 @@ type Account struct {
RelatedFriendships []AccountFriendship `json:"related_friendships" gorm:"foreignKey:RelatedID"` 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 { func (v Account) GetPrimaryEmail() AccountContact {
val, _ := lo.Find(v.Contacts, func(item AccountContact) bool { val, _ := lo.Find(v.Contacts, func(item AccountContact) bool {
return item.Type == EmailAccountContact && item.IsPrimary return item.Type == EmailAccountContact && item.IsPrimary

View File

@ -2,10 +2,11 @@ package server
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/passport/pkg/utils"
"strconv" "strconv"
"time" "time"
"git.solsynth.dev/hydrogen/passport/pkg/utils"
"git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/database"
"git.solsynth.dev/hydrogen/passport/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services" "git.solsynth.dev/hydrogen/passport/pkg/services"
@ -38,7 +39,7 @@ func getUserinfo(c *fiber.Ctx) error {
resp["preferred_username"] = data.Nick resp["preferred_username"] = data.Nick
if data.Avatar != nil { 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) return c.JSON(resp)

View File

@ -2,16 +2,16 @@ package ui
import ( import (
"fmt" "fmt"
"html/template"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/database"
"git.solsynth.dev/hydrogen/passport/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/models"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser" "github.com/gomarkdown/markdown/parser"
"github.com/spf13/viper"
"github.com/sujit-baniya/flash" "github.com/sujit-baniya/flash"
"html/template"
"time"
) )
func selfUserinfoPage(c *fiber.Ctx) error { func selfUserinfoPage(c *fiber.Ctx) error {
@ -27,7 +27,7 @@ func selfUserinfoPage(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
var birthday = "Unknown" birthday := "Unknown"
if data.Profile.Birthday != nil { if data.Profile.Birthday != nil {
birthday = data.Profile.Birthday.Format(time.RFC822) birthday = data.Profile.Birthday.Format(time.RFC822)
} }
@ -45,7 +45,7 @@ func selfUserinfoPage(c *fiber.Ctx) error {
"birthday_at": birthday, "birthday_at": birthday,
"personal_page": template.HTML(markdown.Render(doc, renderer)), "personal_page": template.HTML(markdown.Render(doc, renderer)),
"userinfo": data, "userinfo": data,
"avatar": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Avatar), "avatar": data.GetAvatar(),
"banner": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Banner), "banner": data.GetBanner(),
}, "views/layouts/user-center") }, "views/layouts/user-center")
} }

View File

@ -2,16 +2,16 @@ package ui
import ( import (
"fmt" "fmt"
"html/template"
"time"
"git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/database"
"git.solsynth.dev/hydrogen/passport/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/models"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser" "github.com/gomarkdown/markdown/parser"
"github.com/spf13/viper"
"github.com/sujit-baniya/flash" "github.com/sujit-baniya/flash"
"html/template"
"time"
) )
func otherUserinfoPage(c *fiber.Ctx) error { func otherUserinfoPage(c *fiber.Ctx) error {
@ -27,7 +27,7 @@ func otherUserinfoPage(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
var birthday = "Unknown" birthday := "Unknown"
if data.Profile.Birthday != nil { if data.Profile.Birthday != nil {
birthday = data.Profile.Birthday.Format(time.RFC822) birthday = data.Profile.Birthday.Format(time.RFC822)
} }
@ -45,7 +45,7 @@ func otherUserinfoPage(c *fiber.Ctx) error {
"birthday_at": birthday, "birthday_at": birthday,
"personal_page": template.HTML(markdown.Render(doc, renderer)), "personal_page": template.HTML(markdown.Render(doc, renderer)),
"userinfo": data, "userinfo": data,
"avatar": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Avatar), "avatar": data.GetAvatar(),
"banner": fmt.Sprintf("%s/api/attachments/%d", viper.GetString("paperclip.endpoint"), data.Banner), "banner": data.GetBanner(),
}, "views/layouts/user-center") }, "views/layouts/user-center")
} }