-
Notifications
You must be signed in to change notification settings - Fork 27
/
user.go
142 lines (116 loc) · 3.17 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
package loader
import (
"fmt"
"net/http"
relay "github.com/authgear/graphql-go-relay"
"github.com/authgear/authgear-server/pkg/portal/model"
"github.com/authgear/authgear-server/pkg/portal/service"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)
type UserLoaderAppService interface {
GetManyProjectQuota(userIDs []string) ([]int, error)
}
type UserLoaderCollaboratorService interface {
GetManyProjectOwnerCount(userIDs []string) ([]int, error)
}
type UserLoaderAdminAPIService interface {
SelfDirector(actorUserID string, usage service.Usage) (func(*http.Request), error)
}
type UserLoader struct {
*graphqlutil.DataLoader `wire:"-"`
AdminAPI UserLoaderAdminAPIService
Apps UserLoaderAppService
Collaborators UserLoaderCollaboratorService
}
func NewUserLoader(adminAPI UserLoaderAdminAPIService, apps UserLoaderAppService, collaborators UserLoaderCollaboratorService) *UserLoader {
l := &UserLoader{
AdminAPI: adminAPI,
Apps: apps,
Collaborators: collaborators,
}
l.DataLoader = graphqlutil.NewDataLoader(l.LoadFunc)
return l
}
func (l *UserLoader) LoadFunc(keys []interface{}) ([]interface{}, error) {
var globalIDs []string
var ids []string
for _, iface := range keys {
key := iface.(string)
globalIDs = append(globalIDs, relay.ToGlobalID("User", key))
ids = append(ids, key)
}
params := graphqlutil.DoParams{
OperationName: "getUserNodes",
Query: `
query getUserNodes($ids: [ID!]!) {
nodes(ids: $ids) {
... on User {
id
formattedName
standardAttributes
}
}
}
`,
Variables: map[string]interface{}{
"ids": globalIDs,
},
}
r, err := http.NewRequest("POST", "/graphql", nil)
if err != nil {
return nil, err
}
director, err := l.AdminAPI.SelfDirector("", service.UsageInternal)
if err != nil {
return nil, err
}
director(r)
result, err := graphqlutil.HTTPDo(r, params)
if err != nil {
return nil, err
}
if result.HasErrors() {
return nil, fmt.Errorf("unexpected graphql errors: %v", result.Errors)
}
quotas, err := l.Apps.GetManyProjectQuota(ids)
if err != nil {
return nil, err
}
ownerCounts, err := l.Collaborators.GetManyProjectOwnerCount(ids)
if err != nil {
return nil, err
}
var userModels []interface{}
data := result.Data.(map[string]interface{})
nodes := data["nodes"].([]interface{})
for idx, iface := range nodes {
// It could be null.
userNode, ok := iface.(map[string]interface{})
if !ok {
userModels = append(userModels, nil)
} else {
userModel := &model.User{}
globalID := userNode["id"].(string)
resolvedNodeID := relay.FromGlobalID(globalID)
userModel.ID = resolvedNodeID.ID
standardAttributes := userNode["standardAttributes"].(map[string]interface{})
email, ok := standardAttributes["email"].(string)
if ok {
userModel.Email = email
}
formattedName, ok := userNode["formattedName"].(string)
if ok {
userModel.FormattedName = formattedName
}
quota := quotas[idx]
if quota < 0 {
userModel.ProjectQuota = nil
} else {
userModel.ProjectQuota = "a
}
userModel.ProjectOwnerCount = ownerCounts[idx]
userModels = append(userModels, userModel)
}
}
return userModels, nil
}