public ChatMessage(ChatUser user, string content) { User = user; Content = content; Id = Guid.NewGuid().ToString("d"); When = DateTimeOffset.UtcNow; }
public UserViewModel(ChatUser user) { Name = user.Name; Hash = user.Hash; Id = user.Id; Active = user.Active; }
public UserViewModel(ChatUser user, ChatRoom room) { Name = user.Name; Hash = user.Hash; Id = user.Id; Active = user.Active; Room = room == null ? null : room.Name; }
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); } }
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); } }
public void LogOff(ChatUser user) { chatModel.Users.Remove(user); chatModel.Messages.Add(new ChatMessage() { Text = user.Name + " покинул чат.", Date = DateTime.Now }); }
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; }
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)); } }
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); }
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(); }
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)); }
private bool IsUserInARoom(ChatUser user) { return _db.Rooms.Any(room => room.Users.Any(chatUser => chatUser.Name.Equals(user.Name, StringComparison.OrdinalIgnoreCase))) || user.Rooms.Any(); }
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); }
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); }
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); }
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); } }
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; }
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); }
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); }
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); }
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); }
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); }
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); }
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); } }
private void LeaveAllRooms(ChatUser user) { // Leave all rooms foreach (var room in user.Rooms.ToList()) { HandleLeave(room, user); } }
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; }
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)); } }
private void HandleRejoin(ChatRoom room, ChatUser user) { JoinRoom(user, room); }
public UserViewModel(ChatUser user) : this(user, null) { }
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); }