public async Task <CanReplyResult> CreateReplyCheckAsync(CreateReplyModel replyModel)
        {
            if (string.IsNullOrEmpty(replyModel.Text))
            {
                return(CanReplyResult.TextEmpty);
            }

            var thread = await GetThreadAsync(replyModel.ThreadId);

            if (thread == null || !thread.IsVisible)
            {
                return(CanReplyResult.ThreadNotExisting);
            }
            else if (!thread.IsOpen)
            {
                return(CanReplyResult.ThreadClosed);
            }

            // ToDo: Merge all Groups where the user is a member of and check them
            if (replyModel.Author.UserGroup == null)
            {
                replyModel.Author.UserGroup = await db.UserGroups.AsNoTracking()
                                              .FirstOrDefaultAsync(g => g.Id == replyModel.Author.UserGroupId);
            }
            var  forumFlag = thread.AuthorId == replyModel.Author.Id ? VBForumFlags.CanReplyToOwnThreads : VBForumFlags.CanReplyToOtherThreads;
            bool canPost   = await forumManager.UserCanInForum(thread.ForumId, replyModel.Author.UserGroup, forumFlag);

            if (!canPost)
            {
                return(CanReplyResult.NoReplyPermission);
            }
            return(CanReplyResult.Ok);
        }
        public async Task <VBPost> CreateReplyAsync(CreateReplyModel replyModel)
        {
            // Forum is required to generate view thread url after reply is saved
            var thread = await db.Threads.Include(t => t.Forum)
                         .FirstOrDefaultAsync(t => t.Id == replyModel.ThreadId);

            var post = new VBPost(replyModel.Author, replyModel.Title, replyModel.Text, replyModel.IpAddress, thread.Id);

            var lastPost = await db.Posts.Where(p => p.ThreadId == replyModel.ThreadId)
                           .OrderByDescending(p => p.CreatedTimeRaw)
                           .FirstOrDefaultAsync();

            if (lastPost != null)
            {
                post.ParentPostId = lastPost.Id;
            }

            db.Posts.Add(post);
            await db.SaveChangesAsync();

            if (post.Id <= 0)
            {
                throw new Exception("Couldnt save post: No id generated from database!");
            }

            thread.LastPostAuthorId   = replyModel.Author.Id;
            thread.LastPostAuthorName = replyModel.Author.UserName;
            thread.LastPostId         = post.Id;
            thread.LastPostTime       = DateTime.UtcNow;
            thread.ReplysCount++;
            // ToDo: Maybe also increment author counter if user hasn't posted before there

            bool userPostedInThread = await db.Posts.AnyAsync(p => p.ThreadId == thread.Id && p.AuthorId == replyModel.Author.Id);

            if (!userPostedInThread)
            {
                thread.PosterCount++;
            }

            await userManager.IncrementPostCounterAsync(replyModel.Author.Id, post.Id, post.CreatedTime);

            await db.SaveChangesAsync();

            cache.Remove(VBCacheKey.Thread, thread.Id.ToString());
            cache.Remove(VBCacheKey.ThreadReplys, thread.Id.ToString());
            post.Thread = thread;
            return(post);
        }
        public async Task <ActionResult> Create(CreateReplyModel model)
        {
            var result = await _commandService.SendAsync(
                new CreateReplyCommand(
                    ObjectId.GenerateNewStringId(),
                    model.PostId,
                    model.ParentId,
                    model.Body,
                    _contextService.CurrentAccount.AccountId));

            if (result.Status != AsyncTaskStatus.Success)
            {
                return(Json(new { success = false, errorMsg = result.ErrorMessage }));
            }

            return(Json(new { success = true }));
        }
Exemple #4
0
        public async Task <ActionResult> Create(CreateReplyModel model)
        {
            var currentAccount = _contextService.GetCurrentAccount(HttpContext);
            var result         = await _commandService.ExecuteAsync(
                new CreateReplyCommand(
                    ObjectId.GenerateNewStringId(),
                    model.PostId,
                    model.ParentId,
                    model.Body,
                    currentAccount.AccountId), CommandReturnType.EventHandled);

            if (result.Status == CommandStatus.Failed)
            {
                return(Json(new { success = false, errorMsg = result.Result }));
            }

            return(Json(new { success = true }));
        }
Exemple #5
0
        public async Task <ActionResult> Create(CreateReplyModel model)
        {
            AsyncTaskResult <CommandResult> asyncTaskResult = await _commandService.ExecuteAsync(
                new CreateReplyCommand(
                    model.PostId,
                    model.ParentId,
                    model.Body,
                    _contextService.CurrentAccount.AccountId), CommandReturnType.EventHandled);

            var result = asyncTaskResult.Data;

            if (result.Status == CommandStatus.Failed)
            {
                return(Json(new { success = false, errorMsg = result.ErrorMessage }));
            }

            return(Json(new { success = true }));
        }