Exemple #1
0
        public override async Task <IViewProviderResult> BuildUpdateAsync(Reply reply, IViewProviderContext context)
        {
            if (reply.IsNewReply)
            {
                return(default(IViewProviderResult));
            }

            // Ensure the reply exists
            if (await _replyStore.GetByIdAsync(reply.Id) == null)
            {
                return(await BuildIndexAsync(reply, context));
            }

            // Validate model
            if (await ValidateModelAsync(reply, context.Updater))
            {
                // Update reply
                var result = await _replyManager.UpdateAsync(reply);

                // Was there a problem updating the reply?
                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        context.Updater.ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            return(await BuildEditAsync(reply, context));
        }
        async Task <TEntityReply> EntityReplyUpdating(TEntityReply reply)
        {
            // Get previous history points
            var previousHistories = await _entityHistoryStore.QueryAsync()
                                    .Take(1)
                                    .Select <EntityHistoryQueryParams>(q =>
            {
                q.EntityId.Equals(reply.EntityId);
                q.EntityReplyId.Equals(reply.Id);
            })
                                    .OrderBy("CreatedDate", OrderBy.Desc)
                                    .ToList();

            // Get the most recently added history point
            EntityHistory previousHistory = null;

            if (previousHistories?.Data != null)
            {
                previousHistory = previousHistories.Data[0];
            }

            // If we have previous history we don't need to add a starting point
            if (previousHistory != null)
            {
                return(reply);
            }

            // Get existing reply before any changes
            var existingReply = await _entityReplyStore.GetByIdAsync(reply.Id);

            // We need an existing reply
            if (existingReply == null)
            {
                return(reply);
            }

            // If we don't have any existing history points add our
            // existing reply (before updates) as the starting / original history point
            await _entityHistoryManager.CreateAsync(new EntityHistory()
            {
                EntityId      = existingReply.EntityId,
                EntityReplyId = existingReply.Id,
                Message       = existingReply.Message,
                Html          = existingReply.Html,
                CreatedUserId = existingReply.CreatedUserId,
                CreatedDate   = existingReply.CreatedDate
            });

            return(reply);
        }
Exemple #3
0
        async Task <AggregateRating> UpdateEntityReplyRating(int replyId)
        {
            // Get reply
            var reply = await _entityReplyStore.GetByIdAsync(replyId);

            // Ensure we found the reply
            if (reply == null)
            {
                return(null);
            }

            // Aggregate ratings
            var updatedReply = await _entityReplyRatingsAggregator.UpdateAsync(reply);

            // Return aggregated results
            return(new AggregateRating()
            {
                TotalRatings = updatedReply?.TotalRatings ?? 0,
                SummedRating = updatedReply?.SummedRating ?? 0,
                MeanRating = updatedReply?.MeanRating ?? 0
            });
        }
Exemple #4
0
        public override async Task <IViewProviderResult> BuildUpdateAsync(Comment comment, IViewProviderContext context)
        {
            // Ensure entity reply exists before attempting to update
            var entity = await _replyStore.GetByIdAsync(comment.Id);

            if (entity == null)
            {
                return(await BuildIndexAsync(comment, context));
            }

            // Validate model
            if (await ValidateModelAsync(comment, context.Updater))
            {
                // Get selected tags
                var tagsToAdd = await GetTagsToAddAsync();

                // Build tags to remove
                var tagsToRemove = new List <EntityTag>();

                // Iterate over existing tags
                var existingTags = await GetEntityTagsByEntityReplyIdAsync(comment.Id);

                if (existingTags != null)
                {
                    foreach (var entityTag in existingTags)
                    {
                        // Is our existing tag in our list of tags to add
                        var existingTag = tagsToAdd.FirstOrDefault(t => t.Id == entityTag.TagId);
                        if (existingTag != null)
                        {
                            tagsToAdd.Remove(existingTag);
                        }
                        else
                        {
                            // Entry no longer exist in tags so ensure it's removed
                            tagsToRemove.Add(entityTag);
                        }
                    }
                }

                // Remove entity tags
                foreach (var entityTag in tagsToRemove)
                {
                    var result = await _entityTagManager.DeleteAsync(entityTag);

                    if (!result.Succeeded)
                    {
                        foreach (var error in result.Errors)
                        {
                            context.Updater.ModelState.AddModelError(string.Empty, error.Description);
                        }
                    }
                }

                // Get authenticated user
                var user = await _contextFacade.GetAuthenticatedUserAsync();

                // Add new entity labels
                foreach (var tag in tagsToAdd)
                {
                    var result = await _entityTagManager.CreateAsync(new EntityTag()
                    {
                        EntityId      = comment.EntityId,
                        EntityReplyId = comment.Id,
                        TagId         = tag.Id,
                        CreatedUserId = user?.Id ?? 0,
                        CreatedDate   = DateTime.UtcNow
                    });

                    if (!result.Succeeded)
                    {
                        foreach (var error in result.Errors)
                        {
                            context.Updater.ModelState.AddModelError(string.Empty, error.Description);
                        }
                    }
                }
            }

            return(await BuildEditAsync(comment, context));
        }
Exemple #5
0
        // -----------------
        // Index
        // Displays a summary from StopForumSpam.
        // -----------------

        public async Task <IActionResult> Index(EntityOptions opts)
        {
            if (opts == null)
            {
                opts = new EntityOptions();
            }

            // We always need an entity Id
            if (opts.Id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(opts.Id));
            }

            // We always need an entity
            var entity = await _entityStore.GetByIdAsync(opts.Id);

            if (entity == null)
            {
                return(NotFound());
            }

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User, entity.CategoryId, Permissions.ViewStopForumSpam))
            {
                return(Unauthorized());
            }

            // Get reply
            IEntityReply reply = null;

            if (opts.ReplyId > 0)
            {
                reply = await _entityReplyStore.GetByIdAsync(opts.ReplyId);

                if (reply == null)
                {
                    return(NotFound());
                }
            }

            // Get user to validate
            var user = reply != null
                ? await GetUserToValidateAsync(reply)
                : await GetUserToValidateAsync(entity);

            // Ensure we found the user
            if (user == null)
            {
                return(NotFound());
            }

            // Build view model
            var viewModel = new StopForumSpamViewModel()
            {
                Options = opts,
                Checker = await _spamChecker.CheckAsync(user)
            };

            // Return view
            return(View(viewModel));
        }
