Files
Umbraco-CMS/src/Umbraco.Core/Cache/Refreshers/Implement/MediaCacheRefresher.cs
Elitsa Marinovska 44ff8dc5e1 V15: Implement sorting for the in-memory navigation structures (document and media) (#17280)
* bump version to 15.1.0

* V15 Fixed the failing smoke tests in the pipeline v15 (#17158)

* Fixed the failing tests of Member Group due to UI changes

* Fixed the failing tests of Member due to UI changes

* Fixed the failing tests of User due to UI changes

* Fixed failing tests for Dictionary and Document Type

* Updated tests due to test helper changes

* Bumped version

* Updated assert steps due to the response changes

* Updated tests due to api helper changes

* Updated tests due to UI changes

* Fixed tests for delete partial view

* Fixed tests

* Added more waits

* Updated assert steps

* Fixed failing tests for Block Grid and Media

* Added more waits

* Added skip tests

* Removed waits time

* Updated assertion steps for User

* Added todo

* Updated tests due to api helper changes

* Bumped version

* Added skip tests

* Fetch sortOrder for each navigationNode

* Update NavigationNode to have sortOrder and change Parent and Children props to keys instead of NavigationNodes

* Consider sortOrder when building the navigation structures

* Renaming tests

* Adding tests for items being the last in structure when added, moved, etc.

* Updating names

* Cleanup

* Updating cache refreshers with changes due to sorting

* Refactoring due to sorting changes and resolving key to NavigationNode

* Removing sortOrder params from test as they are calculated automatically

* Adding content and media integration tests to test sorting functionality

* Adding sortOrder param for special case when adding new nodes

* Adding new UpdateSortOrder to INavigationManagementService

* Revert "V15 Fixed the failing smoke tests in the pipeline v15 (#17158)"

This reverts commit 31399c3b15.

* Revert "bump version to 15.1.0"

This reverts commit 5e4d15be

* Fix revert

* Add sort order when creating media

---------

Co-authored-by: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com>
Co-authored-by: Nhu Dinh <150406148+nhudinh0309@users.noreply.github.com>
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2024-10-16 09:51:42 +02:00

233 lines
7.6 KiB
C#

using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Navigation;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Cache;
public sealed class MediaCacheRefresher : PayloadCacheRefresherBase<MediaCacheRefresherNotification, MediaCacheRefresher.JsonPayload>
{
private readonly IIdKeyMap _idKeyMap;
private readonly IMediaNavigationQueryService _mediaNavigationQueryService;
private readonly IMediaNavigationManagementService _mediaNavigationManagementService;
private readonly IMediaService _mediaService;
public MediaCacheRefresher(
AppCaches appCaches,
IJsonSerializer serializer,
IIdKeyMap idKeyMap,
IEventAggregator eventAggregator,
ICacheRefresherNotificationFactory factory,
IMediaNavigationQueryService mediaNavigationQueryService,
IMediaNavigationManagementService mediaNavigationManagementService,
IMediaService mediaService)
: base(appCaches, serializer, eventAggregator, factory)
{
_idKeyMap = idKeyMap;
_mediaNavigationQueryService = mediaNavigationQueryService;
_mediaNavigationManagementService = mediaNavigationManagementService;
_mediaService = mediaService;
}
#region Indirect
public static void RefreshMediaTypes(AppCaches appCaches) => appCaches.IsolatedCaches.ClearCache<IMedia>();
#endregion
#region Json
public class JsonPayload
{
public JsonPayload(int id, Guid? key, TreeChangeTypes changeTypes)
{
Id = id;
Key = key;
ChangeTypes = changeTypes;
}
public int Id { get; }
public Guid? Key { get; }
public TreeChangeTypes ChangeTypes { get; }
}
#endregion
#region Define
public static readonly Guid UniqueId = Guid.Parse("B29286DD-2D40-4DDB-B325-681226589FEC");
public override Guid RefresherUniqueId => UniqueId;
public override string Name => "Media Cache Refresher";
#endregion
#region Refresher
public override void Refresh(JsonPayload[]? payloads)
{
if (payloads == null)
{
return;
}
// actions that always need to happen
AppCaches.RuntimeCache.ClearByKey(CacheKeys.MediaRecycleBinCacheKey);
Attempt<IAppPolicyCache?> mediaCache = AppCaches.IsolatedCaches.Get<IMedia>();
foreach (JsonPayload payload in payloads)
{
if (payload.ChangeTypes == TreeChangeTypes.Remove)
{
_idKeyMap.ClearCache(payload.Id);
}
if (mediaCache.Success)
{
// repository cache
// it *was* done for each pathId but really that does not make sense
// only need to do it for the current media
mediaCache.Result?.Clear(RepositoryCacheKeys.GetKey<IMedia, int>(payload.Id));
mediaCache.Result?.Clear(RepositoryCacheKeys.GetKey<IMedia, Guid?>(payload.Key));
// remove those that are in the branch
if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.RefreshBranch | TreeChangeTypes.Remove))
{
var pathid = "," + payload.Id + ",";
mediaCache.Result?.ClearOfType<IMedia>((_, v) => v.Path?.Contains(pathid) ?? false);
}
}
HandleNavigation(payload);
}
AppCaches.ClearPartialViewCache();
base.Refresh(payloads);
}
private void HandleNavigation(JsonPayload payload)
{
if (payload.Key is null)
{
return;
}
if (payload.ChangeTypes.HasType(TreeChangeTypes.Remove))
{
_mediaNavigationManagementService.MoveToBin(payload.Key.Value);
_mediaNavigationManagementService.RemoveFromBin(payload.Key.Value);
}
if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshAll))
{
_mediaNavigationManagementService.RebuildAsync();
_mediaNavigationManagementService.RebuildBinAsync();
}
if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshNode))
{
IMedia? media = _mediaService.GetById(payload.Id);
if (media is null)
{
return;
}
HandleNavigationForSingleMedia(media);
}
if (payload.ChangeTypes.HasType(TreeChangeTypes.RefreshBranch))
{
IMedia? media = _mediaService.GetById(payload.Id);
if (media is null)
{
return;
}
IEnumerable<IMedia> descendants = _mediaService.GetPagedDescendants(media.Id, 0, int.MaxValue, out _);
foreach (IMedia descendant in media.Yield().Concat(descendants))
{
HandleNavigationForSingleMedia(descendant);
}
}
}
private void HandleNavigationForSingleMedia(IMedia media)
{
// First creation
if (ExistsInNavigation(media.Key) is false && ExistsInNavigationBin(media.Key) is false)
{
_mediaNavigationManagementService.Add(media.Key, GetParentKey(media), media.SortOrder);
if (media.Trashed)
{
// If created as trashed, move to bin
_mediaNavigationManagementService.MoveToBin(media.Key);
}
}
else if (ExistsInNavigation(media.Key) && ExistsInNavigationBin(media.Key) is false)
{
if (media.Trashed)
{
// It must have been trashed
_mediaNavigationManagementService.MoveToBin(media.Key);
}
else
{
if (_mediaNavigationQueryService.TryGetParentKey(media.Key, out var oldParentKey) is false)
{
return;
}
// It must have been saved. Check if parent is different
Guid? newParentKey = GetParentKey(media);
if (oldParentKey != newParentKey)
{
_mediaNavigationManagementService.Move(media.Key, newParentKey);
}
else
{
_mediaNavigationManagementService.UpdateSortOrder(media.Key, media.SortOrder);
}
}
}
else if (ExistsInNavigation(media.Key) is false && ExistsInNavigationBin(media.Key))
{
if (media.Trashed is false)
{
// It must have been restored
_mediaNavigationManagementService.RestoreFromBin(media.Key, GetParentKey(media));
}
}
}
private Guid? GetParentKey(IMedia media) => (media.ParentId == -1) ? null : _idKeyMap.GetKeyForId(media.ParentId, UmbracoObjectTypes.Media).Result;
private bool ExistsInNavigation(Guid contentKey) => _mediaNavigationQueryService.TryGetParentKey(contentKey, out _);
private bool ExistsInNavigationBin(Guid contentKey) => _mediaNavigationQueryService.TryGetParentKeyInBin(contentKey, out _);
// these events should never trigger
// everything should be JSON
public override void RefreshAll() => throw new NotSupportedException();
public override void Refresh(int id) => throw new NotSupportedException();
public override void Refresh(Guid id) => throw new NotSupportedException();
public override void Remove(int id) => throw new NotSupportedException();
#endregion
}