public ActionResult Follow(string username)
        {
            var userId = this.User.Identity.GetUserId();

            var userToBeFollowed = this.data.Users
                .All()
                .FirstOrDefault(u => u.UserName == username);
            var userWantingToFollow = this.data.Users.Find(userId);

            userToBeFollowed.Followers.Add(userWantingToFollow);
            userWantingToFollow.Following.Add(userToBeFollowed);

            this.data.Users.SaveChanges();

            // Send notification
            var currentUsername = this.User.Identity.GetUserName();
            var notification = new Notification()
            {
                UserId = userToBeFollowed.Id,
                CauseUserId = userWantingToFollow.Id,
                Content = currentUsername + " has just followed you!",
                Date = DateTime.Now
            };

            this.data.Notifications.Add(notification);

            this.data.Notifications.SaveChanges();

            this.TempData["followUserSuccess"] = "User followed successfully";

            return RedirectToAction("Index", "Home");
        }
Esempio n. 2
0
        public ActionResult AddTweetToFavorite([FromUri] int tweetId)
        {
            var loggedUserId = this.User.Identity.GetUserId();
            var user = this.Data.Users.Find(loggedUserId);

            var favoriteTweet = this.Data.Tweets.Find(tweetId);
            var notification = new Notification()
            {
                CreatedAt = DateTime.Now,
                ReceiverId = favoriteTweet.UserId,
                SenderId = loggedUserId,
                Type = NotificationType.FavouriteTweet,
                Seen = false
            };

            this.Data.Notifications.Add(notification);
            user.FavouriteTweets.Add(favoriteTweet);

            this.Data.SaveChanges();

            return RedirectToAction("Index", "Home");
        }
Esempio n. 3
0
        public ActionResult RetweetTweet(int tweetId)
        {
            var loggedUserId = this.User.Identity.GetUserId();
            var user = this.Data.Users.Find(loggedUserId);

            var retweetTweet = this.Data.Tweets.Find(tweetId);
            var tweet = new Tweet()
            {
                Content = retweetTweet.Content,
                CreatedAt = DateTime.Now,
                UserId = loggedUserId
            };

            var notification = new Notification()
            {
                CreatedAt = DateTime.Now,
                ReceiverId = retweetTweet.UserId,
                SenderId = loggedUserId,
                Seen = false,
                Type = NotificationType.Retweet
            };

            this.Data.Notifications.Add(notification);
            this.Data.Tweets.Add(tweet);
            this.Data.SaveChanges();

            return RedirectToAction("ShowProfile", "Users");
        }
