-
Notifications
You must be signed in to change notification settings - Fork 44
/
user.go
515 lines (465 loc) · 18.6 KB
/
user.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package biz
import (
"context"
"github.com/go-kratos/kratos/v2/log"
"golang.org/x/sync/errgroup"
)
type UserRepo interface {
UserRegister(ctx context.Context, email, password, code string) error
LoginByPassword(ctx context.Context, account, password, mode string) (string, error)
LoginByCode(ctx context.Context, phone, code string) (string, error)
LoginPasswordReset(ctx context.Context, account, password, code, mode string) error
LoginByWechat(ctx context.Context, code string) (string, error)
LoginByQQ(ctx context.Context, code string) (string, error)
LoginByGithub(ctx context.Context, code string) (*Github, error)
LoginByGitee(ctx context.Context, code string) (string, error)
SendPhoneCode(ctx context.Context, template, phone string) error
SendEmailCode(ctx context.Context, template, email string) error
GetCosSessionKey(ctx context.Context, uuid string) (*Credentials, error)
GetAccount(ctx context.Context, uuid string) (*UserAccount, error)
GetProfile(ctx context.Context, uuid string) (*UserProfile, error)
GetProfileList(ctx context.Context, uuids []string) ([]*UserProfile, error)
GetUserInfo(ctx context.Context, uuid string) (*UserProfile, error)
GetProfileUpdate(ctx context.Context, uuid string) (*UserProfileUpdate, error)
GetFollowList(ctx context.Context, page int32, uuid string) ([]*Follow, error)
GetFollowProfileList(ctx context.Context, page int32, uuid string, followList []*Follow) ([]*UserProfile, error)
GetFollowAchievementList(ctx context.Context, page int32, uuid string, followList []*Follow) ([]*Achievement, error)
GetFollowedProfileList(ctx context.Context, page int32, uuid string, followedList []*Follow) ([]*UserProfile, error)
GetFollowedAchievementList(ctx context.Context, page int32, uuid string, followedList []*Follow) ([]*Achievement, error)
GetSearchAchievementList(ctx context.Context, searchList []*UserSearch) ([]*Achievement, error)
GetFollowListCount(ctx context.Context, uuid string) (int32, error)
GetFollowedList(ctx context.Context, page int32, uuid string) ([]*Follow, error)
GetFollowedListCount(ctx context.Context, uuid string) (int32, error)
GetUserFollow(ctx context.Context, uuid, userUuid string) (bool, error)
GetUserFollows(ctx context.Context, uuid string) (map[string]bool, error)
GetUserSearch(ctx context.Context, page int32, search string) ([]*UserSearch, int32, error)
GetAvatarReview(ctx context.Context, page int32, uuid string) ([]*UserImageReview, error)
GetCoverReview(ctx context.Context, page int32, uuid string) ([]*UserImageReview, error)
SetProfileUpdate(ctx context.Context, profile *UserProfileUpdate) error
SetUserPhone(ctx context.Context, uuid, phone, code string) error
SetUserPassword(ctx context.Context, uuid, password string) error
SetUserWechat(ctx context.Context, uuid, code, redirectUrl string) error
SetUserQQ(ctx context.Context, uuid, code, redirectUrl string) error
SetUserGitee(ctx context.Context, uuid, code, redirectUrl string) error
SetUserGithub(ctx context.Context, uuid, code, redirectUrl string) error
SetUserEmail(ctx context.Context, uuid, email, code string) error
SetUserFollow(ctx context.Context, uuid, userUuid string) error
CancelUserFollow(ctx context.Context, uuid, userUuid string) error
ChangeUserPassword(ctx context.Context, uuid, oldpassword, password string) error
UnbindUserPhone(ctx context.Context, uuid, phone, email, account, password, code, choose, mode, redirectUri string) error
UnbindUserEmail(ctx context.Context, uuid, phone, email, account, password, code, choose, mode, redirectUri string) error
UnbindUserWechat(ctx context.Context, uuid, phone, email, account, password, code, choose, mode, redirectUri string) error
UnbindUserQQ(ctx context.Context, uuid, phone, email, account, password, code, choose, mode, redirectUri string) error
UnbindUserGitee(ctx context.Context, uuid, phone, email, account, password, code, choose, mode, redirectUri string) error
UnbindUserGithub(ctx context.Context, uuid, phone, email, account, password, code, choose, mode, redirectUri string) error
}
type UserUseCase struct {
repo UserRepo
achRepo AchievementRepo
creationRepo CreationRepo
re Recovery
log *log.Helper
}
func NewUserUseCase(repo UserRepo, achRepo AchievementRepo, creationRepo CreationRepo, re Recovery, logger log.Logger) *UserUseCase {
return &UserUseCase{
repo: repo,
achRepo: achRepo,
creationRepo: creationRepo,
re: re,
log: log.NewHelper(log.With(logger, "module", "bff/biz/UserUseCase")),
}
}
func (r *UserUseCase) UserRegister(ctx context.Context, email, password, code string) error {
return r.repo.UserRegister(ctx, email, password, code)
}
func (r *UserUseCase) LoginByPassword(ctx context.Context, account, password, mode string) (string, error) {
return r.repo.LoginByPassword(ctx, account, password, mode)
}
func (r *UserUseCase) LoginByCode(ctx context.Context, phone, code string) (string, error) {
return r.repo.LoginByCode(ctx, phone, code)
}
func (r *UserUseCase) LoginPasswordReset(ctx context.Context, account, password, code, mode string) error {
return r.repo.LoginPasswordReset(ctx, account, password, code, mode)
}
func (r *UserUseCase) LoginByWechat(ctx context.Context, code string) (string, error) {
return r.repo.LoginByWechat(ctx, code)
}
func (r *UserUseCase) LoginByQQ(ctx context.Context, code string) (string, error) {
return r.repo.LoginByQQ(ctx, code)
}
func (r *UserUseCase) LoginByGithub(ctx context.Context, code string) (*Github, error) {
return r.repo.LoginByGithub(ctx, code)
}
func (r *UserUseCase) LoginByGitee(ctx context.Context, code string) (string, error) {
return r.repo.LoginByGitee(ctx, code)
}
func (r *UserUseCase) SendPhoneCode(ctx context.Context, template, phone string) error {
return r.repo.SendPhoneCode(ctx, template, phone)
}
func (r *UserUseCase) SendEmailCode(ctx context.Context, template, email string) error {
return r.repo.SendEmailCode(ctx, template, email)
}
func (r *UserUseCase) GetCosSessionKey(ctx context.Context) (*Credentials, error) {
uuid := ctx.Value("uuid").(string)
return r.repo.GetCosSessionKey(ctx, uuid)
}
func (r *UserUseCase) GetAccount(ctx context.Context) (*UserAccount, error) {
uuid := ctx.Value("uuid").(string)
return r.repo.GetAccount(ctx, uuid)
}
func (r *UserUseCase) GetProfile(ctx context.Context) (*UserProfile, error) {
uuid := ctx.Value("uuid").(string)
return r.repo.GetProfile(ctx, uuid)
}
func (r *UserUseCase) GetProfileList(ctx context.Context, uuids []string) ([]*UserProfile, error) {
return r.repo.GetProfileList(ctx, uuids)
}
func (r *UserUseCase) GetUserInfo(ctx context.Context) (*UserProfile, error) {
uuid := ctx.Value("uuid").(string)
userProfile := &UserProfile{}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
profile, err := r.repo.GetUserInfo(ctx, uuid)
if err != nil {
return err
}
userProfile.Uuid = profile.Uuid
userProfile.Username = profile.Username
userProfile.School = profile.School
userProfile.Company = profile.Company
userProfile.Job = profile.Job
userProfile.Homepage = profile.Homepage
userProfile.Gitee = profile.Gitee
userProfile.Github = profile.Github
userProfile.Introduce = profile.Introduce
userProfile.Created = profile.Created
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
ach, err := r.achRepo.GetUserAchievement(ctx, uuid)
if err != nil {
return err
}
userProfile.Score = ach.Score
userProfile.Agree = ach.Agree
userProfile.Collect = ach.Collect
userProfile.View = ach.View
userProfile.Follow = ach.Follow
userProfile.Followed = ach.Followed
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
creation, err := r.creationRepo.GetCreationUser(ctx, uuid)
if err != nil {
return err
}
userProfile.Article = creation.Article
userProfile.Talk = creation.Talk
userProfile.Column = creation.Column
userProfile.Collections = creation.Collections
userProfile.Subscribe = creation.Subscribe
return nil
}))
err := g.Wait()
if err != nil {
return nil, err
}
return userProfile, err
}
func (r *UserUseCase) GetUserInfoVisitor(ctx context.Context, uuid string) (*UserProfile, error) {
userProfile := &UserProfile{}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
profile, err := r.repo.GetUserInfo(ctx, uuid)
if err != nil {
return err
}
userProfile.Uuid = profile.Uuid
userProfile.Username = profile.Username
userProfile.School = profile.School
userProfile.Company = profile.Company
userProfile.Job = profile.Job
userProfile.Homepage = profile.Homepage
userProfile.Gitee = profile.Gitee
userProfile.Github = profile.Github
userProfile.Introduce = profile.Introduce
userProfile.Created = profile.Created
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
ach, err := r.achRepo.GetUserAchievement(ctx, uuid)
if err != nil {
return err
}
userProfile.Score = ach.Score
userProfile.Agree = ach.Agree
userProfile.Collect = ach.Collect
userProfile.View = ach.View
userProfile.Follow = ach.Follow
userProfile.Followed = ach.Followed
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
creation, err := r.creationRepo.GetCreationUserVisitor(ctx, uuid)
if err != nil {
return err
}
userProfile.Article = creation.Article
userProfile.Talk = creation.Talk
userProfile.Column = creation.Column
userProfile.Collections = creation.Collections
return nil
}))
err := g.Wait()
if err != nil {
return nil, err
}
return userProfile, err
}
func (r *UserUseCase) GetProfileUpdate(ctx context.Context) (*UserProfileUpdate, error) {
uuid := ctx.Value("uuid").(string)
return r.repo.GetProfileUpdate(ctx, uuid)
}
func (r *UserUseCase) GetUserFollow(ctx context.Context, uuid string) (bool, error) {
userUuid := ctx.Value("uuid").(string)
return r.repo.GetUserFollow(ctx, uuid, userUuid)
}
func (r *UserUseCase) GetFollowList(ctx context.Context, page int32, uuid string) ([]*Follow, error) {
followList, err := r.repo.GetFollowList(ctx, page, uuid)
if err != nil {
return nil, err
}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
userProfileList, err := r.repo.GetFollowProfileList(ctx, page, uuid, followList)
if err != nil {
return err
}
for _, item := range userProfileList {
for index, listItem := range followList {
if listItem.Follow == item.Uuid {
followList[index].Username = item.Username
followList[index].Introduce = item.Introduce
}
}
}
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
userAchievementList, err := r.repo.GetFollowAchievementList(ctx, page, uuid, followList)
if err != nil {
return err
}
for _, item := range userAchievementList {
for index, listItem := range followList {
if listItem.Follow == item.Uuid {
followList[index].Agree = item.Agree
followList[index].View = item.View
followList[index].FollowNum = item.Follow
followList[index].FollowedNum = item.Followed
}
}
}
return nil
}))
err = g.Wait()
if err != nil {
return nil, err
}
return followList, nil
}
func (r *UserUseCase) GetFollowListCount(ctx context.Context, uuid string) (int32, error) {
return r.repo.GetFollowListCount(ctx, uuid)
}
func (r *UserUseCase) GetFollowedList(ctx context.Context, page int32, uuid string) ([]*Follow, error) {
followedList, err := r.repo.GetFollowedList(ctx, page, uuid)
if err != nil {
return nil, err
}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
userProfileList, err := r.repo.GetFollowedProfileList(ctx, page, uuid, followedList)
if err != nil {
return err
}
for _, item := range userProfileList {
for index, listItem := range followedList {
if listItem.Followed == item.Uuid {
followedList[index].Username = item.Username
followedList[index].Introduce = item.Introduce
}
}
}
return nil
}))
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
userAchievementList, err := r.repo.GetFollowedAchievementList(ctx, page, uuid, followedList)
if err != nil {
return err
}
for _, item := range userAchievementList {
for index, listItem := range followedList {
if listItem.Followed == item.Uuid {
followedList[index].Agree = item.Agree
followedList[index].View = item.View
followedList[index].FollowNum = item.Follow
followedList[index].FollowedNum = item.Followed
}
}
}
return nil
}))
err = g.Wait()
if err != nil {
return nil, err
}
return followedList, nil
}
func (r *UserUseCase) GetFollowedListCount(ctx context.Context, uuid string) (int32, error) {
return r.repo.GetFollowedListCount(ctx, uuid)
}
func (r *UserUseCase) GetUserFollows(ctx context.Context) (map[string]bool, error) {
uuid := ctx.Value("uuid").(string)
return r.repo.GetUserFollows(ctx, uuid)
}
func (r *UserUseCase) GetTimeLineUsers(ctx context.Context) ([]*TimeLIneFollows, error) {
uuid := ctx.Value("uuid").(string)
follows, err := r.repo.GetUserFollows(ctx, uuid)
if err != nil {
return nil, err
}
followList := make([]*TimeLIneFollows, 0, len(follows))
uuids := make([]string, 0)
for key := range follows {
followList = append(followList, &TimeLIneFollows{
Uuid: key,
})
uuids = append(uuids, key)
}
g, _ := errgroup.WithContext(ctx)
g.Go(r.re.GroupRecover(ctx, func(ctx context.Context) error {
userProfileList, err := r.repo.GetProfileList(ctx, uuids)
if err != nil {
return err
}
for _, item := range userProfileList {
for index, listItem := range followList {
if listItem.Uuid == item.Uuid {
followList[index].Username = item.Username
}
}
}
return nil
}))
err = g.Wait()
if err != nil {
return nil, err
}
return followList, nil
}
func (r *UserUseCase) GetUserSearch(ctx context.Context, page int32, search string) ([]*UserSearch, int32, error) {
searchList, total, err := r.repo.GetUserSearch(ctx, page, search)
if err != nil {
return nil, 0, err
}
userAchievementList, err := r.repo.GetSearchAchievementList(ctx, searchList)
if err != nil {
return nil, 0, err
}
for _, item := range userAchievementList {
for index, listItem := range searchList {
if listItem.Uuid == item.Uuid {
searchList[index].Agree = item.Agree
searchList[index].View = item.View
searchList[index].FollowNum = item.Follow
searchList[index].FollowedNum = item.Followed
}
}
}
return searchList, total, nil
}
func (r *UserUseCase) GetAvatarReview(ctx context.Context, page int32) ([]*UserImageReview, error) {
uuid := ctx.Value("uuid").(string)
reviewList, err := r.repo.GetAvatarReview(ctx, page, uuid)
if err != nil {
return nil, err
}
return reviewList, nil
}
func (r *UserUseCase) GetCoverReview(ctx context.Context, page int32) ([]*UserImageReview, error) {
uuid := ctx.Value("uuid").(string)
reviewList, err := r.repo.GetCoverReview(ctx, page, uuid)
if err != nil {
return nil, err
}
return reviewList, nil
}
func (r *UserUseCase) SetUserProfile(ctx context.Context, profile *UserProfileUpdate) error {
uuid := ctx.Value("uuid").(string)
profile.Uuid = uuid
return r.repo.SetProfileUpdate(ctx, profile)
}
func (r *UserUseCase) SetUserPhone(ctx context.Context, phone, code string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserPhone(ctx, uuid, phone, code)
}
func (r *UserUseCase) SetUserEmail(ctx context.Context, email, code string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserEmail(ctx, uuid, email, code)
}
func (r *UserUseCase) SetUserPassword(ctx context.Context, password string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserPassword(ctx, uuid, password)
}
func (r *UserUseCase) SetUserWechat(ctx context.Context, code, redirectUrl string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserWechat(ctx, uuid, code, redirectUrl)
}
func (r *UserUseCase) SetUserQQ(ctx context.Context, code, redirectUrl string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserQQ(ctx, uuid, code, redirectUrl)
}
func (r *UserUseCase) SetUserGitee(ctx context.Context, code, redirectUrl string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserGitee(ctx, uuid, code, redirectUrl)
}
func (r *UserUseCase) SetUserGithub(ctx context.Context, code, redirectUrl string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.SetUserGithub(ctx, uuid, code, redirectUrl)
}
func (r *UserUseCase) SetUserFollow(ctx context.Context, uuid string) error {
userUuid := ctx.Value("uuid").(string)
return r.repo.SetUserFollow(ctx, uuid, userUuid)
}
func (r *UserUseCase) CancelUserFollow(ctx context.Context, uuid string) error {
userUuid := ctx.Value("uuid").(string)
return r.repo.CancelUserFollow(ctx, uuid, userUuid)
}
func (r *UserUseCase) ChangeUserPassword(ctx context.Context, oldpassword, password string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.ChangeUserPassword(ctx, uuid, oldpassword, password)
}
func (r *UserUseCase) UnbindUserPhone(ctx context.Context, phone, email, account, password, code, choose, mode, redirectUri string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.UnbindUserPhone(ctx, uuid, phone, email, account, password, code, choose, mode, redirectUri)
}
func (r *UserUseCase) UnbindUserEmail(ctx context.Context, phone, email, account, password, code, choose, mode, redirectUri string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.UnbindUserEmail(ctx, uuid, phone, email, account, password, code, choose, mode, redirectUri)
}
func (r *UserUseCase) UnbindUserWechat(ctx context.Context, phone, email, account, password, code, choose, mode, redirectUri string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.UnbindUserWechat(ctx, uuid, phone, email, account, password, code, choose, mode, redirectUri)
}
func (r *UserUseCase) UnbindUserQQ(ctx context.Context, phone, email, account, password, code, choose, mode, redirectUri string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.UnbindUserQQ(ctx, uuid, phone, email, account, password, code, choose, mode, redirectUri)
}
func (r *UserUseCase) UnbindUserGitee(ctx context.Context, phone, email, account, password, code, choose, mode, redirectUri string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.UnbindUserGitee(ctx, uuid, phone, email, account, password, code, choose, mode, redirectUri)
}
func (r *UserUseCase) UnbindUserGithub(ctx context.Context, phone, email, account, password, code, choose, mode, redirectUri string) error {
uuid := ctx.Value("uuid").(string)
return r.repo.UnbindUserGithub(ctx, uuid, phone, email, account, password, code, choose, mode, redirectUri)
}