示例#1
0
        public async Task <ActionResult> Feed(string hashTag, string searchTerm, bool filtering = false, int pageIndex = 0)
        {
            bool isAuthorized = _authorizationManager.IsAuthorized(HttpContext.Session);

            if (!isAuthorized)
            {
                return(RedirectToAction("Index", "TwitterOAuth"));
            }

            // Server side validation, in case the client side is removed by hand.
            if (string.IsNullOrEmpty(searchTerm) && string.IsNullOrEmpty(hashTag))
            {
                return(View("Views/TwitterFeed/Error.cshtml", "The Search term can not be empty!"));
            }

            PageTweetBorders pageTweetBorders = GetPageTweetBorders(pageIndex, filtering);
            int    pageSize = _twitterConfig.PageSize;
            string query    = $"{hashTag} {searchTerm}";

            // Unfortunatelly, twitter does not provide standart page parameters to do paging.
            // Their idea is to provide since and max ids and leave it to the developer to implement their own paging.
            IList <TweetDto> tweetsResult =
                await _twitterService.Search(query, pageSize, pageTweetBorders.SinceId, pageTweetBorders.MaxId);

            if (tweetsResult.Count > 0)
            {
                SetPageTweetBorders(pageIndex, tweetsResult);
            }

            TweetViewModel tweetsViewModel =
                BuildTweetViewModel(tweetsResult, pageSize, pageIndex, hashTag, searchTerm, filtering);

            return(View(tweetsViewModel));
        }
示例#2
0
        private PageTweetBorders GetPageTweetBorders(int pageIndex, bool filtering)
        {
            var result = new PageTweetBorders()
            {
                Page    = 0,
                MaxId   = 0,
                SinceId = 0
            };

            if (filtering)
            {
                return(result);
            }

            string pagesKey = $"tweet-pages";

            if (TempData.ContainsKey(pagesKey))
            {
                var pages =
                    JsonConvert.DeserializeObject <Dictionary <int, PageTweetBorders> >(TempData[pagesKey].ToString());

                // Prev Page
                if (pages.ContainsKey(pageIndex))
                {
                    result = pages[pageIndex];
                }
                // Next Page
                else if (pages.ContainsKey(pageIndex - 1))
                {
                    PageTweetBorders prevPage = pages[pageIndex - 1];

                    result = new PageTweetBorders()
                    {
                        Page    = pageIndex,
                        MaxId   = ulong.MaxValue,
                        SinceId = prevPage.MaxId
                    };
                }
            }

            return(result);
        }