public MessageViewModel(ChatMessage message)
 {
     Id = message.Id;
     Content = message.Content;
     User = new UserViewModel(message.User);
     When = message.When;
 }
Example #2
0
 public MessageViewModel(ChatMessage message, ChatRoom room)
 {
     Id = message.Id;
     Content = message.Content;
     User = new UserViewModel(message.User);
     When = message.When;
     Room = room.Name;
 }
Example #3
0
        private void HandleLeave(ChatRoom room, ChatUser user)
        {
            room.Users.Remove(user);
            user.Rooms.Remove(room);

            var userViewModel = new UserViewModel(user);
            Clients[room.Name].leave(userViewModel).Wait();

            RemoveFromGroup(room.Name).Wait();
        }
Example #4
0
        public bool Join()
        {
            Caller.version = typeof(Chat).Assembly.GetName().Version.ToString();

            // Check the user id cookie
            HttpCookie cookie = Context.Cookies["userid"];
            if (cookie == null) {
                return false;
            }

            ChatUser user = _db.Users.FirstOrDefault(u => u.Id == cookie.Value);

            // If there's no registered user, return false
            if (user == null) {
                return false;
            }

            // Update the users's client id mapping
            user.ClientId = Context.ClientId;
            user.Active = true;
            user.LastActivity = DateTime.UtcNow;

            var userViewModel = new UserViewModel(user);

            LeaveAllRooms(user);
            Caller.room = null;

            // Set some client state
            Caller.id = user.Id;
            Caller.name = user.Name;
            Caller.hash = user.Hash;

            // Add this user to the list of users
            Caller.addUser(userViewModel);
            return true;
        }
Example #5
0
        private void HandleGravatar(ChatUser user, string[] parts)
        {
            string email = String.Join(" ", parts.Skip(1));

            if (String.IsNullOrWhiteSpace(email)) {
                throw new InvalidOperationException("Email was not specified!");
            }

            user.Hash = email.ToLowerInvariant().ToMD5();
            var userViewModel = new UserViewModel(user);

            if (user.Rooms.Any()) {
                foreach (var room in user.Rooms) {
                    Clients[room.Name].changeGravatar(userViewModel);
                }
            }
            else {
                Caller.changeGravatar(userViewModel);
            }
        }
Example #6
0
        private void HandleJoin(string oldRoomName, ChatUser user, string[] parts)
        {
            if (parts.Length < 2) {
                throw new InvalidOperationException("Join which room?");
            }

            var userViewModel = new UserViewModel(user);

            // Only support joining one room at a time for now (until we support tabs)
            ChatRoom oldRoom = _db.Rooms.FirstOrDefault(r => r.Name.Equals(oldRoomName, StringComparison.OrdinalIgnoreCase));
            if (oldRoom != null) {
                HandleLeave(oldRoom, user);
            }

            // Create the room if it doesn't exist
            string newRoomName = parts[1];
            ChatRoom newRoom = _db.Rooms.FirstOrDefault(r => r.Name.Equals(newRoomName, StringComparison.OrdinalIgnoreCase));
            if (newRoom == null) {
                newRoom = new ChatRoom {
                    Name = newRoomName
                };

                _db.Rooms.Add(newRoom);
            }

            if (user.Rooms.Contains(newRoom)) {
                throw new InvalidOperationException("You're already in that room!");
            }

            // Add this room to the user's list of rooms
            user.Rooms.Add(newRoom);

            // Add this user to the list of room's users
            newRoom.Users.Add(user);

            // Tell the people in this room that you're joining
            Clients[newRoom.Name].addUser(userViewModel).Wait();

            // Set the room on the caller
            Caller.room = newRoom.Name;

            // Add the caller to the group so they receive messages
            AddToGroup(newRoomName).Wait();

            Caller.joinRoom(newRoomName);
        }
Example #7
0
        private void AddUser(string name)
        {
            var user = new ChatUser {
                Name = name,
                Active = true,
                Id = Guid.NewGuid().ToString("d"),
                LastActivity = DateTime.UtcNow,
                ClientId = Context.ClientId
            };

            _db.Users.Add(user);

            Caller.name = user.Name;
            Caller.hash = user.Hash;
            Caller.id = user.Id;

            var userViewModel = new UserViewModel(user);
            Caller.addUser(userViewModel);
        }
Example #8
0
        private void ChangeUserName(ChatUser user, string newUserName)
        {
            if (user.Name.Equals(newUserName, StringComparison.OrdinalIgnoreCase)) {
                throw new InvalidOperationException("That's already your username...");
            }

            ChatUser newUser = _db.Users.FirstOrDefault(u => u.Name.Equals(newUserName, StringComparison.OrdinalIgnoreCase));

            if (newUser != null) {
                throw new InvalidOperationException(String.Format("Username '{0}' is already taken!", newUserName));
            }

            string oldUserName = user.Name;
            user.Name = newUserName;
            Caller.name = newUserName;

            var userViewModel = new UserViewModel(user);

            if (user.Rooms.Any()) {
                foreach (var room in user.Rooms) {
                    Clients[room.Name].changeUserName(userViewModel, oldUserName, newUserName);
                }
            }
            else {
                Caller.changeUserName(userViewModel, oldUserName, newUserName);
            }
        }
Example #9
0
        private void JoinRoom(ChatUser user, ChatRoom newRoom)
        {
            var userViewModel = new UserViewModel(user);

            // Add this room to the user's list of rooms
            user.Rooms.Add(newRoom);

            // Add this user to the list of room's users
            newRoom.Users.Add(user);

            // Tell the people in this room that you're joining
            Clients[newRoom.Name].addUser(userViewModel).Wait();

            // Set the room on the caller
            Caller.room = newRoom.Name;

            // Add the caller to the group so they receive messages
            AddToGroup(newRoom.Name).Wait();

            Caller.joinRoom(newRoom.Name);
        }
