Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): Change FCE MediaTypes #1729

Merged
merged 11 commits into from
Feb 3, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public ContentValueDtoValidator(DialogTransmissionContentType contentType)
{
RuleFor(x => x.MediaType)
.NotEmpty()
.Must(value => value is not null && contentType.AllowedMediaTypes.Contains(value))
.Must(value => value is not null && contentType.AllowedMediaTypes
// Manually adding this for backwards compatibility
// until correspondence is updated and deployed
// TODO: https://github.com/Altinn/dialogporten/issues/1782
.Append(MediaTypes.EmbeddableMarkdownDeprecated).Contains(value))
.WithMessage($"{{PropertyName}} '{{PropertyValue}}' is not allowed for content type {contentType.Name}. " +
$"Allowed media types are {string.Join(", ", contentType.AllowedMediaTypes.Select(x => $"'{x}'"))}");

Expand Down Expand Up @@ -73,7 +77,14 @@ private static string[] GetAllowedMediaTypes(DialogContentType contentType, IUse
DialogContentType.Values.AdditionalInfo when UserHasLegacyHtmlScope(user)
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyHtml).ToArray(),
DialogContentType.Values.MainContentReference when UserHasLegacyHtmlScope(user)
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyEmbeddableHtml).ToArray(),
=> contentType.AllowedMediaTypes.Append(MediaTypes.LegacyEmbeddableHtml)
// Manually adding this for backwards compatibility
// until correspondence is updated and deployed
// TODO: https://github.com/Altinn/dialogporten/issues/1782
.Append(MediaTypes.EmbeddableMarkdownDeprecated)
.Append(MediaTypes.LegacyEmbeddableHtmlDeprecated).ToArray(),
DialogContentType.Values.MainContentReference
=> contentType.AllowedMediaTypes.Append(MediaTypes.EmbeddableMarkdownDeprecated).ToArray(),
_ => contentType.AllowedMediaTypes
};
private static bool UserHasLegacyHtmlScope(IUser? user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal sealed class DialogContentInputConverter<TDialogContent> :
{
TypeId = dialogContentType.Id,
Value = sourceValue.Value,
MediaType = sourceValue.MediaType
MediaType = sourceValue.MediaType.MapDeprecatedMediaType()
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal sealed class TransmissionContentInputConverter<TTransmissionContent> :
{
TypeId = transmissionContentType.Id,
Value = sourceValue.Value,
MediaType = sourceValue.MediaType
MediaType = sourceValue.MediaType.MapDeprecatedMediaType()
oskogstad marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
23 changes: 21 additions & 2 deletions src/Digdir.Domain.Dialogporten.Domain/MediaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@ namespace Digdir.Domain.Dialogporten.Domain;
public static class MediaTypes
{
public const string EmbeddablePrefix = "application/vnd.dialogporten.frontchannelembed";
public const string EmbeddableMarkdown = $"{EmbeddablePrefix}+json;type=markdown";
public const string LegacyEmbeddableHtml = $"{EmbeddablePrefix}+json;type=html";

public const string EmbeddableMarkdown = $"{EmbeddablePrefix}-url;type=text/markdown";
public const string EmbeddableMarkdownDeprecated = $"{EmbeddablePrefix}+json;type=markdown";

public const string LegacyEmbeddableHtml = $"{EmbeddablePrefix}-url;type=text/html";
public const string LegacyEmbeddableHtmlDeprecated = $"{EmbeddablePrefix}+json;type=html";

public const string LegacyHtml = "text/html";
public const string Markdown = "text/markdown";
public const string PlainText = "text/plain";
}

// Temporary mapping for deprecated media types,
// we will support both old and new media types
// until correspondence is updated and deployed
// TODO: https://github.com/Altinn/dialogporten/issues/1782
public static class MediaTypeExtensions
{
public static string MapDeprecatedMediaType(this string mediaType)
=> mediaType switch
{
MediaTypes.EmbeddableMarkdownDeprecated => MediaTypes.EmbeddableMarkdown,
MediaTypes.LegacyEmbeddableHtmlDeprecated => MediaTypes.LegacyEmbeddableHtml,
_ => mediaType
};
}
Loading