public ActionResult Tweet(CreateTweetBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return Json(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.Id).ToList(), tweet.Id);

            return RedirectToAction("Index", "Home");
        }
        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;
        }
        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());
        }
        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);
        }
        public ActionResult Retweet(ReTweetBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return Json(this.ModelState);
            }

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

            if (tweet == null)
            {
                return this.Json("Not found");
            }

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

            if (tweet.UserId == user.Id)
            {
                throw new Exception("You can't retweet your own tweets");
            }

            if (user.Tweets.Any(t => t.ReplyToId == tweet.Id))
            {
                throw new Exception("You cannot retweet again");
            }

            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 = "Retweet",
                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");
        }
        public ActionResult Favourite(int id)
        {
            var tweet = this.Data.Tweets.Find(id);

            if (tweet == null)
            {
                throw new NotImplementedException("Tweet does not exists");
            }

            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 = "favourite",
                    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();

            return null;
        }
Ejemplo n.º 7
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");
        }
Ejemplo n.º 8
0
        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.HttpNotFound();
            }

            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();

            TwitterHub hub = new TwitterHub();

            hub.Notification(tweet.Author.Id);

            return this.Redirect("/" + user.UserName);
        }
Ejemplo n.º 9
0
        public ActionResult PostTweet(PostTweetBindingModel model)
        {
            string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

            var user = this.Data.Users.Find(currentUserId);

            if (this.ModelState.IsValid)
            {
                var newTweet = new Tweet
                    {
                        Content = model.Content,
                        PageUrl = string.Empty,
                        Date = DateTime.Now,
                        AuthorId = user.Id
                    };

                this.Data.Tweets.Add(newTweet);
                this.Data.SaveChanges();

                TwitterHub hub = new TwitterHub();

                hub.Tweet(user.Followers.Select(u => u.Id).ToList(), newTweet.Id);

                return this.RedirectToAction("Index", "Home", new { Message = ManageMessageId.PostTweetSucess });
            }

            return this.RedirectToAction("Index", "Home", new { Message = ManageMessageId.Error });
        }
        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));
        }
        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");
        }