| | | 1 | | using ArturRios.Heimdall.Command.Services; |
| | | 2 | | using ArturRios.Heimdall.Shared.Security; |
| | | 3 | | using ArturRios.Messaging.Email; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.WebApi.Email; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Sends GDPR Art. 18(3)'s notification through Mailgun, and reports whether it was accepted. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Deliberately does not extend <see cref="MailgunSender" />. That base class swallows delivery |
| | | 12 | | /// failures on purpose, which is right for every other email this API sends and wrong for this |
| | | 13 | | /// one — see <see cref="IRestrictionLiftNotifier" />. |
| | | 14 | | /// </remarks> |
| | 0 | 15 | | public class MailgunRestrictionLiftNotifier( |
| | 0 | 16 | | IEmailService emailService, |
| | 0 | 17 | | ILogger<MailgunRestrictionLiftNotifier> logger) : IRestrictionLiftNotifier |
| | | 18 | | { |
| | | 19 | | private const string Subject = "The restriction on your account is being lifted"; |
| | | 20 | | |
| | | 21 | | public async Task<bool> NotifyAsync(string email) |
| | | 22 | | { |
| | | 23 | | const string body = """ |
| | | 24 | | You asked us to restrict processing of your data, and that restriction is |
| | | 25 | | being lifted. |
| | | 26 | | |
| | | 27 | | Your account will work normally again. If you did not expect this, or you |
| | | 28 | | believe the matter that led to the restriction is unresolved, reply to |
| | | 29 | | this message. |
| | | 30 | | """; |
| | | 31 | | |
| | | 32 | | try |
| | | 33 | | { |
| | 0 | 34 | | var result = await emailService.SendEmailAsync(email, Subject, body); |
| | | 35 | | |
| | 0 | 36 | | if (!result.Success) |
| | | 37 | | { |
| | 0 | 38 | | logger.LogError( |
| | 0 | 39 | | "Mailgun refused the restriction lift notification for {EmailRef}: {Errors}", |
| | 0 | 40 | | LogSafeEmail.Reference(email), string.Join(" | ", result.Errors)); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return result.Success; |
| | | 44 | | } |
| | 0 | 45 | | catch (Exception exception) |
| | | 46 | | { |
| | 0 | 47 | | logger.LogError( |
| | 0 | 48 | | exception, "Could not deliver the restriction lift notification to {EmailRef}", |
| | 0 | 49 | | LogSafeEmail.Reference(email)); |
| | | 50 | | |
| | 0 | 51 | | return false; |
| | | 52 | | } |
| | 0 | 53 | | } |
| | | 54 | | } |