Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,4 @@ protected IActionResult ContentCollectionOperationStatusResult(ContentCollection
StatusCode = StatusCodes.Status500InternalServerError,
},
});

/// <summary>
/// Populates the signs for the collection response models.
/// </summary>
protected async Task PopulateSigns(IEnumerable<TCollectionResponseModel> itemViewModels)
{
foreach (ISignProvider signProvider in _signProviders.Where(x => x.CanProvideSigns<TCollectionResponseModel>()))
{
await signProvider.PopulateSignsAsync(itemViewModels);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public async Task<IActionResult> ByKey(
}

List<DocumentCollectionResponseModel> collectionResponseModels = await _documentCollectionPresentationFactory.CreateCollectionModelAsync(collectionAttempt.Result!);
await PopulateSigns(collectionResponseModels);
return CollectionResult(collectionResponseModels, collectionAttempt.Result!.Items.Total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,14 @@ public class ItemDocumentItemController : DocumentItemControllerBase
{
private readonly IEntityService _entityService;
private readonly IDocumentPresentationFactory _documentPresentationFactory;
private readonly SignProviderCollection _signProviders;

[ActivatorUtilitiesConstructor]
public ItemDocumentItemController(
IEntityService entityService,
IDocumentPresentationFactory documentPresentationFactory,
SignProviderCollection signProvider)
IDocumentPresentationFactory documentPresentationFactory)
{
_entityService = entityService;
_documentPresentationFactory = documentPresentationFactory;
_signProviders = signProvider;
}

[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 18")]
public ItemDocumentItemController(
IEntityService entityService,
IDocumentPresentationFactory documentPresentationFactory)
: this(entityService, documentPresentationFactory, StaticServiceProvider.Instance.GetRequiredService<SignProviderCollection>())
{
}

[HttpGet]
Expand All @@ -55,15 +44,6 @@ public async Task<IActionResult> Item(
.OfType<IDocumentEntitySlim>();

IEnumerable<DocumentItemResponseModel> responseModels = documents.Select(_documentPresentationFactory.CreateItemResponseModel);
await PopulateSigns(responseModels);
return Ok(responseModels);
}

private async Task PopulateSigns(IEnumerable<DocumentItemResponseModel> itemViewModels)
{
foreach (ISignProvider signProvider in _signProviders.Where(x => x.CanProvideSigns<DocumentItemResponseModel>()))
{
await signProvider.PopulateSignsAsync(itemViewModels);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public async Task<IActionResult> ByKey(
}

List<MediaCollectionResponseModel> collectionResponseModels = await _mediaCollectionPresentationFactory.CreateCollectionModelAsync(collectionAttempt.Result!);
await PopulateSigns(collectionResponseModels);
return CollectionResult(collectionResponseModels, collectionAttempt.Result!.Items.Total);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Management.Services.Signs;
using Umbraco.Cms.Api.Management.ViewModels.Content;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
Expand All @@ -13,9 +16,24 @@ public abstract class ContentCollectionPresentationFactory<TContent, TCollection
where TValueResponseModelBase : ValueResponseModelBase
where TVariantResponseModel : VariantResponseModelBase
{
private readonly SignProviderCollection _signProviderCollection;
private readonly IUmbracoMapper _mapper;

protected ContentCollectionPresentationFactory(IUmbracoMapper mapper) => _mapper = mapper;
[Obsolete("Please use the controller with all parameters, will be removed in Umbraco 18")]
protected ContentCollectionPresentationFactory(IUmbracoMapper mapper)
: this(
mapper,
StaticServiceProvider.Instance.GetRequiredService<SignProviderCollection>())
{
}

protected ContentCollectionPresentationFactory(
IUmbracoMapper mapper,
SignProviderCollection signProviderCollection)
{
_mapper = mapper;
_signProviderCollection = signProviderCollection;
}

public async Task<List<TCollectionResponseModel>> CreateCollectionModelAsync(ListViewPagedModel<TContent> contentCollection)
{
Expand All @@ -36,8 +54,19 @@ public async Task<List<TCollectionResponseModel>> CreateCollectionModelAsync(Lis

await SetUnmappedProperties(contentCollection, collectionResponseModels);


await PopulateSigns(collectionResponseModels);

return collectionResponseModels;
}

protected virtual Task SetUnmappedProperties(ListViewPagedModel<TContent> contentCollection, List<TCollectionResponseModel> collectionResponseModels) => Task.CompletedTask;

private async Task PopulateSigns(IEnumerable<TCollectionResponseModel> models)
{
foreach (ISignProvider signProvider in _signProviderCollection.Where(x => x.CanProvideSigns<TCollectionResponseModel>()))
{
await signProvider.PopulateSignsAsync(models);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Management.Mapping.Content;
using Umbraco.Cms.Api.Management.Services.Signs;
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Api.Management.ViewModels.Document.Item;
using Umbraco.Cms.Api.Management.ViewModels.DocumentBlueprint.Item;
using Umbraco.Cms.Api.Management.ViewModels.DocumentType;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentPublishing;
Expand All @@ -23,21 +26,43 @@ internal sealed class DocumentPresentationFactory : IDocumentPresentationFactory
private readonly IPublicAccessService _publicAccessService;
private readonly TimeProvider _timeProvider;
private readonly IIdKeyMap _idKeyMap;
private readonly SignProviderCollection _signProviderCollection;

[Obsolete("Please use the controller with all parameters. Scheduled for removal in Umbraco 18")]
public DocumentPresentationFactory(
IUmbracoMapper umbracoMapper,
IDocumentUrlFactory documentUrlFactory,
ITemplateService templateService,
IPublicAccessService publicAccessService,
TimeProvider timeProvider,
IIdKeyMap idKeyMap)
: this(
umbracoMapper,
documentUrlFactory,
templateService,
publicAccessService,
timeProvider,
idKeyMap,
StaticServiceProvider.Instance.GetRequiredService<SignProviderCollection>())
{
}

public DocumentPresentationFactory(
IUmbracoMapper umbracoMapper,
IDocumentUrlFactory documentUrlFactory,
ITemplateService templateService,
IPublicAccessService publicAccessService,
TimeProvider timeProvider,
IIdKeyMap idKeyMap,
SignProviderCollection signProviderCollection)
{
_umbracoMapper = umbracoMapper;
_documentUrlFactory = documentUrlFactory;
_templateService = templateService;
_publicAccessService = publicAccessService;
_timeProvider = timeProvider;
_idKeyMap = idKeyMap;
_signProviderCollection = signProviderCollection;
}

[Obsolete("Schedule for removal in v17")]
Expand Down Expand Up @@ -105,6 +130,8 @@ public DocumentItemResponseModel CreateItemResponseModel(IDocumentEntitySlim ent

responseModel.Variants = CreateVariantsItemResponseModels(entity);

PopulateSignsOnDocuments(responseModel);

return responseModel;
}

Expand All @@ -125,23 +152,29 @@ public IEnumerable<DocumentVariantItemResponseModel> CreateVariantsItemResponseM
{
if (entity.Variations.VariesByCulture() is false)
{
yield return new()
var model = new DocumentVariantItemResponseModel()
{
Name = entity.Name ?? string.Empty,
State = DocumentVariantStateHelper.GetState(entity, null),
Culture = null,
};

PopulateSignsOnVariants(model);
yield return model;
yield break;
}

foreach (KeyValuePair<string, string> cultureNamePair in entity.CultureNames)
{
yield return new()
var model = new DocumentVariantItemResponseModel()
{
Name = cultureNamePair.Value,
Culture = cultureNamePair.Key,
State = DocumentVariantStateHelper.GetState(entity, cultureNamePair.Key)
};

PopulateSignsOnVariants(model);
yield return model;
}
}

Expand Down Expand Up @@ -256,4 +289,20 @@ public Attempt<List<CulturePublishScheduleModel>, ContentPublishingOperationStat

return Attempt.SucceedWithStatus(ContentPublishingOperationStatus.Success, model);
}

private void PopulateSignsOnDocuments(DocumentItemResponseModel model)
{
foreach (ISignProvider signProvider in _signProviderCollection.Where(x => x.CanProvideSigns<DocumentItemResponseModel>()))
{
signProvider.PopulateSignsAsync([model]).GetAwaiter().GetResult();
}
}

private void PopulateSignsOnVariants(DocumentVariantItemResponseModel model)
{
foreach (ISignProvider signProvider in _signProviderCollection.Where(x => x.CanProvideSigns<DocumentVariantItemResponseModel>()))
{
signProvider.PopulateSignsAsync([model]).GetAwaiter().GetResult();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Api.Management.ViewModels.Document.Collection;
using Umbraco.Cms.Api.Management.ViewModels.Document.Item;
using Umbraco.Cms.Api.Management.ViewModels.Tree;
using Umbraco.Cms.Core;

namespace Umbraco.Cms.Api.Management.Services.Signs;
Expand All @@ -17,15 +14,15 @@
/// <inheritdoc/>
public bool CanProvideSigns<TItem>()
where TItem : IHasSigns =>
typeof(TItem) == typeof(DocumentTreeItemResponseModel) ||
typeof(TItem) == typeof(DocumentCollectionResponseModel) ||
typeof(TItem) == typeof(DocumentItemResponseModel);
typeof(TItem) == typeof(DocumentVariantItemResponseModel) ||
typeof(TItem) == typeof(DocumentVariantResponseModel);


/// <inheritdoc/>
public Task PopulateSignsAsync<TItem>(IEnumerable<TItem> itemViewModels)
public Task PopulateSignsAsync<TItem>(IEnumerable<TItem> items)
where TItem : IHasSigns
{
foreach (TItem item in itemViewModels)
foreach (TItem item in items)
{
if (HasPendingChanges(item))
{
Expand All @@ -39,11 +36,10 @@
/// <summary>
/// Determines if the given item has any variant that has pending changes.
/// </summary>
private bool HasPendingChanges(object item) => item switch
private static bool HasPendingChanges(object item) => item switch
{
DocumentTreeItemResponseModel { Variants: var v } when v.Any(x => x.State == DocumentVariantState.PublishedPendingChanges) => true,
DocumentCollectionResponseModel { Variants: var v } when v.Any(x => x.State == DocumentVariantState.PublishedPendingChanges) => true,
DocumentItemResponseModel { Variants: var v } when v.Any(x => x.State == DocumentVariantState.PublishedPendingChanges) => true,
DocumentVariantItemResponseModel variant => variant.State == DocumentVariantState.PublishedPendingChanges,
DocumentVariantResponseModel variant => variant.State == DocumentVariantState.PublishedPendingChanges,

Check notice on line 42 in src/Umbraco.Cms.Api.Management/Services/Signs/HasPendingChangesSignProvider.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Complex Method

HasPendingChanges is no longer above the threshold for cyclomatic complexity
_ => false,
};
}
Loading