Esempio n. 1
0
        public ActionResult ThreadValidate(ThreadViewModel model)
        {
            EditorType editorType;
            if (model.PostEditor.PostID == 0)
                editorType = EditorType.Create;
            else
                editorType = EditorType.Edit;

            if (editorType == EditorType.Create)
            {
                if (!_permissionServices.CanCreateThread(model.ForumID, _currentUser.UserID))
                {
                    SetNotice("You don't have permission to create a thread in this forum");
                    return RedirectToAction("ViewForum", "Board", new { ForumID = model.ForumID });
                }
            }
            else
            {
                if (!_postServices.CanEditPost(model.PostEditor.PostID, _currentUser.UserID))
                {
                    SetError("You can't edit this post");
                    return RedirectToAction("ViewThread", "Board", new { ThreadID = model.ThreadEditor.ThreadID });
                }
            }

            if (!_permissionServices.CanCreateThreadType(model.ForumID, _currentUser.UserID, (ThreadType)model.ThreadEditor.ThreadType))
            {
                model.ThreadEditor.ThreadType = (int)ThreadType.Regular;
                ModelState.AddModelError("ThreadEditor.ThreadType", "You don't have permission to create a thread of this type");
            }

            if (!model.Preview.HasValue && _currentUser.LastPostDate.HasValue)
            {
                int MinTimeLimit = int.Parse(SiteConfig.TimeBetweenPosts.Value);
                double ValToCompare = (DateTime.UtcNow - _currentUser.LastPostDate.Value).TotalSeconds;
                if (ValToCompare < MinTimeLimit)
                    ModelState.AddModelError("TimeBetweenPosts", "You may only create new posts every " + MinTimeLimit + " seconds (" + Math.Round((MinTimeLimit - ValToCompare)) + " seconds remaining)");
            }

            ValidateThreadImage(model.ThreadEditor.Image);
            HttpPostedFileBase[] files = null;

            if (_permissionServices.CanCreateAttachment(model.ForumID, _currentUser.UserID))
            {
                if (model.PostEditor.Files != null)
                {
                    ValidatePostedFiles(model.PostEditor.Files);
                    files = model.PostEditor.Files.Where(item => item != null && item.ContentLength > 0 && !string.IsNullOrWhiteSpace(item.FileName)).ToArray();
                }
            }
            else
                model.PostEditor.Delete = null;

            if (!_permissionServices.CanCreatePoll(model.ForumID, _currentUser.UserID))
            {
                model.PollEditor.Text = string.Empty;
                model.PollEditor.Options = string.Empty;
            }

            if (IsModelValidAndPersistErrors() && !model.Preview.HasValue)
            {
                if (editorType == EditorType.Create)
                {
                    Thread thread = _threadServices.CreateThread(
                        model.ForumID,
                        _currentUser.UserID,
                        model.ThreadEditor.Title,
                        (ThreadType)model.ThreadEditor.ThreadType,
                        model.ThreadEditor.Image,
                        model.PostEditor.Message,
                        model.PollEditor.Text,
                        model.PollEditor.OptionsSplit,
                        files);

                    if (_threadServices.IsSubscribed(thread.ThreadID, _currentUser.UserID) && model.PostEditor.SubscribeToThread)
                        _threadServices.Subscribe(thread.ThreadID, _currentUser.UserID);

                    SetSuccess("Thread created");
                    return RedirectToAction("ViewThread", "Board", new { ThreadID = thread.ThreadID });
                }
                else
                {
                    Thread thread = _threadServices.GetThread(model.ThreadEditor.ThreadID);
                    var poll = model.PollEditor;
                    bool createPoll = true;

                    if (thread.Poll != null)
                    {
                        if (thread.Poll.TotalVotes > 0)
                        {
                            if (poll != null && poll.Delete)
                            {
                                _pollServices.DeletePoll(model.ThreadEditor.ThreadID);
                            }
                            else
                                createPoll = false;
                        }
                        else
                        {
                            _pollServices.DeletePoll(model.ThreadEditor.ThreadID);
                        }
                    }

                    if (createPoll && poll != null && !string.IsNullOrWhiteSpace(poll.Text))
                        _pollServices.CreatePoll(poll.Text, poll.OptionsSplit, model.ThreadEditor.ThreadID);

                    if (model.PostEditor.Delete != null)
                        _fileServices.DeleteAttachments(model.PostEditor.Delete);

                    _threadServices.UpdateThread(model.ThreadEditor.ThreadID, model.ThreadEditor.Title, (ThreadType)model.ThreadEditor.ThreadType, model.ThreadEditor.Image);
                    _postServices.UpdatePost(model.PostEditor.PostID, model.PostEditor.Message, files);

                    SetSuccess("Thread edited");
                    return RedirectToAction("ViewThread", "Board", new { ThreadID = model.ThreadEditor.ThreadID });
                }
            }

            if (model.Preview.HasValue)
            {
                TempData["Preview_Text"] = model.PostEditor.Message;
                TempData["Preview_Title"] = model.ThreadEditor.Title;
            }

            if (editorType == EditorType.Create)
                return RedirectToAction("CreateThread", new { ForumID = model.ForumID });
            else
                return RedirectToAction("EditThread", new { ThreadID = model.ThreadEditor.ThreadID });
        }
        public ActionResult ViewForum(int ForumID, int Page = 1, bool LastPost = false)
        {
            Forum           forum           = _forumServices.GetForum(ForumID);
            UserPermissions userPermissions = _permissionServices.GetUserPermissions(forum.ForumID, _currentUser.UserID);

            if (!userPermissions.Visible)
            {
                SetNotice("You can't view this forum");

                return(RedirectToAction("Index", "Board"));
            }

            int threadCount = forum.Threads.Count;
            int pageSize    = SiteConfig.ThreadsPerPage.ToInt();
            int pageCount   = (int)Math.Ceiling(((decimal)threadCount / pageSize));
            int currentPage = LastPost ? pageCount : Page;

            SetBreadCrumb(forum.Name);

            Pagination forumPagination = new Pagination(currentPage, threadCount, pageSize, "ViewForum", "Board", new { ForumID = ForumID });

            IEnumerable <Thread> forumThreads = _threadServices.GetPagedThreads(ForumID, currentPage, pageSize).Where(y => y.Type != 4);

            IEnumerable <ThreadRow> threadRows = forumThreads.ToList().Select((thread, index) => new ThreadRow
            {
                IsOdd         = index % 2 != 0,
                Thread        = thread,
                TotalPosts    = thread.Posts.Count,
                FirstPost     = thread.FirstPost,
                LastPost      = thread.Posts.OrderByDescending(post => post.Date).FirstOrDefault(),
                CurrentUser   = _currentUser,
                HasNewPost    = Request.IsAuthenticated ? _threadServices.HasNewPost(thread.ThreadID, _currentUser.UserID) : false,
                IsSubscribed  = Request.IsAuthenticated ? _threadServices.IsSubscribed(thread.ThreadID, _currentUser.UserID) : false,
                HasAttachment = _threadServices.HasAttachment(thread.ThreadID)
            });

            IEnumerable <Thread> globalAnnouncements = _threadServices.GetGlobalAnnouncements();

            IEnumerable <ThreadRow> gaThreads = globalAnnouncements.Select((thread, index) => new ThreadRow
            {
                IsOdd        = index % 2 != 0,
                Thread       = thread,
                TotalPosts   = thread.Posts.Count,
                FirstPost    = thread.FirstPost,
                LastPost     = thread.Posts.OrderByDescending(post => post.Date).FirstOrDefault(),
                CurrentUser  = _currentUser,
                HasNewPost   = Request.IsAuthenticated ? _threadServices.HasNewPost(thread.ThreadID, _currentUser.UserID) : false,
                IsSubscribed = Request.IsAuthenticated ? _threadServices.IsSubscribed(thread.ThreadID, _currentUser.UserID) : false
            });

            ViewForumViewModel viewForum = new ViewForumViewModel()
            {
                Forum               = forum,
                ThreadRows          = threadRows.ToList(),
                GlobalAnnouncements = gaThreads.ToList(),
                Pagination          = forumPagination,
                UserPermissions     = userPermissions
            };

            return(View(viewForum));
        }