Files
git.stella-ops.org/src/Notify/__Libraries/StellaOps.Notify.Connectors.Email/EmailChannelTestProvider.cs
2026-02-01 21:37:40 +02:00

46 lines
1.7 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using StellaOps.DependencyInjection;
using StellaOps.Notify.Engine;
using StellaOps.Notify.Models;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace StellaOps.Notify.Connectors.Email;
[ServiceBinding(typeof(INotifyChannelTestProvider), ServiceLifetime.Singleton)]
public sealed class EmailChannelTestProvider : INotifyChannelTestProvider
{
public NotifyChannelType ChannelType => NotifyChannelType.Email;
public Task<ChannelTestPreviewResult> BuildPreviewAsync(ChannelTestPreviewContext context, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var subject = context.Request.Title ?? "Stella Ops Notify Preview";
var summary = context.Request.Summary ?? $"Preview generated at {context.Timestamp:O}.";
var htmlBody = !string.IsNullOrWhiteSpace(context.Request.Body)
? context.Request.Body!
: $"<p>{summary}</p><p><small>Trace: {context.TraceId}</small></p>";
var textBody = context.Request.TextBody ?? $"{summary}{Environment.NewLine}Trace: {context.TraceId}";
var preview = NotifyDeliveryRendered.Create(
NotifyChannelType.Email,
NotifyDeliveryFormat.Email,
context.Target,
subject,
htmlBody,
summary,
textBody,
context.Request.Locale,
ChannelTestPreviewUtilities.ComputeBodyHash(htmlBody),
context.Request.Attachments);
var metadata = EmailMetadataBuilder.Build(context);
return Task.FromResult(new ChannelTestPreviewResult(preview, metadata));
}
}