Example #1
0
 /// <summary>
 /// 将数据库中的信息转换成EditModel实体
 /// </summary>
 /// <param name="blogThread">文章实体</param>
 /// <returns>编辑文章的EditModel</returns>
 public static BlogThreadEditModel AsEditModel(this BlogThread blogThread)
 {
     return(new BlogThreadEditModel
     {
         ThreadId = blogThread.ThreadId,
         Subject = blogThread.Subject,
         Body = blogThread.GetBody(),
         IsSticky = blogThread.IsSticky,
         IsLocked = blogThread.IsLocked,
         PrivacyStatus = blogThread.PrivacyStatus,
         Keywords = blogThread.Keywords,
         Summary = blogThread.Summary,
         IsDraft = blogThread.IsDraft,
         FeaturedImageAttachmentId = blogThread.FeaturedImageAttachmentId
     });
 }
Example #2
0
        /// <summary>
        /// 删除日志
        /// 空间主人或管理员可以删除空间用户的日志
        /// </summary>
        public static bool BlogThread_Delete(this Authorizer authorizer, BlogThread blogThread)
        {
            IUser currentUser = UserContext.CurrentUser;

            if (currentUser == null)
            {
                return(false);
            }

            if (blogThread.UserId == currentUser.UserId || authorizer.IsAdministrator(BlogConfig.Instance().ApplicationId))
            {
                return(true);
            }

            return(false);
        }
