Пример #1
0
        public async Task <ActionResult> Index(int?page, string domainname, string sortingmode)
        {
            const int pageSize   = 25;
            int       pageNumber = (page ?? 0);

            if (pageNumber < 0 || String.IsNullOrWhiteSpace(domainname) || pageNumber > 9)
            {
                return(NotFoundErrorView());
            }
            if (domainname.Length < 4)
            {
                return(RedirectToAction("UnAuthorized", "Error"));
            }

            sortingmode = (sortingmode == "new" ? "new" : "hot");

            ViewBag.SelectedSubverse = "domains";
            ViewBag.SelectedDomain   = domainname;
            domainname = domainname.Trim().ToLower();

            //TODO: This needs to moved to Query/Repository
            var results = CacheHandler.Instance.Register(CachingKey.DomainSearch(domainname, pageNumber, sortingmode), () => {
                using (var db = new voatEntities())
                {
                    db.EnableCacheableOutput();

                    //restrict disabled subs from result list
                    IQueryable <Submission> q = (from m in db.Submissions
                                                 join s in db.Subverses on m.Subverse equals s.Name
                                                 where
                                                 !s.IsAdminDisabled.Value &&
                                                 !m.IsDeleted &&
                                                 m.Type == 2 &&
                                                 m.Url.ToLower().Contains(domainname)
                                                 select m);

                    if (sortingmode == "new")
                    {
                        ViewBag.SortingMode = sortingmode;
                        q = q.OrderByDescending(x => x.CreationDate);
                    }
                    else
                    {
                        ViewBag.SortingMode = "hot";
                        q = q.OrderByDescending(x => x.Rank).ThenByDescending(x => x.CreationDate);
                    }

                    var result = q.Skip(pageNumber * pageSize).Take(pageSize).ToList();

                    return(result);
                }
            }, TimeSpan.FromMinutes(60));

            var paginatedSubmissions = new PaginatedList <Submission>(results, page ?? 0, pageSize);

            ViewBag.Title = "Showing all submissions which link to " + domainname;
            return(View("Index", paginatedSubmissions));
        }
