-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSechdulerService.cs
More file actions
238 lines (189 loc) · 8.29 KB
/
SechdulerService.cs
File metadata and controls
238 lines (189 loc) · 8.29 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.TermStore;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Http.Generated;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.Graph.Constants;
namespace MSGraph_Hack_Togother
{
public class SechdulerService:ISechdulerService
{
// Settings object
private Settings? _settings;
// User auth token credential
private DeviceCodeCredential? _deviceCodeCredential;
// Client configured with user authentication
private GraphServiceClient? _userClient;
private HashSet<AttendeeBase> _attendees=Enumerable.Empty<AttendeeBase>().ToHashSet();
private HashSet<User> _users=Enumerable.Empty<User>().ToHashSet();
private async Task<string> GetUserTokenAsync()
{
// Ensure credential isn't null
_ = _deviceCodeCredential ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
// Ensure scopes isn't null
_ = _settings?.GraphUserScopes ?? throw new System.ArgumentNullException("Argument 'scopes' cannot be null");
// Request token with given scopes
var context = new TokenRequestContext(_settings.GraphUserScopes);
var response = await _deviceCodeCredential.GetTokenAsync(context);
return response.Token;
}
public void addAttendee(EmailAddress attendeeEmail, AttendeeType type)
{
var attendeeObj = _attendees.FirstOrDefault(x => x.EmailAddress.Address==attendeeEmail.Address);
if (_attendees.TryGetValue(attendeeObj, out AttendeeBase exist))
{
exist.Type = type;
_attendees.Remove(attendeeObj);
_attendees.Add(exist);
}
else {
_attendees.Add(new AttendeeBase() {EmailAddress=attendeeEmail,Type=type });
}
}
public void addUsersToCache(User user)
{
_users.Add(user);
}
public async Task<Event> CreateMeetingAsync(string subject, string content, DateTimeTimeZone start, DateTimeTimeZone end, bool AllowNewTimeProposals)
{
// Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var requestBody = new Event
{
Subject = subject,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = content,
},
Start =start,
End = end,
Attendees = _attendees.Select(a => new Attendee
{
EmailAddress = a.EmailAddress,
Type = a.Type,
}).ToList(),
IsOnlineMeeting = true,
OnlineMeetingProvider = OnlineMeetingProviderType.TeamsForBusiness,
TransactionId = Guid.NewGuid().ToString(),
};
try {
var result = await _userClient.Me.Events.PostAsync(requestBody, (requestConfiguration) =>
{
requestConfiguration.Headers.Add("Prefer", "outlook.timezone=\"Pacific Standard Time\"");
});
return result;
} catch (Exception ex) {
Console.WriteLine(ex.Message.ToString());
return null;
}
}
public async Task<MeetingTimeSuggestionsResult> FindMeetingTimes( TimeConstraint timeConstraint, bool IsOrganizerOptional, TimeSpan MeetingDuration, bool ReturnSuggestionReasons, double MinimumAttendeePercentage)
{
// Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var requestBody = new Microsoft.Graph.Me.FindMeetingTimes.FindMeetingTimesPostRequestBody
{
Attendees = _attendees.ToList(),
TimeConstraint = timeConstraint,
IsOrganizerOptional = IsOrganizerOptional,
MeetingDuration = MeetingDuration,
ReturnSuggestionReasons = ReturnSuggestionReasons,
MinimumAttendeePercentage = MinimumAttendeePercentage,
};
try
{
var result = await _userClient.Me.FindMeetingTimes.PostAsync(requestBody, (requestConfiguration) =>
{
requestConfiguration.Headers.Add("Prefer", "outlook.timezone=\"Pacific Standard Time\"");
});
return result;
}
catch (Exception ex) {
return null;
}
}
public async Task<User> GetAuthorizedUserAsync()
{
// Ensure client isn't null
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var req = await _userClient.Me.GetAsync();
return new User
{
// Only request specific properties
DisplayName = req.DisplayName,
Mail = req.Mail,
UserPrincipalName = req.UserPrincipalName
};
}
public void InitializeGraphForUserAuth(Settings settings, Func<DeviceCodeInfo, CancellationToken, Task> deviceCodePrompt)
{
_settings = settings;
_deviceCodeCredential = new DeviceCodeCredential(deviceCodePrompt,
settings.TenantId, settings.ClientId);
_userClient = new GraphServiceClient(_deviceCodeCredential, settings.GraphUserScopes);
}
public async Task<IEnumerable<User>> ListUsers(int? limit)
{
if(_users.Count>limit)
return _users.Take(limit??10);
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var result = await _userClient.Users.GetAsync((requestConfiguration) => {
requestConfiguration.QueryParameters.Top = limit>0?limit:10;
});
return result?.Value;
}
public void removeAttendee(EmailAddress attendeeEmail)
{
_attendees.RemoveWhere(x => x.EmailAddress.Address==attendeeEmail.Address);
}
public async Task<IEnumerable<User>> SearchUsers(string? keyword, int? limit)
{
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
var result = await _userClient.Users.GetAsync((requestConfiguration) => {
requestConfiguration.QueryParameters.Top = limit > 0 ? limit : 10;
requestConfiguration.QueryParameters.Search = $"\"mail:{keyword}\"";
requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
});
return result?.Value;
}
public List<AttendeeBase> getSelectedAttendees() {
return _attendees.ToList();
}
public async Task<User> GetUserByEmail(string email)
{
_ = _userClient ??
throw new System.NullReferenceException("Graph has not been initialized for user auth");
try {
var result = await _userClient.Users.GetAsync((requestConfiguration) => {
requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
requestConfiguration.QueryParameters.Filter = $"mail eq '{email.Trim()}'";
});
return result?.Value.SingleOrDefault();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
return null;
}
}
public void ShowAttendees() {
foreach (var att in _attendees) {
Console.WriteLine($"{att.EmailAddress.Address} {att.Type.ToString()}");
}
}
}
}