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

CmsKit - Add EntityType configuration to Comments #7923

Merged
merged 5 commits into from
Mar 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.CmsKit.Blogs;
using Volo.CmsKit.Comments;
using Volo.CmsKit.GlobalFeatures;
using Volo.CmsKit.Localization;
using Volo.CmsKit.MediaDescriptors;
Expand All @@ -27,6 +28,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)

ConfigureTagOptions();

ConfigureCommentOptions();

Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<CmsKitAdminApplicationModule>(validate: true);
Expand All @@ -43,20 +46,20 @@ private void ConfigureTagOptions()
new TagEntityTypeDefiniton(
BlogPostConsts.EntityType,
LocalizableString.Create<CmsKitResource>("BlogPost"),
createPolicies: new[]
createPolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
},
updatePolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
updatePolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
},
deletePolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
deletePolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
}));
}
});
Expand All @@ -70,9 +73,9 @@ private void ConfigureTagOptions()
options.EntityTypes.AddIfNotContains(
new MediaDescriptorDefinition(
BlogPostConsts.EntityType,
createPolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
createPolicies: new[]
{
CmsKitAdminPermissions.BlogPosts.Create,
CmsKitAdminPermissions.BlogPosts.Update
},
deletePolicies: new[]
Expand All @@ -88,20 +91,36 @@ private void ConfigureTagOptions()
options.EntityTypes.AddIfNotContains(
new MediaDescriptorDefinition(
PageConsts.EntityType,
createPolicies: new[]
createPolicies: new[]
{
CmsKitAdminPermissions.Pages.Create,
CmsKitAdminPermissions.Pages.Update
CmsKitAdminPermissions.Pages.Update
},
deletePolicies: new[]
{
CmsKitAdminPermissions.Pages.Create,
CmsKitAdminPermissions.Pages.Update,
CmsKitAdminPermissions.Pages.Delete
CmsKitAdminPermissions.Pages.Delete
}));
}
});
}
}

