Beispiel #1
0
        public JsonResult getMessages(Guid id)
        {
            JodADataContext db = new JodADataContext();
            List<dynamic> jasons = new List<dynamic>();

            ChatRoom room = (from p in db.ChatRooms
                                 where p.RoomID == id
                                 select p).First();

            ChatMessage[] msg = room.ChatMessages.OrderByDescending(d => d.Date).ToArray();

            foreach (var message in msg)
            {
                bool missing = !System.IO.File.Exists(Server.MapPath("~/uploads/" + message.Image));
                string file = (missing ? null : message.Image);
                var js = new
                {
                    date = message.Date,
                    user = message.aspnet_User.UserName,
                    text = message.Text,
                    image = file,
                    missing = missing
                };

                jasons.Add(js);
            }

            return Json(jasons, JsonRequestBehavior.AllowGet);
        }
Beispiel #2
0
        public ActionResult delete(Guid id, FormCollection collection)
        {
            JodADataContext db = new JodADataContext();
            ChatRoom deleteChat = (from p in db.ChatRooms
                                 where p.RoomID == id
                                 select p).First();

            aspnet_User user = (from p in db.aspnet_Users
                                        where p.UserName == User.Identity.Name
                                        select p).First();
            if (deleteChat.aspnet_User != user)
            {
                Session["jomarmessage"] = "You can't delete a room you don't own.";
                return Redirect(Url.Action("", "Rooms"));
            }

            if (ModelState.IsValid)
            {
                db.UserRooms.DeleteAllOnSubmit(deleteChat.UserRooms);
                db.ChatMessages.DeleteAllOnSubmit(deleteChat.ChatMessages);
                db.ChatRooms.DeleteOnSubmit(deleteChat);
                db.SubmitChanges();

                return Redirect(Url.Action("", "Rooms"));
            }
            return View(deleteChat);
        }
Beispiel #3
0
        public ActionResult delete(Guid id)
        {
            JodADataContext db = new JodADataContext();
            ChatRoom room = (from p in db.ChatRooms
                             where p.RoomID.ToString() == (String)Url.RequestContext.RouteData.Values.Last().Value
                             select p).First();

            return View(room);
        }
Beispiel #4
0
        /// <summary>
        /// Find a private room for two users.
        /// </summary>
        /// <param name="user1">First user's Profile</param>
        /// <param name="user2">Second user's Profile</param>
        /// <param name="db">JodADataContext</param>
        /// <returns></returns>
        public static ChatRoom Private(Profile user1, Profile user2, JodADataContext db)
        {
            // Possible names of private rooms between these users.
            String A = user1.CellPhone + user2.CellPhone;
            String B = user2.CellPhone + user1.CellPhone;

            // No rooms with oneself
            if (A == B) return null;

            // Select rooms where name is A or B and the room is private, not public and with only two users
            List<ChatRoom> rooms = (from r in db.ChatRooms
                                     where (r.Name == A || r.Name == B && r.isPrivate && !r.isPublic && r.UserRooms.Count == 2)
                                     select r).ToList();

            ChatRoom newRoom;
            // Return private room, create if necessary
            if (rooms.Count == 0)
            {
                // Prepare new private room
                newRoom = new ChatRoom();
                newRoom.UserID = user1.UserId;
                newRoom.RoomID = Guid.NewGuid();
                newRoom.isPrivate = true;
                newRoom.Name = A;

                // Insert new room
                db.ChatRooms.InsertOnSubmit(newRoom);

                // Join user 1 to room
                UserRoom nr = new UserRoom();
                nr.UserID = user1.UserId;
                nr.RoomID = newRoom.RoomID;

                // Join user 2 to room
                UserRoom nr2 = new UserRoom();
                nr2.UserID = user2.UserId;
                nr2.RoomID = newRoom.RoomID;

                // Insert user_room relationsships
                db.UserRooms.InsertOnSubmit(nr);
                db.UserRooms.InsertOnSubmit(nr2);

                // Submit changes to DB
                db.SubmitChanges();

                return newRoom;
            }
            else return rooms.First();
        }
