Updating aliases during profile sync for Gmail.

This commit is contained in:
Burak Kaan Köse
2024-08-16 01:29:31 +02:00
parent 1791df236c
commit cf9f308b7f
7 changed files with 121 additions and 44 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using SQLite;
namespace Wino.Core.Domain.Entities
@@ -38,5 +39,30 @@ namespace Wino.Core.Domain.Entities
/// Non-verified alias messages might be rejected by SMTP server.
/// </summary>
public bool IsVerified { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
var other = (MailAccountAlias)obj;
return other != null &&
AccountId == other.AccountId &&
AliasAddress == other.AliasAddress &&
ReplyToAddress == other.ReplyToAddress &&
IsPrimary == other.IsPrimary &&
IsVerified == other.IsVerified;
}
public override int GetHashCode()
{
int hashCode = -753829106;
hashCode = hashCode * -1521134295 + AccountId.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(AliasAddress);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ReplyToAddress);
hashCode = hashCode * -1521134295 + IsPrimary.GetHashCode();
hashCode = hashCode * -1521134295 + IsVerified.GetHashCode();
return hashCode;
}
}
}

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using Wino.Core.Domain.Entities;
namespace Wino.Core.Domain.Extensions
{
public static class EntityExtensions
{
public static List<MailAccountAlias> GetFinalAliasList(List<MailAccountAlias> localAliases, List<MailAccountAlias> networkAliases)
{
var finalAliases = new List<MailAccountAlias>();
var networkAliasDict = networkAliases.ToDictionary(a => a, a => a);
// Handle updating and retaining existing aliases
foreach (var localAlias in localAliases)
{
if (networkAliasDict.TryGetValue(localAlias, out var networkAlias))
{
// If alias exists in both lists, update it with the network alias (preserving Id from local)
networkAlias.Id = localAlias.Id; // Preserve the local Id
finalAliases.Add(networkAlias);
networkAliasDict.Remove(localAlias); // Remove from dictionary to track what's been handled
}
// If the alias isn't in the network list, it's considered deleted and not added to finalAliases
}
// Add new aliases that were not in the local list
finalAliases.AddRange(networkAliasDict.Values);
return finalAliases;
}
}
}

View File

@@ -100,5 +100,13 @@ namespace Wino.Core.Domain.Interfaces
/// </summary>
/// <param name="accountIdOrderPair">AccountId-OrderNumber pair for all accounts.</param>
Task UpdateAccountOrdersAsync(Dictionary<Guid, int> accountIdOrderPair);
/// <summary>
/// Updated account's aliases.
/// </summary>
/// <param name="accountId">Account id to update aliases for.</param>
/// <param name="aliases">Full list of updated aliases.</param>
/// <returns></returns>
Task UpdateAccountAliases(Guid accountId, List<MailAccountAlias> aliases);
}
}

View File

@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Google.Apis.Gmail.v1.Data;
using MimeKit;
using Wino.Core.Domain.Entities;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Extensions;
namespace Wino.Core.Extensions
{
@@ -205,44 +205,21 @@ namespace Wino.Core.Extensions
};
}
public static Tuple<MailCopy, MimeMessage, IEnumerable<string>> GetMailDetails(this Message message)
public static List<MailAccountAlias> GetMailAliases(this ListSendAsResponse response, MailAccount currentAccount)
{
MimeMessage mimeMessage = message.GetGmailMimeMessage();
if (response == null || response.SendAs == null) return currentAccount.Aliases;
if (mimeMessage == null)
var remoteAliases = response.SendAs.Select(a => new MailAccountAlias()
{
// This should never happen.
Debugger.Break();
AccountId = currentAccount.Id,
AliasAddress = a.SendAsEmail,
IsPrimary = a.IsPrimary.GetValueOrDefault(),
ReplyToAddress = string.IsNullOrEmpty(a.ReplyToAddress) ? currentAccount.Address : a.ReplyToAddress,
IsVerified = string.IsNullOrEmpty(a.VerificationStatus) ? true : a.VerificationStatus == "accepted",
Id = Guid.NewGuid()
}).ToList();
return default;
}
bool isUnread = message.GetIsUnread();
bool isFocused = message.GetIsFocused();
bool isFlagged = message.GetIsFlagged();
bool isDraft = message.GetIsDraft();
var mailCopy = new MailCopy()
{
CreationDate = mimeMessage.Date.UtcDateTime,
Subject = HttpUtility.HtmlDecode(mimeMessage.Subject),
FromName = MailkitClientExtensions.GetActualSenderName(mimeMessage),
FromAddress = MailkitClientExtensions.GetActualSenderAddress(mimeMessage),
PreviewText = HttpUtility.HtmlDecode(message.Snippet),
ThreadId = message.ThreadId,
Importance = (MailImportance)mimeMessage.Importance,
Id = message.Id,
IsDraft = isDraft,
HasAttachments = mimeMessage.Attachments.Any(),
IsRead = !isUnread,
IsFlagged = isFlagged,
IsFocused = isFocused,
InReplyTo = mimeMessage.InReplyTo,
MessageId = mimeMessage.MessageId,
References = mimeMessage.References.GetReferences()
};
return new Tuple<MailCopy, MimeMessage, IEnumerable<string>>(mailCopy, mimeMessage, message.LabelIds);
return EntityExtensions.GetFinalAliasList(currentAccount.Aliases, remoteAliases);
}
}
}

