public void Should_not_have_error_when_text_is_specified()
        {
            var model = new EditForumPostModel();

            model.Text = "some comment";
            _validator.ShouldNotHaveValidationErrorFor(x => x.Text, model);
        }
        public void Should_have_error_when_text_is_null_or_empty()
        {
            var model = new EditForumPostModel();

            model.Text = null;
            _validator.ShouldHaveValidationErrorFor(x => x.Text, model);
            model.Text = "";
            _validator.ShouldHaveValidationErrorFor(x => x.Text, model);
        }
        public void ShouldNotHaveErrorWhenTextIsSpecified()
        {
            var model = new EditForumPostModel
            {
                Text = "some comment"
            };

            _validator.ShouldNotHaveValidationErrorFor(x => x.Text, model);
        }
示例#4
0
        public virtual async Task <IActionResult> PostEdit(string id)
        {
            if (!_forumSettings.ForumsEnabled)
            {
                return(RedirectToRoute("HomePage"));
            }

            var forumPost = await _forumService.GetPostById(id);

            if (forumPost == null)
            {
                return(RedirectToRoute("Boards"));
            }
            if (!_forumService.IsCustomerAllowedToEditPost(_workContext.CurrentCustomer, forumPost))
            {
                return(new ChallengeResult());
            }
            var forumTopic = await _forumService.GetTopicById(forumPost.TopicId);

            if (forumTopic == null)
            {
                return(RedirectToRoute("Boards"));
            }
            var forum = await _forumService.GetForumById(forumTopic.ForumId);

            if (forum == null)
            {
                return(RedirectToRoute("Boards"));
            }

            var model = new EditForumPostModel
            {
                Id                           = forumPost.Id,
                ForumTopicId                 = forumTopic.Id,
                IsEdit                       = true,
                ForumEditor                  = _forumSettings.ForumEditor,
                ForumName                    = forum.Name,
                ForumTopicSubject            = forumTopic.Subject,
                ForumTopicSeName             = forumTopic.GetSeName(),
                IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer),
                Subscribed                   = false,
                Text                         = forumPost.Text,
            };

            //subscription
            if (model.IsCustomerAllowedToSubscribe)
            {
                var forumSubscription = (await _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                 "", forumTopic.Id, 0, 1)).FirstOrDefault();
                model.Subscribed = forumSubscription != null;
            }

            return(View(model));
        }
        public void ShouldHaveErrorWhenTextIsNullOrEmpty()
        {
            var model = new EditForumPostModel
            {
                Text = null
            };

            _validator.ShouldHaveValidationErrorFor(x => x.Text, model);
            model.Text = string.Empty;
            _validator.ShouldHaveValidationErrorFor(x => x.Text, model);
        }
