const int TIMEOUT_MAIL_WAIT_m = 8;
const int ANTIDDOS_LAG_ms = 2900;
using var timeout_cts = new CancellationTokenSource();
using var timeout = Task.Delay(TimeSpan.FromMinutes(TIMEOUT_MAIL_WAIT_m), timeout_cts.Token);
string query = $"ReceivedDateTime gt {DateTime.UtcNow:yyyy-MM-ddTHH:mm:ssZ} and from/emailAddress/address eq '{emailAddress}'";
var messageRepository = graphClient.Users[config.UserMailAddress].MailFolders.Inbox.Messages;
while (timeout.IsCompleted is false)
{
var msg = await messageRepository.Request().Filter(query).GetAsync();
if (msg.Count is not 0)
{
Console.WriteLine("Message detected");
// TODO: write logic to the captured message right here
timeout_cts.Cancel();
break;
}
if (Task.WhenAny(Task.Delay(TimeSpan.FromMinutes(1)), timeout) == timeout) break;
Thread.Sleep(ANTIDDOS_LAG_ms);
}
if (timeout.IsCompleted && timeout.IsCanceled is false)
{
// TODO: better exception message, mail not received whitin time limit frame
throw new TimeoutException("damn");
}