Ejemplo n.º 1
0
        public ActionResult CreateTweet(TweetBindingModel model)
        {
            var loggedUserId = this.User.Identity.GetUserId();
            User user = this.Data.Users.Find(loggedUserId);
            if (ModelState.IsValid)
            {
                if (model.Content == "")
                {
                    return RedirectToAction("Index", "Home");
                }
                var tweet = new Tweet()
                {
                    Content = model.Content,
                    CreatedAt = DateTime.Now,
                    UserId = loggedUserId
                };

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

                return RedirectToAction("ShowProfile", "Users");
            }

            return View("Tweet/_CreateTweetPartial");
        }
Ejemplo n.º 2
0
        public ActionResult PostTweet(TweetBindingModel model)
        {
            string userId = this.User.Identity.GetUserId();
            var user = this.Data.Users.All()
                .FirstOrDefault(u => u.Id == userId);

            if (!this.ModelState.IsValid)
            {
                this.TempData["resultMessage"] = "Error";
            }
            else
            { 
                var newTweet = new Tweet()
                {
                    Content = model.Content,
                    CreatedOn = DateTime.Now,
                    URL = "asd",
                    UserId = userId
                };

                this.Data.Tweets.Add(newTweet);
                this.Data.SaveChanges();
                this.TempData["resultMessage"] = "Success";
            }

            var data = this.TempData["resultMessage"];
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<TweetsHub>();

            hubContext.Clients.All.receiveTweet(user.UserName, model.Content);

            return Content("Tweeted");
        }
        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 Create(TweetViewModel tweet)
        {
            var tagsMatches = Regex.Matches(tweet.Content, @"(#[a-zA-Z_\d-]+)");
            var tags = new List<Tag>();

            foreach (Match tag in tagsMatches)
            {
                tags.Add(new Tag()
                {
                    Name = tag.Value
                });
            }

            var userId = User.Identity.GetUserId();

            var newTweet = new Tweet()
            {
                AuthorId = userId,
                Content = tweet.Content,
                Tags = tags
            };

            this.tweets.Create(newTweet);

            return this.RedirectToAction("Index", "Home");

        }
 public ActionResult Edit(Tweet tweet)
 {
     if (ModelState.IsValid)
     {
         db.Entry(tweet).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(tweet);
 }
        public ActionResult Create(Tweet tweet)
        {
            if (ModelState.IsValid)
            {
                db.Tweets.Add(tweet);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tweet);
        }
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, Tweet tweet)
        {
            var results = new List<Tweet>();

            if (ModelState.IsValid)
            {
                this.Data.Tweets.Add(tweet);
                this.Data.SaveChanges();
                results.Add(tweet);
            }

            return Json(results.ToDataSourceResult(request, ModelState));
        }
 public ActionResult post(string content)
 {
     var userId = User.Identity.GetUserId();
     var tweet = new Tweet()
     {
         Content = content,
         SendOn = DateTime.Now,
         UserId = userId
     };
     this.Data.Tweets.Add(tweet);
     this.Data.SaveChanges();
     return RedirectToAction("index", "Home");
 }
