Skip to content

Commit

Permalink
feat: Front channel embeds (#792)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue(s)

- #329 

## Verification

- [x] **Your** code builds clean without any errors or warnings
- [x] Manual testing done (required)
- [ ] Relevant automated test added (if you find this hard, leave it and
we'll help out)

## Documentation

- [ ] Documentation is updated (either in `docs`-directory, Altinnpedia
or a separate linked PR in
[altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if
applicable)

Co-authored-by: Knut Haug <knut.espen.haug@digdir.no>
  • Loading branch information
oskogstad and knuhau authored Jun 7, 2024
1 parent c19f47a commit c3000bd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerables;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
using Digdir.Domain.Dialogporten.Domain;
using Digdir.Domain.Dialogporten.Domain.Common;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Actions;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Content;
Expand Down Expand Up @@ -171,6 +172,11 @@ public CreateDialogDialogElementUrlDtoValidator()
.MaximumLength(Constants.DefaultMaxUriLength);
RuleFor(x => x.MediaType)
.MaximumLength(Constants.DefaultMaxStringLength);
RuleFor(x => x.MediaType)
.Must(MediaTypes.IsValid!)
.When(x => x.MediaType != null)
.WithMessage("Invalid media type, see docs for complete list <URL TDB>");

RuleFor(x => x.ConsumerType)
.IsInEnum();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerables;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
using Digdir.Domain.Dialogporten.Domain;
using Digdir.Domain.Dialogporten.Domain.Common;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Actions;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Content;
Expand Down Expand Up @@ -168,6 +169,10 @@ public UpdateDialogDialogElementUrlDtoValidator()
.MaximumLength(Constants.DefaultMaxUriLength);
RuleFor(x => x.MediaType)
.MaximumLength(Constants.DefaultMaxStringLength);
RuleFor(x => x.MediaType)
.Must(MediaTypes.IsValid!)
.When(x => x.MediaType != null)
.WithMessage("Invalid media type, see docs for complete list <URL TDB>");
RuleFor(x => x.ConsumerType)
.IsInEnum();
}
Expand Down
45 changes: 45 additions & 0 deletions src/Digdir.Domain.Dialogporten.Domain/MediaTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Net.Mime;
using System.Reflection;

namespace Digdir.Domain.Dialogporten.Domain;

public static class MediaTypes
{
// Custom MediaTypes for embedding content in frontend applications
private static readonly string[] CustomDialogportenMediaTypes =
[
"application/vnd.dialogporten.frontchannelembed+json;type=markdown"
];

private static string[]? _validMediaTypes;

public static bool IsValid(string mediaType) => GetValidMediaTypes().Contains(mediaType);

private static string[] GetValidMediaTypes()
{
if (_validMediaTypes != null)
{
return _validMediaTypes;
}

var applicationMediaTypes = GetMediaTypes(typeof(MediaTypeNames.Application));
var imageMediaTypes = GetMediaTypes(typeof(MediaTypeNames.Image));
var textMediaTypes = GetMediaTypes(typeof(MediaTypeNames.Text));

_validMediaTypes = applicationMediaTypes
.Concat(imageMediaTypes)
.Concat(textMediaTypes)
.Concat(CustomDialogportenMediaTypes)
.ToArray();

return _validMediaTypes;
}

private static IEnumerable<string> GetMediaTypes(Type mediaTypeClass)
{
return mediaTypeClass
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(field => field.FieldType == typeof(string))
.Select(field => (string)field.GetValue(null)!);
}
}

0 comments on commit c3000bd

Please sign in to comment.