コード例 #1
0
        private ThreadViewModel GenerateThreadViewModel(int forumID, EditorType editorType, int? postID = null)
        {
            if (editorType == EditorType.Edit && !postID.HasValue)
                throw new InvalidOperationException("postID is required to generate an edit ThreadViewModel");

            Forum forum = _forumServices.GetForum(forumID);

            var permittedThreadTypes = _permissionServices.GetAllowedThreadTypes(forumID, _currentUser.UserID).ToList();
            var model = new ThreadViewModel()
            {
                EditorType = editorType,
                Forum = forum,
                ForumID = forum.ForumID,
                ThreadEditor = new ThreadEditorViewModel()
                {
                    AllowThreadImages = SiteConfig.AllowThreadImage.ToBool(),
                    PermittedThreadTypes = permittedThreadTypes,
                    ThreadImages = _threadServices.GetThreadImages(),
                },
                CanUploadAttachments = _permissionServices.CanCreateAttachment(forumID, _currentUser.UserID),
                CanCreatePoll = _permissionServices.CanCreatePoll(forumID, _currentUser.UserID)
            };

            if (editorType == EditorType.Edit)
            {
                var post = _postServices.GetPost(postID.Value);
                var thread = post.Thread;
                model.ThreadEditor.ThreadType = thread.Type;
                model.ThreadEditor.Image = thread.Image;
                model.ThreadEditor.ThreadID = post.ThreadID;
                model.ThreadEditor.Title = thread.Title;
                model.PostEditor = GeneratePostEditorViewModel(thread.ThreadID, EditorType.Edit, post);

                if (post.Thread.Poll != null)
                {
                    Poll poll = post.Thread.Poll;
                    IEnumerable<PollOption> options = poll.PollOptions;
                    bool hasVotes = options.Any(item => item.PollVotes.Count > 0);
                    model.PollEditor = new PollEditorViewModel()
                    {
                        HasVotes = hasVotes,
                        Options = poll.PollOptionsAsString(),
                        Text = poll.Question,
                        PollID = poll.PollID
                    };
                }
            }

            if (TempData.ContainsKey("Preview_Text"))
            {
                string previewText = (string)TempData["Preview_Text"];
                model.PreviewText = _parseServices.ParseBBCodeText(previewText);
                model.Preview = true;
                model.PreviewTitle = (string)TempData["Preview_Title"];
            }

            return model;
        }
コード例 #2
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 });
        }