Exemplo n.º 1
0
        public ActionResult Room(ChatModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                ChatMessage newMessage = new ChatMessage
                {
                    From = User.Identity.Name,
                    Body = model.Body,
                    Date = DateTime.Now,
                    isRead = false
                };

                if (model.ChatExists)
                {
                    var chat = db.ChatRooms.Find(model.ChatId);

                    if (model.Body != null)
                    {
                        chat.Messages.Add(newMessage);
                        db.SaveChanges();
                    }

                    return RedirectToLocal(returnUrl);
                }
            }

            return View(model);
        }
Exemplo n.º 2
0
        public ActionResult Room(string id)
        {
            // Get current user's id
            var myId = WebSecurity.GetUserId(User.Identity.Name);

            ChatModel model = new ChatModel();

            // Check for existing room in user's inbox
            if (db.ChatInboxes.Single(i => i.UserId == myId).ChatRooms.Any(cr => cr.ConnectionId == string.Format("{0};{1}", User.Identity.Name, id)
                || cr.ConnectionId == string.Format("{0};{1}", id, User.Identity.Name)))
            {
                var room = db.ChatInboxes.Single(i => i.UserId == myId).ChatRooms.Single(cr => cr.ConnectionId == string.Format("{0};{1}", User.Identity.Name, id)
                || cr.ConnectionId == string.Format("{0};{1}", id, User.Identity.Name));

                model.ChatExists = true;
                model.ChatId = room.ChatRoomId;
                model.ConnectionId = room.ConnectionId;
                model.ChatHistory = room.Messages.ToList();
            }

            else
            {
                model.ChatExists = false;
            }

            return View(model);
        }