Ejemplo n.º 9
0
        public ActionResult Add(TweetInputModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                return this.View(model);
            }

            var tweet = new Tweet { Text = model.Text, UserId = this.UserProfille.Id };
            this.Data.Tweets.Add(tweet);
            this.Data.SaveChanges();

            return this.RedirectToAction("View", new { id = tweet.Id});
        }
        public IHttpActionResult AddTweet([FromBody]AddTweetBindingModel model)
        {
            var loggedUserId = this.User.Identity.GetUserId();
            var loggedUser = this.TwitterData.Users.Find(loggedUserId);
            if (loggedUser == null)
            {
                return this.BadRequest("Invalid session token.");
            }

            if (model == null)
            {
                return this.BadRequest("Model cannot be null (no data in request)");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var wallOwner = this.TwitterData.Users.All()
                .FirstOrDefault(u => u.UserName == model.WallOwnerUsername);

            if (wallOwner == null)
            {
                return this.BadRequest(string.Format(
                    "User {0} does not exist",
                    model.WallOwnerUsername));
            }

            var tweet = new Tweet()
            {
                Author = loggedUser,
                AuthorId = loggedUserId,
                WallOwnerId = wallOwner.Id,
                WallOwner = wallOwner,
                Content = model.Content,
                PostedOn = DateTime.UtcNow,
            };

            this.TwitterData.Tweets.Add(tweet);
            this.TwitterData.SaveChanges();

            var data = this.TwitterData.Tweets.All()
                .Where(p => p.Id == tweet.Id)
                .Select(TweetViewModel.Create)
                .FirstOrDefault();

            return this.Ok(data);
        }
        public ActionResult Edit([DataSourceRequest] DataSourceRequest request, Tweet tweet)
        {
            var results = new List<Tweet>();

            if (ModelState.IsValid)
            {
                var tweetToUpdate = this.Data.Tweets.GetById(tweet.Id);
                tweetToUpdate.Content = tweet.Content;
                this.Data.Tweets.Update(tweetToUpdate);
                this.Data.SaveChanges();
                results.Add(tweet);
            }

            return Json(results.ToDataSourceResult(request, ModelState));
        }
 public TweetViewModel(Tweet tweet)
 {
     this.Id = tweet.Id;
     this.Content = tweet.Content;
     this.PublishDate = tweet.PublishDate;
     this.Owner = new UserViewModel(tweet.Owner);
     this.IsReported = tweet.IsReported;
     this.RetweetsCount = tweet.Retweeters.Count;
     this.FavoritesCount = tweet.FavoritedUsers.Count;
     this.Replies = new List<RepliesViewModel>();
     foreach (var reply in tweet.Replies)
     {
         this.Replies.Add(new RepliesViewModel(reply));
     }
 }
 public ActionResult Tweet(TweetBindingModel model)
 {
     var loggedUser = this.Data.Users.Find(this.User.Identity.GetUserId());
     if (ModelState.IsValid)
     {
         var tweet = new Tweet
         {
             Content = model.Content,
             PublishDate = DateTime.Now,
             OwnerId = loggedUser.Id
         };
         this.Data.Tweets.Add(tweet);
         this.Data.SaveChanges();
         return RedirectToAction("UserHome", "Home");
     }
     ModelState.AddModelError("contentError", "Message");
     return RedirectToAction("Tweet", "Tweets", model) ;
 }
Ejemplo n.º 14
0
        public ActionResult Add(AddTweetBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var currentUserId = this.User.Identity.GetUserId();
                var tweet = new Tweet
                {
                    Content = model.Content,
                    Page = model.PageUrl,
                    TweetedAt = DateTime.Now,
                    UserId = currentUserId
                };
                this.TwitterData.Tweets.Add(tweet);
                this.TwitterData.SaveChanges();
                return this.RedirectToAction("Home", "Users");
            }

            return this.View("_AddTweetPartial", model);
        }
        public ActionResult CreateTweet(TweetBindingModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid Data");
            }

            var tweet = new Tweet
            {
                Message = model.Message,
                TimeStamp = DateTime.Now,
                User = this.Data.ApplicationUsers.GetById(this.User.Identity.GetUserId())
            };

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

            return this.RedirectToAction("Index", "Home");
        }
