public ActionResult Follow(string username)
        {
            var user = this.Data.Users.All().FirstOrDefault(u => u.UserName == username);

            if (user == null)
            {
                throw new ArgumentException("User not found");
            }

            var currentUser = this.Data.Users.Find(this.User.Identity.GetUserId());

            if (currentUser.UserName == user.UserName)
            {
                throw new InvalidOperationException("Cannot follow yourself");
            }

            user.Followers.Add(currentUser);

            user.Notifications.Add(new Notification()
            {
                Content = "test",
                CreatorId = currentUser.Id,
                RecipientId = user.Id,
                Date = DateTime.Now,
                Type = NotificationType.NewFollower
            });

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.NotificationInform(new List<string>() { user.UserName });

            return null;
        }
Esempio n. 2
0
        public ActionResult Tweet(CreateTweetBindingModel model)
        {
            if (model == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Missing data"));
            }

            if (!this.ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(this.ModelState)));
            }

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            var tweet = new Tweet()
            {
                Content   = model.Content,
                UserId    = user.Id,
                TweetDate = DateTime.Now
            };

            this.Data.Tweets.Add(tweet);

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.InformFollowers(user.Followers.Select(u => u.UserName).ToList(), tweet.Id);

            return(PartialView("_FullTweetPartial", Mapper.Map <TweetViewModel>(tweet)));
        }
Esempio n. 3
0
        public ActionResult Retweet(ReTweetBindingModel model)
        {
            if (model == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Missing data"));
            }

            if (!this.ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, JsonConvert.SerializeObject(this.ModelState)));
            }

            var tweet = this.Data.Tweets.Find(model.TweetId);

            if (tweet == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Tweet not found."));
            }

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            if (tweet.UserId == user.Id)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "You can't retweet your own tweets."));
            }

            if (user.Tweets.Any(t => t.ReplyToId == tweet.Id))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Retweeting retweet is not possible at the time."));
            }

            this.Data.Tweets.Add(new Tweet()
            {
                Content          = model.Content,
                UserId           = user.Id,
                TweetDate        = DateTime.Now,
                RetweetedTweetId = tweet.Id
            });

            tweet.User.Notifications.Add(new Notification()
            {
                Content     = "retweeted one of your tweets",
                CreatorId   = user.Id,
                RecipientId = tweet.UserId,
                Date        = DateTime.Now,
                Type        = NotificationType.Retweet
            });

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.NotificationInform(new List <string>()
            {
                tweet.User.UserName
            });

            return(RedirectToAction("Index", "Home"));
        }
Esempio n. 4
0
        public ActionResult Favourite(int id)
        {
            var tweet = this.Data.Tweets.Find(id);

            if (tweet == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "Tweet not found."));
            }

            var user = this.Data.Users.Find(this.User.Identity.GetUserId());

            if (user.FavouritedTweets.Any(t => t == tweet))
            {
                user.FavouritedTweets.Remove(tweet);
            }
            else
            {
                user.FavouritedTweets.Add(tweet);

                tweet.User.Notifications.Add(new Notification()
                {
                    Content     = "favourited one of your tweets",
                    RecipientId = tweet.UserId,
                    Date        = DateTime.Now,
                    CreatorId   = user.Id,
                    Type        = NotificationType.FavouriteTweet,
                });

                TwitterHub hub = new TwitterHub();

                hub.NotificationInform(new List <string>()
                {
                    tweet.User.UserName
                });
            }

            this.Data.SaveChanges();

            this.Response.StatusCode = (int)HttpStatusCode.OK;
            return(this.Content(" " + tweet.FavouritedBy.Count()));
        }
Esempio n. 5
0
        public ActionResult Follow(string username)
        {
            var user = this.Data.Users.All().FirstOrDefault(u => u.UserName == username);

            if (user == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound, "User not found."));
            }

            var currentUser = this.Data.Users.Find(this.User.Identity.GetUserId());

            if (currentUser.UserName == user.UserName)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "You cannot follow yourself."));
            }

            user.Followers.Add(currentUser);

            user.Notifications.Add(new Notification()
            {
                Content     = "followed you",
                CreatorId   = currentUser.Id,
                RecipientId = user.Id,
                Date        = DateTime.Now,
                Type        = NotificationType.NewFollower
            });

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.NotificationInform(new List <string>()
            {
                user.UserName
            });

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }