Merge pull request #175 from Tiktack/improve-unsubscribe-with-oneclick

Added support for one click unsubscribe with confirmation dialog.
This commit is contained in:
Burak Kaan Köse
2024-05-02 00:24:11 +02:00
committed by GitHub
8 changed files with 166 additions and 22 deletions

View File

@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using Wino.Core.Domain.Models.Reader;
namespace Wino.Core.Domain.Interfaces
{
public interface IUnsubscriptionService
{
/// <summary>
/// Unsubscribes from the subscription using one-click method.
/// </summary>
/// <param name="info">Unsubscribtion information.</param>
/// <returns>Whether the unsubscription is succeeded or not.</returns>
Task<bool> OneClickUnsubscribeAsync(UnsubscribeInfo info);
}
}

View File

@@ -11,10 +11,9 @@ namespace Wino.Core.Domain.Models.Reader
{
public string RenderHtml { get; }
public MailRenderingOptions MailRenderingOptions { get; }
public List<MimePart> Attachments { get; set; } = new List<MimePart>();
public List<MimePart> Attachments { get; set; } = [];
public string UnsubscribeLink { get; set; }
public bool CanUnsubscribe => !string.IsNullOrEmpty(UnsubscribeLink);
public UnsubscribeInfo UnsubscribeInfo { get; set; }
public MailRenderModel(string renderHtml, MailRenderingOptions mailRenderingOptions = null)
{
@@ -22,4 +21,12 @@ namespace Wino.Core.Domain.Models.Reader
MailRenderingOptions = mailRenderingOptions;
}
}
public class UnsubscribeInfo
{
public string HttpLink { get; set; }
public string MailToLink { get; set; }
public bool IsOneClick { get; set; }
public bool CanUnsubscribe => HttpLink != null || MailToLink != null;
}
}

View File

@@ -76,6 +76,11 @@
"DialogMessage_UnlinkAccountsConfirmationTitle": "Unlink Accounts",
"DialogMessage_EmptySubjectConfirmation": "Missin Subject",
"DialogMessage_EmptySubjectConfirmationMessage": "Message has no subject. Do you want to continue?",
"DialogMessage_UnsubscribeConfirmationTitle": "Unsubscribe",
"DialogMessage_UnsubscribeConfirmationOneClickMessage": "Do you want to stop getting messages from {0}?",
"DialogMessage_UnsubscribeConfirmationGoToWebsiteMessage": "To stop getting messages from {0}, go to their website to unsubscribe.",
"DialogMessage_UnsubscribeConfirmationGoToWebsiteConfirmButton": "Go to website",
"DialogMessage_UnsubscribeConfirmationMailtoMessage": "Do you want to stop getting messages from {0}? Wino will unsubscribe for you by sending an email from your email account to {1}.",
"Dialog_DontAskAgain": "Don't ask again",
"DiscordChannelDisclaimerMessage": "Wino doesn't have it's own Discord server, but special 'wino-mail' channel is hosted at 'Developer Sanctuary' server.\nTo get the updates about Wino please join Developer Sanctuary server and follow 'wino-mail' channel under 'Community Projects'\n\nYou will be directed to server URL since Discord doesn't support channel invites.",
"DiscordChannelDisclaimerTitle": "Important Discord Information",
@@ -230,6 +235,8 @@
"Info_UnsupportedFunctionalityTitle": "Unsupported",
"Info_UnsubscribeLinkInvalidTitle": "Invalid Unsubscribe Uri",
"Info_UnsubscribeLinkInvalidMessage": "This unsubscribe link is invalid. Failed to unsubscribe from the list.",
"Info_UnsubscribeSuccessMessage": "Successfully unsubscribed from {0}.",
"Info_UnsubscribeErrorMessage": "Failed to unsubscribe",
"ImapAdvancedSetupDialog_AuthenticationMethod": "Authentication method",
"ImapAdvancedSetupDialog_ConnectionSecurity": "Connection security",
"ImapAuthenticationMethod_Auto": "Auto",

View File