Ejemplo n.º 16
0
 public ActionResult Add(AddTweetBindingModel model)
 {
     if (model != null && this.ModelState.IsValid)
     {
         var currentUserId = this.User.Identity.GetUserId();
         var tweet = new Tweet
                     {
                         Content = model.Content,
                         Page = model.PageUrl,
                         TweetedAt = DateTime.Now,
                         UserId = currentUserId
                     };
         this.TwitterData.Tweets.Add(tweet);
         this.TwitterData.SaveChanges();
         this.TempData[GlobalConstants.TempMessageKey] = "You have successfully added your tweet!";
         return this.RedirectToAction("Home", "Users");
     }
     this.TempData[GlobalConstants.TempMessageKey] = "Error during add tweet";
     return this.View("_AddTweetPartial", model);
 }
        public ActionResult Create(TweetInputModel model)
        {
            if (this.ModelState.IsValid && this.ModelState != null)
            {
                var authorId = this.User.Identity.GetUserId();
                var user = this.Data.Users.All().FirstOrDefault(u => u.Id == authorId);
                var tweet = new Tweet()
                {
                    Content = model.Content,
                    Author = user,
                    CreatedOn = DateTime.Now
                };
                this.Data.Tweets.Add(tweet);
                this.Data.SaveChanges();
                
                return RedirectToAction("Profile", "User",new {Id=user.Id});

            }
            TempData["shortMessage"] = this.ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage;
            return RedirectToAction("Index", "Home");
        }
Ejemplo n.º 18
0
        public ActionResult PostTweet(TweetCreateModel tweet)
        {
            if (ModelState.IsValid)
            {
                var tweetToAdd = new Tweet() { Text = tweet.Text, DateCreated = DateTime.Now };
                foreach (var tag in tweet.Tags)
                {
                    var tagToAdd = this.Data.Tags.GetById(tag);
                    tweetToAdd.Tags.Add(tagToAdd);
                }

                var username = User.Identity.Name;
                var user = this.Data.Users.All().SingleOrDefault(u => u.UserName == username);
                tweetToAdd.User = user;

                this.Data.Tweets.Add(tweetToAdd);
                this.Data.SaveChanges();
                return RedirectToAction("Index", new { successMessage = "Tweet posted" });
            }

            return RedirectToAction("Index", new { errorMessage = "Tweet text out of character limit - [5, 240]" });
        }