private void ConfigureCommentOptions()
{
if (GlobalFeatureManager.Instance.IsEnabled<CommentsFeature>())
{
Configure<CmsKitCommentOptions>(options =>
{
if (GlobalFeatureManager.Instance.IsEnabled<BlogsFeature>())
{
options.EntityTypes.AddIfNotContains(
new CommentEntityTypeDefinition(BlogPostConsts.EntityType));

}
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static class BlogPosts
{
public const string SlugAlreadyExist = "CmsKit:BlogPost:0001";
}

public static class Comments
{
public const string EntityNotCommentable = "CmsKit:Comments:0001";
}

public static class MediaDescriptors
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"CmsKit:0003": "The entity {0} is not taggable.",
"CmsKit:Blog:0001": "The given slug ({Slug}) already exists!",
"CmsKit:BlogPost:0001": "The given slug already exists!",
"CmsKit:Comments:0001": "The entity {0} is not commentable.",
"CmsKit:Media:0001": "'{Name}' is not a valid media name.",
"CmsKit:Media:0002": "The entity can't have media.",
"CmsKit:Page:0001": "The given url ({0}) already exists.",
"CmsKit:Tag:0002": "The entity is not taggable!",
"CmsKit:Media:0002": "The entity can't have media.",
"CommentAuthorizationExceptionMessage": "Those comments are not allowed for public display.",
"CommentDeletionConfirmationMessage": "This comment and all replies will be deleted!",
"Comments": "Comments",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"CmsKit.Ratings": "Puanlama",
"CmsKit.Reactions": "Tepkiler",
"CmsKit.Tags": "Etiketler",
"CmsKit:Comments:0001": "{0} ögesi yorumlanabilir değil.",
"CmsKit:0002": "İçerik zaten mevcut!",
"CmsKit:0003": "{0} ögesi etiketlenebilir değil.",
"CmsKit:BlogPost:0001": "Aynı url etiketi zaten mevcut.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using JetBrains.Annotations;
using System.Collections.Generic;

namespace Volo.CmsKit.Comments
{
public class CmsKitCommentOptions
{
[NotNull]
public List<CommentEntityTypeDefinition> EntityTypes { get; } = new List<CommentEntityTypeDefinition>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected Comment()

}

public Comment(
internal Comment(
Guid id,
[NotNull] string entityType,
[NotNull] string entityId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using JetBrains.Annotations;
using System;
using Volo.Abp;

namespace Volo.CmsKit.Comments
{
public class CommentEntityTypeDefinition : IEquatable<CommentEntityTypeDefinition>
{
public CommentEntityTypeDefinition([NotNull] string entityType)
{
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType));
}

public string EntityType { get; }

public bool Equals(CommentEntityTypeDefinition other)
{
return EntityType == other?.EntityType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using JetBrains.Annotations;
using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
using Volo.CmsKit.Users;

namespace Volo.CmsKit.Comments
{
public class CommentManager : DomainService
{
protected ICommentEntityTypeDefinitionStore DefinitionStore { get; }

public CommentManager(ICommentEntityTypeDefinitionStore definitionStore)
{
DefinitionStore = definitionStore;
}

public virtual async Task<Comment> CreateAsync([NotNull] CmsUser creator,
[NotNull] string entityType,
[NotNull] string entityId,
[NotNull] string text,
[CanBeNull] Guid? repliedCommentId = null)
{
Check.NotNull(creator, nameof(creator));

enisn marked this conversation as resolved.
Show resolved Hide resolved
if (!await DefinitionStore.IsDefinedAsync(entityType))
{
throw new EntityNotCommentableException(entityType);
}

return new Comment(
GuidGenerator.Create(),
entityType,
entityId,
text,
repliedCommentId,
creator.Id,
CurrentTenant.Id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using JetBrains.Annotations;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.DependencyInjection;

namespace Volo.CmsKit.Comments
{
public class DefaultCommentEntityTypeDefinitionStore : ICommentEntityTypeDefinitionStore, ITransientDependency
{
protected CmsKitCommentOptions Options { get; }

public DefaultCommentEntityTypeDefinitionStore(IOptions<CmsKitCommentOptions> options)
{
Options = options.Value;
}

public virtual Task<CommentEntityTypeDefinition> GetDefinitionAsync([NotNull] string entityType)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));

var result = Options.EntityTypes.SingleOrDefault(x => x.EntityType.Equals(entityType, StringComparison.InvariantCultureIgnoreCase)) ??
throw new EntityNotCommentableException(entityType);

return Task.FromResult(result);
}

public virtual Task<bool> IsDefinedAsync([NotNull] string entityType)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));

var isDefined = Options.EntityTypes.Any(x => x.EntityType == entityType);

return Task.FromResult(isDefined);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;

namespace Volo.CmsKit.Comments
{
public class EntityNotCommentableException : BusinessException
enisn marked this conversation as resolved.
Show resolved Hide resolved
{
public EntityNotCommentableException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context)
{
}

public EntityNotCommentableException(string entityType)
{
Code = CmsKitErrorCodes.Comments.EntityNotCommentable;
EntityType = entityType;
WithData(nameof(EntityType), EntityType);
}

public string EntityType { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.CmsKit.Comments;

namespace Volo.CmsKit.Comments
{
public interface ICommentEntityTypeDefinitionStore
{
Task<CommentEntityTypeDefinition> GetDefinitionAsync([NotNull] string entityType);

Task<bool> IsDefinedAsync([NotNull] string entityType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ public class CommentPublicAppService : ApplicationService, ICommentPublicAppServ
protected ICommentRepository CommentRepository { get; }
protected ICmsUserLookupService CmsUserLookupService { get; }
public IDistributedEventBus DistributedEventBus { get; }
public IUnitOfWorkManager UnitOfWorkManager { get; }
protected CommentManager CommentManager { get; }

public CommentPublicAppService(
ICommentRepository commentRepository,
ICmsUserLookupService cmsUserLookupService,
IDistributedEventBus distributedEventBus,
IUnitOfWorkManager unitOfWorkManager,
IOptions<CmsKitOptions> cmsKitOptions)
IOptions<CmsKitOptions> cmsKitOptions,
CommentManager commentManager)
{
CmsKitOptions = cmsKitOptions.Value;
CommentRepository = commentRepository;
CmsUserLookupService = cmsUserLookupService;
DistributedEventBus = distributedEventBus;
UnitOfWorkManager = unitOfWorkManager;
CommentManager = commentManager;
}

public virtual async Task<ListResultDto<CommentWithDetailsDto>> GetListAsync(string entityType, string entityId)
Expand All @@ -58,14 +58,12 @@ public virtual async Task<CommentDto> CreateAsync(string entityType, string enti
}

var comment = await CommentRepository.InsertAsync(
new Comment(
GuidGenerator.Create(),
await CommentManager.CreateAsync(
user,
entityType,
entityId,
input.Text,
input.RepliedCommentId,
user.Id,
CurrentTenant.Id
input.RepliedCommentId
)
);

Expand Down