示例#6
0
        public virtual async Task <EditForumPostModel> PrepareEditForumPost(Forum forum, ForumTopic forumTopic, string quote)
        {
            var model = new EditForumPostModel
            {
                Id                           = "",
                ForumTopicId                 = forumTopic.Id,
                IsEdit                       = false,
                ForumEditor                  = _forumSettings.ForumEditor,
                ForumName                    = forum.Name,
                ForumTopicSubject            = forumTopic.Subject,
                ForumTopicSeName             = forumTopic.GetSeName(),
                IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer),
                Subscribed                   = false,
            };

            //subscription
            if (model.IsCustomerAllowedToSubscribe)
            {
                var forumSubscription = (await _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                 "", forumTopic.Id, 0, 1)).FirstOrDefault();
                model.Subscribed = forumSubscription != null;
            }

            // Insert the quoted text
            string text = string.Empty;

            if (!String.IsNullOrEmpty(quote))
            {
                var quotePost = await _forumService.GetPostById(quote);

                if (quotePost != null && quotePost.TopicId == forumTopic.Id)
                {
                    var quotePostText = quotePost.Text;
                    var customer      = await _serviceProvider.GetRequiredService <ICustomerService>().GetCustomerById(quotePost.CustomerId);

                    switch (_forumSettings.ForumEditor)
                    {
                    case EditorType.SimpleTextBox:
                        text = String.Format("{0}:\n{1}\n", customer.FormatUserName(_customerSettings.CustomerNameFormat), quotePostText);
                        break;

                    case EditorType.BBCodeEditor:
                        text = String.Format("[quote={0}]{1}[/quote]", customer.FormatUserName(_customerSettings.CustomerNameFormat), BBCodeHelper.RemoveQuotes(quotePostText));
                        break;
                    }
                    model.Text = text;
                }
            }
            return(model);
        }
        /// <summary>
        /// Prepare the forum post edit model
        /// </summary>
        /// <param name="forumPost">Forum post</param>
        /// <param name="excludeProperties">Whether to exclude populating of model properties from the entity</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the edit forum post model
        /// </returns>
        public virtual async Task <EditForumPostModel> PreparePostEditModelAsync(ForumPost forumPost, bool excludeProperties)
        {
            if (forumPost == null)
            {
                throw new ArgumentNullException(nameof(forumPost));
            }

            var forumTopic = await _forumService.GetTopicByIdAsync(forumPost.TopicId);

            if (forumTopic == null)
            {
                throw new ArgumentException("forum topic cannot be loaded");
            }

            var forum = await _forumService.GetForumByIdAsync(forumTopic.ForumId);

            if (forum == null)
            {
                throw new ArgumentException("forum cannot be loaded");
            }

            var customer = await _workContext.GetCurrentCustomerAsync();

            var model = new EditForumPostModel
            {
                Id                           = forumPost.Id,
                ForumTopicId                 = forumTopic.Id,
                IsEdit                       = true,
                ForumEditor                  = _forumSettings.ForumEditor,
                ForumName                    = forum.Name,
                ForumTopicSubject            = forumTopic.Subject,
                ForumTopicSeName             = await _forumService.GetTopicSeNameAsync(forumTopic),
                IsCustomerAllowedToSubscribe = await _forumService.IsCustomerAllowedToSubscribeAsync(customer),
                DisplayCaptcha               = _captchaSettings.Enabled && _captchaSettings.ShowOnForum
            };

            if (!excludeProperties)
            {
                model.Text = forumPost.Text;
                //subscription
                if (model.IsCustomerAllowedToSubscribe)
                {
                    var forumSubscription = (await _forumService.GetAllSubscriptionsAsync(customer.Id, 0, forumTopic.Id, 0, 1)).FirstOrDefault();
                    model.Subscribed = forumSubscription != null;
                }
            }

            return(model);
        }
