Esempio n. 1
0
        /* this entire process of setting up the models should be refactored
         * in terms of partitioning the models and partial views */
        private HomeViewModel GenerateIndexModel(long displayFeed)
        {
            HomeViewModel model = new HomeViewModel();

            // get a user that matches the name of the currently logged in user
            User user = _userService.FindUserForName(User.Identity.Name);

            /* Find all of the tweets that should be displayed. The tweets that
             * should be displayed are either authored by the user or those that
             * are found in a user subscription */
            List <Tweet> tweetsToDisplay = new List <Tweet>();

            if (displayFeed == -1)
            {
                if (user.Feeds != null)
                {
                    foreach (Feed feed in user.Feeds)
                    {
                        if (feed.Tweets != null && feed.Tweets.Count > 0)
                        {
                            tweetsToDisplay.AddRange(feed.Tweets);
                        }
                    }
                }

                if (user.Subscriptions != null)
                {
                    foreach (Feed feed in user.Subscriptions)
                    {
                        if (feed.Tweets != null && feed.Tweets.Count > 0)
                        {
                            tweetsToDisplay.AddRange(feed.Tweets);
                        }
                    }
                }
            }
            else
            {
                Feed feed = _twitterService.GetFeed(displayFeed);
                tweetsToDisplay = (feed == null) ? new List <Tweet>() : feed.Tweets.ToList <Tweet>();
            }
            model.DisplayFeed = displayFeed;

            model.Tweets = tweetsToDisplay.OrderByDescending(t => t.PostDate).ToList <Tweet>();

            if (user.Feeds != null && user.Feeds.Count > 0)
            {
                model.HasFeeds = true;
            }

            // Add the user's feeds to the feed model that will be rendered as a partial view
            model.Feeds = user.Feeds == null ? new List <Feed>() : user.Feeds;
            model.Feeds = model.Feeds.OrderBy(f => f.Name).ToList();

            model.Subscriptions = user.Subscriptions == null ? new List <Feed>() : user.Subscriptions;

            StringBuilder sb = new StringBuilder();

            sb.Append("[");

            List <Feed> possible = _twitterService.GetPossibleSubscriptionsFor(user);

            foreach (Feed feed in possible)
            {
                string subscription = string.Format("{{value: \"{0} by @{1}\", tokens: [\"{2}\",\"{3}\",\"{4}\"]}}", feed.Name, feed.Owner.UserName,
                                                    feed.Owner.Name.Split(' ')[0], feed.Owner.Name.Split(' ')[1], feed.Name);

                sb.Append(string.Format("{0},", subscription));
            }
            if (possible.Count > 0)
            {
                sb.Replace(',', ']', sb.Length - 1, 1);
            }
            else
            {
                sb.Append("]");
            }
            model.SubscriptionTypeahead = sb.ToString();

            model.FeedList = model.Feeds.Select(f => new SelectListItem()
            {
                Text = f.Name, Value = Convert.ToString(f.ID)
            });

            return(model);
        }