-
Notifications
You must be signed in to change notification settings - Fork 107
/
user.go
90 lines (74 loc) · 2.83 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
package wtf
import (
"context"
"time"
)
// User represents a user in the system. Users are typically created via OAuth
// using the AuthService but users can also be created directly for testing.
type User struct {
ID int `json:"id"`
// User's preferred name & email.
Name string `json:"name"`
Email string `json:"email"`
// Randomly generated API key for use with the CLI.
APIKey string `json:"-"`
// Timestamps for user creation & last update.
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
// List of associated OAuth authentication objects.
// Currently only GitHub is supported so there should only be a maximum of one.
Auths []*Auth `json:"auths"`
}
// Validate returns an error if the user contains invalid fields.
// This only performs basic validation.
func (u *User) Validate() error {
if u.Name == "" {
return Errorf(EINVALID, "User name required.")
}
return nil
}
// AvatarURL returns a URL to the avatar image for the user.
// This loops over all auth providers to find the first available avatar.
// Currently only GitHub is supported. Returns blank string if no avatar URL available.
func (u *User) AvatarURL(size int) string {
for _, auth := range u.Auths {
if s := auth.AvatarURL(size); s != "" {
return s
}
}
return ""
}
// UserService represents a service for managing users.
type UserService interface {
// Retrieves a user by ID along with their associated auth objects.
// Returns ENOTFOUND if user does not exist.
FindUserByID(ctx context.Context, id int) (*User, error)
// Retrieves a list of users by filter. Also returns total count of matching
// users which may differ from returned results if filter.Limit is specified.
FindUsers(ctx context.Context, filter UserFilter) ([]*User, int, error)
// Creates a new user. This is only used for testing since users are typically
// created during the OAuth creation process in AuthService.CreateAuth().
CreateUser(ctx context.Context, user *User) error
// Updates a user object. Returns EUNAUTHORIZED if current user is not
// the user that is being updated. Returns ENOTFOUND if user does not exist.
UpdateUser(ctx context.Context, id int, upd UserUpdate) (*User, error)
// Permanently deletes a user and all owned dials. Returns EUNAUTHORIZED
// if current user is not the user being deleted. Returns ENOTFOUND if
// user does not exist.
DeleteUser(ctx context.Context, id int) error
}
// UserFilter represents a filter passed to FindUsers().
type UserFilter struct {
// Filtering fields.
ID *int `json:"id"`
Email *string `json:"email"`
APIKey *string `json:"apiKey"`
// Restrict to subset of results.
Offset int `json:"offset"`
Limit int `json:"limit"`
}
// UserUpdate represents a set of fields to be updated via UpdateUser().
type UserUpdate struct {
Name *string `json:"name"`
Email *string `json:"email"`
}