Beispiel #5
0
        /// <summary>
        /// Post message to room.
        /// </summary>
        /// <param name="msg">The message.</param>
        /// <param name="user">The user id</param>
        /// <param name="room">The room id</param>
        /// <param name="db">JodADataContext</param>
        public static void Post(string msg, Guid user, Guid room, JodADataContext db, string image="")
        {
            ChatMessage message = new ChatMessage();
            message.Date = DateTime.Now;
            message.MessageID = Guid.NewGuid();
            message.UserID = user;
            message.RoomID = room;
            message.Text = msg;
            if (image != "")
                message.Image = image;

            // Submit message to DB
            db.ChatMessages.InsertOnSubmit(message);
            db.SubmitChanges();
        }
Beispiel #6
0
        public ChatModel(ChatRoom room, JodADataContext db)
        {
            MessageBoard = room.ChatMessages.ToArray();

            Users = (from user in db.aspnet_Users
                           join m2m in db.UserRooms on user.UserId equals m2m.UserID
                           where m2m.RoomID == room.RoomID
                           select user).ToList();

            if (!Users.Contains(room.aspnet_User))
                Users.Add(room.aspnet_User);

            Name = room.Name;
            RoomID = room.RoomID;
            Private = room.isPrivate;
        }
Beispiel #7
0
        public ActionResult create(ChatRoom model, string returnUrl)
        {
            JodADataContext db = new JodADataContext();

            if (ModelState.IsValid)
            {
                model.UserID = (from p in db.aspnet_Users
                                where p.UserName == User.Identity.Name
                                select p).First().UserId;
                db.ChatRooms.InsertOnSubmit(model);
                model.RoomID = Guid.NewGuid();

                db.SubmitChanges();

                return Redirect(Url.Action(model.Name, "Chat"));
            }
            return View(model);
        }
Beispiel #8
0
        /// <summary>
        /// Is the given user a member in this room?
        /// </summary>
        /// <param name="user">user id</param>
        /// <param name="db">JodADataContext</param>
        /// <returns>true/false</returns>
        public bool IsMember(Guid user, JodADataContext db)
        {
            // See if user is a member already
            foreach (aspnet_User u in Users)
                if (u.UserId == user) return true;

            if (Private) // Private room
                return false;
            else
            {
                UserRoom r = new UserRoom();
                r.UserID = user;
                r.RoomID = RoomID;
                db.UserRooms.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;
            }
        }
Beispiel #9
0
        public ActionResult Index(string name, string id, FormCollection collection)
        {
            if (name == User.Identity.Name)
            {
                Session["jomarmessage"] = "Schizophrenia!";
                return Redirect(Url.Action("Public", "Rooms"));
            }

            JodADataContext db = new JodADataContext();
            ChatRoom room = null;

            Profile user = JoMAR.Models.Profile.GetProfile(User.Identity.Name);
            Profile user2 = JoMAR.Models.Profile.GetProfile(name);

            // Does the second user exist?
            if (user2 != null) // find private room
                room = Rooms.Private(user, user2, db);
            else // Find public room
                room = Rooms.Public(name, db);

            // No room found?
            if (room == null)
            {
                Session["jomarmessage"] = "No room found by that name!";
                return Redirect(Url.Action("", "Rooms"));
            }

            var model = new ChatModel(room, db);
            ViewBag.Title = model.Name;

            // Is the user member while the room is private?
            if (model.IsMember(user.UserId, db))
            {
                Rooms.Post(collection["Message"], user.UserId, room.RoomID, db);
                return View(model);
            }
            else
            {
                Session["jomarmessage"] = "The room you tried to access is private, members only!";
                return Redirect(Url.Action("", "Rooms"));
            }
        }