Example #3
0
        /// <summary>
        /// BlogThread转换成<see cref="Lucene.Net.Documents.Document"/>
        /// </summary>
        /// <param name="blogThread">日志实体</param>
        /// <returns>Lucene.Net.Documents.Document</returns>
        public static Document Convert(BlogThread blogThread)
        {
            Document doc = new Document();

            //索引日志基本信息
            doc.Add(new Field(BlogIndexDocument.ThreadId, blogThread.ThreadId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.TenantTypeId, blogThread.TenantTypeId, Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.OwnerId, blogThread.OwnerId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.IsEssential, blogThread.IsEssential ? "1" : "0", Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.Keywords, blogThread.Keywords ?? "", Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(BlogIndexDocument.Summary, blogThread.Summary ?? "", Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(BlogIndexDocument.UserId, blogThread.UserId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.Subject, blogThread.Subject.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(BlogIndexDocument.Body, HtmlUtility.TrimHtml(blogThread.GetBody(), 0).ToLower(), Field.Store.NO, Field.Index.ANALYZED));
            doc.Add(new Field(BlogIndexDocument.Author, blogThread.Author, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field(BlogIndexDocument.DateCreated, DateTools.DateToString(blogThread.DateCreated, DateTools.Resolution.DAY), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.AuditStatus, ((int)blogThread.AuditStatus).ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            doc.Add(new Field(BlogIndexDocument.PrivacyStatus, ((int)blogThread.PrivacyStatus).ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

            //索引日志tag
            foreach (string tagName in blogThread.TagNames)
            {
                doc.Add(new Field(BlogIndexDocument.Tag, tagName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
            }

            //索引日志用户分类名称
            IEnumerable <string> ownerCategoryNames = blogThread.OwnerCategoryNames;

            if (ownerCategoryNames != null)
            {
                foreach (string ownerCategoryName in ownerCategoryNames)
                {
                    doc.Add(new Field(BlogIndexDocument.OwnerCategoryName, ownerCategoryName.ToLower(), Field.Store.YES, Field.Index.ANALYZED));
                }
            }

            //索引日志站点分类ID
            long?siteCategoryId = blogThread.SiteCategoryId;

            if (siteCategoryId.HasValue)
            {
                doc.Add(new Field(BlogIndexDocument.SiteCategoryId, siteCategoryId.Value.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            }

            return(doc);
        }
Example #4
0
        /// <summary>
        /// 评论日志
        /// 1.锁定的日志不允许评论;
        /// 2.对于匿名用户,根据站点匿名用户;
        /// </summary>
        public static bool BlogComment_Create(this Authorizer authorizer, BlogThread blogThread)
        {
            //锁定的日志不允许评论
            if (blogThread.IsLocked)
            {
                return(false);
            }

            //允许登录用户
            if (UserContext.CurrentUser != null)
            {
                return(true);
            }

            //判断是否允许匿名用户评论
            ISiteSettingsManager siteSettingsManager = DIContainer.Resolve <ISiteSettingsManager>();
            SiteSettings         siteSettings        = siteSettingsManager.Get();

            return(siteSettings.EnableAnonymousPosting);
        }
Example #5
0
        /// <summary>
        /// 置顶/取消置顶
        /// </summary>
        /// <param name="threadId">日志Id</param>
        /// <param name="isSticky">是否置顶</param>
        public void SetSticky(long threadId, bool isSticky)
        {
            //设计要点
            //1、置顶状态未变化不用进行任何操作;
            //2、需要触发的事件参见《设计说明书-日志》;

            BlogThread blogThread = blogThreadRepository.Get(threadId);

            if (blogThread.IsSticky == isSticky)
            {
                return;
            }

            blogThread.IsSticky = isSticky;
            this.Update(blogThread, false);

            string operationType = isSticky ? EventOperationType.Instance().SetSticky() : EventOperationType.Instance().CancelSticky();

            EventBus <BlogThread> .Instance().OnAfter(blogThread, new CommonEventArgs(operationType));
        }
Example #6
0
        /// <summary>
        /// 转换成BlogThread类型
        /// </summary>
        /// <returns>文章实体</returns>
        public BlogThread AsBlogThread()
        {
            BlogThread blogThread = null;

            //写文章
            if (this.ThreadId <= 0)
            {
                blogThread        = BlogThread.New();
                blogThread.UserId = UserContext.CurrentUser.UserId;
                blogThread.Author = UserContext.CurrentUser.DisplayName;
                if (this.OwnerId.HasValue)
                {
                    blogThread.OwnerId      = this.OwnerId.Value;
                    blogThread.TenantTypeId = TenantTypeIds.Instance().Group();
                }
                else
                {
                    blogThread.OwnerId      = UserContext.CurrentUser.UserId;
                    blogThread.TenantTypeId = TenantTypeIds.Instance().User();
                }
                blogThread.OriginalAuthorId = UserContext.CurrentUser.UserId;
                blogThread.IsDraft          = this.IsDraft;
            }
            //编辑文章
            else
            {
                BlogService blogService = new BlogService();
                blogThread = blogService.Get(this.ThreadId);
                blogThread.LastModified = DateTime.UtcNow;
            }

            blogThread.Subject       = this.Subject;
            blogThread.Body          = this.Body;
            blogThread.IsSticky      = this.IsSticky;
            blogThread.IsLocked      = this.IsLocked;
            blogThread.PrivacyStatus = this.PrivacyStatus;
            blogThread.Keywords      = this.Keywords;
            if (string.IsNullOrEmpty(blogThread.Keywords))
            {
                string[] keywords = ClauseScrubber.TitleToKeywords(this.Subject);
                if (keywords.Length > 0)
                {
                    blogThread.Keywords = string.Join(" ", keywords);
                }
                else
                {
                    blogThread.Keywords = string.Empty;
                }
            }
            blogThread.Summary = this.Summary;
            if (string.IsNullOrEmpty(this.Summary))
            {
                blogThread.Summary = HtmlUtility.TrimHtml(this.Body, 100);
            }

            blogThread.FeaturedImageAttachmentId = this.FeaturedImageAttachmentId;
            if (blogThread.FeaturedImageAttachmentId > 0)
            {
                Attachment attachment = attachmentService.Get(blogThread.FeaturedImageAttachmentId);
                if (attachment != null)
                {
                    blogThread.FeaturedImage = attachment.GetRelativePath() + "\\" + attachment.FileName;
                }
                else
                {
                    blogThread.FeaturedImageAttachmentId = 0;
                }
            }
            else
            {
                blogThread.FeaturedImage = string.Empty;
            }

            return(blogThread);
        }
Example #7
0
        /// <summary>
        /// 更新索引
        /// </summary>
        /// <param name="blogThread">待更新的日志</param>
        public void Update(BlogThread blogThread)
        {
            Document doc = BlogIndexDocument.Convert(blogThread);

            searchEngine.Update(doc, blogThread.ThreadId.ToString(), BlogIndexDocument.ThreadId);
        }
Example #8
0
 /// <summary>
 /// 添加索引
 /// </summary>
 /// <param name="blogThread">待添加的日志</param>
 public void Insert(BlogThread blogThread)
 {
     Insert(new BlogThread[] { blogThread });
 }
Example #9
0
        /// <summary>
        /// 撰写日志/转载日志
        /// </summary>
        /// <param name="blogThread">日志实体</param>
        public bool Create(BlogThread blogThread, string privacyStatus1 = null, string privacyStatus2 = null)
        {
            //设计要点
            //1、使用AuditService设置审核状态;
            //2、注意调用AttachmentService转换临时附件;
            //3、需要触发的事件参见《设计说明书-日志》
            //4、草稿的审核状态为待审核;
            //5、转载的日志还需要为原日志转载数+1(调用计数服务);

            EventBus <BlogThread> .Instance().OnBefore(blogThread, new CommonEventArgs(EventOperationType.Instance().Create()));

            //设置审核状态,草稿的审核状态为待审核
            if (blogThread.IsDraft)
            {
                blogThread.AuditStatus = AuditStatus.Pending;
            }
            else
            {
                AuditService auditService = new AuditService();
                auditService.ChangeAuditStatusForCreate(blogThread.UserId, blogThread);
            }

            long threadId = 0;

            long.TryParse(blogThreadRepository.Insert(blogThread).ToString(), out threadId);

            if (threadId > 0)
            {
                //转换临时附件
                AttachmentService attachmentService = new AttachmentService(TenantTypeIds.Instance().BlogThread());
                attachmentService.ToggleTemporaryAttachments(blogThread.OwnerId, TenantTypeIds.Instance().BlogThread(), blogThread.ThreadId);

                //原日志转载数+1
                if (blogThread.IsReproduced)
                {
                    CountService countService = new CountService(TenantTypeIds.Instance().BlogThread());
                    countService.ChangeCount(CountTypes.Instance().ReproduceCount(), blogThread.OriginalThreadId, blogThread.OwnerId, 1, true);
                }

                //用户内容计数+1
                if (!blogThread.IsDraft)
                {
                    OwnerDataService ownerDataService = new OwnerDataService(TenantTypeIds.Instance().User());
                    ownerDataService.Change(blogThread.UserId, OwnerDataKeys.Instance().ThreadCount(), 1);
                }

                AtUserService atUserService = new AtUserService(TenantTypeIds.Instance().BlogThread());
                atUserService.ResolveBodyForEdit(blogThread.GetBody(), blogThread.UserId, blogThread.ThreadId);

                //设置隐私状态
                UpdatePrivacySettings(blogThread, privacyStatus1, privacyStatus2);

                EventBus <BlogThread> .Instance().OnAfter(blogThread, new CommonEventArgs(EventOperationType.Instance().Create()));

                EventBus <BlogThread, AuditEventArgs> .Instance().OnAfter(blogThread, new AuditEventArgs(null, blogThread.AuditStatus));

                return(true);
            }

            return(false);
        }
Example #10
0
 /// <summary>
 /// 下一日志ThreadId
 /// </summary>
 /// <param name="blogThread">当前日志实体</param>
 /// <returns>下一日志ThreadId</returns>
 public long GetNextThreadId(BlogThread blogThread)
 {
     return(blogThreadRepository.GetNextThreadId(blogThread));
 }