/
user_schema.go
433 lines (391 loc) · 11.8 KB
/
user_schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package schema
import (
"encoding/json"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/validator"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/pkg/checker"
"github.com/answerdev/answer/pkg/converter"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors"
)
// UserVerifyEmailReq user verify email request
type UserVerifyEmailReq struct {
// code
Code string `validate:"required,gt=0,lte=500" form:"code"`
// content
Content string `json:"-"`
}
// GetUserResp get user response
type GetUserResp struct {
// user id
ID string `json:"id"`
// create time
CreatedAt int64 `json:"created_at"`
// last login date
LastLoginDate int64 `json:"last_login_date"`
// username
Username string `json:"username"`
// email
EMail string `json:"e_mail"`
// mail status(1 pass 2 to be verified)
MailStatus int `json:"mail_status"`
// notice status(1 on 2off)
NoticeStatus int `json:"notice_status"`
// follow count
FollowCount int `json:"follow_count"`
// answer count
AnswerCount int `json:"answer_count"`
// question count
QuestionCount int `json:"question_count"`
// rank
Rank int `json:"rank"`
// authority group
AuthorityGroup int `json:"authority_group"`
// display name
DisplayName string `json:"display_name"`
// avatar
Avatar string `json:"avatar"`
// mobile
Mobile string `json:"mobile"`
// bio markdown
Bio string `json:"bio"`
// bio html
BioHTML string `json:"bio_html"`
// website
Website string `json:"website"`
// location
Location string `json:"location"`
// ip info
IPInfo string `json:"ip_info"`
// language
Language string `json:"language"`
// access token
AccessToken string `json:"access_token"`
// is admin
IsAdmin bool `json:"is_admin"`
// user status
Status string `json:"status"`
}
func (r *GetUserResp) GetFromUserEntity(userInfo *entity.User) {
_ = copier.Copy(r, userInfo)
r.Avatar = FormatAvatarInfo(userInfo.Avatar)
r.CreatedAt = userInfo.CreatedAt.Unix()
r.LastLoginDate = userInfo.LastLoginDate.Unix()
statusShow, ok := UserStatusShow[userInfo.Status]
if ok {
r.Status = statusShow
}
}
type GetUserToSetShowResp struct {
*GetUserResp
Avatar *AvatarInfo `json:"avatar"`
}
func (r *GetUserToSetShowResp) GetFromUserEntity(userInfo *entity.User) {
_ = copier.Copy(r, userInfo)
r.CreatedAt = userInfo.CreatedAt.Unix()
r.LastLoginDate = userInfo.LastLoginDate.Unix()
statusShow, ok := UserStatusShow[userInfo.Status]
if ok {
r.Status = statusShow
}
avatarInfo := &AvatarInfo{}
_ = json.Unmarshal([]byte(userInfo.Avatar), avatarInfo)
// if json.Unmarshal Error avatarInfo.Type is Empty
r.Avatar = avatarInfo
}
func FormatAvatarInfo(avatarJson string) string {
if avatarJson == "" {
return ""
}
avatarInfo := &AvatarInfo{}
err := json.Unmarshal([]byte(avatarJson), avatarInfo)
if err != nil {
return ""
}
switch avatarInfo.Type {
case "gravatar":
return avatarInfo.Gravatar
case "custom":
return avatarInfo.Custom
default:
return ""
}
}
// GetUserStatusResp get user status info
type GetUserStatusResp struct {
// user status
Status string `json:"status"`
}
// GetOtherUserInfoByUsernameResp get user response
type GetOtherUserInfoByUsernameResp struct {
// user id
ID string `json:"id"`
// create time
CreatedAt int64 `json:"created_at"`
// last login date
LastLoginDate int64 `json:"last_login_date"`
// username
Username string `json:"username"`
// email
// follow count
FollowCount int `json:"follow_count"`
// answer count
AnswerCount int `json:"answer_count"`
// question count
QuestionCount int `json:"question_count"`
// rank
Rank int `json:"rank"`
// display name
DisplayName string `json:"display_name"`
// avatar
Avatar string `json:"avatar"`
// mobile
Mobile string `json:"mobile"`
// bio markdown
Bio string `json:"bio"`
// bio html
BioHTML string `json:"bio_html"`
// website
Website string `json:"website"`
// location
Location string `json:"location"`
// ip info
IPInfo string `json:"ip_info"`
// is admin
IsAdmin bool `json:"is_admin"`
Status string `json:"status"`
StatusMsg string `json:"status_msg,omitempty"`
}
func (r *GetOtherUserInfoByUsernameResp) GetFromUserEntity(userInfo *entity.User) {
_ = copier.Copy(r, userInfo)
Avatar := FormatAvatarInfo(userInfo.Avatar)
r.Avatar = Avatar
r.CreatedAt = userInfo.CreatedAt.Unix()
r.LastLoginDate = userInfo.LastLoginDate.Unix()
statusShow, ok := UserStatusShow[userInfo.Status]
if ok {
r.Status = statusShow
}
if userInfo.MailStatus == entity.EmailStatusToBeVerified {
statusMsgShow, ok := UserStatusShowMsg[11]
if ok {
r.StatusMsg = statusMsgShow
}
} else {
statusMsgShow, ok := UserStatusShowMsg[userInfo.Status]
if ok {
r.StatusMsg = statusMsgShow
}
}
}
const (
MailStatePass = 1
MailStateVerifi = 2
NoticeStatusOn = 1
NoticeStatusOff = 2
ActionRecordTypeLogin = "login"
ActionRecordTypeEmail = "e_mail"
ActionRecordTypeFindPass = "find_pass"
)
var UserStatusShow = map[int]string{
1: "normal",
9: "forbidden",
10: "deleted",
}
var UserStatusShowMsg = map[int]string{
1: "",
9: "<strong>This user was suspended forever.</strong> This user doesn’t meet a community guideline.",
10: "This user was deleted.",
11: "This user is inactive.",
}
// EmailLogin
type UserEmailLogin struct {
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail"` // e_mail
Pass string `validate:"required,gte=8,lte=32" json:"pass"` // password
CaptchaID string `json:"captcha_id"` // captcha_id
CaptchaCode string `json:"captcha_code"` // captcha_code
}
// UserRegisterReq user register request
type UserRegisterReq struct {
// name
Name string `validate:"required,gt=3,lte=30" json:"name"`
// email
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail" `
// password
Pass string `validate:"required,gte=8,lte=32" json:"pass"`
IP string `json:"-" `
CaptchaID string `json:"captcha_id"` // captcha_id
CaptchaCode string `json:"captcha_code"` // captcha_code
}
func (u *UserRegisterReq) Check() (errFields []*validator.FormErrorField, err error) {
// TODO i18n
err = checker.CheckPassword(8, 32, 0, u.Pass)
if err != nil {
errField := &validator.FormErrorField{
ErrorField: "pass",
ErrorMsg: err.Error(),
}
errFields = append(errFields, errField)
return errFields, err
}
return nil, nil
}
// UserModifyPassWordRequest
type UserModifyPassWordRequest struct {
UserID string `json:"-" ` // user_id
OldPass string `json:"old_pass" ` // old password
Pass string `json:"pass" ` // password
}
func (u *UserModifyPassWordRequest) Check() (errFields []*validator.FormErrorField, err error) {
// TODO i18n
err = checker.CheckPassword(8, 32, 0, u.Pass)
if err != nil {
errField := &validator.FormErrorField{
ErrorField: "pass",
ErrorMsg: err.Error(),
}
errFields = append(errFields, errField)
return errFields, err
}
return nil, nil
}
type UpdateInfoRequest struct {
// display_name
DisplayName string `validate:"required,gt=0,lte=30" json:"display_name"`
// username
Username string `validate:"omitempty,gt=3,lte=30" json:"username"`
// avatar
Avatar AvatarInfo `json:"avatar"`
// bio
Bio string `validate:"omitempty,gt=0,lte=4096" json:"bio"`
// bio
BioHTML string `json:"-"`
// website
Website string `validate:"omitempty,gt=0,lte=500" json:"website"`
// location
Location string `validate:"omitempty,gt=0,lte=100" json:"location"`
// user id
UserID string `json:"-" `
}
type AvatarInfo struct {
Type string `validate:"omitempty,gt=0,lte=100" json:"type"`
Gravatar string `validate:"omitempty,gt=0,lte=200" json:"gravatar"`
Custom string `validate:"omitempty,gt=0,lte=200" json:"custom"`
}
func (req *UpdateInfoRequest) Check() (errFields []*validator.FormErrorField, err error) {
if len(req.Username) > 0 {
if checker.IsInvalidUsername(req.Username) {
errField := &validator.FormErrorField{
ErrorField: "username",
ErrorMsg: reason.UsernameInvalid,
}
errFields = append(errFields, errField)
return errFields, errors.BadRequest(reason.UsernameInvalid)
}
}
req.BioHTML = converter.Markdown2BasicHTML(req.Bio)
return nil, nil
}
// UpdateUserInterfaceRequest update user interface request
type UpdateUserInterfaceRequest struct {
// language
Language string `validate:"required,gt=1,lte=100" json:"language"`
// user id
UserId string `json:"-" `
}
type UserRetrievePassWordRequest struct {
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail" ` // e_mail
CaptchaID string `json:"captcha_id" ` // captcha_id
CaptchaCode string `json:"captcha_code" ` // captcha_code
}
type UserRePassWordRequest struct {
Code string `validate:"required,gt=0,lte=100" json:"code" ` // code
Pass string `validate:"required,gt=0,lte=32" json:"pass" ` // Password
Content string `json:"-"`
}
func (u *UserRePassWordRequest) Check() (errFields []*validator.FormErrorField, err error) {
// TODO i18n
err = checker.CheckPassword(8, 32, 0, u.Pass)
if err != nil {
errField := &validator.FormErrorField{
ErrorField: "pass",
ErrorMsg: err.Error(),
}
errFields = append(errFields, errField)
return errFields, err
}
return nil, nil
}
type UserNoticeSetRequest struct {
NoticeSwitch bool `json:"notice_switch"`
UserID string `json:"-"`
}
type UserNoticeSetResp struct {
NoticeSwitch bool `json:"notice_switch"`
}
type ActionRecordReq struct {
// action
Action string `validate:"required,oneof=login e_mail find_pass" form:"action"`
IP string `json:"-"`
}
type ActionRecordResp struct {
CaptchaID string `json:"captcha_id"`
CaptchaImg string `json:"captcha_img"`
Verify bool `json:"verify"`
}
type UserBasicInfo struct {
ID string `json:"id"` // user_id
Username string `json:"username" ` // name
Rank int `json:"rank" ` // rank
DisplayName string `json:"display_name"` // display_name
Avatar string `json:"avatar" ` // avatar
Website string `json:"website" ` // website
Location string `json:"location" ` // location
IPInfo string `json:"ip_info"` // ip info
Status string `json:"status"` // status
}
type GetOtherUserInfoByUsernameReq struct {
Username string `validate:"required,gt=0,lte=500" form:"username"`
}
type GetOtherUserInfoResp struct {
Info *GetOtherUserInfoByUsernameResp `json:"info"`
Has bool `json:"has"`
}
type UserChangeEmailSendCodeReq struct {
UserVerifyEmailSendReq
Email string `validate:"required,email,gt=0,lte=500" json:"e_mail"`
UserID string `json:"-"`
}
type UserChangeEmailVerifyReq struct {
Code string `validate:"required,gt=0,lte=500" json:"code"`
Content string `json:"-"`
}
type UserVerifyEmailSendReq struct {
CaptchaID string `validate:"omitempty,gt=0,lte=500" json:"captcha_id"`
CaptchaCode string `validate:"omitempty,gt=0,lte=500" json:"captcha_code"`
}
// UserRankingResp user ranking response
type UserRankingResp struct {
UsersWithTheMostReputation []*UserRankingSimpleInfo `json:"users_with_the_most_reputation"`
UsersWithTheMostVote []*UserRankingSimpleInfo `json:"users_with_the_most_vote"`
Staffs []*UserRankingSimpleInfo `json:"staffs"`
}
// UserRankingSimpleInfo user ranking simple info
type UserRankingSimpleInfo struct {
// username
Username string `json:"username"`
// rank
Rank int `json:"rank"`
// vote
VoteCount int `json:"vote_count"`
// display name
DisplayName string `json:"display_name"`
// avatar
Avatar string `json:"avatar"`
}
// UserUnsubscribeEmailNotificationReq user unsubscribe email notification request
type UserUnsubscribeEmailNotificationReq struct {
Code string `validate:"required,gt=0,lte=500" json:"code"`
Content string `json:"-"`
}