/// <summary> /// 是否具有编辑BarPost的权限 /// </summary> /// <param name="post"></param> /// <returns></returns> public static bool BarPost_Edit(this Authorizer authorizer, BarPost post) { if (authorizer.IsAdministrator(BarConfig.Instance().ApplicationId)) { return(true); } if (post == null) { return(false); } IUser currentUser = UserContext.CurrentUser; if (currentUser == null) { return(false); } if (post.UserId == currentUser.UserId) { return(true); } BarSectionService barSectionService = new BarSectionService(); if (authorizer.BarSection_Manage(barSectionService.Get(post.SectionId))) { return(true); } return(false); }
public void ProcessRequest(HttpContext context) { string url = string.Empty; long anchorPostId = context.Request.QueryString.Get <long>("anchorPostId"); BarPostService barPostService = new BarPostService(); BarPost post = barPostService.Get(anchorPostId); if (post == null) { WebUtility.Return404(context); } IBarUrlGetter urlGetter = BarUrlGetterFactory.Get(post.TenantTypeId); if (post != null) { int?childPostIndex = 0; if (post.ParentId != 0) { childPostIndex = barPostService.GetPageIndexForChildrenPost(post.ParentId, post.PostId); } int pageIndex = barPostService.GetPageIndexForPostInThread(post.ThreadId, anchorPostId); url = urlGetter.ThreadDetail(post.ThreadId, pageIndex: pageIndex, anchorPostId: anchorPostId, childPostIndex: childPostIndex); } context.Response.RedirectPermanent(url); }
/// <summary> /// 创建回复贴 /// </summary> /// <param name="post">回复贴</param> public bool Create(BarPost post) { BarSectionService barSectionService = new BarSectionService(); BarSection barSection = barSectionService.Get(post.SectionId); if (barSection == null) { return(false); } EventBus <BarPost> .Instance().OnBefore(post, new CommonEventArgs(EventOperationType.Instance().Create())); //设置审核状态 auditService.ChangeAuditStatusForCreate(post.UserId, post); long id = 0; long.TryParse(barPostRepository.Insert(post).ToString(), out id); if (id > 0) { new AttachmentService(TenantTypeIds.Instance().BarPost()).ToggleTemporaryAttachments(post.UserId, TenantTypeIds.Instance().BarPost(), id); //计数 CountService countService = new CountService(TenantTypeIds.Instance().BarSection()); countService.ChangeCount(CountTypes.Instance().ThreadAndPostCount(), barSection.SectionId, barSection.UserId, 1, true); if (post.TenantTypeId == TenantTypeIds.Instance().Group()) { //群组内容计数+1 OwnerDataService groupOwnerDataService = new OwnerDataService(TenantTypeIds.Instance().Group()); groupOwnerDataService.Change(post.SectionId, OwnerDataKeys.Instance().PostCount(), 1); } else if (post.TenantTypeId == TenantTypeIds.Instance().Bar()) { //用户内容计数+1 OwnerDataService ownerDataService = new OwnerDataService(TenantTypeIds.Instance().User()); ownerDataService.Change(post.UserId, OwnerDataKeys.Instance().PostCount(), 1); } //更新帖子主题计数缓存 RealTimeCacheHelper realTimeCacheHelper = EntityData.ForType(typeof(BarThread)).RealTimeCacheHelper; string cacheKey = realTimeCacheHelper.GetCacheKeyOfEntity(post.ThreadId); ICacheService cacheService = DIContainer.Resolve <ICacheService>(); BarThread barThread = cacheService.Get <BarThread>(cacheKey); if (barThread != null && barThread.ThreadId > 0) { barThread.PostCount++; cacheService.Set(cacheKey, barThread, CachingExpirationType.SingleObject); } new AtUserService(TenantTypeIds.Instance().BarPost()).ResolveBodyForEdit(post.GetBody(), post.UserId, post.PostId); EventBus <BarPost> .Instance().OnAfter(post, new CommonEventArgs(EventOperationType.Instance().Create())); EventBus <BarPost, AuditEventArgs> .Instance().OnAfter(post, new AuditEventArgs(null, post.AuditStatus)); } return(id > 0); }
/// <summary> /// 更新回复贴 /// </summary> /// <param name="post">回复贴</param> public void Update(BarPost post, long userId) { EventBus <BarPost> .Instance().OnBefore(post, new CommonEventArgs(EventOperationType.Instance().Update())); auditService.ChangeAuditStatusForUpdate(userId, post); barPostRepository.Update(post); new AtUserService(TenantTypeIds.Instance().BarPost()).ResolveBodyForEdit(post.GetBody(), post.UserId, post.PostId); EventBus <BarPost> .Instance().OnAfter(post, new CommonEventArgs(EventOperationType.Instance().Update())); }
/// <summary> /// 新建实体时使用 /// </summary> //todo:需要检查成员初始化的类型是否正确 public static BarPost New() { BarPost barPost = new BarPost() { Author = string.Empty, Subject = string.Empty, IP = WebUtility.GetIP(), DateCreated = DateTime.UtcNow, LastModified = DateTime.UtcNow }; return(barPost); }
/// <summary> /// 转换成BarPost类型 /// </summary> /// <returns></returns> public BarPost AsBarPost() { BarThread thread = new BarThreadService().Get(this.ThreadId); BarPostService service = new BarPostService(); BarPost post = null; //编辑的情况 if (this.PostId.HasValue) { post = service.Get(this.PostId ?? 0); if (post == null) { return(null); } } else { //创建的情况 post = BarPost.New(); post.AuditStatus = AuditStatus.Success; post.TenantTypeId = thread.TenantTypeId; post.ThreadId = this.ThreadId; if (UserContext.CurrentUser != null) { post.UserId = UserContext.CurrentUser.UserId; post.Author = UserContext.CurrentUser.DisplayName; } else { post.UserId = 0; post.Author = "匿名用户"; } post.OwnerId = thread == null ? 0 : thread.OwnerId; post.SectionId = thread == null ? 0 : thread.SectionId; post.ParentId = this.ParentId; } if (!string.IsNullOrEmpty(this.Body)) { post.Body = this.Body; } else { this.MultilineBody = WebUtility.HtmlEncode(this.MultilineBody); this.MultilineBody = new EmotionService().EmoticonTransforms(this.MultilineBody); post.Body = this.MultilineBody; } return(post); }
/// <summary> /// 数据库中的对象转换为EditModel /// </summary> /// <returns></returns> public static BarPostEditModel AsEditModel(this BarPost post) { BarSettings barSettings = DIContainer.Resolve <ISettingsManager <BarSettings> >().Get(); BarThread thread = new BarThreadService().Get(post.ThreadId); return(new BarPostEditModel { Body = post.GetBody(), ParentId = post.ParentId, PostId = post.PostId, Subject = thread.Subject, ThreadId = post.ThreadId, SectionId = post.SectionId }); }
/// <summary> /// 删除回复贴 /// </summary> /// <param name="postId">回复贴Id</param> public void Delete(long postId) { BarPost post = barPostRepository.Get(postId); if (post == null) { return; } BarSectionService barSectionService = new BarSectionService(); BarSection barSection = barSectionService.Get(post.SectionId); if (barSection == null) { return; } EventBus <BarPost> .Instance().OnBefore(post, new CommonEventArgs(EventOperationType.Instance().Delete())); int affectCount = barPostRepository.Delete(post); if (affectCount > 0) { //更新帖吧的计数 CountService countService = new CountService(TenantTypeIds.Instance().BarSection()); countService.ChangeCount(CountTypes.Instance().ThreadAndPostCount(), barSection.SectionId, barSection.UserId, -1 - post.ChildPostCount, true); if (post.TenantTypeId == TenantTypeIds.Instance().Group()) { //群组内容计数-1 OwnerDataService groupOwnerDataService = new OwnerDataService(TenantTypeIds.Instance().Group()); groupOwnerDataService.Change(post.SectionId, OwnerDataKeys.Instance().PostCount(), -1); } else if (post.TenantTypeId == TenantTypeIds.Instance().Bar()) { //用户内容计数-1 OwnerDataService ownerDataService = new OwnerDataService(TenantTypeIds.Instance().User()); ownerDataService.Change(post.UserId, OwnerDataKeys.Instance().PostCount(), -1); } EventBus <BarPost> .Instance().OnAfter(post, new CommonEventArgs(EventOperationType.Instance().Delete())); EventBus <BarPost, AuditEventArgs> .Instance().OnAfter(post, new AuditEventArgs(post.AuditStatus, null)); } }
/// <summary> /// 更新审核状态 /// </summary> /// <param name="postId">待被更新的回帖Id</param> /// <param name="isApproved">是否通过审核</param> public void UpdateAuditStatus(long postId, bool isApproved) { BarPost post = barPostRepository.Get(postId); AuditStatus oldAuditStatus = post.AuditStatus; AuditStatus auditStatus = isApproved ? AuditStatus.Success : AuditStatus.Fail; if (post.AuditStatus == auditStatus) { return; } post.AuditStatus = auditStatus; barPostRepository.Update(post); string operationType = isApproved ? EventOperationType.Instance().Approved() : EventOperationType.Instance().Disapproved(); EventBus <BarPost> .Instance().OnAfter(post, new CommonEventArgs(operationType)); EventBus <BarPost, AuditEventArgs> .Instance().OnAfter(post, new AuditEventArgs(oldAuditStatus, post.AuditStatus)); }
public AssociatedInfo GetAssociatedInfo(long associateId, string tenantTypeId = "") { BarPostService barPostService = new BarPostService(); BarPost barPost = barPostService.Get(associateId); if (barPost == null) { return(null); } BarThread barThread = new BarThreadService().Get(barPost.ThreadId); if (barThread != null) { IBarUrlGetter urlGetter = BarUrlGetterFactory.Get(barThread.TenantTypeId); return(new AssociatedInfo() { DetailUrl = urlGetter.ThreadDetail(barThread.ThreadId, anchorPostId: barPost.PostId), Subject = barThread.Subject }); } return(null); }
/// <summary> /// 是否具有删除BarPost的权限 /// </summary> /// <param name="postId"></param> /// <returns></returns> public static bool BarPost_Delete(this Authorizer authorizer, BarPost post) { return(authorizer.BarPost_Edit(post)); }