示例#8
0
        /// <summary>
        /// Prepare the forum post edit model
        /// </summary>
        /// <param name="forumPost">Forum post</param>
        /// <param name="excludeProperties">Whether to exclude populating of model properties from the entity</param>
        /// <returns>Edit forum post model</returns>
        public virtual EditForumPostModel PreparePostEditModel(ForumPost forumPost, bool excludeProperties)
        {
            if (forumPost == null)
            {
                throw new ArgumentNullException(nameof(forumPost));
            }

            var forumTopic = forumPost.ForumTopic;

            if (forumTopic == null)
            {
                throw new ArgumentException("forum topic cannot be loaded");
            }

            var forum = forumTopic.Forum;

            if (forum == null)
            {
                throw new ArgumentException("forum cannot be loaded");
            }

            var model = new EditForumPostModel
            {
                Id                           = forumPost.Id,
                ForumTopicId                 = forumTopic.Id,
                IsEdit                       = true,
                ForumEditor                  = _forumSettings.ForumEditor,
                ForumName                    = forum.Name,
                ForumTopicSubject            = forumTopic.Subject,
                ForumTopicSeName             = forumTopic.GetSeName(),
                IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer)
            };

            if (!excludeProperties)
            {
                model.Text = forumPost.Text;
                //subscription
                if (model.IsCustomerAllowedToSubscribe)
                {
                    var forumSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id, 0, forumTopic.Id, 0, 1).FirstOrDefault();
                    model.Subscribed = forumSubscription != null;
                }
            }

            return(model);
        }
        public virtual async Task <IActionResult> PostCreate(EditForumPostModel model, [FromServices] ICustomerService customerService)
        {
            if (!_forumSettings.ForumsEnabled)
            {
                return(RedirectToRoute("HomePage"));
            }

            var forumTopic = await _forumService.GetTopicById(model.ForumTopicId);

            if (forumTopic == null)
            {
                return(RedirectToRoute("Boards"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (!_forumService.IsCustomerAllowedToCreatePost(_workContext.CurrentCustomer, forumTopic))
                    {
                        return(new ChallengeResult());
                    }

                    var text          = model.Text;
                    var maxPostLength = _forumSettings.PostMaxLength;
                    if (maxPostLength > 0 && text.Length > maxPostLength)
                    {
                        text = text.Substring(0, maxPostLength);
                    }

                    string ipAddress = _webHelper.GetCurrentIpAddress();

                    DateTime nowUtc = DateTime.UtcNow;

                    var forumPost = new ForumPost {
                        TopicId      = forumTopic.Id,
                        ForumId      = forumTopic.ForumId,
                        ForumGroupId = forumTopic.ForumGroupId,
                        CustomerId   = _workContext.CurrentCustomer.Id,
                        Text         = text,
                        IPAddress    = ipAddress,
                        CreatedOnUtc = nowUtc,
                        UpdatedOnUtc = nowUtc
                    };
                    await _forumService.InsertPost(forumPost, true);

                    if (!_workContext.CurrentCustomer.HasContributions)
                    {
                        await customerService.UpdateContributions(_workContext.CurrentCustomer);
                    }

                    //subscription
                    if (_forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer))
                    {
                        var forumSubscription = (await _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                         "", forumPost.TopicId, 0, 1)).FirstOrDefault();
                        if (model.Subscribed)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = new ForumSubscription {
                                    SubscriptionGuid = Guid.NewGuid(),
                                    CustomerId       = _workContext.CurrentCustomer.Id,
                                    TopicId          = forumPost.TopicId,
                                    CreatedOnUtc     = nowUtc
                                };

                                await _forumService.InsertSubscription(forumSubscription);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                await _forumService.DeleteSubscription(forumSubscription);
                            }
                        }
                    }

                    int pageSize = _forumSettings.PostsPageSize > 0 ? _forumSettings.PostsPageSize : 10;

                    int pageIndex   = (await _forumService.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.Id) + 1);
                    var url         = string.Empty;
                    var _forumTopic = await _forumService.GetTopicById(forumPost.TopicId);

                    if (pageIndex > 1)
                    {
                        url = Url.RouteUrl("TopicSlugPaged", new { id = forumPost.TopicId, slug = _forumTopic.GetSeName(), pageNumber = pageIndex });
                    }
                    else
                    {
                        url = Url.RouteUrl("TopicSlug", new { id = forumPost.TopicId, slug = _forumTopic.GetSeName() });
                    }
                    return(Redirect($"{url}#{forumPost.Id}"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            // redisplay form
            var forum = await _forumService.GetForumById(forumTopic.ForumId);

            if (forum == null)
            {
                return(RedirectToRoute("Boards"));
            }

            model.IsEdit            = false;
            model.ForumName         = forum.Name;
            model.ForumTopicId      = forumTopic.Id;
            model.ForumTopicSubject = forumTopic.Subject;
            model.ForumTopicSeName  = forumTopic.GetSeName();
            model.Id = "";
            model.IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer);
            model.ForumEditor = _forumSettings.ForumEditor;

            return(View(model));
        }
        public virtual async Task <IActionResult> PostEdit(EditForumPostModel model)
        {
            if (!_forumSettings.ForumsEnabled)
            {
                return(RedirectToRoute("HomePage"));
            }

            var forumPost = await _forumService.GetPostById(model.Id);

            if (forumPost == null)
            {
                return(RedirectToRoute("Boards"));
            }

            if (!_forumService.IsCustomerAllowedToEditPost(_workContext.CurrentCustomer, forumPost))
            {
                return(new ChallengeResult());
            }

            var forumTopic = await _forumService.GetTopicById(forumPost.TopicId);

            if (forumTopic == null)
            {
                return(RedirectToRoute("Boards"));
            }

            var forum = await _forumService.GetForumById(forumTopic.ForumId);

            if (forum == null)
            {
                return(RedirectToRoute("Boards"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    DateTime nowUtc = DateTime.UtcNow;

                    var text          = model.Text;
                    var maxPostLength = _forumSettings.PostMaxLength;
                    if (maxPostLength > 0 && text.Length > maxPostLength)
                    {
                        text = text.Substring(0, maxPostLength);
                    }

                    forumPost.UpdatedOnUtc = nowUtc;
                    forumPost.Text         = text;
                    await _forumService.UpdatePost(forumPost);

                    //subscription
                    if (_forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer))
                    {
                        var forumSubscription = (await _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                         "", forumPost.TopicId, 0, 1)).FirstOrDefault();
                        if (model.Subscribed)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = new ForumSubscription {
                                    SubscriptionGuid = Guid.NewGuid(),
                                    CustomerId       = _workContext.CurrentCustomer.Id,
                                    TopicId          = forumPost.TopicId,
                                    CreatedOnUtc     = nowUtc
                                };
                                await _forumService.InsertSubscription(forumSubscription);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                await _forumService.DeleteSubscription(forumSubscription);
                            }
                        }
                    }

                    int pageSize   = _forumSettings.PostsPageSize > 0 ? _forumSettings.PostsPageSize : 10;
                    int pageIndex  = (await _forumService.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.Id) + 1);
                    var url        = string.Empty;
                    var forumtopic = await _forumService.GetTopicById(forumPost.TopicId);

                    if (pageIndex > 1)
                    {
                        url = Url.RouteUrl("TopicSlugPaged", new { id = forumPost.TopicId, slug = forumtopic.GetSeName(), page = pageIndex });
                    }
                    else
                    {
                        url = Url.RouteUrl("TopicSlug", new { id = forumPost.TopicId, slug = forumtopic.GetSeName() });
                    }
                    return(Redirect(string.Format("{0}#{1}", url, forumPost.Id)));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            //redisplay form
            model.IsEdit            = true;
            model.ForumName         = forum.Name;
            model.ForumTopicId      = forumTopic.Id;
            model.ForumTopicSubject = forumTopic.Subject;
            model.ForumTopicSeName  = forumTopic.GetSeName();
            model.Id = forumPost.Id;
            model.IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer);
            model.ForumEditor = _forumSettings.ForumEditor;

            return(View(model));
        }