View File

@@ -46,6 +46,7 @@ namespace Wino.Core.Integration.Processors
Task UpdateFolderLastSyncDateAsync(Guid folderId);
Task<List<MailItemFolder>> GetExistingFoldersAsync(Guid accountId);
Task UpdateAccountAliasesAsync(Guid accountId, List<MailAccountAlias> aliases);
}
public interface IGmailChangeProcessor : IDefaultChangeProcessor
@@ -176,5 +177,8 @@ namespace Wino.Core.Integration.Processors
public Task UpdateAccountAsync(MailAccount account)
=> AccountService.UpdateAccountAsync(account);
public Task UpdateAccountAliasesAsync(Guid accountId, List<MailAccountAlias> aliases)
=> AccountService.UpdateAccountAliases(accountId, aliases);
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Diagnostics;
@@ -241,7 +240,11 @@ namespace Wino.Core.Services
// By default all accounts must have at least 1 primary alias to create drafts for.
// If there's no alias, create one from the existing account address. Migration doesn't exists to create one for older messages.
var aliases = await Connection.Table<MailAccountAlias>().ToListAsync().ConfigureAwait(false);
var aliases = await Connection
.Table<MailAccountAlias>()
.Where(a => a.AccountId == accountId)
.ToListAsync()
.ConfigureAwait(false);
if (!aliases.Any())
{
@@ -350,17 +353,24 @@ namespace Wino.Core.Services
public async Task UpdateAccountAsync(MailAccount account)
{
if (account.Preferences == null)
{
Debugger.Break();
}
await Connection.UpdateAsync(account.Preferences);
await Connection.UpdateAsync(account);
await Connection.UpdateAsync(account.Preferences).ConfigureAwait(false);
await Connection.UpdateAsync(account).ConfigureAwait(false);
ReportUIChange(new AccountUpdatedMessage(account));
}
public async Task UpdateAccountAliases(Guid accountId, List<MailAccountAlias> aliases)
{
// Delete existing ones.
await Connection.Table<MailAccountAlias>().DeleteAsync(a => a.AccountId == accountId).ConfigureAwait(false);
// Insert new ones.
foreach (var alias in aliases)
{
await Connection.InsertAsync(alias).ConfigureAwait(false);
}
}
public async Task CreateAccountAsync(MailAccount account, TokenInformation tokenInformation, CustomServerInformation customServerInformation)
{
Guard.IsNotNull(account);

View File

@@ -103,6 +103,24 @@ namespace Wino.Core.Synchronizers
Account.ProfilePictureBase64 = base64ProfilePicture;
}
// Sync aliases
var sendAsListRequest = _gmailService.Users.Settings.SendAs.List("me");
var sendAsListResponse = await sendAsListRequest.ExecuteAsync();
var updatedAliases = sendAsListResponse.GetMailAliases(Account);
bool shouldUpdateAliases =
Account.Aliases.Any(a => updatedAliases.Any(b => a.Id == b.Id) == false) ||
updatedAliases.Any(a => Account.Aliases.Any(b => a.Id == b.Id) == false);
if (shouldUpdateAliases)
{
Account.Aliases = updatedAliases;
await _gmailChangeProcessor.UpdateAccountAliasesAsync(Account.Id, updatedAliases);
}
if (shouldUpdateAccountProfile)
{
await _gmailChangeProcessor.UpdateAccountAsync(Account).ConfigureAwait(false);