forked from open-networks/go-msgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.go
More file actions
142 lines (124 loc) · 5.47 KB
/
Copy pathUser.go
File metadata and controls
142 lines (124 loc) · 5.47 KB
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 msgraph
import (
"fmt"
"net/url"
"strings"
"time"
)
// User represents a user from the ms graph API
type User struct {
ID string `json:"id"`
BusinessPhones []string `json:"businessPhones"`
DisplayName string `json:"displayName"`
GivenName string `json:"givenName"`
Mail string `json:"mail"`
MobilePhone string `json:"mobilePhone"`
PreferredLanguage string `json:"preferredLanguage"`
Surname string `json:"surname"`
UserPrincipalName string `json:"userPrincipalName"`
activePhone string // private cache for the active phone number
graphClient *GraphClient // the graphClient that called the user
}
func (u *User) String() string {
return fmt.Sprintf("User(ID: \"%v\", BusinessPhones: \"%v\", DisplayName: \"%v\", GivenName: \"%v\", "+
"Mail: \"%v\", MobilePhone: \"%v\", PreferredLanguage: \"%v\", Surname: \"%v\", UserPrincipalName: \"%v\", "+
"ActivePhone: \"%v\", DirectAPIConnection: %v)",
u.ID, u.BusinessPhones, u.DisplayName, u.GivenName, u.Mail, u.MobilePhone, u.PreferredLanguage, u.Surname,
u.UserPrincipalName, u.activePhone, u.graphClient != nil)
}
// setGraphClient sets the graphClient instance in this instance and all child-instances (if any)
func (u *User) setGraphClient(gC *GraphClient) {
u.graphClient = gC
}
// ListCalendars returns all calendars associated to that user.
//
// Reference: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list_calendars
func (u User) ListCalendars() (Calendars, error) {
if u.graphClient == nil {
return Calendars{}, ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/users/%v/calendars", u.ID)
var marsh struct {
Calendars Calendars `json:"value"`
}
err := u.graphClient.makeGETAPICall(resource, nil, &marsh)
marsh.Calendars.setGraphClient(u.graphClient)
return marsh.Calendars, err
}
// ListCalendarView returns the CalendarEvents of the given user within the specified
// start- and endDateTime. The calendar used is the default calendar of the user.
// Returns an error if the user it not GraphClient sourced or if there is any error
// during the API-call.
//
// See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list_calendarview
func (u User) ListCalendarView(startDateTime, endDateTime time.Time) (CalendarEvents, error) {
if u.graphClient == nil {
return CalendarEvents{}, ErrNotGraphClientSourced
}
if len(globalSupportedTimeZones.Value) == 0 {
var err error
globalSupportedTimeZones, err = u.getTimeZoneChoices()
if err != nil {
return CalendarEvents{}, err
}
}
resource := fmt.Sprintf("/users/%v/calendar/calendarview", u.ID)
// set GET-Params for start and end time
getParams := url.Values{}
getParams.Add("startdatetime", startDateTime.Format("2006-01-02T00:00:00"))
getParams.Add("enddatetime", endDateTime.Format("2006-01-02T00:00:00"))
var calendarEvents CalendarEvents
return calendarEvents, u.graphClient.makeGETAPICall(resource, getParams, &calendarEvents)
}
// getTimeZoneChoices grabs all supported time zones from microsoft for this user.
// This should actually be the same for every user. Only used internally by this
// msgraph package.
//
// See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/outlookuser_supportedtimezones
func (u User) getTimeZoneChoices() (supportedTimeZones, error) {
var ret supportedTimeZones
err := u.graphClient.makeGETAPICall(fmt.Sprintf("/users/%s/outlook/supportedTimeZones", u.ID), nil, &ret)
return ret, err
}
// GetActivePhone returns the space-trimmed active phone-number of the user. The active
// phone number is either the MobilePhone number or the first business-Phone number
func (u *User) GetActivePhone() string {
if u.activePhone != "" { // use cached value if any
return u.activePhone
}
// no cached active phone number, evaluate & cache it now:
u.activePhone = strings.Replace(u.MobilePhone, " ", "", -1)
if u.activePhone == "" && len(u.BusinessPhones) > 0 {
u.activePhone = strings.Replace(u.BusinessPhones[0], " ", "", -1)
}
return u.activePhone
}
// GetShortName returns the first part of UserPrincipalName before the @. If there
// is no @, then just the UserPrincipalName will be returned
func (u User) GetShortName() string {
supn := strings.Split(u.UserPrincipalName, "@")
if len(supn) != 2 {
return u.UserPrincipalName
}
return strings.ToUpper(supn[0])
}
// GetFullName returns the full name in that format: <firstname> <lastname>
func (u User) GetFullName() string {
return fmt.Sprintf("%v %v", u.GivenName, u.Surname)
}
// PrettySimpleString returns the User-instance simply formatted for logging purposes: {FullName (email) (activePhone)}
func (u User) PrettySimpleString() string {
return fmt.Sprintf("{ %v (%v) (%v) }", u.GetFullName(), u.Mail, u.GetActivePhone())
}
// Equal returns wether the user equals the other User by comparing every property
// of the user including the ID
func (u User) Equal(other User) bool {
var equalBool = true
for i := 0; i < len(u.BusinessPhones) && i < len(other.BusinessPhones); i++ {
equalBool = equalBool && u.BusinessPhones[i] == other.BusinessPhones[i]
}
equalBool = equalBool && len(u.BusinessPhones) == len(other.BusinessPhones)
return equalBool && u.ID == other.ID && u.DisplayName == other.DisplayName && u.GivenName == other.GivenName &&
u.Mail == other.Mail && u.MobilePhone == other.MobilePhone && u.PreferredLanguage == other.PreferredLanguage &&
u.Surname == other.Surname && u.UserPrincipalName == other.UserPrincipalName
}