示例#11
0
        public virtual IActionResult PostEdit(EditForumPostModel model)
        {
            if (!_forumSettings.ForumsEnabled)
            {
                return(RedirectToRoute("HomePage"));
            }

            var forumPost = _forumService.GetPostById(model.Id);

            if (forumPost == null)
            {
                return(RedirectToRoute("Boards"));
            }

            if (!_forumService.IsCustomerAllowedToEditPost(_workContext.CurrentCustomer, forumPost))
            {
                return(Challenge());
            }

            var forumTopic = forumPost.ForumTopic;

            if (forumTopic == null)
            {
                return(RedirectToRoute("Boards"));
            }

            var forum = forumTopic.Forum;

            if (forum == null)
            {
                return(RedirectToRoute("Boards"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var nowUtc = DateTime.UtcNow;

                    var text          = model.Text;
                    var maxPostLength = _forumSettings.PostMaxLength;
                    if (maxPostLength > 0 && text.Length > maxPostLength)
                    {
                        text = text.Substring(0, maxPostLength);
                    }

                    forumPost.UpdatedOnUtc = nowUtc;
                    forumPost.Text         = text;
                    _forumService.UpdatePost(forumPost);

                    //subscription
                    if (_forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer))
                    {
                        var forumSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                  0, forumPost.TopicId, 0, 1).FirstOrDefault();
                        if (model.Subscribed)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = new ForumSubscription
                                {
                                    SubscriptionGuid = Guid.NewGuid(),
                                    CustomerId       = _workContext.CurrentCustomer.Id,
                                    TopicId          = forumPost.TopicId,
                                    CreatedOnUtc     = nowUtc
                                };
                                _forumService.InsertSubscription(forumSubscription);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                _forumService.DeleteSubscription(forumSubscription);
                            }
                        }
                    }

                    var pageSize  = _forumSettings.PostsPageSize > 0 ? _forumSettings.PostsPageSize : 10;
                    var pageIndex = (_forumService.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.Id) + 1);
                    var url       = string.Empty;
                    if (pageIndex > 1)
                    {
                        url = Url.RouteUrl("TopicSlugPaged", new { id = forumPost.TopicId, slug = _forumService.GetTopicSeName(forumPost.ForumTopic), pageNumber = pageIndex });
                    }
                    else
                    {
                        url = Url.RouteUrl("TopicSlug", new { id = forumPost.TopicId, slug = _forumService.GetTopicSeName(forumPost.ForumTopic) });
                    }
                    return(Redirect($"{url}#{forumPost.Id}"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            //redisplay form
            _forumModelFactory.PreparePostEditModel(forumPost, true);
            return(View(model));
        }
示例#12
0
        /// <summary>
        /// Prepare the forum post create model
        /// </summary>
        /// <param name="forumTopic">Forum topic</param>
        /// <param name="quote">Identifier of the quoted post; pass null to load the empty text</param>
        /// <param name="excludeProperties">Whether to exclude populating of model properties from the entity</param>
        /// <returns>Edit forum post model</returns>
        public virtual EditForumPostModel PreparePostCreateModel(ForumTopic forumTopic, int?quote, bool excludeProperties)
        {
            if (forumTopic == null)
            {
                throw new ArgumentNullException(nameof(forumTopic));
            }

            var forum = forumTopic.Forum;

            if (forum == null)
            {
                throw new ArgumentException("forum cannot be loaded");
            }

            var model = new EditForumPostModel
            {
                ForumTopicId                 = forumTopic.Id,
                IsEdit                       = false,
                ForumEditor                  = _forumSettings.ForumEditor,
                ForumName                    = forum.Name,
                ForumTopicSubject            = forumTopic.Subject,
                ForumTopicSeName             = forumTopic.GetSeName(),
                IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer)
            };

            if (!excludeProperties)
            {
                //subscription
                if (model.IsCustomerAllowedToSubscribe)
                {
                    var forumSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                              0, forumTopic.Id, 0, 1).FirstOrDefault();
                    model.Subscribed = forumSubscription != null;
                }

                // Insert the quoted text
                var text = string.Empty;
                if (quote.HasValue)
                {
                    var quotePost = _forumService.GetPostById(quote.Value);
                    if (quotePost != null && quotePost.TopicId == forumTopic.Id)
                    {
                        var quotePostText = quotePost.Text;

                        switch (_forumSettings.ForumEditor)
                        {
                        case EditorType.SimpleTextBox:
                            text = $"{quotePost.Customer.FormatUserName()}:\n{quotePostText}\n";
                            break;

                        case EditorType.BBCodeEditor:
                            text = $"[quote={quotePost.Customer.FormatUserName()}]{BBCodeHelper.RemoveQuotes(quotePostText)}[/quote]";
                            break;
                        }
                        model.Text = text;
                    }
                }
            }

            return(model);
        }
        public ActionResult PostCreate(EditForumPostModel model)
        {
            if (!_forumSettings.ForumsEnabled)
            {
                return(RedirectToRoute("HomePage"));
            }

            var forumTopic = _forumService.GetTopicById(model.ForumTopicId);

            if (forumTopic == null)
            {
                return(RedirectToRoute("Boards"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (!_forumService.IsCustomerAllowedToCreatePost(_workContext.CurrentCustomer, forumTopic))
                    {
                        return(new HttpUnauthorizedResult());
                    }

                    var text          = model.Text;
                    var maxPostLength = _forumSettings.PostMaxLength;
                    if (maxPostLength > 0 && text.Length > maxPostLength)
                    {
                        text = text.Substring(0, maxPostLength);
                    }

                    string ipAddress = _webHelper.GetCurrentIpAddress();

                    DateTime nowUtc = DateTime.UtcNow;

                    var forumPost = new ForumPost
                    {
                        TopicId      = forumTopic.Id,
                        CustomerId   = _workContext.CurrentCustomer.Id,
                        Text         = text,
                        IPAddress    = ipAddress,
                        CreatedOnUtc = nowUtc,
                        UpdatedOnUtc = nowUtc
                    };
                    _forumService.InsertPost(forumPost, true);

                    //subscription
                    if (_forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer))
                    {
                        var forumSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                  0, forumPost.TopicId, 0, 1).FirstOrDefault();
                        if (model.Subscribed)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = new ForumSubscription
                                {
                                    SubscriptionGuid = Guid.NewGuid(),
                                    CustomerId       = _workContext.CurrentCustomer.Id,
                                    TopicId          = forumPost.TopicId,
                                    CreatedOnUtc     = nowUtc
                                };

                                _forumService.InsertSubscription(forumSubscription);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                _forumService.DeleteSubscription(forumSubscription);
                            }
                        }
                    }

                    int pageSize = _forumSettings.PostsPageSize > 0 ? _forumSettings.PostsPageSize : 10;

                    int pageIndex = (_forumService.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.Id) + 1);
                    var url       = string.Empty;
                    if (pageIndex > 1)
                    {
                        url = Url.RouteUrl("TopicSlugPaged", new { id = forumPost.TopicId, slug = forumPost.ForumTopic.GetSeName(), page = pageIndex });
                    }
                    else
                    {
                        url = Url.RouteUrl("TopicSlug", new { id = forumPost.TopicId, slug = forumPost.ForumTopic.GetSeName() });
                    }
                    return(Redirect(string.Format("{0}#{1}", url, forumPost.Id)));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            //redisplay form
            _forumModelFactory.PreparePostCreateModel(forumTopic, 0, true);
            return(View(model));
        }