Beispiel #10
0
        public JsonResult getUsersOnRoom(Guid id)
        {
            JodADataContext db = new JodADataContext();
            List<Jason> userList = new List<Jason>();

            ChatRoom room = (from p in db.ChatRooms where p.RoomID == id select p).First();

            foreach (UserRoom ur in room.UserRooms)
            {
                Profile p = Profile.GetProfile(ur.aspnet_User.UserName);
                Jason jj = new Jason();
                jj.Gravatar = p.Image;
                jj.Email = p.Email;
                jj.UserId = p.UserId;
                jj.UserName = p.UserName;
                userList.Add(jj);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();

            return Json(js.Serialize(userList), JsonRequestBehavior.AllowGet);
        }
Beispiel #11
0
        public ContentResult Index(Guid room, HttpPostedFileBase file)
        {
            Profile user = JoMAR.Models.Profile.GetProfile(User.Identity.Name);
            JodADataContext db = new JodADataContext();

            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
                file.SaveAs(path);

                Rooms.Post("File uploaded by "+ user.UserName, user.UserId, room, db, fileName);
            }

            ContentResult r = new ContentResult();
            r.Content = "<script type='text/javascript'>window.close();</script>";
            return r;
        }
Beispiel #12
0
        public JsonResult getRooms()
        {
            JodADataContext db = new JodADataContext();
            List<dynamic> rooms = new List<dynamic>();

            ChatRoom[] rA = (from p in db.ChatRooms
                            where p.isPublic
                            select p).ToArray();

            foreach (var r in rA)
            {
                var js = new
                {
                    name = r.Name,
                    owner = r.aspnet_User.UserName,
                    avatar = Tools.Gravatar(r.aspnet_User.aspnet_Membership.Email, r.Name),
                    isPrivate = r.isPrivate
                };
                rooms.Add(js);
            }

            return Json(rooms, JsonRequestBehavior.AllowGet);
        }
Beispiel #13
0
        public ActionResult Public()
        {
            JodADataContext db = new JodADataContext();
            var rooms = db.ChatRooms.ToList();

            return View(rooms);
        }
Beispiel #14
0
        public ActionResult Private(FormCollection collection)
        {
            JodADataContext db = new JodADataContext();
            aspnet_User newMember = (from p in db.aspnet_Users
                                where p.UserId.ToString() == collection["SelectedValue"]
                                select p).First();
            ChatRoom room = (from p in db.ChatRooms
                                where p.RoomID.ToString() == collection["item.RoomID"]
                                select p).First();
            aspnet_User user = (from p in db.aspnet_Users
                                where p.UserName == User.Identity.Name
                                select p).First();

            if (room.UserID != user.UserId)
            {
                Session["jomarmessage"] = "You can't add users to a room you don't own.";
                return Redirect(Url.Action("Private", "Rooms"));
            }

            List<Guid> mmm = new List<Guid>();
            foreach (var s in room.UserRooms)
                mmm.Add(s.UserID);

            if (!mmm.Contains(newMember.UserId)) {
                UserRoom ur = new UserRoom();
                ur.RoomID = room.RoomID;
                ur.UserID = newMember.UserId;
                db.UserRooms.InsertOnSubmit(ur);
                db.SubmitChanges();
            }

            Models.MyRoom model = new Models.MyRoom(user, db);
            return View(model);
        }
Beispiel #15
0
        public ActionResult Profile(FormCollection collection)
        {
            JodADataContext db = new JodADataContext();

            // Prepare message for submit
            aspnet_User usr = (from p in db.aspnet_Users
                              where p.UserName == User.Identity.Name
                              select p).First();

            aspnet_Membership ma = (from m in db.aspnet_Memberships
                                   where m.UserId == usr.UserId
                                   select m).First();
            ma.Email = collection["Email"];

            // Submit message to DB
            db.SubmitChanges();

            JoMAR.Models.Profile user = JoMAR.Models.Profile.GetProfile(User.Identity.Name);

            user.FirstName = collection["FirstName"];
            user.LastName = collection["LastName"];

            user.CellPhone = collection["CellPhone"];
            user.Save();
            return View(user);
        }
