Exemplo n.º 1
0
 public ChatMessage(ChatUser user, string content)
 {
     User = user;
     Content = content;
     Id = Guid.NewGuid().ToString("d");
     When = DateTimeOffset.UtcNow;
 }
Exemplo n.º 2
0
 public UserViewModel(ChatUser user)
 {
     Name = user.Name;
     Hash = user.Hash;
     Id = user.Id;
     Active = user.Active;
 }
Exemplo n.º 3
0
 public UserViewModel(ChatUser user, ChatRoom room)
 {
     Name = user.Name;
     Hash = user.Hash;
     Id = user.Id;
     Active = user.Active;
     Room = room == null ? null : room.Name;
 }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
 public void LogOff(ChatUser user)
 {
     chatModel.Users.Remove(user);
     chatModel.Messages.Add(new ChatMessage()
     {
         Text = user.Name + " покинул чат.",
         Date = DateTime.Now
     });
 }
Exemplo n.º 7
0
        private ChatUser AddUser(string newUserName)
        {
            var offset = TimeSpan.FromHours(ResolveOffset());
            var user = new ChatUser(newUserName) {
                ClientId = Context.ClientId,
                Offset = offset,
                Timezone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(tz => tz.BaseUtcOffset == offset)
            };

            _users[newUserName] = user;
            _userRooms[newUserName] = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

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

            Caller.addUser(user);

            return user;
        }
Exemplo n.º 8
0
 private void HandleNudge(ChatRoom room, ChatUser user, string[] parts)
 {
     var betweenNudges = TimeSpan.FromMinutes(1);
     if (room.LastNudged == null || room.LastNudged < DateTime.Now.Subtract(betweenNudges)) {
         room.LastNudged = DateTime.Now;
         Clients[room.Name].nudge(user.Name);
     }
     else {
         throw new InvalidOperationException(String.Format("Room can only be nudged once every {0} seconds", betweenNudges.TotalSeconds));
     }
 }
Exemplo n.º 9
0
        private void HandleMsg(ChatUser user, string[] parts)
        {
            if (_db.Users.Count == 1) {
                throw new InvalidOperationException("You're the only person in here...");
            }

            if (parts.Length < 2 || String.IsNullOrWhiteSpace(parts[1])) {
                throw new InvalidOperationException("Who are you trying send a private message to?");
            }

            ChatUser toUser = _db.Users.FirstOrDefault(u => u.Name.Equals(parts[1], StringComparison.OrdinalIgnoreCase));

            if (toUser == null) {
                throw new InvalidOperationException(String.Format("Couldn't find any user named '{0}'.", toUser.Name));
            }

            if (toUser == user) {
                throw new InvalidOperationException("You can't private message yourself!");
            }

            string messageText = String.Join(" ", parts.Skip(2)).Trim();

            if (String.IsNullOrEmpty(messageText)) {
                throw new InvalidOperationException(String.Format("What did you want to say to '{0}'.", toUser.Name));
            }

            // Send a message to the sender and the sendee
            Clients[toUser.ClientId].sendPrivateMessage(user.Name, toUser.Name, messageText);
            Caller.sendPrivateMessage(user.Name, toUser.Name, messageText);
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
0
 private bool IsUserInRoom(ChatRoom room, ChatUser user)
 {
     return room.Users.Any(x => x.Name.Equals(user.Name, StringComparison.OrdinalIgnoreCase)) || user.Rooms.Any(x => x.Name.Equals(room.Name, StringComparison.OrdinalIgnoreCase));
 }
Exemplo n.º 12
0
 private bool IsUserInARoom(ChatUser user)
 {
     return _db.Rooms.Any(room => room.Users.Any(chatUser => chatUser.Name.Equals(user.Name, StringComparison.OrdinalIgnoreCase))) || user.Rooms.Any();
 }
Exemplo n.º 13
0
        private void HandleRejoin(ChatRoom room, ChatUser user)
        {
            // check if the user is in a room
            if (IsUserInRoom(room, user))
            {

                // Only support joining one room at a time for now (until we support tabs)
                HandleLeave(room, user);
            }

            JoinRoom(user, room);
        }
Exemplo n.º 14
0
        private void HandleJoin(string oldRoomName, ChatUser user, string[] parts)
        {
            if (parts.Length < 2)
            {
                throw new InvalidOperationException("Join which room?");
            }

            string newRoomName = parts[1];

            if (IsUserInRoom(newRoomName, user))
            {
                throw new InvalidOperationException("You're already in that room!");
            }

            // 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
            ChatRoom newRoom = _db.Rooms.FirstOrDefault(r => r.Name.Equals(newRoomName, StringComparison.OrdinalIgnoreCase));
            if (newRoom == null)
            {
                newRoom = AddRoom(newRoomName);
            }

            JoinRoom(user, newRoom);
        }
Exemplo n.º 15
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!");
            }

            string hash = CreateGravatarHash(email);

            SetGravatar(user, hash);
        }
Exemplo n.º 16
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);
            }
        }
