public async Task <IActionResult> Delete(int id) { var redirectPath = ForumViewResult.GetReferrer(this); if (ModelState.IsValid) { var message = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == id); if (message is null || message.Deleted) { throw new HttpNotFoundError(); } if (message.PostedById != UserContext.ApplicationUser.Id && !UserContext.IsAdmin) { throw new HttpForbiddenError(); } var topic = await DbContext.Topics.SingleAsync(m => m.Id == message.TopicId); if (topic.FirstMessageId == message.Id) { redirectPath = base.Url.Action(nameof(Topics.Delete), nameof(Controllers.Topics), new { topic.Id }); } else { await MessageRepository.DeleteMessageFromTopic(message, topic); await TopicRepository.RebuildTopicReplies(topic); await DbContext.SaveChangesAsync(); redirectPath = Url.Action(nameof(Topics.Latest), nameof(Topics), new { id = topic.Id }); await ForumHub.Clients.All.SendAsync("deleted-message", new HubModels.Message { TopicId = topic.Id, MessageId = message.Id }); } } return(Redirect(redirectPath)); }