Exemple #6
0
        public async Task <IActionResult> ToAnswer(string id)
        {
            // Get authenticated user
            var user = await _contextFacade.GetAuthenticatedUserAsync();

            // We need to be authenticated
            if (user == null)
            {
                return(Unauthorized());
            }

            // Ensure we have a valid id
            var ok = int.TryParse(id, out var replyId);

            if (!ok)
            {
                return(NotFound());
            }

            var reply = await _entityReplyStore.GetByIdAsync(replyId);

            if (reply == null)
            {
                return(NotFound());
            }

            var entity = await _entityStore.GetByIdAsync(reply.EntityId);

            // Ensure the entity exists
            if (entity == null)
            {
                return(NotFound());
            }

            // Get permission
            var permission = entity.CreatedUserId == user.Id
                ? Permissions.MarkOwnRepliesAnswer
                : Permissions.MarkAnyReplyAnswer;

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User, entity.CategoryId, permission))
            {
                return(Unauthorized());
            }

            // Update reply
            reply.ModifiedUserId = user?.Id ?? 0;
            reply.ModifiedDate   = DateTimeOffset.UtcNow;
            reply.IsAnswer       = true;

            // Save changes and return results
            var result = await _replyManager.UpdateAsync(reply);

            if (result.Succeeded)
            {
                await UpdateEntityAsync(entity);

                _alerter.Success(T["Marked As Answer Successfully"]);
            }
            else
            {
                _alerter.Danger(T["Could not mark the reply as an answer"]);
            }

            // Redirect back to reply
            return(Redirect(_contextFacade.GetRouteUrl(new RouteValueDictionary()
            {
                ["area"] = "Plato.Questions",
                ["controller"] = "Home",
                ["action"] = "Reply",
                ["opts.id"] = entity.Id,
                ["opts.alias"] = entity.Alias,
                ["opts.replyId"] = reply.Id
            })));
        }
