public ActionResult Read(int? id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
     var currentUser = manager.FindById(User.Identity.GetUserId());
     User user;
     using (var dbContext = new YATContext())
     {
         user = dbContext.User.Where(p => p.Id.Contains(currentUser.Id)).FirstOrDefault();
     }
     string userid = user.Id;
     Message message = db.Messages.Find(id);
     Messaging msging = new Messaging();
     var messages = msging.getConversation(message.To.Id, message.From.Id).ToList();
     foreach (Message mess in messages)
     {
         if (mess.ToId.Equals(userid)) 
         { 
             msging.read(mess.To.Id, mess.Id);
         }
     }
     if (message == null)
     {
         return HttpNotFound();
     }
     return View(messages);
 }
        // GET: Messages
        public ActionResult Index()
        {
            
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var currentUser = manager.FindById(User.Identity.GetUserId());
            User user;
            using (var dbContext = new YATContext())
            {
                user = dbContext.User.Where(p => p.Id.Contains(currentUser.Id)).FirstOrDefault();
            }
            string userid = user.Id;
            if (userid == null)
            {
                var messages = db.Messages.Include(m => m.From).Include(m => m.To);
                return View(messages.ToList());
            }
            else
            {
                Messaging msg = new Messaging();
                var messages = msg.getInbox(userid).ToList();
                messages.Reverse();
                List<String> noDupes = new List<String>();
                List<Message> result = new List<Message>();
                foreach (Message message in messages){

                    if (noDupes.Contains(message.From.Id))
                    {
                        continue;
                    }
                    if (userid.Equals(message.From.Id))
                    {
                        if (!noDupes.Contains(message.To.Id))
                        {
                        noDupes.Add(message.To.Id);
                        result.Add(message);
                        }
                        continue;
                    }
                    noDupes.Add(message.From.Id);
                    result.Add(message);
                }

                return View(result.ToList());
            }
        }
        public ActionResult Read(string text, int hidden)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var currentUser = manager.FindById(User.Identity.GetUserId());
            User user;
            using (var dbContext = new YATContext())
            {
                user = dbContext.User.Where(p => p.Id.Contains(currentUser.Id)).FirstOrDefault();
            }
            string userid = user.Id;

            Message message = db.Messages.Find(hidden);
            Messaging msging = new Messaging();
            var toID = message.ToId;
            if (toID.Equals(userid))
            {
                toID = message.FromId;
            }
            msging.sendMessage(toID, userid, text);
            return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
        }
 public ActionResult SendMessage(string text, string hidden)
 {
     var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
     var currentUser = manager.FindById(User.Identity.GetUserId());
     User fromUser;
     using (var dbContext = new YATContext())
     {
         fromUser = dbContext.User.Where(p => p.Id.Contains(currentUser.Id)).FirstOrDefault();
     }
     var user = db.User.Find(hidden);
     Messaging msging = new Messaging();
     msging.sendMessage(hidden, fromUser.Id, text);
     return Redirect(Url.Content("~/Messages"));
 }