public ActionResult Reply(WritePostViewModel model, string button, int QuoteId = 0) { using (ForumRespository db = new ForumRespository()) { Forum_Thread RepliedToThread = db.GetThreadByID(model.id); if (RepliedToThread == null) return NotFoundView("Thread"); model.AddNavigation(RepliedToThread); model.AddNavigation("Reply to thread"); Forum_User Replier = GetCurrentUser(db); if (!db.CheckCategoryPermissions(RepliedToThread.Forum_Category, Replier, P => P.AllowReply)) return AuthenticationHelper.AccessDeniedView(model); if (RepliedToThread.Locked) return AuthenticationHelper.AccessDeniedView(model); if (IsHttpPost) { if (String.Equals(button, "preview", StringComparison.InvariantCultureIgnoreCase)) { model.ShowPost = true; model.PostHtml = PostParser.Parse(model.PostText); ModelState.Clear(); } else if (!AntiForgeryTokenValid) { ModelState.AddModelError("AntiForgery", "The antiforgery token was invalid."); } else if (ModelState.IsValid) { Forum_Post ReplyPost = new Forum_Post(); ReplyPost.TimeStamp = DateTime.Now; ReplyPost.PosterID = Replier.UserID; ReplyPost.PostText = model.PostText; RepliedToThread.Forum_Posts.Add(ReplyPost); RepliedToThread.LastPostTime = ReplyPost.TimeStamp; RepliedToThread.Posts = RepliedToThread.Forum_Posts.Count; // Save to database db.Save(); int PostIndex = RepliedToThread.Forum_Posts.IndexOf(ReplyPost); int NewPostPage = PostIndex / POSTS_PER_PAGE + 1; int NewPostNumber = PostIndex % POSTS_PER_PAGE + 1; return RedirectToAction("ViewThread", new { id = RepliedToThread.ThreadID, page = NewPostPage }).AddFragment(String.Format("Post_{0}", NewPostNumber)); } } else { ModelState.Clear(); Forum_Post QuotedPost = db.GetPostByID(QuoteId); if (QuotedPost != null) { model.PostText = String.Format("[quote={0}]{1}[/quote]", QuotedPost.Forum_User.Username, QuotedPost.PostText); } } model.ThreadID = model.id; model.Title = "Reply to Thread"; return View("WritePost", model); } }
public ActionResult ViewThread(ThreadViewModel model) { using (ForumRespository db = new ForumRespository()) { Forum_Thread Thread = db.GetThreadByID(model.Id); if (Thread == null) { return NotFoundView("Thread"); } if (model.Page < 1) return RedirectToAction("ViewThread", new { id = model.Id, page = 1}); // page less than 0 for existing thread equals redirect to valid page. model.AddNavigation(Thread); Forum_User ThreadViewUser = GetCurrentUser(db); if (!db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => P.AllowView)) return AuthenticationHelper.AccessDeniedView(model); model.AllowEditThread = db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => (P.AllowDeleteOwnThread && Thread.Forum_Posts[0].PosterID == ThreadViewUser.UserID && Thread.Forum_Posts[0].PosterID != (int)BuildInUser.Guest) || P.AllowDeleteAllThread || P.AllowMoveThread || P.AllowLockThread); model.Locked = Thread.Locked; model.ThreadTitle = Thread.Title; int UserID = 0; Forum_User U = GetCurrentUser(db); if (U != null) { UserID = U.UserID; db.SetLastPost(Thread, U, Math.Min(model.Page * POSTS_PER_PAGE, Thread.Posts)); db.Save(); } model.LastPage = (Thread.Posts - 1) / POSTS_PER_PAGE + 1; if (model.Page > model.LastPage) return RedirectToAction("ViewThread", new { id = model.Id, page = model.LastPage }); // page greater than what exists equals redirect to last page. IEnumerable<Forum_Post> Posts = Thread.Forum_Posts.Skip((model.Page - 1)* POSTS_PER_PAGE).Take(POSTS_PER_PAGE); int PostNumber = 0; foreach (Forum_Post Post in Posts) { PostViewModel PostModel = new PostViewModel(); PostModel.Locked = model.Locked; PostModel.PostNumber = ++PostNumber; PostModel.ThreadID = model.Id; PostModel.PostText = PostParser.Parse(Post.PostText); PostModel.PostTime = Post.TimeStamp; PostModel.Poster = new UserViewModel(); PostModel.PostID = Post.PostID; PostModel.Poster.Name = Post.Forum_User.Username; PostModel.Poster.UserID = Post.PosterID; PostModel.AllowDelete = (PostNumber > 1 || model.Page > 1) && db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => (P.AllowDeleteOwnPost && Post.PosterID == ThreadViewUser.UserID && Post.PosterID != (int)BuildInUser.Guest) || P.AllowDeleteAllPosts); PostModel.AllowEdit = db.CheckCategoryPermissions(Thread.Forum_Category, ThreadViewUser, P => (P.AllowEditOwnPost && Post.PosterID == ThreadViewUser.UserID && Post.PosterID != (int)BuildInUser.Guest) || P.AllowEditAllPosts); model.PostList.Add(PostModel); } return View(model); } }
public ActionResult EditThread(int id, int? MoveTo, string Lock, string Delete) { using (ForumRespository db = new ForumRespository()) { Forum_Thread EditedThread = db.GetThreadByID(id); if (EditedThread == null) return NotFoundView("Post"); var model = new EditThreadViewModel(); var Category = EditedThread.Forum_Category; model.AddNavigation(EditedThread); model.AddNavigation("Edit Thread"); var Editor = GetCurrentUser(db); model.AllowDelete = db.CheckCategoryPermissions(Category, Editor, P => (P.AllowDeleteOwnThread && EditedThread.Forum_Posts[0].PosterID == Editor.UserID && EditedThread.PosterID != (int)BuildInUser.Guest) || P.AllowDeleteAllThread); model.AllowMove = db.CheckCategoryPermissions(Category, Editor, P => P.AllowMoveThread); model.AllowLock = db.CheckCategoryPermissions(Category, Editor, P => P.AllowLockThread); if (!model.AllowDelete && !model.AllowLock && !model.AllowMove) return AuthenticationHelper.AccessDeniedView(model); model.id = id; model.ThreadName = EditedThread.Title; model.CategoryID = Category.CategoryID; model.CategoryName = Category.Name; model.IsLocked = EditedThread.Locked; foreach (var MoveToCategory in db.GetAllCategories()) { if (MoveToCategory == Category) continue; // Cannot move the where the thread is already if (!MoveToCategory.AllowPosts) continue; // Cannot move to a category that does not allow posts if (!db.CheckCategoryPermissions(MoveToCategory, Editor, P => P.AllowNewThread)) continue; // Cannot move to a category where you are not allowed to create new threads. model.ValidMoveDestinations.Add(new AdminNamedID() { ID = MoveToCategory.CategoryID, Name = MoveToCategory.Name}); } if (IsHttpPost) { if (!AntiForgeryTokenValid) { ModelState.AddModelError("AntiForgery", "The antiforgery token was invalid."); } else { if (model.AllowDelete && !String.IsNullOrEmpty(Delete)) { db.DeleteThread(EditedThread); db.Save(); return RedirectToAction("ViewCategory", new { id = model.CategoryID }); } if (model.AllowMove) { var Destination = db.GetCategoryByID((int)MoveTo); if (Destination != null && model.ValidMoveDestinations.Exists(D => D.ID == Destination.CategoryID)) { EditedThread.Forum_Category = Destination; } } if (model.AllowLock) EditedThread.Locked = !String.IsNullOrEmpty(Lock); db.Save(); return RedirectToAction("ViewThread", new { id = model.id }); } } return View(model); } }