Exemplo n.º 17
0
        public ChatRoom GetUserRoom(ChatUser user)
        {
            ChatRoom room = null;

            // check if the user has an room
            if (user.Rooms.Any())
            {

                // retrieve room
                var tempRoom = user.Rooms.FirstOrDefault();

                // ensure room is valid
                if (tempRoom != null)
                {
                    room = tempRoom;
                }
            }

            // check if rooms has the user
            if (room == null)
            {
                //retrieve room
                var tempRoom = _db.Rooms.First(x => x.Users.Any(u => u.Name.Equals(user.Name, StringComparison.OrdinalIgnoreCase)));

                // ensure room is valid
                if (tempRoom != null)
                {
                    room = tempRoom;
                }
            }

            return room;
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
        private void HandleLeave(ChatUser user, string[] parts)
        {
            if (String.IsNullOrWhiteSpace(parts[1])) {
                throw new InvalidOperationException("Room name cannot be blank!");
            }

            var room = _db.Rooms.FirstOrDefault(r => r.Name.Equals(parts[1], StringComparison.OrdinalIgnoreCase));

            if (room == null) {
                throw new InvalidOperationException("No room with that name!");
            }

            HandleLeave(room, user);
        }
Exemplo n.º 20
0
        private bool IsUserInRoom(string roomName, ChatUser user)
        {
            var room = _db.Rooms.FirstOrDefault(x => x.Name.Equals(roomName, StringComparison.OrdinalIgnoreCase));

            if (room == null)
            {
                return false;
            }

            return IsUserInRoom(room, user);
        }
Exemplo n.º 21
0
        private void HandleMe(ChatRoom room, ChatUser user, string[] parts)
        {
            if (parts.Length < 2) {
                throw new InvalidOperationException("You what?");
            }

            var content = String.Join(" ", parts.Skip(1));

            Clients[room.Name].sendMeMessage(user.Name, content);
        }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
0
        private void HandleNudge(ChatUser user, string[] parts)
        {
            if (_db.Users.Count == 1) {
                throw new InvalidOperationException("You're the only person in here...");
            }

            ChatUser toUser = _db.Users.FirstOrDefault(u => u.Name.Equals(parts[1], StringComparison.OrdinalIgnoreCase));

            if (toUser == null) {
                throw new InvalidOperationException(String.Format("Couldn't find any user named '{0}'.", parts[1]));
            }

            if (toUser == user) {
                throw new InvalidOperationException("You can't nudge yourself!");
            }

            string messageText = String.Format("{0} nudged you", user);

            var betweenNudges = TimeSpan.FromSeconds(60);
            if (toUser.LastNudged.HasValue && toUser.LastNudged > DateTime.Now.Subtract(betweenNudges)) {
                throw new InvalidOperationException(String.Format("User can only be nudged once every {0} seconds", betweenNudges.TotalSeconds));
            }

            toUser.LastNudged = DateTime.Now;
            // Send a nudge message to the sender and the sendee
            Clients[toUser.ClientId].nudge(user.Name, toUser.Name);
            Caller.sendPrivateMessage(user.Name, toUser.Name, "nudged " + toUser.Name);
        }
Exemplo n.º 24
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);
            }
        }
Exemplo n.º 25
0
 private void LeaveAllRooms(ChatUser user)
 {
     // Leave all rooms
     foreach (var room in user.Rooms.ToList()) {
         HandleLeave(room, user);
     }
 }
Exemplo n.º 26
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;
        }
Exemplo n.º 27
0
        private void HandleNick(string name, string[] parts)
        {
            string newUserName = String.Join(" ", parts.Skip(1));

            if (String.IsNullOrEmpty(newUserName)) {
                throw new InvalidOperationException("No username specified!");
            }

            if (newUserName.Equals(name, StringComparison.OrdinalIgnoreCase)) {
                throw new InvalidOperationException("That's already your username...");
            }

            if (!_users.ContainsKey(newUserName)) {
                if (String.IsNullOrEmpty(name) || !_users.ContainsKey(name)) {
                    var user = AddUser(newUserName);
                }
                else {
                    var oldUser = _users[name];
                    var newUser = new ChatUser {
                        Name = newUserName,
                        Hash = newUserName.ToMD5(),
                        Id = oldUser.Id,
                        ClientId = oldUser.ClientId,
                        Offset = oldUser.Offset,
                        Timezone = oldUser.Timezone
                    };

                    _users[newUserName] = newUser;
                    _userRooms[newUserName] = new HashSet<string>(_userRooms[name]);

                    bool inRooms = _userRooms[name].Any();

                    if (inRooms) {
                        foreach (var room in _userRooms[name]) {
                            _rooms[room].Users.Remove(name);
                            _rooms[room].Users.Add(newUserName);
                            Clients[room].changeUserName(oldUser, newUser);
                        }
                    }

                    _userRooms.Remove(name);
                    _users.Remove(name);

                    Caller.hash = newUser.Hash;
                    Caller.name = newUser.Name;

                    if (!inRooms) {
                        Caller.changeUserName(oldUser, newUser);
                    }
                }
            }
            else {
                throw new InvalidOperationException(String.Format("Username '{0}' is already taken!", newUserName));
            }
        }
Exemplo n.º 28
0
 private void HandleRejoin(ChatRoom room, ChatUser user)
 {
     JoinRoom(user, room);
 }
Exemplo n.º 29
0
 public UserViewModel(ChatUser user)
     : this(user, null)
 {
 }
Exemplo n.º 30
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);
        }