Пример #2
0
        // GET: rss/{subverseName}
        public ActionResult Rss(string subverseName)
        {
            var submissions = CacheHandler.Instance.Register <List <Submission> >(CachingKey.RssFeed(subverseName), new Func <List <Submission> >(() => {
                var result = new List <Submission>();
                using (var _db = new voatEntities())
                {
                    _db.EnableCacheableOutput();

                    if (subverseName != null && subverseName != "all")
                    {
                        // return only frontpage submissions from a given subverse
                        var subverse = DataCache.Subverse.Retrieve(subverseName); // _db.Subverse.Find(subverseName);
                        if (subverse != null)
                        {
                            //HACK: Disable subverse
                            if (subverse.IsAdminDisabled.HasValue && subverse.IsAdminDisabled.Value)
                            {
                                //ViewBag.Subverse = subverse.Name;
                                //return SubverseDisabledErrorView();
                                return(result);
                            }

                            result = (from message in _db.Submissions
                                      where !message.IsDeleted && message.Subverse == subverse.Name
                                      select message)
                                     .OrderByDescending(s => s.Rank)
                                     .Take(25)
                                     .ToList();
                        }
                    }
                    else if (subverseName == "all")
                    {
                        // return submissions from all subs
                        result = (from message in _db.Submissions
                                  join subverse in _db.Subverses on message.Subverse equals subverse.Name
                                  where !message.IsDeleted && subverse.IsPrivate != true && subverse.IsAdminPrivate != true && message.Rank > 0.00009
                                  where !(from bu in _db.BannedUsers select bu.UserName).Contains(message.UserName)
                                  select message).OrderByDescending(s => s.Rank).ThenByDescending(s => s.CreationDate).Take(25).ToList();
                    }
                    else
                    {
                        // return site-wide frontpage submissions
                        result = (from message in _db.Submissions
                                  where !message.IsDeleted
                                  join defaultsubverse in _db.DefaultSubverses on message.Subverse equals defaultsubverse.Subverse
                                  select message)
                                 .OrderByDescending(s => s.Rank)
                                 .Take(25)
                                 .ToList();
                    }
                }
                return(result);
            }), TimeSpan.FromMinutes(30));

            var feed = new SyndicationFeed("Voat", "Have your say", new Uri("http://www.voat.co"))
            {
                Language = "en-US",
                ImageUrl =
                    new Uri("http://" + System.Web.HttpContext.Current.Request.Url.Authority +
                            "/Graphics/voat-logo.png")
            };

            var feedItems = new List <SyndicationItem>();

            foreach (var submission in submissions)
            {
                var commentsUrl = new Uri("http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse + "/comments/" + submission.ID);
                var subverseUrl = new Uri("http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/v/" + submission.Subverse);

                var authorName = submission.UserName;
                // submission type submission
                if (submission.IsAnonymized)
                {
                    authorName = submission.ID.ToString(CultureInfo.InvariantCulture);
                }

                if (submission.Type == 1)
                {
                    var item = new SyndicationItem(
                        submission.Title,
                        submission.Content + "</br>" + "Submitted by " + "<a href='u/" + authorName + "'>" + authorName + "</a> to <a href='" + subverseUrl + "'>" + submission.Subverse + "</a> | <a href='" + commentsUrl + "'>" + CommentCounter.CommentCount(submission.ID) + " comments",
                        commentsUrl,
                        submission.ID.ToString(CultureInfo.InvariantCulture),
                        submission.CreationDate);
                    feedItems.Add(item);
                }
                else
                {
                    // link type submission
                    var linkUrl = new Uri(submission.Url);

                    // add a thumbnail if submission has one
                    if (submission.Thumbnail != null)
                    {
                        string thumbnailUrl;

                        if (Settings.UseContentDeliveryNetwork)
                        {
                            thumbnailUrl = new Uri("http://cdn." + System.Web.HttpContext.Current.Request.Url.Authority + "/thumbs/" + submission.Thumbnail).ToString();
                        }
                        else
                        {
                            thumbnailUrl = new Uri("http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/Thumbs/" + submission.Thumbnail).ToString();
                        }

                        var item = new SyndicationItem(
                            submission.Title,
                            "<a xmlns='http://www.w3.org/1999/xhtml' href='" + commentsUrl + "'><img title='" + submission.Title + "' alt='" + submission.Title + "' src='" + thumbnailUrl + "' /></a>" +
                            "</br>" +
                            "Submitted by " + "<a href='u/" + authorName + "'>" + authorName + "</a> to <a href='" + subverseUrl + "'>" + submission.Subverse + "</a> | <a href='" + commentsUrl + "'>" + CommentCounter.CommentCount(submission.ID) + " comments</a>" +
                            " | <a href='" + linkUrl + "'>link</a>",
                            commentsUrl,
                            submission.ID.ToString(CultureInfo.InvariantCulture),
                            submission.CreationDate);

                        feedItems.Add(item);
                    }
                    else
                    {
                        var item = new SyndicationItem(
                            submission.Title,
                            "Submitted by " + "<a href='u/" + authorName + "'>" + authorName + "</a> to <a href='" + subverseUrl + "'>" + submission.Subverse + "</a> | <a href='" + commentsUrl + "'>" + CommentCounter.CommentCount(submission.ID) + " comments",
                            commentsUrl,
                            submission.ID.ToString(CultureInfo.InvariantCulture),
                            submission.CreationDate);
                        feedItems.Add(item);
                    }
                }
            }

            feed.Items = feedItems;
            return(new FeedResult(new Rss20FeedFormatter(feed)));
        }
Пример #3
0
        // return user statistics for user profile overview
        public static UserStatsModel UserStatsModel(string userName)
        {
            var loadFunc = new Func <UserStatsModel>(() =>
            {
                var userStatsModel = new UserStatsModel();

                using (var db = new voatEntities())
                {
                    db.EnableCacheableOutput();

                    // 5 subverses user submitted to most
                    var subverses = db.Submissions.Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted)
                                    .GroupBy(a => new { a.UserName, a.Subverse })
                                    .Select(g => new SubverseStats {
                        SubverseName = g.Key.Subverse, Count = g.Count()
                    })
                                    .OrderByDescending(s => s.Count)
                                    .Take(5)
                                    .ToList();

                    // total comment count
                    var comments = db.Comments.Count(a => a.UserName == userName && !a.IsDeleted);

                    // voting habits
                    var userData = new Domain.UserData(userName);

                    var commentUpvotes      = userData.Information.CommentVoting.UpCount;
                    var commentDownvotes    = userData.Information.CommentVoting.DownCount;
                    var submissionUpvotes   = userData.Information.SubmissionVoting.UpCount;
                    var submissionDownvotes = userData.Information.SubmissionVoting.DownCount;

                    //var commentUpvotes = db.CommentVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == 1);
                    //var commentDownvotes = db.CommentVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == -1);
                    //var submissionUpvotes = db.SubmissionVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == 1);
                    //var submissionDownvotes = db.SubmissionVoteTrackers.Count(a => a.UserName == userName && a.VoteStatus == -1);

                    // get 3 highest rated comments
                    var highestRatedComments = db.Comments
                                               .Include("Submission").AsNoTracking()
                                               .Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted)
                                               .OrderByDescending(s => s.UpCount - s.DownCount)
                                               .Take(3)
                                               .ToList();

                    // get 3 lowest rated comments
                    var lowestRatedComments = db.Comments
                                              .Include("Submission").AsNoTracking()
                                              .Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted)
                                              .OrderBy(s => s.UpCount - s.DownCount)
                                              .Take(3)
                                              .ToList();

                    var linkSubmissionsCount    = db.Submissions.Count(a => a.UserName == userName && a.Type == 2 && !a.IsDeleted);
                    var messageSubmissionsCount = db.Submissions.Count(a => a.UserName == userName && a.Type == 1 && !a.IsDeleted);

                    // get 5 highest rated submissions
                    var highestRatedSubmissions = db.Submissions.Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted)
                                                  .OrderByDescending(s => s.UpCount - s.DownCount)
                                                  .Take(5)
                                                  .ToList();

                    // get 5 lowest rated submissions
                    var lowestRatedSubmissions = db.Submissions.Where(a => a.UserName == userName && !a.IsAnonymized && !a.IsDeleted)
                                                 .OrderBy(s => s.UpCount - s.DownCount)
                                                 .Take(5)
                                                 .ToList();

                    userStatsModel.TopSubversesUserContributedTo = subverses;
                    userStatsModel.LinkSubmissionsSubmitted      = linkSubmissionsCount;
                    userStatsModel.MessageSubmissionsSubmitted   = messageSubmissionsCount;
                    userStatsModel.LowestRatedSubmissions        = lowestRatedSubmissions;
                    userStatsModel.HighestRatedSubmissions       = highestRatedSubmissions;
                    userStatsModel.TotalCommentsSubmitted        = comments;
                    userStatsModel.HighestRatedComments          = highestRatedComments;
                    userStatsModel.LowestRatedComments           = lowestRatedComments;

                    userStatsModel.TotalCommentsUpvoted      = commentUpvotes;
                    userStatsModel.TotalCommentsDownvoted    = commentDownvotes;
                    userStatsModel.TotalSubmissionsUpvoted   = submissionUpvotes;
                    userStatsModel.TotalSubmissionsDownvoted = submissionDownvotes;

                    //HACK: EF causes JSON to StackOverflow on the highest/lowest comments because of the nested loading EF does with the include option, therefore null the refs here.
                    highestRatedComments.ForEach(x => x.Submission.Comments = null);
                    lowestRatedComments.ForEach(x => x.Submission.Comments  = null);
                }

                return(userStatsModel);
            });

            var cachedData = CacheHandler.Instance.Register(CachingKey.UserOverview(userName), loadFunc, TimeSpan.FromMinutes(30));

            return(cachedData);
        }