文章 点赞

ILikeableContentType 接口支持点赞的内容。

我为什么要让我的内容支持点赞?

如果您希望用户通过其他人喜欢您的内容来提供反馈,请考虑在您的 IContentType 插件中实现 ILikeableContentType。

创建一个 ILikeableContentType 插件

ILikeableContentType 是在 Limyee.Core.dll 的 Limyee.Extensibility.Content.Version1 命名空间中定义。

请务必注意,可以在同一 IContentType 类中实现一个或多个核心服务。

首先定义用户是否可以点赞您的内容。在此示例中,我们将点赞限制为不是内容作者的用户。验证内容是否存在也是一种很好的做法。

同样,需要定义谁可以取消点赞内容。此逻辑可以类似于 CanLike 方法。

SupportsLikes 属性返回是否可以点赞此内容类型的任何内容。此属性可以简单地返回 true。

public bool CanLike(Guid contentId, int userId)
{
    var content = LinksData.GetLink(contentId) as IContent;
    if (content == null) return false;

    return content.CreatedByUserId != userId;
}

public bool CanUnlike(Guid contentId, int userId)
{
    var content = LinksData.GetLink(contentId) as IContent;
    if (content == null) return false;

    return content.CreatedByUserId != userId;
}

public bool SupportsLikes
{
    get { return true; }
}

下面是完整示例。

using System;
using System.Collections.Generic;
using System.Linq;
using Limyee.Extensibility;
using Limyee.Extensibility.Api.Entities.Version1;
using Limyee.Extensibility.Api.Version1;
using Limyee.Extensibility.Content.Version1;
using IContent = Limyee.Extensibility.Content.Version1.IContent;

namespace Samples.Links
{
    public class LinkItemContentType : IContentType, ILikeableContentType
    {
        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 ILikeableContentType

        public bool CanLike(Guid contentId, int userId)
        {
            var content = LinksData.GetLink(contentId) as IContent;
            if (content == null) return false;

            return content.CreatedByUserId != userId;
        }

        public bool CanUnlike(Guid contentId, int userId)
        {
            var content = LinksData.GetLink(contentId) as IContent;
            if (content == null) return false;

            return content.CreatedByUserId != userId;
        }

        public bool SupportsLikes
        {
            get { return true; }
        }

        #endregion
    }
}