-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
158 lines (142 loc) · 4.27 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
package myradio
import (
"errors"
"time"
"github.com/UniversityRadioYork/myradio-go/api"
)
// User represents a MyRadio user.
type User struct {
MemberID int
Fname, Sname string
Email string `json:"public_email"`
Receiveemail bool `json:"receive_email"`
//@TODO: fix the api and make it return a photo object
Photo string
Bio string
}
// Officership represents an officership a user holds.
type Officership struct {
OfficerId uint `json:"officerid,string"`
OfficerName string `json:"officer_name"`
TeamId uint `json:"teamid,string"`
FromDateRaw string `json:"from_date,omitempty"`
FromDate time.Time
TillDateRaw string `json:"till_date,omitempty"`
TillDate time.Time
}
// Photo represents a photo of a user.
type Photo struct {
PhotoId uint `json:"photoid"`
DateAddedRaw string `json:"date_added"`
DateAdded time.Time
Format string `json:"format"`
Owner uint `json:"owner"`
Url string `json:"url"`
}
// UserAlias represents a user alias.
type UserAlias struct {
Source string
Destination string
}
// College represents a college.
type College struct {
CollegeId int `json:"value,string"`
CollegeName string `json:"text"`
}
// GetUser retrieves the User with the given ID.
// This consumes one API request.
func (s *Session) GetUser(id int) (user *User, err error) {
rq := api.NewRequestf("/user/%d", id)
rq.Mixins = []string{"personal_data"}
err = s.do(rq).Into(&user)
return
}
// GetUserBio retrieves the biography of the user with the given ID.
// This consumes one API request.
func (s *Session) GetUserBio(id int) (bio string, err error) {
rs := s.getf("/user/%d/bio/", id)
if rs.IsEmpty() {
err = errors.New("No bio set")
return
}
err = rs.Into(&bio)
return
}
// GetUserName retrieves the name of the user with the given ID.
// This consumes one API request.
func (s *Session) GetUserName(id int) (name string, err error) {
err = s.getf("/user/%d/name/", id).Into(&name)
return
}
// GetUserProfilePhoto retrieves the profile photo of the user with the given ID.
// This consumes one API request.
func (s *Session) GetUserProfilePhoto(id int) (profilephoto Photo, err error) {
rs := s.getf("/user/%d/profilephoto/", id)
if rs.IsEmpty() {
err = errors.New("No profile picture set")
return
}
err = rs.Into(&profilephoto)
if err != nil {
return
}
profilephoto.DateAdded, err = time.Parse("02/01/2006 15:04", profilephoto.DateAddedRaw)
return
}
// GetUserOfficerships retrieves all officerships held by the user with the given ID.
// This consumes one API request.
func (s *Session) GetUserOfficerships(id int) (officerships []Officership, err error) {
err = s.getf("/user/%d/officerships/", id).Into(&officerships)
if err != nil {
return
}
for k, v := range officerships {
if officerships[k].FromDateRaw != "" {
officerships[k].FromDate, err = time.Parse("2006-01-02", v.FromDateRaw)
if err != nil {
return
}
}
if officerships[k].TillDateRaw != "" {
officerships[k].TillDate, err = time.Parse("2006-01-02", v.TillDateRaw)
if err != nil {
return
}
}
}
return
}
// GetUserShowCredits retrieves all show credits associated with the user with the given ID.
// This consumes one API request.
func (s *Session) GetUserShowCredits(id int) (shows []ShowMeta, err error) {
err = s.getf("/user/%d/shows/", id).Into(&shows)
return
}
// GetUserAliases retrieves all aliases associated with the user with the given ID.
// This consumes one API request.
func (s *Session) GetUserAliases() ([]UserAlias, error) {
raw := [][]string{}
err := s.get("/user/allaliases/").Into(&raw)
if err != nil {
return nil, err
}
var aliases = make([]UserAlias, len(raw))
for k, v := range raw {
aliases[k].Source = v[0]
aliases[k].Destination = v[1]
}
return aliases, nil
}
// CreateOrActivateUser creates oir activates a new myradio user with the given parameters
// This consumes one API request.
func (s *Session) CreateOrActivateUser(formParams map[string][]string) (user *User, err error) {
rs := s.post("/user/createoractivate", formParams)
err = rs.Into(&user)
return
}
// GetColleges retrieves a list of all current colleges
// This consumes one API request.
func (s *Session) GetColleges() (colleges []College, err error) {
err = s.get("/user/colleges").Into(&colleges)
return
}