Files
hrynco-notification-service/HrynCo.NotificationService.Worker.Services/Push/PushMessageValidator.cs
T
agrynco aba4173827 feat(templates): add localized push templates and actions
Share template keys across email and push with independent editors and revisions. Render localized actions and preserve version-one delivery.

Ref: IT-1627
2026-10-31 09:38:05 +02:00

35 lines
1.7 KiB
C#

namespace HrynCo.NotificationService.Worker.Services.Push;
using HrynCo.NotificationService.Contracts.Messages;
public static class PushMessageValidator
{
public static bool IsValid(SendPushMessage? message)
{
if (message?.Data is not { } data || message.CorrelationContext is null) return false;
return Guid.TryParse(message.CorrelationContext.CorrelationId, out _) &&
data.RegistrationId != Guid.Empty && data.BindingId != Guid.Empty &&
data.ServiceName is { Length: > 0 and <= 100 } && data.UserId != Guid.Empty &&
data.Token is { Length: > 0 and <= 2048 } && data.TokenHash is { Length: 64 } &&
IsValidContent(data) && data.ExpiresAt <= DateTimeOffset.UtcNow.AddMinutes(15);
}
public static bool IsSupportedVersion(int version) => version is 1 or 2;
public static bool IsValidContent(SendPushMessageData data) => data.Version switch
{
1 => data.Title is { Length: > 0 and <= 100 } && data.Body is { Length: > 0 and <= 500 } && data.Route == "/home",
2 => ValidTemplateData(data),
_ => false
};
private static bool ValidTemplateData(SendPushMessageData data) =>
data.TemplateKey is { Length: > 0 and <= 100 } &&
data.LanguageCode is { Length: > 0 and <= 10 } &&
data.TemplateData?.Bindings is not null && data.TemplateData.Actions is not null &&
data.TemplateData.Bindings.All(pair => !string.IsNullOrWhiteSpace(pair.Key) && pair.Value is not null) &&
data.TemplateData.Actions.All(pair => !string.IsNullOrWhiteSpace(pair.Key) && pair.Value is not null &&
(pair.Value.ObjectId is null || pair.Value.ObjectId is { Length: > 0 and <= 256 }));
}