public ActionResult MarkInappropriate_POST(MarkInappropriateViewModel model)
        {
            var contentItem = _orchardServices.ContentManager.Get(model.Report.PostId);

            /*  This is what should be done so people can only manage reports on their own forums
             *  however for now, allowing anyone with ManageForum permissions to administer reports regardless of if its
             *  their forum because there is no filtering in place to show reports only for a particular user (owner)
             *  nor to give permissions to moderators to mod only particular forums. For now.. if you can mod reports or are an owner
             *  you can manage all innapropriate posts
             *
            ContentItem forumsHomePage = null;
            if (contentItem.Is<ThreadPart>())
            {
                forumsHomePage = _orchardServices.ContentManager.Get(contentItem.As<ThreadPart>().ForumsHomepageId);
            }
            else
            {
                forumsHomePage = _orchardServices.ContentManager.Get(contentItem.As<PostPart>().ThreadPart.ForumsHomepageId);
            }
            if (!_orchardServices.Authorizer.Authorize(Permissions.ModerateInappropriatePosts, forumsHomePage, T("You do not have the proper permissions to administer inappropriate posts")))
                return new HttpUnauthorizedResult();
            
            */
            if (!_orchardServices.Authorizer.Authorize(Permissions.ModerateInappropriatePosts,  T("You do not have the proper permissions to administer inappropriate posts")))
                return new HttpUnauthorizedResult();
            //see if there is an existing report
            ReportedPostRecord report = _reportPostService.GetReportsForPost(model.Report.PostId).FirstOrDefault();
            
            //if not pre-existing report exists make one
            if (report == null)
            {
                //don't know who the poster is at this point so set it after below
                report = new ReportedPostRecord
                {
                    PostId = model.Report.PostId,      
                    ReportedDate = DateTime.UtcNow,
                    ReportedByUserId = _orchardServices.WorkContext.CurrentUser.Id,
                    Note = model.Report.Note,
                    PostedByUserId = contentItem.As<CommonPart>().Owner.Id,                   

                };

                _reportPostService.CreateReport(report);
            }

            report.IsResolved = model.Report.IsResolved;
            if (report.IsResolved)
            {
                report.ResolvedByUserId = _orchardServices.WorkContext.CurrentUser.Id;
                report.ResolvedDate = DateTime.UtcNow;
            }
            report.Note = model.Report.Note;
            report.PostId = report.PostId;
            
            _reportPostService.UpdateReport(report);

            if (report == null)
            {
                _orchardServices.Notifier.Add(NotifyType.Error, T("Something went wrong. No report exists for this post."));
            }

            var thread = contentItem.As<ThreadPart>();            
            if (thread != null)
            {
                /* i think this is dead code.. the threadpart is never directly marked as inappropriate
                 * instead its corresponding post part is marked
                 */
                thread.IsInappropriate = true;
                _countersService.UpdateForumPartCounters(thread);
                _orchardServices.Notifier.Information(T("Thread has been marked as inappropriate."));                
            }

            var post = contentItem.As<PostPart>();
            if (post != null)
            {

                if (post.IsParentThread())
                {
                    post.ThreadPart.IsInappropriate = true;
                    post.IsInappropriate = true;
                }
                else
                {
                    post.IsInappropriate = true;
                }
                _countersService.UpdateThreadPartAndForumPartCounters(post);
            }

            return Redirect(model.ReturnUrl);

        }
        public ActionResult MarkInappropriate_GET(int contentId)
        {
            if (!_orchardServices.Authorizer.Authorize(Permissions.ModerateInappropriatePosts, T("You do not have the proper permissions to administer inappropriate posts")))
                return new HttpUnauthorizedResult();

            var contentItem = _orchardServices.ContentManager.Get(contentId);

            ReportedPostRecord report = _reportPostService.GetReportsForPost(contentId).FirstOrDefault();
            var model = new RemoveInappropriateFlagViewModel();

            //if no pre-existing report exists make one
            if (report == null)
            {
                //don't know who the poster is at this point so set it after below
                report = new ReportedPostRecord
                {
                    IsResolved = true,
                    PostId = contentId,               
                };
            }

            model.Report = report;
            model.ReturnUrl = Request.UrlReferrer.AbsoluteUri;

            return View(model); ;
        }
        public void  UpdateReport(ReportedPostRecord report )
        {
            
            _postReportRepository.Update(report);

        }
        public ActionResult ReportInappropriatePost_POST(ReportInappropriatePostConfirmationViewModel model)
        {

            if (!_orchardServices.WorkContext.HttpContext.User.Identity.IsAuthenticated)
                return new HttpUnauthorizedResult(T("You must be logged in to report an inappropriate post.").ToString());

            if (!_orchardServices.Authorizer.Authorize(Permissions.CreateThreadsAndPosts, T("You do not have permissions to post on the forums and therefore cannot report posts.")))
                return new HttpUnauthorizedResult();

            var userId = _orchardServices.WorkContext.CurrentUser.Id;
            var post = _postService.Get( model.PostId, VersionOptions.Published);

            if (model.ReasonReported.Length > 2048)
            {
                this.ModelState.AddModelError("ReasonReported", T("The reason cannot be longer than 2048 characters.  You entered {0} characters.", model.ReasonReported.Length).ToString());
                return View(model);
            }

            if (post != null)
            {
                var reportedPostRecord = new ReportedPostRecord
                {
                    PostId = model.PostId,
                    IsResolved = false,
                    PostedByUserId = post.As<CommonPart>().Owner.Id,
                    ReasonReported = model.ReasonReported,
                    ReportedByUserId = userId,
                    ResolvedByUserId = 0,
                };
                _reportPostService.CreateReport(reportedPostRecord);
            }
            return RedirectToActionPermanent("InappropriatePostReportedSuccessfully", new { returnUrl = model.ReturnUrl });
        }
        public void CreateReport(ReportedPostRecord record)
        {


            //check if this post has already been reported
            var existingRecord = _postReportRepository.Table.Where(post => post.PostId == record.PostId).ToList();

            record.ReportedDate = DateTime.UtcNow;

            //This follows the same logic as the 'PostReportStatus' above so that posts are only recorded if appropriate
            //Yes, the logic could be simplified here but will model the above function exactly to make keeping the logic verifiable and in sync easier.
            if (existingRecord.Count == 0  )
            {
                //its a new report so automatically accept it
                _postReportRepository.Create(record);

            } else {
                

                if (existingRecord.Where(p => p.ReportedByUserId == record.ReportedByUserId).Count() > 0)
                {
                    //this user has already reported the post, so don't accept a second submission. 
                    //This will stop a disgruntled user from repeatedly reporting the same post.  
                    //If the post is truly inappropriate someone else will report it.
                } 
                else if (existingRecord.Where(report => report.IsResolved == false).Count() > 0)
                {
                    //is reported and pending resolution so don't make a new entry
                }
                else if (existingRecord.Where(report => report.IsResolved == true).Count() > 0)
                {    //this report has already been reported and resolved but is being re-reported
                    _postReportRepository.Create(record);
                }
            }
        }