mirror of
https://github.com/cyanfish/naps2.git
synced 2024-08-17 03:00:21 +03:00
Oauth: Implement autosend for Outlook Web
This commit is contained in:
parent
42cb1f5340
commit
f7054b8230
@ -169,6 +169,12 @@ public abstract class OauthProvider
|
|||||||
return JObject.Parse(response);
|
return JObject.Parse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async Task PostAuthorizedNoResponse(string url)
|
||||||
|
{
|
||||||
|
using var client = AuthorizedClient();
|
||||||
|
await client.UploadStringTaskAsync(url, "POST", "");
|
||||||
|
}
|
||||||
|
|
||||||
private WebClient AuthorizedClient()
|
private WebClient AuthorizedClient()
|
||||||
{
|
{
|
||||||
var client = new WebClient();
|
var client = new WebClient();
|
||||||
|
@ -31,10 +31,18 @@ internal class OutlookWebEmailProvider : IEmailProvider
|
|||||||
["contentBytes"] = Convert.ToBase64String(File.ReadAllBytes(attachment.FilePath))
|
["contentBytes"] = Convert.ToBase64String(File.ReadAllBytes(attachment.FilePath))
|
||||||
}))
|
}))
|
||||||
};
|
};
|
||||||
var respUrl = await _outlookWebOauthProvider.UploadDraft(messageObj.ToString(), progress);
|
var draft = await _outlookWebOauthProvider.UploadDraft(messageObj.ToString(), progress);
|
||||||
|
|
||||||
// Open the draft in the user's browser
|
|
||||||
ProcessHelper.OpenUrl(respUrl + "&ispopout=0");
|
if (emailMessage.AutoSend)
|
||||||
|
{
|
||||||
|
await _outlookWebOauthProvider.SendDraft(draft.MessageId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Open the draft in the user's browser
|
||||||
|
ProcessHelper.OpenUrl(draft.WebLink + "&ispopout=0");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -67,11 +67,20 @@ public class OutlookWebOauthProvider : OauthProvider
|
|||||||
return resp.Value<string>("mail") ?? throw new InvalidOperationException("Could not get Id from Outlook profile response");
|
return resp.Value<string>("mail") ?? throw new InvalidOperationException("Could not get Id from Outlook profile response");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> UploadDraft(string messageRaw, ProgressHandler progress = default)
|
public async Task<DraftInfo> UploadDraft(string messageRaw, ProgressHandler progress = default)
|
||||||
{
|
{
|
||||||
var resp = await PostAuthorized("https://graph.microsoft.com/v1.0/me/messages", messageRaw, "application/json", progress);
|
var resp = await PostAuthorized("https://graph.microsoft.com/v1.0/me/messages", messageRaw, "application/json", progress);
|
||||||
return resp.Value<string>("webLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response");
|
var webLink = resp.Value<string>("webLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response");
|
||||||
|
var messageId = resp.Value<string>("id") ?? throw new InvalidOperationException("Could not get ID from Outlook messages response");
|
||||||
|
return new DraftInfo(webLink, messageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task SendDraft(string draftMessageId)
|
||||||
|
{
|
||||||
|
await PostAuthorizedNoResponse($"https://graph.microsoft.com/v1.0/me/messages/{draftMessageId}/send");
|
||||||
|
}
|
||||||
|
|
||||||
|
public record DraftInfo(string WebLink, string MessageId);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user