public ActionResult New(FormCollection form) { ContestEntity contest = ViewData["Contest"] as ContestEntity; ForumTopicEntity topic = new ForumTopicEntity() { Title = form["title"], }; String userip = this.GetCurrentUserIP(); if (!ForumTopicManager.InsertForumTopic(topic, contest.ContestID.ToString(), String.Empty, form["content"], userip)) { return RedirectToErrorMessagePage("Failed to post your topic!"); } return RedirectToAction("List", "Forum", new { area = "Contest", cid = contest.ContestID }); }
public ActionResult New(FormCollection form) { ForumTopicEntity topic = new ForumTopicEntity() { Title = form["title"] }; String pid = form["pid"]; String userip = this.GetCurrentUserIP(); if (!ForumTopicManager.InsertForumTopic(topic, String.Empty, pid, form["content"], userip)) { return RedirectToErrorMessagePage("Failed to post your topic!"); } if (String.IsNullOrEmpty(pid)) { return RedirectToAction("Main", "Forum"); } else { return RedirectToAction("Problem", "Forum", new { pid = pid }); } }
/// <summary> /// 向缓存中写入指定主题信息 /// </summary> /// <param name="topic">指定主题信息</param> public static void SetForumTopicCache(ForumTopicEntity topic) { if (topic != null) CacheManager.Set(GetForumTopicCacheKey(topic.TopicID), topic); }
/// <summary> /// 发布新主题 /// </summary> /// <param name="topic">主题实体</param> /// <param name="cid">竞赛ID</param> /// <param name="pid">题目ID</param> /// <param name="content">主题帖内容</param> /// <param name="postip">发布者IP</param> /// <returns>是否成功发布</returns> public static Boolean InsertForumTopic(ForumTopicEntity topic, String cid, String pid, String content, String postip) { if (!UserManager.IsUserLogined) { throw new UserUnLoginException(); } if (String.IsNullOrEmpty(topic.Title)) { throw new InvalidInputException("Topic title can not be NULL!"); } if (topic.Title.Length > ForumPostRepository.TITLE_MAXLEN) { throw new InvalidInputException("Topic title is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(topic.Title)) { throw new InvalidInputException("Topic title can not contain illegal keywords!"); } if (String.IsNullOrEmpty(content) || content.Length < ForumPostRepository.POST_MINLEN) { throw new InvalidInputException("Topic content is too short!"); } if (content.Length > ForumPostRepository.POST_MAXLEN) { throw new InvalidInputException("Topic content is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(content)) { throw new InvalidInputException("Topic content can not contain illegal keywords!"); } if (!UserSubmitStatus.CheckLastSubmitForumPostTime(UserManager.CurrentUserName)) { throw new InvalidInputException(String.Format("You can not submit post more than twice in {0} seconds!", ConfigurationManager.SubmitInterval.ToString())); } topic.Type = ForumTopicManager.GetForumTopicType(cid, pid); topic.RelativeID = (topic.Type == ForumTopicType.Default ? 0 : ForumTopicManager.GetRelativeID(cid, pid)); if (topic.Type == ForumTopicType.Problem && !ProblemManager.InternalExistsProblem(topic.RelativeID)) { throw new InvalidRequstException(RequestType.Problem); } else if (topic.Type == ForumTopicType.Contest && !ContestManager.InternalExistsContest(topic.RelativeID)) { throw new InvalidRequstException(RequestType.Contest); } topic.UserName = UserManager.CurrentUserName; topic.LastDate = DateTime.Now; topic.Title = HtmlEncoder.HtmlEncode(topic.Title); content = HtmlEncoder.HtmlEncode(content); Boolean success = ForumTopicRepository.Instance.InsertEntity(topic, content, postip) > 0; if (success) { ForumTopicCache.IncreaseForumTopicCountCache(topic.Type, topic.RelativeID);//更新缓存 if (topic.Type == ForumTopicType.Problem) { ForumTopicCache.IncreaseForumTopicCountCache(ForumTopicType.Default, 0);//更新缓存 } } return success; }
/// <summary> /// 增加一条回帖 /// </summary> /// <param name="post">帖子实体</param> /// <param name="topic">主题实体</param> /// <param name="parentPost">回复的帖子实体</param> /// <param name="postip">发布者IP</param> /// <param name="link">当前页面地址</param> /// <returns>是否成功增加</returns> public static Boolean InsertForumPost(ForumPostEntity post, ForumTopicEntity topic, ForumPostEntity parentPost, String postip, String link) { if (!UserManager.IsUserLogined) { throw new UserUnLoginException(); } if (String.IsNullOrEmpty(post.Title)) { throw new InvalidInputException("Reply title can not be NULL!"); } if (post.Title.Length > ForumPostRepository.TITLE_MAXLEN) { throw new InvalidInputException("Reply title is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(post.Title)) { throw new InvalidInputException("Reply title can not contain illegal keywords!"); } if (String.IsNullOrEmpty(post.Content) || post.Content.Length < ForumPostRepository.POST_MINLEN) { throw new InvalidInputException("Reply content is too short!"); } if (post.Content.Length > ForumPostRepository.POST_MAXLEN) { throw new InvalidInputException("Reply content is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(post.Content)) { throw new InvalidInputException("Reply content can not contain illegal keywords!"); } if (parentPost.Deepth + 1 < ForumPostRepository.DEEPTH_MIN) { throw new InvalidInputException("Reply deepth is INVALID!"); } if (parentPost.Deepth + 1 > ForumPostRepository.DEEPTH_MAX) { throw new InvalidInputException("Reply deepth is too deep!"); } if (topic.IsLocked) { throw new NoPermissionException("You have no privilege to reply the post!"); } if (!UserSubmitStatus.CheckLastSubmitForumPostTime(UserManager.CurrentUserName)) { throw new InvalidInputException(String.Format("You can not submit post more than twice in {0} seconds!", ConfigurationManager.SubmitInterval.ToString())); } post.TopicID = parentPost.TopicID; post.Title = HtmlEncoder.HtmlEncode(post.Title); post.Content = HtmlEncoder.HtmlEncode(post.Content); post.UserName = UserManager.CurrentUserName; post.Deepth = parentPost.Deepth + 1; post.ParentPostID = parentPost.PostID; post.PostDate = DateTime.Now; post.PostIP = postip; Boolean success = ForumPostRepository.Instance.InsertEntity(post) > 0; if (success && !String.Equals(parentPost.UserName, post.UserName)) { if (ConfigurationManager.ReplyPostMailNotification) { try { UserMailEntity mail = new UserMailEntity(); String url = ConfigurationManager.DomainUrl + ((link[0] == '/') ? link.Substring(1) : link); mail.FromUserName = ConfigurationManager.SystemAccount; mail.ToUserName = parentPost.UserName; mail.Title = "Your post has new reply!"; mail.Content = String.Format("Your post \"{0}\" has new reply, <br/>", parentPost.Title) + String.Format("Please visit <a href=\"{0}\" target=\"_blank\">{0}</a>", url); UserMailManager.InternalSendUserMail(mail); } catch { } } } return success; }
/// <summary> /// 创建树形列表 /// </summary> /// <param name="topic">主题帖</param> /// <param name="postID">帖子ID</param> /// <returns>树形列表</returns> public static List<TreeNode<ForumPostEntity>> GetPostTreeList(ForumTopicEntity topic, Int32 postID) { List<ForumPostEntity> listPost = ForumPostManager.GetForumPostList(topic, false); if (listPost == null || listPost.Count == 0) { return new List<TreeNode<ForumPostEntity>>(); } //将主题帖放入树节点数组 List<TreeNode<ForumPostEntity>> listTreeNode = new List<TreeNode<ForumPostEntity>>(); //将父节点是帖子ID的节点放入树节点数组 for (Int32 i = 0; i < listPost.Count; i++) { if (listPost[i].ParentPostID == postID) { TreeNode<ForumPostEntity> node = new TreeNode<ForumPostEntity>(listPost[i].PostID.ToString(), listPost[i]); listTreeNode.Add(node); listPost.RemoveAt(i--); } } //根据树节点数组创建树 if (listTreeNode.Count > 0) { for (Int32 i = 0; i < listTreeNode.Count; i++) { ForumPostManager.CreateTree(listTreeNode[i], listPost); } return listTreeNode; } else { return new List<TreeNode<ForumPostEntity>>(); } }
/// <summary> /// 获取帖子列表 /// </summary> /// <param name="topic">主题实体</param> /// <param name="includeHide">是否包含隐藏主题</param> /// <returns>帖子列表</returns> public static List<ForumPostEntity> GetForumPostList(ForumTopicEntity topic, Boolean includeHide) { List<ForumTopicEntity> topics = new List<ForumTopicEntity>() { topic }; return GetForumPostList(topics, includeHide); }