Ejemplo n.º 19
0
        public ActionResult Create(TweetCreateViewModel tweet)
        {
            if (tweet != null && ModelState.IsValid)
            {
                var tags = tweet.Tags
                    .Split(new string[] { ",", ", ", " , ", " ," }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(t => new Tag() { Name = t });
                var user = this.Data.Users.All().FirstOrDefault(u => u.UserName == User.Identity.Name);
                var tweetToCreate = new Tweet()
                {
                    User = user, 
                    Content = tweet.Content,
                    Tags = new HashSet<Tag>(tags)
                };
                tweet.User = user;
                this.Data.Tweets.Add(tweetToCreate);
                this.Data.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tweet);
        }
Ejemplo n.º 20
0
        public ActionResult PostTweet(PostTweetBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.TempData["postTweetError"] =
                    "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 tweet = new Tweet()
            {
                Content = model.Content,
                CreatedOn = DateTime.Now,
                UserId = userId
            };

            this.data.Tweets.Add(tweet);
            this.data.Tweets.SaveChanges();

            this.TempData["postTweetSuccess"] = "Tweet sent successfully";

            // Broadcast the new tweet to all users
            //var tweetsHubContext = GlobalHost.ConnectionManager.GetHubContext<TweetsHub>();

            //tweetsHubContext.Clients.All.updateFeed(tweet);

            var hostname = HttpContext.Request.Url.Host;

            return Content(ReloadScript);
        }
        public ActionResult AddTweetInCategory(Tweet tweet, int id)
        {
            if (ModelState.IsValid)
            {
                this.Data.Tweets.Add(new Tweet
                {
                    Title = tweet.Title,
                    Details = tweet.Details,
                    ImageUrl = tweet.ImageUrl,
                    SentToDate = DateTime.Now,
                    CategoryId = id,
                    AuthorId = this.UserProfile.Id
                });
                this.Data.SaveChanges();

                return this.RedirectToAction("NewTweet");
            }

            return this.View();
        }
        public ActionResult Reply(int id, string content)
        {
            var tweet = this.Data.Tweets.All().FirstOrDefault(t => t.Id == id);
            if (tweet == null)
            {
                // todo: set error message
                return RedirectToAction("index", "home");
            }

            var replyTweet = new Tweet()
            {
                Content = content,
                SendOn = DateTime.Now,
                UserId = User.Identity.GetUserId(),
                RepliedTweetId = tweet.Id
            };

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

            return RedirectToAction("getReply", new { id = tweet.Id });
        }
        public ActionResult EditNewTweet(int id, Tweet newTweet)
        {
            var currentTweet = this.Data.Tweets.Find(id);
            if (currentTweet == null)
            {
                return this.HttpNotFound();
            }

            if (ModelState.IsValid)
            {
                currentTweet.Title = newTweet.Title;
                currentTweet.Details = newTweet.Details;
                currentTweet.ImageUrl = newTweet.ImageUrl;
                this.Data.SaveChanges();
                return RedirectToAction("NewTweet");
            }

            return this.View(currentTweet);
        }
 public ActionResult DeleteConfirmed([DataSourceRequest] DataSourceRequest request, Tweet tweet)
 {
     var results = new List<Tweet>();
     Tweet tweetToRemove = this.Data.Tweets.GetById((int)tweet.Id);
     this.Data.Tweets.Delete(tweetToRemove);
     this.Data.SaveChanges();
     results.Add(tweet);
     return Json(results.ToDataSourceResult(request, ModelState));
 }
Ejemplo n.º 25
0
        public ActionResult Edit(
            //[Bind(Include = "Content,Creator,User")] 
            Tweet tweet)
        {
            if (ModelState.IsValid)
            {
                var currTweet = db.Tweets.GetById(tweet.Id);

                var content = tweet.Content;
                currTweet.Content = content.ParseURL().ParseHashtag();
                currTweet.Creator = tweet.Creator;
                currTweet.User = tweet.User;
                currTweet.Tags = content.GetHashtag().Select(t => new Tag() { Text = t }).ToList();

                this.db.Tweets.Update(currTweet);
                this.db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tweet);
        }
Ejemplo n.º 26
0
        private ICollection<RepliesViewModel> AddTweetViewModelToResult(
            SortedSet<RepliesViewModel> result, Tweet tweet)
        {
            var user = new UserTweetViewModel();
            user.UserName = tweet.User.UserName;
            user.PictureUrl = tweet.User.PictureUrl;

            var repliesViewModel = new RepliesViewModel();
            repliesViewModel.User = user;
            repliesViewModel.Content = tweet.Content;
            repliesViewModel.CreatedOn = tweet.CreatedOn;
            repliesViewModel.Id = tweet.Id;

            if (tweet.FavoritedBy == null)
            {
                tweet.FavoritedBy = new HashSet<User>();
            }

            repliesViewModel.FavoritedBy = tweet.FavoritedBy;

            if (tweet.Replies == null)
            {
                tweet.Replies = new HashSet<Tweet>();
            }

            repliesViewModel.RepliesToOriginal = tweet.Replies;

            result.Add(repliesViewModel);

            return result;
        }
Ejemplo n.º 27
0
        private ICollection<RepliesViewModel> GetTweetRepliesTree(
            SortedSet<RepliesViewModel> result, Tweet reply)
        {
            if (reply.Replies.Count == 0 || reply.Replies == null)
            {
                return null;
            }

            foreach (var childReply in reply.Replies)
            {
                if (childReply.Replies == null)
                {
                    childReply.Replies = new HashSet<Tweet>();
                }

                AddTweetViewModelToResult(result, childReply);

                GetTweetRepliesTree(result, childReply);
            }

            return result;
        }        
Ejemplo n.º 28
0
        public async Task<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();

                return this.RedirectToAction("Index", "Home");
            }

            return this.PartialView("~/Views/Shared/_FormTweet.cshtml");
        }
Ejemplo n.º 29
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");
        }
Ejemplo n.º 30
0
        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 });
        }