The ICommentableContentType interface enables comments to be created for your content type.
[toc]
Why should I support comments?
When considering social features comments can be a great way to allow users to provide feedback. One example is if you were creating a shopping cart and wanted to allow comments on your products. In this sample comments can be added to links. This can be used as a way to allow users to add feedback on the content in the links.
Creating an ICommentableContentType plugin
To add support for commenting implement the ICommentableContentType interface that is defined in the Telligent.Evolution.Extensibility.Content.Version1
namespace of Telligent.Evolution.Core.dll
.
It is important to note that one or more Core Services can be implemented in the same IContentType class.
This sample further extends the IContentType sample from the Application/Content document.
using System;
using Telligent.Evolution.Extensibility;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.Content.Version1;
using IContent = Telligent.Evolution.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
}
}
After implementing the IPlugin and IContentType interface, begin by defining if the user can comment with the CanCreateComment
method. You can do something as simple as checking if the user is logged in or you can use the permissions API to check if the user has a certain type of permission. For this sample we make sure the user is logged in and registered.
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(); }
Next you will want to check if the user has permission to delete a comment. This is similar to above but we added a check to see if the user accessing the comment is the author. Here we allow the user to delete their own comment.
Editing a comment can be similar to deleting a comment. You can verify the accessing user is the same as the comment author.
And unless you want to restrict viewing comments, the CanReadComment
can simply return 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; }
Here is the full sample.
using System; using Telligent.Evolution.Extensibility; using Telligent.Evolution.Extensibility.Api.Version1; using Telligent.Evolution.Extensibility.Content.Version1; using IContent = Telligent.Evolution.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 } }