-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-extractor.go
78 lines (62 loc) · 1.65 KB
/
user-extractor.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
package goautowp
import (
"context"
"fmt"
"time"
"github.com/autowp/goautowp/image/storage"
"github.com/autowp/goautowp/users"
"github.com/casbin/casbin"
"github.com/drexedam/gravatar"
"google.golang.org/protobuf/types/known/timestamppb"
)
const avatarSize = 70
type UserExtractor struct {
enforcer *casbin.Enforcer
imageStorage *storage.Storage
}
func NewUserExtractor(enforcer *casbin.Enforcer, imageStorage *storage.Storage) *UserExtractor {
return &UserExtractor{
enforcer: enforcer,
imageStorage: imageStorage,
}
}
func (s *UserExtractor) Extract(ctx context.Context, row *users.DBUser) (*APIUser, error) {
longAway := true
if row.LastOnline != nil {
date := time.Now().AddDate(0, -6, 0)
longAway = date.After(*row.LastOnline)
}
isGreen := row.Role != "" && s.enforcer.Enforce(row.Role, "status", "be-green")
route := []string{"/users", fmt.Sprintf("user%d", row.ID)}
if row.Identity != nil {
route = []string{"/users", *row.Identity}
}
identity := ""
if row.Identity != nil {
identity = *row.Identity
}
user := APIUser{
Id: row.ID,
Name: row.Name,
Deleted: row.Deleted,
LongAway: longAway,
Green: isGreen,
Route: route,
Identity: identity,
SpecsWeight: row.SpecsWeight,
}
if row.LastOnline != nil {
user.LastOnline = timestamppb.New(*row.LastOnline)
}
if row.EMail != nil {
user.Gravatar = gravatar.New(*row.EMail).Size(avatarSize).Rating(gravatar.G).AvatarURL()
}
if row.Img != nil {
avatar, err := s.imageStorage.FormattedImage(ctx, *row.Img, "avatar")
if err != nil {
return nil, err
}
user.Avatar = APIImageToGRPC(avatar)
}
return &user, nil
}