Files
Wino-Mail/Wino.Services/ContactService.cs

74 lines
2.5 KiB
C#
Raw Normal View History

2025-02-28 18:21:31 +01:00
using System;
using System.Collections.Generic;
2024-04-18 01:44:37 +02:00
using System.Linq;
using System.Threading.Tasks;
using MimeKit;
2025-02-28 18:21:31 +01:00
using Serilog;
2024-04-18 01:44:37 +02:00
using SqlKata;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Interfaces;
using Wino.Services.Extensions;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Services;
public class ContactService : BaseDatabaseService, IContactService
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
public ContactService(IDatabaseService databaseService) : base(databaseService) { }
public async Task<AccountContact> CreateNewContactAsync(string address, string displayName)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var contact = new AccountContact() { Address = address, Name = displayName };
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await Connection.InsertAsync(contact).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
return contact;
}
2025-02-16 11:54:23 +01:00
public Task<List<AccountContact>> GetAddressInformationAsync(string queryText)
{
if (queryText == null || queryText.Length < 2)
return Task.FromResult<List<AccountContact>>(null);
2025-02-16 11:54:23 +01:00
var query = new Query(nameof(AccountContact));
query.WhereContains("Address", queryText);
query.OrWhereContains("Name", queryText);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var rawLikeQuery = query.GetRawQuery();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return Connection.QueryAsync<AccountContact>(rawLikeQuery);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public Task<AccountContact> GetAddressInformationByAddressAsync(string address)
=> Connection.Table<AccountContact>().FirstOrDefaultAsync(a => a.Address == address);
2025-02-16 11:54:23 +01:00
public async Task SaveAddressInformationAsync(MimeMessage message)
{
var recipients = message
.GetRecipients(true)
.Where(a => !string.IsNullOrEmpty(a.Name) && !string.IsNullOrEmpty(a.Address));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var addressInformations = recipients.Select(a => new AccountContact() { Name = a.Name, Address = a.Address });
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var info in addressInformations)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var currentContact = await GetAddressInformationByAddressAsync(info.Address).ConfigureAwait(false);
2025-02-28 18:21:31 +01:00
try
2025-02-16 11:35:43 +01:00
{
2025-02-28 18:21:31 +01:00
if (currentContact == null)
{
await Connection.InsertAsync(info).ConfigureAwait(false);
}
else if (!currentContact.IsRootContact) // Don't update root contacts. They belong to accounts.
{
await Connection.InsertOrReplaceAsync(info).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
}
2025-02-28 18:21:31 +01:00
catch (Exception ex)
2025-02-16 11:54:23 +01:00
{
2025-02-28 18:21:31 +01:00
Log.Error("Failed to add contact information to the database.", ex);
2024-08-23 02:07:50 +02:00
}
2024-04-18 01:44:37 +02:00
}
}
}