Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this years members #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package myradio

import (
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/UniversityRadioYork/myradio-go/api"
)

const BaseEmailDomain = "@york.ac.uk"

// User represents a MyRadio user.
type User struct {
MemberID int
Expand All @@ -16,6 +21,7 @@ type User struct {
//@TODO: fix the api and make it return a photo object
Photo string
Bio string
Eduroam string
}

// Officership represents an officership a user holds.
Expand Down Expand Up @@ -51,6 +57,45 @@ type College struct {
CollegeName string `json:"text"`
}

// GetThisYearsMembers retrieves all the users.
// This consumes one API request.
func (s *Session) GetThisYearsMembers() (users []User, err error) {
var preProcessedUsers []struct {
Name string `json:"name"`
MemberID string `json:"memberid"`
Email string `json:"email"`
Eduroam string `json:"eduroam"`
}

rq := api.NewRequest("/profile/thisyearsmembers")
err = s.do(rq).Into(&preProcessedUsers)
if err != nil {
return
}

for _, user := range preProcessedUsers {
splitName := strings.Split(user.Name, ", ")
memberID, err := strconv.Atoi(user.MemberID)
if err != nil {
return nil, err
}

if user.Email == "" {
user.Email = fmt.Sprintf("%s%s", user.Eduroam, BaseEmailDomain)
}

users = append(users, User{
Fname: splitName[1],
Sname: splitName[0],
Email: user.Email,
MemberID: memberID,
Eduroam: user.Eduroam,
})
}

return
}

// GetUser retrieves the User with the given ID.
// This consumes one API request.
func (s *Session) GetUser(id int) (user *User, err error) {
Expand Down Expand Up @@ -156,3 +201,26 @@ func (s *Session) GetColleges() (colleges []College, err error) {
err = s.get("/user/colleges").Into(&colleges)
return
}

// UserCredentialsTest takes a username and password and checks it against myradio
// If it's valid, it returns the user pointer
// If it's invalid login, it returns nil pointer, but also no error
// This consumes one API request.
func (s *Session) UserCredentialsTest(username string, password string) (*User, error) {
var response interface{}
rs := s.post("/auth/testcredentials", map[string][]string{"user": {username}, "pass": {password}})
if err := rs.Into(&response); err != nil {
return nil, err
}

switch response.(type) {
case bool:
// not valid credentials
return nil, nil
case map[string]interface{}:
var user User
rs.Into(&user)
return &user, nil
}
return nil, fmt.Errorf("wrong type")
}