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

Improve handling of publish/unpublish operations #17441

Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -469,13 +469,23 @@ public async Task<IActionResult> EditAndPublishPOST(

return await EditPOST(contentItemId, returnUrl, stayOnSamePage, async contentItem =>
{
await _contentManager.PublishAsync(contentItem);
var published = await _contentManager.PublishAsync(contentItem);

var typeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(contentItem.ContentType);

await _notifier.SuccessAsync(string.IsNullOrWhiteSpace(typeDefinition?.DisplayName)
if (published)
{
await _notifier.SuccessAsync(string.IsNullOrWhiteSpace(typeDefinition?.DisplayName)
? H["Your content has been published."]
: H["Your {0} has been published.", typeDefinition.DisplayName]);
}
else
{
await _notifier.ErrorAsync(string.IsNullOrWhiteSpace(typeDefinition?.DisplayName)
? H["Your content could not be published."]
: H["Your {0} could not be published.", typeDefinition.DisplayName]);
MichaelPetrinolis marked this conversation as resolved.
Show resolved Hide resolved

MichaelPetrinolis marked this conversation as resolved.
Show resolved Hide resolved
}
});
}

Expand Down Expand Up @@ -585,17 +595,32 @@ public async Task<IActionResult> Publish(string contentItemId, string returnUrl)
return Forbid();
}

await _contentManager.PublishAsync(contentItem);
var published = await _contentManager.PublishAsync(contentItem);

var typeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(contentItem.ContentType);

if (string.IsNullOrEmpty(typeDefinition?.DisplayName))
if (published)
{
await _notifier.SuccessAsync(H["That content has been published."]);
if (string.IsNullOrEmpty(typeDefinition?.DisplayName))
MichaelPetrinolis marked this conversation as resolved.
Show resolved Hide resolved
{
await _notifier.SuccessAsync(H["That content has been published."]);
}
else
{
await _notifier.SuccessAsync(H["That {0} has been published.", typeDefinition.DisplayName]);
}
}
else
{
await _notifier.SuccessAsync(H["That {0} has been published.", typeDefinition.DisplayName]);
if (string.IsNullOrEmpty(typeDefinition?.DisplayName))
{
await _notifier.ErrorAsync(H["That content could not be published."]);
}
else
{
await _notifier.ErrorAsync(H["That {0} could not be published.", typeDefinition.DisplayName]);
}

MichaelPetrinolis marked this conversation as resolved.
Show resolved Hide resolved
}

return Url.IsLocalUrl(returnUrl)
Expand All @@ -618,17 +643,32 @@ public async Task<IActionResult> Unpublish(string contentItemId, string returnUr
return Forbid();
}

await _contentManager.UnpublishAsync(contentItem);
var unpublished = await _contentManager.UnpublishAsync(contentItem);

var typeDefinition = await _contentDefinitionManager.GetTypeDefinitionAsync(contentItem.ContentType);

if (string.IsNullOrEmpty(typeDefinition?.DisplayName))
if (unpublished)
{
await _notifier.SuccessAsync(H["The content has been unpublished."]);
if (string.IsNullOrEmpty(typeDefinition?.DisplayName))
{
await _notifier.SuccessAsync(H["The content has been unpublished."]);
}
else
{
await _notifier.SuccessAsync(H["The {0} has been unpublished.", typeDefinition.DisplayName]);
}
}
else
{
await _notifier.SuccessAsync(H["The {0} has been unpublished.", typeDefinition.DisplayName]);
if (string.IsNullOrEmpty(typeDefinition?.DisplayName))
{
await _notifier.ErrorAsync(H["The content could not be unpublished."]);
}
else
{
await _notifier.ErrorAsync(H["The {0} could not be unpublished.", typeDefinition.DisplayName]);
}

MichaelPetrinolis marked this conversation as resolved.
Show resolved Hide resolved
}

return Url.IsLocalUrl(returnUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public interface IContentManager
/// <param name="contentItem"></param>
Task SaveDraftAsync(ContentItem contentItem);

Task PublishAsync(ContentItem contentItem);
Task<bool> PublishAsync(ContentItem contentItem);

Task UnpublishAsync(ContentItem contentItem);
Task<bool> UnpublishAsync(ContentItem contentItem);

Task<TAspect> PopulateAspectAsync<TAspect>(IContent content, TAspect aspect);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ public async Task SaveDraftAsync(ContentItem contentItem)
await ReversedHandlers.InvokeAsync((handler, context) => handler.DraftSavedAsync(context), context, _logger);
}

public async Task PublishAsync(ContentItem contentItem)
public async Task<bool> PublishAsync(ContentItem contentItem)
{
ArgumentNullException.ThrowIfNull(contentItem);

if (contentItem.Published)
{
return;
return true;
}

// Create a context for the item and it's previous published record
Expand All @@ -385,7 +385,7 @@ public async Task PublishAsync(ContentItem contentItem)

if (context.Cancel)
{
return;
return false;
}

if (previous != null)
Expand All @@ -398,9 +398,11 @@ public async Task PublishAsync(ContentItem contentItem)
await _session.SaveAsync(contentItem, checkConcurrency: true);

await ReversedHandlers.InvokeAsync((handler, context) => handler.PublishedAsync(context), context, _logger);

return true;
}

public async Task UnpublishAsync(ContentItem contentItem)
public async Task<bool> UnpublishAsync(ContentItem contentItem)
{
ArgumentNullException.ThrowIfNull(contentItem);

Expand All @@ -425,7 +427,7 @@ public async Task UnpublishAsync(ContentItem contentItem)
if (publishedItem == null)
{
// No published version exists. no work to perform.
return;
return true;
}

// Create a context for the item. the publishing version is null in this case
Expand All @@ -438,11 +440,18 @@ public async Task UnpublishAsync(ContentItem contentItem)

await Handlers.InvokeAsync((handler, context) => handler.UnpublishingAsync(context), context, _logger);

if (context.Cancel)
{
return false;
}

publishedItem.Published = false;
publishedItem.ModifiedUtc = _clock.UtcNow;
await _session.SaveAsync(publishedItem, checkConcurrency: true);

await ReversedHandlers.InvokeAsync((handler, context) => handler.UnpublishedAsync(context), context, _logger);

return true;
}

protected async Task<ContentItem> BuildNewVersionAsync(ContentItem existingContentItem)
Expand Down