Exemplo n.º 1
0
        public ActionResult Frontpage(PostsSortBy? sort, TimeFilter? time, int? pageNumber, int? pageSize)
        {
            var subs = _contextService.GetSubscribedSubIds();

            if (sort == null)
                sort = PostsSortBy.Hot;

            if (time == null)
                time = TimeFilter.All;

            if (pageNumber == null || pageNumber < 1)
                pageNumber = 1;
            if (pageSize == null)
                pageSize = 25;
            if (pageSize > 100)
                pageSize = 100;
            if (pageSize < 1)
                pageSize = 1;

            var postIds = _postDao.GetPosts(subs, sort.Value, time.Value, true /*hide removed posts TODO: only hide if not post admin*/, ((pageNumber - 1) * pageSize), pageSize);

            var model = new SubPostsModel();
            model.SortBy = sort.Value;
            model.TimeFilter = time;
            if (subs.Any()) // maybe the user hasn't subscribed to any subs?
                model.Posts = new PagedList<PostWrapped>(_postWrapper.Wrap(postIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, postIds.HasMore);

            return View("Posts", model);
        }
Exemplo n.º 2
0
        public ActionResult Posts(string name, PostsSortBy? sort, TimeFilter? time, int? pageNumber, int? pageSize)
        {
            if (string.IsNullOrEmpty(name))
                return Redirect(Url.Subs());

            var subs = new List<Guid>();

            Sub sub = null;

            if (name.Equals("all", StringComparison.InvariantCultureIgnoreCase))
            {
                // TODO: Filter only by subs that want to be including in "all". For now, we will do nothing, which will effectively return all posts.
            }
            else
            {
                // the user wants to view a specific sub

                sub = _subDao.GetSubByName(name);

                if (sub == null)
                    return Redirect(Url.Subs(name));

                if(_userContext.CurrentUser != null)
                    _subActivityDao.MarkSubActive(_userContext.CurrentUser.Id, sub.Id);

                subs.Add(sub.Id);
            }

            if (sort == null)
                sort = PostsSortBy.Hot; // TODO: get default from sub

            if (time == null)
                time = TimeFilter.All;

            if (pageNumber == null || pageNumber < 1)
                pageNumber = 1;
            if (pageSize == null)
                pageSize = 25;
            if (pageSize > 100)
                pageSize = 100;
            if (pageSize < 1)
                pageSize = 1;

            var postIds = _postDao.GetPosts(subs, sort.Value, time.Value, true /*hide removed posts TODO: only hide if not post admin*/, ((pageNumber - 1) * pageSize), pageSize);

            var model = new SubPostsModel();
            model.Sub = sub != null ? _subWrapper.Wrap(sub.Id, _userContext.CurrentUser) : null;
            model.SortBy = sort.Value;
            model.TimeFilter = time;
            model.Posts = new PagedList<PostWrapped>(_postWrapper.Wrap(postIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, postIds.HasMore);

            return View(model);
        }
Exemplo n.º 3
0
        public ActionResult Posts(string subName, PostsSortBy? sort, PostsTimeFilter? time, int? pageNumber, int? pageSize)
        {
            if (string.IsNullOrEmpty(subName))
                return Redirect(Url.Subs());

            var model = new SubPostsModel();

            var subs = new List<Guid>();
            Sub sub = null;

            if (subName.Equals("all", StringComparison.InvariantCultureIgnoreCase))
            {
                // TODO: Filter only by subs that want to be including in "all". For now, we will do nothing, which will effectively return all posts.
                model.IsAll = true;
            }
            else
            {
                // the user wants to view a specific sub

                sub = _subDao.GetSubByName(subName);

                if (sub == null)
                    throw new NotFoundException();

                if (_userContext.CurrentUser != null)
                    _subActivityDao.MarkSubActive(_userContext.CurrentUser.Id, sub.Id);

                subs.Add(sub.Id);
            }

            if (sort == null)
                sort = PostsSortBy.Hot; // TODO: get default from sub

            if (time == null)
                time = PostsTimeFilter.All;

            if (pageNumber == null || pageNumber < 1)
                pageNumber = 1;
            if (pageSize == null)
                pageSize = 25;
            if (pageSize > 100)
                pageSize = 100;
            if (pageSize < 1)
                pageSize = 1;

            var postIds = _postDao.GetPosts(subs,
                sortby: sort.Value,
                timeFilter: time.Value,
                onlyAll: model.IsAll,
                // anonymous users don't see NSFW content.
                // logged in users only see NSFW if preferences say so.
                // If they want to see NSFW, they will see all content (SFW/NSFW).
                nsfw: _userContext.CurrentUser == null ? false : (_userContext.CurrentUser.ShowNsfw ? (bool?)null : false),
                // we are showing posts for a specific sub, so we can show stickies
                stickyFirst: sub != null,
                skip: ((pageNumber - 1) * pageSize),
                take: pageSize);

            model.Sub = sub != null ? _subWrapper.Wrap(sub.Id, _userContext.CurrentUser) : null;
            model.SortBy = sort.Value;
            model.TimeFilter = time;
            model.Posts = new PagedList<PostWrapped>(_postWrapper.Wrap(postIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, postIds.HasMore);

            return View(model);
        }
Exemplo n.º 4
0
        public ActionResult Frontpage(PostsSortBy? sort, PostsTimeFilter? time, int? pageNumber, int? pageSize)
        {
            var subs = _contextService.GetSubscribedSubIds();

            // if the user is not subscribe to any subs, show the default content.
            if (subs.Count == 0)
                subs = _subDao.GetDefaultSubs();

            if (sort == null)
                sort = PostsSortBy.Hot;

            if (time == null)
                time = PostsTimeFilter.All;

            if (pageNumber == null || pageNumber < 1)
                pageNumber = 1;
            if (pageSize == null)
                pageSize = 25;
            if (pageSize > 100)
                pageSize = 100;
            if (pageSize < 1)
                pageSize = 1;

            var postIds = _postDao.GetPosts(subs,
                sortby: sort.Value,
                timeFilter: time.Value,
                // anonymous users don't see NSFW content.
                // logged in users only see NSFW if preferences say so.
                // If they want to see NSFW, they will see all content (SFW/NSFW).
                nsfw: _userContext.CurrentUser == null ? false : (_userContext.CurrentUser.ShowNsfw ? (bool?)null : false),
                skip: ((pageNumber - 1) * pageSize),
                take: pageSize);

            var model = new SubPostsModel();
            model.SortBy = sort.Value;
            model.TimeFilter = time;
            if (subs.Any()) // maybe the user hasn't subscribed to any subs?
                model.Posts = new PagedList<PostWrapped>(_postWrapper.Wrap(postIds, _userContext.CurrentUser), pageNumber.Value, pageSize.Value, postIds.HasMore);

            return View("Posts", model);
        }
Exemplo n.º 5
0
        public SeekedList<Guid> GetPosts(List<Guid> subs = null, PostsSortBy sortby = PostsSortBy.New, TimeFilter timeFilter = TimeFilter.All, bool hideRemovedPosts = true, int? skip = null, int? take = null)
        {
            return _conn.Perform(conn =>
            {
                var query = conn.From<Post>();
                if (subs != null && subs.Count > 0)
                {
                    query.Where(x => subs.Contains(x.SubId));
                }

                if (timeFilter != TimeFilter.All)
                {
                    TimeSpan timeSpan;
                    switch (timeFilter)
                    {
                        case TimeFilter.Hour:
                            timeSpan = TimeSpan.FromHours(1);
                            break;
                        case TimeFilter.Day:
                            timeSpan = TimeSpan.FromDays(1);
                            break;
                        case TimeFilter.Week:
                            timeSpan = TimeSpan.FromDays(7);
                            break;
                        case TimeFilter.Month:
                            timeSpan = TimeSpan.FromDays(30);
                            break;
                        case TimeFilter.Year:
                            timeSpan = TimeSpan.FromDays(365);
                            break;
                        default:
                            throw new Exception("unknown time filter");
                    }

                    var from = Common.CurrentTime() - timeSpan;

                    query.Where(x => x.DateCreated >= from);
                }

                if (hideRemovedPosts)
                    query.Where(x => x.Verdict != (int)Verdict.ModRemoved);

                var totalCount = conn.Count(query);

                query.Skip(skip).Take(take);

                switch (sortby)
                {
                    case PostsSortBy.Hot:
                        query.OrderByExpression = "ORDER BY (hot(vote_up_count, vote_down_count, date_created), date_created) DESC";
                        break;
                    case PostsSortBy.New:
                        query.OrderByDescending(x => x.DateCreated);
                        break;
                    case PostsSortBy.Rising:
                        throw new Exception("not implemented");
                    case PostsSortBy.Controversial:
                        query.OrderByExpression = "ORDER BY (controversy(vote_up_count, vote_down_count), date_created) DESC";
                        break;
                    case PostsSortBy.Top:
                        query.OrderByExpression = "ORDER BY (score(vote_up_count, vote_down_count), date_created) DESC";
                        break;
                    default:
                        throw new Exception("uknown sort");
                }

                query.SelectExpression = "SELECT \"id\"";

                return new SeekedList<Guid>(conn.Select(query).Select(x => x.Id), skip ?? 0, take, totalCount);
            });
        }
Exemplo n.º 6
0
        public SeekedList <Guid> GetPosts(List <Guid> subs           = null,
                                          PostsSortBy sortby         = PostsSortBy.New,
                                          PostsTimeFilter timeFilter = PostsTimeFilter.All,
                                          Guid?userId           = null,
                                          bool hideRemovedPosts = true,
                                          bool showDeleted      = false,
                                          bool onlyAll          = false,
                                          bool?nsfw             = null,
                                          bool?sticky           = null,
                                          bool stickyFirst      = false,
                                          int?skip = null,
                                          int?take = null)
        {
            return(_conn.Perform(conn =>
            {
                var query = conn.From <Post>();
                if (subs != null && subs.Count > 0)
                {
                    query.Where(x => subs.Contains(x.SubId));
                }

                if (timeFilter != PostsTimeFilter.All)
                {
                    TimeSpan timeSpan;

                    switch (timeFilter)
                    {
                    case PostsTimeFilter.Hour:
                        timeSpan = TimeSpan.FromHours(1);
                        break;

                    case PostsTimeFilter.Day:
                        timeSpan = TimeSpan.FromDays(1);
                        break;

                    case PostsTimeFilter.Week:
                        timeSpan = TimeSpan.FromDays(7);
                        break;

                    case PostsTimeFilter.Month:
                        timeSpan = TimeSpan.FromDays(30);
                        break;

                    case PostsTimeFilter.Year:
                        timeSpan = TimeSpan.FromDays(365);
                        break;

                    default:
                        throw new Exception("unknown time filter");
                    }

                    var from = Common.CurrentTime() - timeSpan;

                    query.Where(x => x.DateCreated >= from);
                }

                if (hideRemovedPosts)
                {
                    query.Where(x => x.Verdict != (int)Verdict.ModRemoved);
                }

                if (!showDeleted)
                {
                    query.Where(x => x.Deleted == false);
                }

                if (onlyAll)
                {
                    query.Where(x => x.InAll);
                }

                if (nsfw.HasValue)
                {
                    query.Where(x => x.Nsfw == nsfw.Value);
                }

                if (userId.HasValue)
                {
                    query.Where(x => x.UserId == userId.Value);
                }

                if (sticky.HasValue)
                {
                    query.Where(x => x.Sticky == sticky);
                }

                var totalCount = conn.Count(query);

                query.Skip(skip).Take(take);

                var orders = new List <string>();

                if (stickyFirst)
                {
                    orders.Add("sticky");
                }

                switch (sortby)
                {
                case PostsSortBy.Hot:
                    orders.Add("hot(vote_up_count, vote_down_count, date_created)");
                    orders.Add("date_created");
                    break;

                case PostsSortBy.New:
                    orders.Add("date_created");
                    break;

                case PostsSortBy.Rising:
                    throw new Exception("not implemented");

                case PostsSortBy.Controversial:
                    orders.Add("controversy(vote_up_count, vote_down_count)");
                    orders.Add("date_created");
                    break;

                case PostsSortBy.Top:
                    orders.Add("score(vote_up_count, vote_down_count)");
                    orders.Add("date_created");
                    query.OrderByExpression = "ORDER BY (, date_created) DESC";
                    break;

                default:
                    throw new Exception("uknown sort");
                }

                if (orders.Count > 0)
                {
                    query.OrderByExpression = "ORDER BY (" + string.Join(", ", orders) + ") DESC";
                }

                query.SelectExpression = "SELECT \"id\"";

                return new SeekedList <Guid>(conn.Select(query).Select(x => x.Id), skip ?? 0, take, totalCount);
            }));
        }
Exemplo n.º 7
0
        public SeekedList<Guid> GetPosts(List<Guid> subs = null,
            PostsSortBy sortby = PostsSortBy.New,
            PostsTimeFilter timeFilter = PostsTimeFilter.All,
            Guid? userId = null,
            bool hideRemovedPosts = true,
            bool showDeleted = false,
            bool onlyAll = false,
            bool? nsfw = null,
            bool? sticky = null,
            bool stickyFirst = false,
            int? skip = null,
            int? take = null)
        {
            return _conn.Perform(conn =>
            {
                var query = conn.From<Post>();
                if (subs != null && subs.Count > 0)
                {
                    query.Where(x => subs.Contains(x.SubId));
                }

                if (timeFilter != PostsTimeFilter.All)
                {
                    TimeSpan timeSpan;

                    switch (timeFilter)
                    {
                        case PostsTimeFilter.Hour:
                            timeSpan = TimeSpan.FromHours(1);
                            break;
                        case PostsTimeFilter.Day:
                            timeSpan = TimeSpan.FromDays(1);
                            break;
                        case PostsTimeFilter.Week:
                            timeSpan = TimeSpan.FromDays(7);
                            break;
                        case PostsTimeFilter.Month:
                            timeSpan = TimeSpan.FromDays(30);
                            break;
                        case PostsTimeFilter.Year:
                            timeSpan = TimeSpan.FromDays(365);
                            break;
                        default:
                            throw new Exception("unknown time filter");
                    }

                    var from = Common.CurrentTime() - timeSpan;

                    query.Where(x => x.DateCreated >= from);
                }

                if (hideRemovedPosts)
                    query.Where(x => x.Verdict != (int)Verdict.ModRemoved);

                if (!showDeleted)
                    query.Where(x => x.Deleted == false);

                if (onlyAll)
                    query.Where(x => x.InAll);

                if (nsfw.HasValue)
                    query.Where(x => x.Nsfw == nsfw.Value);

                if (userId.HasValue)
                    query.Where(x => x.UserId == userId.Value);

                if (sticky.HasValue)
                    query.Where(x => x.Sticky == sticky);

                var totalCount = conn.Count(query);

                query.Skip(skip).Take(take);

                var orders = new List<string>();

                if(stickyFirst)
                    orders.Add("sticky");

                switch (sortby)
                {
                    case PostsSortBy.Hot:
                        orders.Add("hot(vote_up_count, vote_down_count, date_created)");
                        orders.Add("date_created");
                        break;
                    case PostsSortBy.New:
                        orders.Add("date_created");
                        break;
                    case PostsSortBy.Rising:
                        throw new Exception("not implemented");
                    case PostsSortBy.Controversial:
                        orders.Add("controversy(vote_up_count, vote_down_count)");
                        orders.Add("date_created");
                        break;
                    case PostsSortBy.Top:
                        orders.Add("score(vote_up_count, vote_down_count)");
                        orders.Add("date_created");
                        query.OrderByExpression = "ORDER BY (, date_created) DESC";
                        break;
                    default:
                        throw new Exception("uknown sort");
                }

                if (orders.Count > 0)
                {
                    query.OrderByExpression = "ORDER BY (" + string.Join(", ", orders) + ") DESC";
                }

                query.SelectExpression = "SELECT \"id\"";

                return new SeekedList<Guid>(conn.Select(query).Select(x => x.Id), skip ?? 0, take, totalCount);
            });
        }