@@ -403,6 +403,31 @@ namespace Wino.Core.Domain
/// </summary>
public static string DialogMessage_EmptySubjectConfirmationMessage => Resources.GetTranslatedString(@"DialogMessage_EmptySubjectConfirmationMessage");
/// <summary>
/// Unsubscribe
/// </summary>
public static string DialogMessage_UnsubscribeConfirmationTitle => Resources.GetTranslatedString(@"DialogMessage_UnsubscribeConfirmationTitle");
/// <summary>
/// Do you want to stop getting messages from {0}?
/// </summary>
public static string DialogMessage_UnsubscribeConfirmationOneClickMessage => Resources.GetTranslatedString(@"DialogMessage_UnsubscribeConfirmationOneClickMessage");
/// <summary>
/// To stop getting messages from {0}, go to their website to unsubscribe.
/// </summary>
public static string DialogMessage_UnsubscribeConfirmationGoToWebsiteMessage => Resources.GetTranslatedString(@"DialogMessage_UnsubscribeConfirmationGoToWebsiteMessage");
/// <summary>
/// Go to website
/// </summary>
public static string DialogMessage_UnsubscribeConfirmationGoToWebsiteConfirmButton => Resources.GetTranslatedString(@"DialogMessage_UnsubscribeConfirmationGoToWebsiteConfirmButton");
/// <summary>
/// Do you want to stop getting messages from {0}? WinoMail will unsubscribe for you by sending an email from your email account to {1}.
/// </summary>
public static string DialogMessage_UnsubscribeConfirmationMailtoMessage => Resources.GetTranslatedString(@"DialogMessage_UnsubscribeConfirmationMailtoMessage");
/// <summary>
/// Don't ask again
/// </summary>
@@ -1173,6 +1198,16 @@ namespace Wino.Core.Domain
/// </summary>
public static string Info_UnsubscribeLinkInvalidMessage => Resources.GetTranslatedString(@"Info_UnsubscribeLinkInvalidMessage");
/// <summary>
/// Successfully unsubscribed from {0}.
/// </summary>
public static string Info_UnsubscribeSuccessMessage => Resources.GetTranslatedString(@"Info_UnsubscribeSuccessMessage");
/// <summary>
/// Failed to unsubscribe
/// </summary>
public static string Info_UnsubscribeErrorMessage => Resources.GetTranslatedString(@"Info_UnsubscribeErrorMessage");
/// <summary>
/// Authentication method
/// </summary>

View File

@@ -37,6 +37,7 @@ namespace Wino.Core
services.AddTransient<IAutoDiscoveryService, AutoDiscoveryService>();
services.AddTransient<IContextMenuItemService, ContextMenuItemService>();
services.AddTransient<IFontService, FontService>();
services.AddTransient<IUnsubscriptionService, UnsubscriptionService>();
services.AddTransient<OutlookThreadingStrategy>();
services.AddTransient<GmailThreadingStrategy>();

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MimeKit;
@@ -221,22 +222,24 @@ namespace Wino.Core.Services
}
}
// Check for List-Unsubscribe link if possible.
if (message.Headers.Contains(HeaderId.ListUnsubscribe))
{
renderingModel.UnsubscribeLink = message.Headers[HeaderId.ListUnsubscribe].Normalize();
var unsubscribeLinks = message.Headers[HeaderId.ListUnsubscribe]
.Normalize()
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim([' ', '<', '>']));
// Sometimes this link is wrapped with < >, remove them.
if (renderingModel.UnsubscribeLink.StartsWith("<"))
// Only two types of unsubscribe links are possible.
// So each has it's own property to simplify the usage.
renderingModel.UnsubscribeInfo = new UnsubscribeInfo()
{
renderingModel.UnsubscribeLink = renderingModel.UnsubscribeLink.Substring(1, renderingModel.UnsubscribeLink.Length - 2);
}
HttpLink = unsubscribeLinks.FirstOrDefault(x => x.StartsWith("http", StringComparison.OrdinalIgnoreCase)),
MailToLink = unsubscribeLinks.FirstOrDefault(x => x.StartsWith("mailto", StringComparison.OrdinalIgnoreCase)),
IsOneClick = message.Headers.Contains(HeaderId.ListUnsubscribePost)
};
}
return renderingModel;
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Serilog;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Reader;
namespace Wino.Core.Services
{
public class UnsubscriptionService : IUnsubscriptionService
{
public async Task<bool> OneClickUnsubscribeAsync(UnsubscribeInfo info)
{
try
{
using var httpClient = new HttpClient();
var unsubscribeRequest = new HttpRequestMessage(HttpMethod.Post, info.HttpLink)
{
Content = new StringContent("List-Unsubscribe=One-Click", Encoding.UTF8, "application/x-www-form-urlencoded")
};
var result = await httpClient.SendAsync(unsubscribeRequest).ConfigureAwait(false);
return result.IsSuccessStatusCode;
}
catch (Exception ex)
{
Log.Error("Failed to unsubscribe from {HttpLink} - {Message}", info.HttpLink, ex.Message);
}
return false;
}
}
}

View File