Esempio n. 4
0
        // POST: {username}/Users/Follow
        public ActionResult Follow(string followUserId)
        {
            var currentUserId = this.User.Identity.GetUserId();
            var currentUser = this.Data.Users.GetAll().FirstOrDefault(u => u.Id == currentUserId);
            var followUser = this.Data.Users.GetAll().FirstOrDefault(u => u.Id == followUserId);

            if (currentUser == null || followUser == null)
            {
                return this.RedirectToAction("Following");
            }

            followUser.Following.Add(currentUser);

            var notification = new Notification()
                {
                    Content = string.Format("You have been followed by {0}", currentUser.UserName),
                    Date = DateTime.Now,
                    ReceiverId = followUserId,
                    NotificationType = NotificationType.NewFollower
                };

            this.Data.Notifications.Add(notification);
            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.Notification(followUserId);

            return this.RedirectToAction("Following");
        }
        public ActionResult ReTweet(int tweetId)
        {
            var userId = this.User.Identity.GetUserId();
            var user = this.Data.Users.GetAll().FirstOrDefault(u => u.Id == userId);
            var tweet = this.Data.Tweets.GetAll().FirstOrDefault(t => t.Id == tweetId);

            if (user == null && tweet == null)
            {
                return this.Redirect("/" + user.UserName);
            }
            
            tweet.RetweetedBy.Add(user);

            var notification = new Notification()
            {
                Content = string.Format("Your tweet was retweeted by {0}", user.UserName),
                Date = DateTime.Now,
                ReceiverId = tweet.AuthorId,
                NotificationType = NotificationType.Retweet
            };

            this.Data.Notifications.Add(notification);
            this.Data.SaveChanges();

            return this.Redirect("/" + user.UserName);
        }
        public ActionResult Favorite(int id)
        {
            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return new HttpUnauthorizedResult("You need to be logged in.");
            }

            var tweetToFavorite = this.data.Tweets.Find(id);

            if (tweetToFavorite == null)
            {
                return new HttpNotFoundResult("The tweet is missing.");
            }

            var user = this.data.Users.Find(userId);

            if (user.Favorites.Contains(tweetToFavorite))
            {
                this.TempData["favoriteTweetError"] = "You cannot favorite a tweet more than once";

                return Content(ReloadScript);
            }

            user.Favorites.Add(tweetToFavorite);

            this.data.Users.SaveChanges();

            // Send notification
            var username = this.User.Identity.GetUserName();
            var notification = new Notification()
            {
                UserId = tweetToFavorite.UserId,
                CauseUserId = userId,
                Content = username + " has just favorited your tweet!",
                Date = DateTime.Now,
                AuthorTweetId = tweetToFavorite.Id
            };

            this.data.Notifications.Add(notification);

            this.data.Notifications.SaveChanges();

            this.TempData["favoriteTweetSuccess"] = "Tweet favorited successfully";

            return Content(ReloadScript);
        }
        public ActionResult Reply(PostReplyBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.TempData["postReplyError"] =
                    "A tweet's length should be between 1 and 140 characters long";

                return Content(ReloadScript);
            }

            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return new HttpUnauthorizedResult("You need to be logged in.");
            }

            var tweetToReplyTo = this.data.Tweets.Find(model.Id);

            if (tweetToReplyTo == null)
            {
                return new HttpNotFoundResult("The tweet is missing.");
            }

            var reply = new Tweet()
            {
                Content = model.Content,
                CreatedOn = DateTime.Now,
                UserId = userId,
                User = this.data.Users.Find(userId)
            };

            // Necessary, otherwise throws null reference exception
            try
            {
                tweetToReplyTo.Replies.Add(reply);

            }
            catch (NullReferenceException)
            {
                tweetToReplyTo.Replies = new HashSet<Tweet>();

                tweetToReplyTo.Replies.Add(reply);
            }

            this.data.Tweets.SaveChanges();

            // Send notification
            var username = this.User.Identity.GetUserName();
            var notification = new Notification()
            {
                UserId = tweetToReplyTo.UserId,
                CauseUserId = userId,
                Content = username + " has just replied to your tweet!",
                Date = DateTime.Now,
                AuthorTweetId = tweetToReplyTo.Id
            };

            this.data.Notifications.Add(notification);

            this.TempData["postReplySuccess"] = "Reply sent successfully";

            return RedirectToAction("Replies", "Tweets", new { id = model.Id });
        }
        public ActionResult Retweet(int id)
        {
            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return new HttpUnauthorizedResult("You need to be logged in.");
            }

            var tweetToRetweet = this.data.Tweets.Find(id);

            if (tweetToRetweet == null)
            {
                return new HttpNotFoundResult("The tweet is missing.");
            }

            var user = this.data.Users.Find(userId);

            user.Retweets.Add(tweetToRetweet);

            var retweetedTweetAsUnique = tweetToRetweet;

            retweetedTweetAsUnique.User.PictureUrl = tweetToRetweet.User.PictureUrl;
            retweetedTweetAsUnique.User.UserName = tweetToRetweet.User.UserName;
            retweetedTweetAsUnique.CreatedOn = DateTime.Now;

            this.data.Tweets.Add(retweetedTweetAsUnique);

            this.data.Tweets.SaveChanges();

            this.data.Users.SaveChanges();

            // Send notification
            var username = this.User.Identity.GetUserName();
            var notification = new Notification()
            {
                UserId = tweetToRetweet.UserId,
                CauseUserId = userId,
                Content = username + " has just retweeted your tweet!",
                Date = DateTime.Now,
                AuthorTweetId = tweetToRetweet.Id
            };

            this.data.Notifications.Add(notification);

            this.data.Notifications.SaveChanges();

            this.TempData["retweetTweetSuccess"] = "Tweet retweeted successfully";

            return Content(ReloadScript);
        }
        public ActionResult FavorTweet(int tweetId)
        {
            var currentUserId = this.User.Identity.GetUserId();
            var user = this.Data.Users.GetAll().FirstOrDefault(u => u.Id == currentUserId);
            var tweet = this.Data.Tweets.GetAll().FirstOrDefault(t => t.Id == tweetId);

            if (tweet == null && user == null)
            {
                return this.HttpNotFound();
            }

            tweet.FavoritedBy.Add(user);

            var notification = new Notification()
                {
                    Content = string.Format("Your tweet was favoured by {0}", user.UserName),
                    Date = DateTime.Now,
                    ReceiverId = tweet.AuthorId,
                    NotificationType = NotificationType.FavouriteTweet
                };

            this.Data.Notifications.Add(notification);

            this.Data.SaveChanges();

            TwitterHub hub = new TwitterHub();

            hub.Notification(tweet.Author.Id);

            return this.Redirect("/" + user.UserName);
        }