github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/serializer/user.go (about) 1 package serializer 2 3 import ( 4 "fmt" 5 6 model "github.com/cloudreve/Cloudreve/v3/models" 7 "github.com/cloudreve/Cloudreve/v3/pkg/hashid" 8 "github.com/duo-labs/webauthn/webauthn" 9 "time" 10 ) 11 12 // CheckLogin 检查登录 13 func CheckLogin() Response { 14 return Response{ 15 Code: CodeCheckLogin, 16 Msg: "Login required", 17 } 18 } 19 20 // User 用户序列化器 21 type User struct { 22 ID string `json:"id"` 23 Email string `json:"user_name"` 24 Nickname string `json:"nickname"` 25 Status int `json:"status"` 26 Avatar string `json:"avatar"` 27 CreatedAt time.Time `json:"created_at"` 28 PreferredTheme string `json:"preferred_theme"` 29 Anonymous bool `json:"anonymous"` 30 Group group `json:"group"` 31 Tags []tag `json:"tags"` 32 } 33 34 type group struct { 35 ID uint `json:"id"` 36 Name string `json:"name"` 37 AllowShare bool `json:"allowShare"` 38 AllowRemoteDownload bool `json:"allowRemoteDownload"` 39 AllowArchiveDownload bool `json:"allowArchiveDownload"` 40 ShareDownload bool `json:"shareDownload"` 41 CompressEnabled bool `json:"compress"` 42 WebDAVEnabled bool `json:"webdav"` 43 SourceBatchSize int `json:"sourceBatch"` 44 AdvanceDelete bool `json:"advanceDelete"` 45 AllowWebDAVProxy bool `json:"allowWebDAVProxy"` 46 } 47 48 type tag struct { 49 ID string `json:"id"` 50 Name string `json:"name"` 51 Icon string `json:"icon"` 52 Color string `json:"color"` 53 Type int `json:"type"` 54 Expression string `json:"expression"` 55 } 56 57 type storage struct { 58 Used uint64 `json:"used"` 59 Free uint64 `json:"free"` 60 Total uint64 `json:"total"` 61 } 62 63 // WebAuthnCredentials 外部验证器凭证 64 type WebAuthnCredentials struct { 65 ID []byte `json:"id"` 66 FingerPrint string `json:"fingerprint"` 67 } 68 69 // BuildWebAuthnList 构建设置页面凭证列表 70 func BuildWebAuthnList(credentials []webauthn.Credential) []WebAuthnCredentials { 71 res := make([]WebAuthnCredentials, 0, len(credentials)) 72 for _, v := range credentials { 73 credential := WebAuthnCredentials{ 74 ID: v.ID, 75 FingerPrint: fmt.Sprintf("% X", v.Authenticator.AAGUID), 76 } 77 res = append(res, credential) 78 } 79 80 return res 81 } 82 83 // BuildUser 序列化用户 84 func BuildUser(user model.User) User { 85 tags, _ := model.GetTagsByUID(user.ID) 86 return User{ 87 ID: hashid.HashID(user.ID, hashid.UserID), 88 Email: user.Email, 89 Nickname: user.Nick, 90 Status: user.Status, 91 Avatar: user.Avatar, 92 CreatedAt: user.CreatedAt, 93 PreferredTheme: user.OptionsSerialized.PreferredTheme, 94 Anonymous: user.IsAnonymous(), 95 Group: group{ 96 ID: user.GroupID, 97 Name: user.Group.Name, 98 AllowShare: user.Group.ShareEnabled, 99 AllowRemoteDownload: user.Group.OptionsSerialized.Aria2, 100 AllowArchiveDownload: user.Group.OptionsSerialized.ArchiveDownload, 101 ShareDownload: user.Group.OptionsSerialized.ShareDownload, 102 CompressEnabled: user.Group.OptionsSerialized.ArchiveTask, 103 WebDAVEnabled: user.Group.WebDAVEnabled, 104 AllowWebDAVProxy: user.Group.OptionsSerialized.WebDAVProxy, 105 SourceBatchSize: user.Group.OptionsSerialized.SourceBatchSize, 106 AdvanceDelete: user.Group.OptionsSerialized.AdvanceDelete, 107 }, 108 Tags: buildTagRes(tags), 109 } 110 } 111 112 // BuildUserResponse 序列化用户响应 113 func BuildUserResponse(user model.User) Response { 114 return Response{ 115 Data: BuildUser(user), 116 } 117 } 118 119 // BuildUserStorageResponse 序列化用户存储概况响应 120 func BuildUserStorageResponse(user model.User) Response { 121 total := user.Group.MaxStorage 122 storageResp := storage{ 123 Used: user.Storage, 124 Free: total - user.Storage, 125 Total: total, 126 } 127 128 if total < user.Storage { 129 storageResp.Free = 0 130 } 131 132 return Response{ 133 Data: storageResp, 134 } 135 } 136 137 // buildTagRes 构建标签列表 138 func buildTagRes(tags []model.Tag) []tag { 139 res := make([]tag, 0, len(tags)) 140 for i := 0; i < len(tags); i++ { 141 newTag := tag{ 142 ID: hashid.HashID(tags[i].ID, hashid.TagID), 143 Name: tags[i].Name, 144 Icon: tags[i].Icon, 145 Color: tags[i].Color, 146 Type: tags[i].Type, 147 } 148 if newTag.Type != 0 { 149 newTag.Expression = tags[i].Expression 150 151 } 152 res = append(res, newTag) 153 } 154 155 return res 156 }