-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
177 lines (141 loc) · 3.98 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
package wordpress
import (
"go.opencensus.io/trace"
"crypto/md5"
"encoding/base64"
"fmt"
"github.com/elgris/sqrl"
"golang.org/x/net/context"
"strconv"
"strings"
"time"
)
// User represents a WordPress user
type User struct {
Id int64 `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
Description string `json:"description"`
Email string `json:"-"` // don't leak my email info!! >:[
Gravatar string `json:"gravatar"`
Website string `json:"url"`
Registered time.Time `json:"-"`
}
// UserQueryOptions represents the available parameters for querying
type UserQueryOptions struct {
After string `param:"after"`
Limit int `param:"limit"`
Id int64 `param:"user_id"`
IdIn []int64 `param:"user_id__in"`
IdNotIn []int64 `param:"user_id__not_in"`
Slug string `param:"slug"`
SlugIn []string `param:"slug__in"`
SlugNotIn []string `param:"slug__not_in"`
}
// GetUsers gets all user data from the database
func GetUsers(c context.Context, userIds ...int64) ([]*User, error) {
c, span := trace.StartSpan(c, "/wordpress.GetUsers")
defer span.End()
if len(userIds) == 0 {
return []*User{}, nil
}
ids, idMap := dedupe(userIds)
stmt, args, err := sqrl.Select("u.ID", "u.user_nicename", "u.display_name", "um.meta_value", "u.user_email", "u.user_url", "u.user_registered").
From(table(c, "users") + " AS u").
Join(table(c, "usermeta") + " AS um ON um.user_id = u.ID").
Where(sqrl.Eq{"meta_key": "description"}).
GroupBy("u.ID, um.meta_value").
Where(sqrl.Eq{"u.ID": ids}).ToSql()
if err != nil {
return nil, err
}
rows, err := database(c).Query(stmt, args...)
if err != nil {
return nil, err
}
ret := make([]*User, len(userIds))
for rows.Next() {
var u User
if err := rows.Scan(&u.Id, &u.Slug, &u.Name, &u.Description, &u.Email, &u.Website, &u.Registered); err != nil {
return nil, err
}
u.Gravatar = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(u.Email)))))
// insert into return set
for _, index := range idMap[u.Id] {
ret[index] = &u
}
}
var mre MissingResourcesError
for i, term := range ret {
if term == nil {
mre = append(mre, userIds[i])
}
}
if len(mre) > 0 {
return nil, err
}
return ret, nil
}
// QueryUsers returns the ids of the users that match the query
func QueryUsers(c context.Context, opts *UserQueryOptions) (Iterator, error) {
c, span := trace.StartSpan(c, "/wordpress.QueryUsers")
defer span.End()
q := sqrl.Select("ID").From(table(c, "users")).OrderBy("ID ASC")
if opts.Id != 0 {
q = q.Where(sqrl.Eq{"ID": opts.Id})
} else if len(opts.IdIn) > 0 {
q = q.Where(sqrl.Eq{"ID": opts.IdIn})
} else if len(opts.IdNotIn) > 0 {
q = q.Where(sqrl.NotEq{"ID": opts.IdNotIn})
}
if opts.Slug != "" {
q = q.Where(sqrl.Eq{"user_nicename": opts.Slug})
} else if len(opts.SlugIn) > 0 {
q = q.Where(sqrl.Eq{"user_nicename": opts.SlugIn})
} else if len(opts.SlugNotIn) > 0 {
q = q.Where(sqrl.NotEq{"user_nicename": opts.SlugNotIn})
}
if opts.After != "" {
// ignore `q.After` if any errors occur
if b, err := base64.URLEncoding.DecodeString(opts.After); err == nil {
q = q.Where("ID > ?", string(b))
}
}
if opts.Limit == 0 {
opts.Limit = 10
}
if opts.Limit > 0 {
q = q.Limit(uint64(opts.Limit))
}
stmt, args, err := q.ToSql()
if err != nil {
return nil, err
}
span.AddAttributes(trace.StringAttribute("wp/user/query", stmt))
rows, err := database(c).Query(stmt, args...)
if err != nil {
return nil, err
}
var ids []int64
for rows.Next() {
var id int64
if err = rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
span.AddAttributes(trace.Int64Attribute("wp/user/count", int64(len(ids))))
it := iteratorImpl{cursor: opts.After}
var counter int
it.next = func() (id int64, err error) {
if counter < len(ids) {
id = ids[counter]
it.cursor = base64.URLEncoding.EncodeToString([]byte(strconv.FormatInt(id, 10)))
counter++
} else {
return it.exit(Done)
}
return id, err
}
return &it, nil
}