Example #10
0
        private void SetGravatar(ChatUser user, string hash)
        {
            // Set user hash
            user.Hash = hash;

            var userViewModel = new UserViewModel(user);

            if (user.Rooms.Any())
            {
                foreach (var room in user.Rooms)
                {
                    Clients[room.Name].changeGravatar(userViewModel);
                }
            }
            else
            {
                Caller.changeGravatar(userViewModel);
            }
        }
Example #11
0
        private void ChangeUserName(ChatUser user, string newUserName)
        {
            if (user.Name.Equals(newUserName, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("That's already your username...");
            }

            EnsureUserNameIsAvailable(newUserName);

            string oldUserName = user.Name;
            user.Name = newUserName;
            Caller.name = newUserName;

            var userViewModel = new UserViewModel(user);

            if (user.Rooms.Any())
            {
                foreach (var room in user.Rooms)
                {
                    Clients[room.Name].changeUserName(userViewModel, oldUserName, newUserName);
                }
            }
            else
            {
                Caller.changeUserName(userViewModel, oldUserName, newUserName);
            }
        }
Example #12
0
        public void Typing(bool isTyping)
        {
            Tuple<ChatUser, ChatRoom> tuple = EnsureUserAndRoom();

            ChatUser user = tuple.Item1;
            ChatRoom chatRoom = tuple.Item2;
            var userViewModel = new UserViewModel(user);

            if (user.Rooms.Any())
            {
                foreach (var room in user.Rooms)
                {
                    Clients[room.Name].setTyping(userViewModel, isTyping);
                }
            }
            else
            {
                Caller.setTyping(userViewModel, isTyping);
            }
        }
Example #13
0
        public bool Join()
        {
            Caller.version = typeof(Chat).Assembly.GetName().Version.ToString();

            // Check the user id cookie
            HttpCookie userIdCookie = Context.Cookies["userid"];
            HttpCookie userNameCookie = Context.Cookies["username"];
            HttpCookie userRoomCookie = Context.Cookies["userroom"];
            HttpCookie userHashCookie = Context.Cookies["userhash"];

            // setup user
            ChatUser user = null;

            // First try to retrieve user by id if exists
            if (userIdCookie != null && !String.IsNullOrWhiteSpace(userIdCookie.Value))
            {
                user = _db.Users.FirstOrDefault(u => u.Id == userIdCookie.Value);
            }

            // If we couldn't get user by id try it by name server could be reset
            if (user == null && userNameCookie != null && !String.IsNullOrWhiteSpace(userNameCookie.Value))
            {
                user = AddUser(userNameCookie.Value);
            }

            // If we have no user return false will force user to set new nick
            if (user == null)
            {
                return false;
            }

            // If we have user hash cookie set gravatar
            if (userHashCookie != null)
            {
                SetGravatar(user, userHashCookie.Value);
            }

            // Update the users's client id mapping
            user.ClientId = Context.ClientId;
            user.Active = true;
            user.LastActivity = DateTime.UtcNow;

            var userViewModel = new UserViewModel(user);

            Caller.room = null;

            // Set some client state
            Caller.id = user.Id;
            Caller.name = user.Name;
            Caller.hash = user.Hash;

            // If we have room add user to room
            if (userRoomCookie != null && !String.IsNullOrWhiteSpace(userRoomCookie.Value))
            {
                var userRoom = userRoomCookie.Value;

                var room = _db.Rooms.Where(x => x.Name == userRoom).FirstOrDefault();

                // If user has room name in the cookie but it doesn't exists create it!
                if (room == null)
                {
                    room = AddRoom(userRoom);
                }

                // Check if the user is already in the room if so let him rejoin
                if (IsUserInRoom(room, user))
                {
                    HandleRejoin(room, user);
                }
                // if the user is not in the room join the room
                else
                {
                    HandleJoin(null, user, new[] { room.Name, room.Name });
                }
            }
            // if user is in a room rejoin it
            else if (IsUserInARoom(user))
            {

                // retrieve user room
                var room = GetUserRoom(user);

                // handle the join of the room
                HandleRejoin(room, user);
            }

            // Add this user to the list of users
            Caller.addUser(userViewModel);
            return true;
        }
Example #14
0
        private ChatUser AddUser(string name)
        {
            if (!IsValidUserName(name))
            {
                throw new InvalidOperationException(String.Format("'{0}' is not a valid user name.", name));
            }

            EnsureUserNameIsAvailable(name);

            var user = new ChatUser
            {
                Name = name,
                Active = true,
                Id = Guid.NewGuid().ToString("d"),
                LastActivity = DateTime.UtcNow,
                ClientId = Context.ClientId
            };

            _db.Users.Add(user);

            Caller.name = user.Name;
            Caller.hash = user.Hash;
            Caller.id = user.Id;

            var userViewModel = new UserViewModel(user);
            Caller.addUser(userViewModel);

            return user;
        }
Example #15
0
        public void Typing(bool isTyping)
        {
            Tuple<ChatUser, ChatRoom> tuple = EnsureUserAndRoom();

            ChatUser user = tuple.Item1;
            ChatRoom chatRoom = tuple.Item2;
            var userViewModel = new UserViewModel(user, chatRoom);

            if (isTyping)
            {
                UpdateActivity();
            }

            if (chatRoom != null)
            {
                Clients[chatRoom.Name].setTyping(userViewModel, isTyping);
            }
            else
            {
                Caller.setTyping(userViewModel, isTyping);
            }
        }