ICommentableContentType 接口允许为您的内容类型创建评论。
我为什么要支持评论?
在考虑平台功能时,评论可能是允许用户提供反馈的好方法。例如,如果您正在创建购物车并希望允许对产品发表评论。在此示例中,可以将评论添加到链接中。这可以用作允许用户对链接中的内容添加反馈的一种方式。
创建 ICommentableContentType 插件
若要添加对评论的支持,请实现ICommentableContentType 接口,该接口在 Limyee.Core.dll 的 Limyee.Extensibility.Content.Version1 命名空间中定义 。
请务必注意,可以在同一 IContentType 类中实现一个或多个核心服务。
此示例进一步扩展了应用程序/内容文档中的 IContentType。
using System;
using Limyee.Extensibility;
using Limyee.Extensibility.Api.Version1;
using Limyee.Extensibility.Content.Version1;
using IContent = Limyee.Extensibility.Content.Version1.IContent;
namespace Samples.Links
{
public class LinkItemContentType : IContentType, ICommentableContentType
{
IContentStateChanges _contentState = null;
#region IPlugin Members
//...
#endregion
#region IContentType Members
//...
#endregion
#region ICommentableContentType
//...
#endregion
}
}
实现 IPlugin 和 IContentType 接口后,首先定义用户是否可以使用 CanCreateComment 方法进行评论。您可以执行一些简单的操作,例如检查用户是否已登录,也可以使用权限 API 检查用户是否具有某种类型的权限。对于此示例,我们确保用户已登录。
public bool CanCreateComment(Guid contentId, int userId)
{
var content = LinksData.GetLink(contentId);
if (content == null) return false;
return !Apis.Get<IUsers>().Get(new UsersGetOptions{ Id = userId }).IsSystemAccount.GetValueOrDefault();
}
接下来,您需要检查用户是否有权删除评论。这与上面类似,但我们添加了一个检查,以查看访问评论的用户是否是作者。在这里,我们允许用户删除自己的评论。
编辑评论类似于删除评论。您可以验证访问用户是否与评论作者相同。
除非您要限制查看评论,否则 CanReadComment 可以简单地返回 true。
public bool CanDeleteComment(Guid commentId, int userId)
{
var comment = Apis.Get<IComments>().Get(commentId);
if (comment == null) return false;
return comment.UserId == userId;
}
public bool CanModifyComment(Guid commentId, int userId)
{
var comment = Apis.Get<IComments>().Get(commentId);
if (comment == null) return false;
return comment.UserId == userId;
}
public bool CanReadComment(Guid commentId, int userId)
{
return true;
}
下面是完整示例。
using System;
using Limyee.Extensibility;
using Limyee.Extensibility.Api.Version1;
using Limyee.Extensibility.Content.Version1;
using IContent = Limyee.Extensibility.Content.Version1.IContent;
namespace Samples.Links
{
public class LinkItemContentType : IContentType, ICommentableContentType
{
IContentStateChanges _contentState = null;
#region IPlugin Members
public string Description
{
get { return "Items in a Links collection"; }
}
public void Initialize()
{
}
public string Name
{
get { return "Link Items"; }
}
#endregion
#region IContentType Members
public Guid[] ApplicationTypes
{
get { return new Guid[] { ContentTypes.LinksApplicationId }; }
}
public void AttachChangeEvents(IContentStateChanges stateChanges)
{
_contentState = stateChanges;
}
public Guid ContentTypeId
{
get { return ContentTypes.LinksItemId; }
}
public string ContentTypeName
{
get { return "Links Item"; }
}
public IContent Get(Guid contentId)
{
return LinksData.GetLink(contentId);
}
#endregion
#region ICommentableContentType
public bool CanCreateComment(Guid contentId, int userId)
{
var content = LinksData.GetLink(contentId);
if (content == null) return false;
return !Apis.Get<IUsers>().Get(new UsersGetOptions { Id = userId }).IsSystemAccount.GetValueOrDefault();
}
public bool CanDeleteComment(Guid commentId, int userId)
{
var comment = Apis.Get<IComments>().Get(commentId);
if (comment == null) return false;
return comment.UserId == userId;
}
public bool CanModifyComment(Guid commentId, int userId)
{
var comment = Apis.Get<IComments>().Get(commentId);
if (comment == null) return false;
return comment.UserId == userId;
}
public bool CanReadComment(Guid commentId, int userId)
{
return true;
}
#endregion
}
}