-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOutlookService.cs
102 lines (82 loc) · 3.68 KB
/
OutlookService.cs
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
using Meziantou.Framework.Win32;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using ExchangeAppointment = Microsoft.Exchange.WebServices.Data.Appointment;
namespace BetterOutlookReminder
{
internal class OutlookService
{
private string[] scopes = new[] { "Calendars.Read" };
private AppointmentGroup nextAppointments;
public WebCredentials credentials;
private bool lastUpdateSucceeded = true;
IPublicClientApplication app;
public OutlookService()
{
app = PublicClientApplicationBuilder
.Create("bff2bbd0-39a1-4263-9e06-f6bb37ce8679")
.WithDefaultRedirectUri()
.Build();
TokenCacheHelper.EnableSerialization(app.UserTokenCache);
}
public async Task<AppointmentGroup> GetNextAppointments()
{
try
{
InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(app, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var queryOptions = new List<QueryOption>()
{
new QueryOption("startDateTime", DateTime.UtcNow.AddMinutes(-15).ToString("o", CultureInfo.InvariantCulture)),
new QueryOption("endDateTime", DateTime.Today.ToUniversalTime().AddDays(1).ToString("o", CultureInfo.InvariantCulture))
};
var events = await graphClient.Me.Calendar.CalendarView
.Request(queryOptions)
.Top(20)
.GetAsync();
var newAppointments = new AppointmentGroup();
IEnumerable<Appointment> appointments = events.Select(MakeAppointment);
newAppointments.Next =
appointments.Where(o => o != null && o.End >= DateTime.Now && o.Start >= DateTime.Now.AddMinutes(-29))
.OrderBy(o => o.Start).ToList();
nextAppointments = newAppointments;
lastUpdateSucceeded = true;
return newAppointments;
}
catch (Exception e)
{
lastUpdateSucceeded = false;
Trace.WriteLine(e.ToString());
return nextAppointments;
}
}
private Appointment MakeAppointment(Event appointmentItem)
{
Appointment newAppointment = appointmentItem == null
? null
: new Appointment(
appointmentItem.Id,
ConvertDateTime(appointmentItem.Start),
ConvertDateTime(appointmentItem.End),
appointmentItem.Subject,
appointmentItem.Location.DisplayName,
appointmentItem.Organizer.EmailAddress.Name,
appointmentItem.Attendees.Select(a => a.EmailAddress.Name),
appointmentItem.Body.Content);
return newAppointment;
}
private DateTime ConvertDateTime(DateTimeTimeZone dttz)
{
return TimeZoneInfo.ConvertTime(DateTime.Parse(dttz.DateTime), TimeZoneInfo.FindSystemTimeZoneById(dttz.TimeZone), TimeZoneInfo.Local);
}
}
}