示例#14
0
        /// <summary>
        /// Prepare the forum post create model
        /// </summary>
        /// <param name="forumTopic">Forum topic</param>
        /// <param name="quote">Identifier of the quoted post; pass null to load the empty text</param>
        /// <param name="excludeProperties">Whether to exclude populating of model properties from the entity</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the edit forum post model
        /// </returns>
        public virtual async Task <EditForumPostModel> PreparePostCreateModelAsync(ForumTopic forumTopic, int?quote, bool excludeProperties)
        {
            if (forumTopic == null)
            {
                throw new ArgumentNullException(nameof(forumTopic));
            }

            var forum = await _forumService.GetForumByIdAsync(forumTopic.ForumId);

            if (forum == null)
            {
                throw new ArgumentException("forum cannot be loaded");
            }

            var model = new EditForumPostModel
            {
                ForumTopicId                 = forumTopic.Id,
                IsEdit                       = false,
                ForumEditor                  = _forumSettings.ForumEditor,
                ForumName                    = forum.Name,
                ForumTopicSubject            = forumTopic.Subject,
                ForumTopicSeName             = await _forumService.GetTopicSeNameAsync(forumTopic),
                IsCustomerAllowedToSubscribe = await _forumService.IsCustomerAllowedToSubscribeAsync(await _workContext.GetCurrentCustomerAsync()),
                DisplayCaptcha               = _captchaSettings.Enabled && _captchaSettings.ShowOnForum
            };

            if (!excludeProperties)
            {
                //subscription
                if (model.IsCustomerAllowedToSubscribe)
                {
                    var forumSubscription = (await _forumService.GetAllSubscriptionsAsync((await _workContext.GetCurrentCustomerAsync()).Id,
                                                                                          0, forumTopic.Id, 0, 1)).FirstOrDefault();
                    model.Subscribed = forumSubscription != null;
                }

                // Insert the quoted text
                var text = string.Empty;
                if (quote.HasValue)
                {
                    var quotePost = await _forumService.GetPostByIdAsync(quote.Value);

                    if (quotePost != null && quotePost.TopicId == forumTopic.Id)
                    {
                        var customer = await _customerService.GetCustomerByIdAsync(quotePost.CustomerId);

                        var quotePostText = quotePost.Text;

                        switch (_forumSettings.ForumEditor)
                        {
                        case EditorType.SimpleTextBox:
                            text = $"{await _customerService.FormatUsernameAsync(customer)}:\n{quotePostText}\n";
                            break;

                        case EditorType.BBCodeEditor:
                            text = $"[quote={await _customerService.FormatUsernameAsync(customer)}]{BBCodeHelper.RemoveQuotes(quotePostText)}[/quote]";
                            break;
                        }
                        model.Text = text;
                    }
                }
            }

            return(model);
        }
