public ActionResult Login()
        {
            string user_name = Request.Form["username"];

            if (user_name.Trim() == "")
            {
                return(Redirect("/"));
            }


            using (var db = new Models.ChatContext()) {
                User user = db.Users.FirstOrDefault(u => u.Name == user_name);

                if (user == null)
                {
                    user = new User {
                        Name = user_name, CreatedAt = DateTime.Now
                    };

                    db.Users.Add(user);
                    db.SaveChanges();
                }

                Session["user"] = user;
            }

            return(Redirect("/chat"));
        }
        public JsonResult MessageDelivered(int message_id)
        {
            Conversation convo = null;

            using (var db = new Models.ChatContext())
            {
                convo = db.Conversations.FirstOrDefault(c => c.id == message_id);
                if (convo != null)
                {
                    convo.status          = Conversation.messageStatus.Delivered;
                    db.Entry(convo).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
            string socket_id           = Request.Form["socket_id"];
            var    conversationChannel = getConvoChannel(convo.sender_id, convo.receiver_id);

            pusher.TriggerAsync(
                conversationChannel,
                "message_delivered",
                convo,
                new TriggerOptions()
            {
                SocketId = socket_id
            });
            return(Json(convo));
        }
        public JsonResult SendMessage()
        {
            if (Session["user"] == null)
            {
                return(Json(new { status = "error", message = "User is not logged in" }));
            }
            var          currentUser = (User)Session["user"];
            var          contact     = Convert.ToInt32(Request.Form["contact"]);
            string       socket_id   = Request.Form["socket_id"];
            Conversation convo       = new Conversation
            {
                sender_id   = currentUser.id,
                message     = Request.Form["message"],
                receiver_id = contact
            };

            using (var db = new Models.ChatContext()) {
                db.Conversations.Add(convo);
                db.SaveChanges();
            }
            var conversationChannel = getConvoChannel(currentUser.id, contact);

            pusher.TriggerAsync(
                conversationChannel,
                "new_message",
                convo,
                new TriggerOptions()
            {
                SocketId = socket_id
            });
            return(Json(convo));
        }
Ejemplo n.º 4
0
        public JsonResult ConversationWithContact(int contact)
        {
            if (Session["user"] == null)
            {
                return(Json(new { status = "error", message = "User is not logged in" }));
            }

            var currentUser = (Models.User)Session["user"];

            var conversations = new List <Models.Conversation>();

            using (var db = new Models.ChatContext())
            {
                conversations = db.Conversations.
                                Where(c => (c.receiver_id == currentUser.id &&
                                            c.sender_id == contact) ||
                                      (c.receiver_id == contact &&
                                       c.sender_id == currentUser.id))
                                .OrderBy(c => c.created_at)
                                .ToList();
            }

            return(Json(
                       new { status = "success", data = conversations },
                       JsonRequestBehavior.AllowGet
                       ));
        }
Ejemplo n.º 5
0
        public JsonResult SendMessage()
        {
            if (Session["user"] == null)
            {
                return(Json(new { status = "error", message = "User is not logged in" }));
            }

            var currentUser = (User)Session["user"];

            string socket_id = Request.Form["socket_id"];

            Conversation convo = new Conversation
            {
                sender_id   = currentUser.id,
                message     = Request.Form["message"],
                receiver_id = Convert.ToInt32(Request.Form["contact"])
            };

            using (var db = new Models.ChatContext())
            {
                db.Conversations.Add(convo);
                db.SaveChanges();
            }

            return(Json(convo));
        }
Ejemplo n.º 6
0
        public ActionResult DeleteAll()
        {
            using (var db = new Models.ChatContext())
            {
                Console.WriteLine("DELETE ALL MOFOS");
                db.Users.RemoveRange(db.Users);
                db.Conversations.RemoveRange(db.Conversations);
                db.SaveChanges();
            }

            return(Json(new { status = "success" }, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 7
0
        // GET: Chat
        public ActionResult Index()
        {
            if (Session["user"] == null)
            {
                return(Redirect("/"));
            }
            var currentUser = (Models.User)Session["user"];

            using (var db = new Models.ChatContext())
            {
                ViewBag.allUsers = db.Users.Where(u => u.Name != currentUser.Name).ToList();
            }
            ViewBag.currentUser = currentUser;
            return(View());
        }
Ejemplo n.º 8
0
        public JsonResult SendMessage()
        {
            if (Session["user"] == null)
            {
                return(Json(new { status = "error", message = "User is not logged in" }));
            }

            var currentUser = (User)Session["user"];

            string socket_id = Request.Form["socket_id"];

            Conversation convo = new Conversation
            {
                sender_id   = currentUser.id,
                message     = Request.Form["message"],
                receiver_id = Convert.ToInt32(Request.Form["contact"])
            };

            using (var db = new Models.ChatContext()) {
                db.Conversations.Add(convo);
                db.SaveChanges();
            }


            var options = new PusherOptions();

            options.Cluster = "PUSHER_APP_CLUSTER";

            var pusher = new Pusher(
                "PUSHER_APP_ID",
                "PUSHER_APP_KEY",
                "PUSHER_APP_SECRET", options);

            pusher.TriggerAsync(
                "presence-chat",
                "new_message",
                convo,
                new TriggerOptions()
            {
                SocketId = socket_id
            });

            return(Json(convo));
        }
Ejemplo n.º 9
0
        public JsonResult MessageDelivered(int message_id)
        {
            Conversation convo = null;

            using (var db = new Models.ChatContext())
            {
                convo = db.Conversations.FirstOrDefault(c => c.id == message_id);

                if (convo != null)
                {
                    convo.status          = Conversation.messageStatus.Delivered;
                    db.Entry(convo).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }

            string socket_id = Request.Form["socket_id"];


            var options = new PusherOptions();

            options.Cluster = "PUSHER_APP_CLUSTER";

            var pusher = new Pusher(
                "PUSHER_APP_ID",
                "PUSHER_APP_KEY",
                "PUSHER_APP_SECRET", options);

            pusher.TriggerAsync(
                "presence-chat",
                "message_delivered",
                convo,
                new TriggerOptions()
            {
                SocketId = socket_id
            });

            return(Json(convo));
        }