@@ -38,6 +38,7 @@ namespace Wino.Mail.ViewModels
private readonly IWinoSynchronizerFactory _winoSynchronizerFactory;
private readonly IWinoRequestDelegator _requestDelegator;
private readonly IClipboardService _clipboardService;
private readonly IUnsubscriptionService _unsubscriptionService;
private bool forceImageLoading = false;
@@ -47,7 +48,7 @@ namespace Wino.Mail.ViewModels
#region Properties
public bool ShouldDisplayDownloadProgress => IsIndetermineProgress || (CurrentDownloadPercentage > 0 && CurrentDownloadPercentage <= 100);
public bool CanUnsubscribe => CurrentRenderModel?.CanUnsubscribe ?? false;
public bool CanUnsubscribe => CurrentRenderModel?.UnsubscribeInfo?.CanUnsubscribe ?? false;
public bool IsJunkMail => initializedMailItemViewModel?.AssignedFolder != null && initializedMailItemViewModel.AssignedFolder.SpecialFolderType == SpecialFolderType.Junk;
public bool IsImageRenderingDisabled
@@ -125,6 +126,7 @@ namespace Wino.Mail.ViewModels
IWinoRequestDelegator requestDelegator,
IStatePersistanceService statePersistanceService,
IClipboardService clipboardService,
IUnsubscriptionService unsubscriptionService,
IPreferencesService preferencesService) : base(dialogService)
{
NativeAppService = nativeAppService;
@@ -132,6 +134,7 @@ namespace Wino.Mail.ViewModels
PreferencesService = preferencesService;
_clipboardService = clipboardService;
_unsubscriptionService = unsubscriptionService;
_underlyingThemeService = underlyingThemeService;
_mimeFileService = mimeFileService;
_mailService = mailService;
@@ -172,18 +175,55 @@ namespace Wino.Mail.ViewModels
[RelayCommand]
private async Task UnsubscribeAsync()
{
if (!CurrentRenderModel?.CanUnsubscribe ?? false) return;
if (!(CurrentRenderModel?.UnsubscribeInfo?.CanUnsubscribe ?? false)) return;
// TODO: Support for List-Unsubscribe-Post header. It can be done without launching browser.
// https://certified-senders.org/wp-content/uploads/2017/07/CSA_one-click_list-unsubscribe.pdf
bool confirmed;
// TODO: Sometimes unsubscribe link can be a mailto: link.
// or sometimes with mailto AND http link. We need to handle this.
// Try to unsubscribe by http first.
if (CurrentRenderModel.UnsubscribeInfo.HttpLink is not null)
{
if (!Uri.IsWellFormedUriString(CurrentRenderModel.UnsubscribeInfo.HttpLink, UriKind.RelativeOrAbsolute))
{
DialogService.InfoBarMessage(Translator.Info_UnsubscribeLinkInvalidTitle, Translator.Info_UnsubscribeLinkInvalidMessage, InfoBarMessageType.Error);
return;
}
if (Uri.IsWellFormedUriString(CurrentRenderModel.UnsubscribeLink, UriKind.RelativeOrAbsolute))
await NativeAppService.LaunchUriAsync(new Uri((CurrentRenderModel.UnsubscribeLink)));
else
DialogService.InfoBarMessage(Translator.Info_UnsubscribeLinkInvalidTitle, Translator.Info_UnsubscribeLinkInvalidMessage, InfoBarMessageType.Error);
// Support for List-Unsubscribe-Post header. It can be done without launching browser.
// https://datatracker.ietf.org/doc/html/rfc8058
if (CurrentRenderModel.UnsubscribeInfo.IsOneClick)
{
confirmed = await DialogService.ShowConfirmationDialogAsync(string.Format(Translator.DialogMessage_UnsubscribeConfirmationOneClickMessage, FromName), Translator.DialogMessage_UnsubscribeConfirmationTitle, Translator.Unsubscribe);
if (!confirmed) return;
bool isOneClickUnsubscribed = await _unsubscriptionService.OneClickUnsubscribeAsync(CurrentRenderModel.UnsubscribeInfo);
if (isOneClickUnsubscribed)
{
DialogService.InfoBarMessage(Translator.Unsubscribe, string.Format(Translator.Info_UnsubscribeSuccessMessage, FromName), InfoBarMessageType.Success);
}
else
{
DialogService.InfoBarMessage(Translator.GeneralTitle_Error, Translator.Info_UnsubscribeErrorMessage, InfoBarMessageType.Error);
}
}
else
{
confirmed = await DialogService.ShowConfirmationDialogAsync(string.Format(Translator.DialogMessage_UnsubscribeConfirmationGoToWebsiteMessage, FromName), Translator.DialogMessage_UnsubscribeConfirmationTitle, Translator.DialogMessage_UnsubscribeConfirmationGoToWebsiteConfirmButton);
if (!confirmed) return;
await NativeAppService.LaunchUriAsync(new Uri(CurrentRenderModel.UnsubscribeInfo.HttpLink));
}
}
else if (CurrentRenderModel.UnsubscribeInfo.MailToLink is not null)
{
confirmed = await DialogService.ShowConfirmationDialogAsync(string.Format(Translator.DialogMessage_UnsubscribeConfirmationMailtoMessage, FromName, new string(CurrentRenderModel.UnsubscribeInfo.MailToLink.Skip(7).ToArray())), Translator.DialogMessage_UnsubscribeConfirmationTitle, Translator.Unsubscribe);
if (!confirmed) return;
// TODO: Implement automatic mail send after user confirms the action.
// Currently it will launch compose page and user should manually press send button.
await NativeAppService.LaunchUriAsync(new Uri(CurrentRenderModel.UnsubscribeInfo.MailToLink));
}
}
[RelayCommand]