示例#15
0
        public virtual IActionResult PostCreate(EditForumPostModel model, bool captchaValid)
        {
            if (!_forumSettings.ForumsEnabled)
            {
                return(RedirectToRoute("Homepage"));
            }

            var forumTopic = _forumService.GetTopicById(model.ForumTopicId);

            if (forumTopic == null)
            {
                return(RedirectToRoute("Boards"));
            }

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnForum && !captchaValid)
            {
                ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptchaMessage"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (!_forumService.IsCustomerAllowedToCreatePost(_workContext.CurrentCustomer, forumTopic))
                    {
                        return(Challenge());
                    }

                    var text          = model.Text;
                    var maxPostLength = _forumSettings.PostMaxLength;
                    if (maxPostLength > 0 && text.Length > maxPostLength)
                    {
                        text = text.Substring(0, maxPostLength);
                    }

                    var ipAddress = _webHelper.GetCurrentIpAddress();

                    var nowUtc = DateTime.UtcNow;

                    var forumPost = new ForumPost
                    {
                        TopicId      = forumTopic.Id,
                        CustomerId   = _workContext.CurrentCustomer.Id,
                        Text         = text,
                        IPAddress    = ipAddress,
                        CreatedOnUtc = nowUtc,
                        UpdatedOnUtc = nowUtc
                    };
                    _forumService.InsertPost(forumPost, true);

                    //subscription
                    if (_forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer))
                    {
                        var forumSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id,
                                                                                  0, forumPost.TopicId, 0, 1).FirstOrDefault();
                        if (model.Subscribed)
                        {
                            if (forumSubscription == null)
                            {
                                forumSubscription = new ForumSubscription
                                {
                                    SubscriptionGuid = Guid.NewGuid(),
                                    CustomerId       = _workContext.CurrentCustomer.Id,
                                    TopicId          = forumPost.TopicId,
                                    CreatedOnUtc     = nowUtc
                                };

                                _forumService.InsertSubscription(forumSubscription);
                            }
                        }
                        else
                        {
                            if (forumSubscription != null)
                            {
                                _forumService.DeleteSubscription(forumSubscription);
                            }
                        }
                    }

                    var pageSize = _forumSettings.PostsPageSize > 0 ? _forumSettings.PostsPageSize : 10;

                    var pageIndex = (_forumService.CalculateTopicPageIndex(forumPost.TopicId, pageSize, forumPost.Id) + 1);
                    var url       = string.Empty;
                    if (pageIndex > 1)
                    {
                        url = Url.RouteUrl("TopicSlugPaged", new { id = forumPost.TopicId, slug = _forumService.GetTopicSeName(forumTopic), pageNumber = pageIndex });
                    }
                    else
                    {
                        url = Url.RouteUrl("TopicSlug", new { id = forumPost.TopicId, slug = _forumService.GetTopicSeName(forumTopic) });
                    }
                    return(Redirect($"{url}#{forumPost.Id}"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            //redisplay form
            model = _forumModelFactory.PreparePostCreateModel(forumTopic, 0, true);

            return(View(model));
        }