//----------------------------------------------------------------------- // // Copyright (c) lanedirt. All rights reserved. // Licensed under the MIT license. See LICENSE.md file in the project root for full license information. // //----------------------------------------------------------------------- namespace AliasVault.Api.Controllers; using AliasServerDb; using AliasVault.Api.Helpers; using AliasVault.Shared.Models.Spamok; using Asp.Versioning; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; /// /// Email controller for retrieving emails from the database. /// /// DbContext instance. /// UserManager instance. [ApiVersion("1")] public class EmailBoxController(IDbContextFactory dbContextFactory, UserManager userManager) : AuthenticatedRequestController(userManager) { /// /// Get the newest version of the vault for the current user. /// /// The full email address including @ sign. /// List of aliases in JSON format. [HttpGet(template: "{to}", Name = "GetEmailBox")] public async Task GetEmailBox(string to) { await using var context = await dbContextFactory.CreateDbContextAsync(); var user = await GetCurrentUserAsync(); if (user is null) { return Unauthorized("Not authenticated."); } // See if this user has a valid claim to the email address. var emailClaim = await context.UserEmailClaims .FirstOrDefaultAsync(x => x.UserId == user.Id && x.Address == to); if (emailClaim is null) { return Unauthorized("User does not have a claim to this email address."); } // Retrieve emails from database. List emails = await context.Emails.AsNoTracking().Select(x => new MailboxEmailApiModel() { Id = x.Id, Subject = x.Subject, FromDisplay = ConversionHelper.ConvertFromToFromDisplay(x.From), FromDomain = x.FromDomain, FromLocal = x.FromLocal, ToDomain = x.ToDomain, ToLocal = x.ToLocal, Date = x.Date, DateSystem = x.DateSystem, SecondsAgo = (int)DateTime.UtcNow.Subtract(x.DateSystem).TotalSeconds, MessagePreview = x.MessagePreview ?? string.Empty, EncryptedSymmetricKey = x.EncryptedSymmetricKey, EncryptionKey = x.EncryptionKey.PublicKey, }).OrderByDescending(x => x.DateSystem).Take(75).ToListAsync(); MailboxApiModel returnValue = new MailboxApiModel(); returnValue.Address = to; returnValue.Subscribed = false; returnValue.Mails = emails; return Ok(returnValue); } }