//----------------------------------------------------------------------- // // 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 EmailController(IDbContextFactory dbContextFactory, UserManager userManager) : AuthenticatedRequestController(userManager) { /// /// Get the newest version of the vault for the current user. /// /// The email ID to open. /// List of aliases in JSON format. [HttpGet(template: "{id}", Name = "GetEmail")] public async Task GetEmail(int id) { await using var context = await dbContextFactory.CreateDbContextAsync(); var user = await GetCurrentUserAsync(); if (user is null) { return Unauthorized("Not authenticated."); } // Retrieve email from database. var email = await context.Emails.Include(x => x.EncryptionKey).AsNoTracking().FirstOrDefaultAsync(x => x.Id == id); if (email is null) { return NotFound("Email not found."); } // 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 == email.To); if (emailClaim is null) { return Unauthorized("User does not have a claim to this email address."); } var returnEmail = new EmailApiModel { Id = email.Id, Subject = email.Subject, FromDisplay = ConversionHelper.ConvertFromToFromDisplay(email.From), FromDomain = email.FromDomain, FromLocal = email.FromLocal, ToDomain = email.ToDomain, ToLocal = email.ToLocal, Date = email.Date, DateSystem = DateTime.SpecifyKind(email.DateSystem, DateTimeKind.Utc), SecondsAgo = (int)DateTime.UtcNow.Subtract(email.DateSystem).TotalSeconds, MessageHtml = email.MessageHtml, MessagePlain = email.MessagePlain, EncryptedSymmetricKey = email.EncryptedSymmetricKey, EncryptionKey = email.EncryptionKey.PublicKey, }; // Add attachment metadata (without the filebytes) var attachments = await context.EmailAttachments.Where(x => x.EmailId == email.Id).Select(x => new AttachmentApiModel() { Id = x.Id, Email_Id = x.EmailId, Filename = x.Filename, MimeType = x.MimeType, Filesize = x.Filesize, }).ToListAsync(); returnEmail.Attachments = attachments; // Enrich HTML by changing all anchor tags to open in new tab if (returnEmail.MessageHtml != null && !string.IsNullOrEmpty(email.MessageHtml)) { returnEmail.MessageHtml = ConversionHelper.ConvertAnchorTagsToOpenInNewTab(email.MessageHtml); } return Ok(returnEmail); } }