Beispiel #16
0
        public MyRoom(aspnet_User user, JodADataContext db)
        {
            Users = db.aspnet_Users.Select(x => new SelectListItem
            {
                Text = x.UserName,
                Value = x.UserId.ToString()
            }).ToList();

            Memberships = (from room in db.ChatRooms
                     join m2m in db.UserRooms on room.RoomID equals m2m.RoomID
                     where m2m.UserID == user.UserId
                     select room).ToList();

            MyRooms = user.ChatRooms;
            foreach (ChatRoom room in MyRooms)
                Memberships.Remove(room);
        }
Beispiel #17
0
        public ActionResult Private()
        {
            JodADataContext db = new JodADataContext();
            aspnet_User user = (from p in db.aspnet_Users
                                where p.UserName == User.Identity.Name
                                select p).First();

            Models.MyRoom model = new Models.MyRoom(user, db);

            return View(model);
        }
Beispiel #18
0
        /// <summary>
        /// Find a public room.
        /// </summary>
        /// <param name="name">name to look for</param>
        /// <param name="db">JodADataContext</param>
        /// <returns>ChatRoom</returns>
        public static ChatRoom Public(string name, JodADataContext db)
        {
            List<ChatRoom> rooms;
            rooms = (from p in db.ChatRooms where p.Name == name select p).ToList();

            // Found any rooms by that name?
            if (rooms.Count == 0)
                return null;
            else  // Return the first room found
                return rooms.First();
        }
Beispiel #19
0
        /// <summary>
        /// Remove task from cache, perform task and insert task again for next run
        /// </summary>
        public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
        {
            // Read SMS's from server
            if (k == "ReadSMS")
            {
                JodADataContext db = new JodADataContext();
                IQueryable<string> users = (from p in db.aspnet_Users select p.UserName.ToLower());
                IQueryable<string> rooms = (from p in db.ChatRooms select p.Name.ToLower());

                foreach (string user in users)
                {
                    // Fetch user profile
                    Profile p = Profile.GetProfile(user);

                    // If user has a cell phone
                    if (p.CellPhone.Length == 10)
                    {
                        // Get phone number
                        long nr = Int64.Parse(p.CellPhone);

                        // Retrieve all SMS from this user
                        while (true)
                        {
                            SMS sms = SMS.Get(nr);
                            if (sms.status == "NoSuchMessage")
                                break;
                            else
                            {
                                string[] text = Regex.Split(sms.txt, @"\W+");
                                bool found = false;
                                string candidate = text[0].ToLower();

                                // If UserName found, post to private room
                                if (users.Contains(candidate))
                                {
                                    found = true;
                                    Profile user1 = Profile.GetProfile(user);
                                    Profile user2 = Profile.GetProfile(candidate);
                                    ChatRoom room = Rooms.Private(user1, user2, db);

                                    // Prepare message
                                    string msg = "";
                                    foreach (string s in text)
                                        if (s != text[0])
                                            msg += s + " ";

                                    // Post message to room
                                    Rooms.Post(msg, user1.UserId, room.RoomID, db);

                                    // Forward message to the other user
                                    SMS.Send(user1.UserName + " said: " + msg, "JoMAR", user2.CellPhone);
                                    //Debug.WriteLine("Found message to user '" + text[0] + "'");
                                }

                                // If Room found, post.
                                if (rooms.Contains(text[0].ToLower()))
                                {
                                    found = true;

                                    // Prepare message
                                    string msg = "";
                                    foreach (string s in text)
                                        if (s != text[0])
                                            msg += s + " ";

                                    // Find user and room
                                    Profile user1 = Profile.GetProfile(user);
                                    ChatRoom room = Rooms.Public(candidate, db);

                                    // Post message to room
                                    Rooms.Post(msg, user1.UserId, room.RoomID, db);
                                    //Debug.WriteLine("Found message to room '" + text[0] + "'");
                                }

                                // Reply with not found if no room found.
                                if (!found)
                                    SMS.Send("Thanks for you interest in us, but we didn't find the user or room you seek.", "JoMAR", sms.snd);
                            }

                        }
                    }
                }
            }

            AddTask(k, Convert.ToInt32(v));
        }