Exemple #7
0
        public async Task <IActionResult> Rollback(int id)
        {
            // Validate
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }

            // Get history point
            var history = await _entityHistoryStore.GetByIdAsync(id);

            // Ensure we found the history point
            if (history == null)
            {
                return(NotFound());
            }

            // Get entity for history point
            var entity = await _entityStore.GetByIdAsync(history.EntityId);

            // Ensure we found the entity
            if (entity == null)
            {
                return(NotFound());
            }

            // Get reply
            IdeaComment reply = null;

            if (history.EntityReplyId > 0)
            {
                reply = await _entityReplyStore.GetByIdAsync(history.EntityReplyId);

                // Ensure we found a reply if supplied
                if (reply == null)
                {
                    return(NotFound());
                }
            }

            // Get current user
            var user = await _contextFacade.GetAuthenticatedUserAsync();

            // We always need to be logged in to edit entities
            if (user == null)
            {
                return(Unauthorized());
            }

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(HttpContext.User,
                                                            entity.CategoryId, reply != null
                    ? Permissions.RevertReplyHistory
                    : Permissions.RevertEntityHistory))
            {
                return(Unauthorized());
            }

            ICommandResultBase result;

            if (reply != null)
            {
                // Only update edited information if the message changes
                if (history.Message != reply.Message)
                {
                    reply.Message      = history.Message;
                    reply.EditedUserId = user?.Id ?? 0;
                    reply.EditedDate   = DateTimeOffset.UtcNow;
                }

                // Update reply to history point
                result = await _entityReplyManager.UpdateAsync(reply);
            }
            else
            {
                // Only update edited information if the message changes
                if (history.Message != entity.Message)
                {
                    entity.Message      = history.Message;
                    entity.EditedUserId = user?.Id ?? 0;
                    entity.EditedDate   = DateTimeOffset.UtcNow;
                }

                // Update entity to history point
                result = await _entityManager.UpdateAsync(entity);
            }

            // Add result
            if (result.Succeeded)
            {
                _alerter.Success(T["Version Rolled Back Successfully!"]);
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    _alerter.Danger(T[error.Description]);
                }
            }

            // Redirect
            return(Redirect(_contextFacade.GetRouteUrl(new RouteValueDictionary()
            {
                ["area"] = "Plato.Ideas",
                ["controller"] = "Home",
                ["action"] = "Reply",
                ["opts.id"] = entity.Id,
                ["opts.alias"] = entity.Alias,
                ["opts.replyId"] = reply?.Id ?? 0
            })));
        }
Exemple #8
0
        public async Task <IActionResult> HideReply(string id)
        {
            // Ensure we have a valid id
            var ok = int.TryParse(id, out var replyId);

            if (!ok)
            {
                return(NotFound());
            }

            var reply = await _entityReplyStore.GetByIdAsync(replyId);

            if (reply == null)
            {
                return(NotFound());
            }

            var topic = await _entityStore.GetByIdAsync(reply.EntityId);

            // Ensure the topic exists
            if (topic == null)
            {
                return(NotFound());
            }

            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User, topic.CategoryId, ModeratorPermissions.HideReplies))
            {
                return(Unauthorized());
            }

            var user = await _contextFacade.GetAuthenticatedUserAsync();

            // Update topic
            reply.ModifiedUserId = user?.Id ?? 0;
            reply.ModifiedDate   = DateTimeOffset.UtcNow;
            reply.IsHidden       = true;

            // Save changes and return results
            var result = await _replyManager.UpdateAsync(reply);

            if (result.Succeeded)
            {
                _alerter.Success(T["Reply Hidden Successfully"]);
            }
            else
            {
                _alerter.Danger(T["Could not hide the reply"]);
            }

            // Redirect back to reply
            return(Redirect(_contextFacade.GetRouteUrl(new RouteValueDictionary()
            {
                ["area"] = "Plato.Discuss",
                ["controller"] = "Home",
                ["action"] = "Reply",
                ["opts.id"] = topic.Id,
                ["opts.alias"] = topic.Alias,
                ["opts.replyId"] = reply.Id
            })));
        }