Exemple #1
0
 public ChatMessage()
 {
     Author = new ChatRoomUser();
     Message = "";
     MessageDate = DateTime.Now;
     Room = new ChatRoom();
 }
 public MessageViewModel(ChatMessage message, ChatRoom room)
 {
     Id = message.Id;
     Content = message.Content;
     User = new UserViewModel(message.User);
     When = message.When;
     Room = room.Name;
 }
Exemple #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;
 }
        public static ChatRoomModel Convert(ChatRoom chatRoom)
        {
            ChatRoomModel model = new ChatRoomModel
            {
                ChatRoomId = chatRoom.Id,
                Name = chatRoom.Name,
                PostCount = chatRoom.Posts.Count,
                UserCount = chatRoom.Users.Count
            };

            return model;
        }
        public static ChatRoomFullModel Convert(ChatRoom chatRoom)
        {
            ChatRoomFullModel model = new ChatRoomFullModel
            {
                ChatRoomId = chatRoom.Id,
                Name = chatRoom.Name,
                PostCount = chatRoom.Posts.Count,
                UserCount = chatRoom.Users.Count,
                Posts = (
                from post in chatRoom.Posts
                select PostModel.Convert(post)).ToList(),
                Users = (
                from user in chatRoom.Users
                select UserModel.Convert(user)).ToList()
            };

            return model;
        }
        public ActionResult CreateRoom(string nm)
        {
            ChatRoom cr = new ChatRoom();
            cr.name = nm;
            Db.chatRooms.Add(cr);

            if (Db.currentRoom == null)
            {
                Db.currentRoom = Db.chatRooms.FirstOrDefault();
                if (Db.currentRoom != null)
                {
                    Db.currentRoom.isCurrent = true;
                }
            }

            Db.SaveChanges();

            return RedirectToAction("ChatIndex");
        }
        static void Main()
        {
            //Create and initialize the database

            Database.SetInitializer(new MigrateDatabaseToLatestVersion
                <ChatContext, Chat.Data.Migrations.Configuration>());

            var context = new ChatContext();
            using (context)
            {
                var user = new User { Username = "******", Password = "******", Picture = "....." };
                context.Users.Add(user);
                var chatRoom = new ChatRoom { Name = "chatroom", Users = new User[] { user } };
                context.ChatRooms.Add(chatRoom);
                var post = new Post { Date = DateTime.Now, UserId = user.Id, ChatRoomId = chatRoom.Id, Content = "ok" };
                context.Posts.Add(post);
                context.SaveChanges();
            }
        }
Exemple #8
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();
        }
Exemple #9
0
        private void HandleJoin(string room, string name, string[] parts)
        {
            if (parts.Length < 2) {
                throw new InvalidOperationException("Join which room?");
            }

            string newRoom = parts[1];
            ChatRoom chatRoom;
            // Create the room if it doesn't exist
            if (!_rooms.TryGetValue(newRoom, out chatRoom)) {
                chatRoom = new ChatRoom();
                _rooms.Add(newRoom, chatRoom);
            }

            // Only support one room at a time for now (until we support tabs)
            // Remove the old room
            if (!String.IsNullOrEmpty(room)) {
                _userRooms[name].Remove(room);
                _rooms[room].Users.Remove(name);
                RemoveFromGroup(room);
                Clients[room].leave(_users[name]).Wait();
            }

            _userRooms[name].Add(newRoom);
            if (!chatRoom.Users.Add(name)) {
                throw new InvalidOperationException("You're already in that room!");
            }

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

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

            AddToGroup(newRoom).Wait();

            Caller.joinRoom(newRoom);
        }
 public ICollection<Post> GetPosts(ChatRoom chatRoom)
 {
     var entity = this.chatRoomRepository.Get(chatRoom.Id).Posts;
     return entity;
 }
 public ICollection<User> GetUsers(ChatRoom chatRoom)
 {
     var entity = this.chatRoomRepository.Get(chatRoom.Id).Users;
     return entity;
 }
Exemple #12
0
        private ChatRoom AddRoom(string name)
        {
            if (name.Equals("Lobby", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("Lobby is not a valid chat room.");
            }
            if (!IsValidRoomName(name))
            {
                throw new InvalidOperationException(String.Format("'{0}' is not a valid room name.", name));
            }
            var chatRoom = new ChatRoom { Name = name };

            _db.Rooms.Add(chatRoom);

            return chatRoom;
        }
        public HttpResponseMessage Post(ChatRoom model)
        {
            if (model.Name == null)
            {
                var errResponse = this.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, "ChatRoom Name could not be null");
                throw new HttpResponseException(errResponse);
            }

            var entity = this.chatRoomRepository.Add(model);
            var response =
                Request.CreateResponse(HttpStatusCode.Created, entity);
            response.Headers.Location = new Uri(Url.Link("DefaultApi",
                new { id = entity.Id }));

            return response;
        }
Exemple #14
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));
     }
 }
Exemple #15
0
        private void ProcessUrls(IEnumerable<string> links, ChatRoom chatRoom, ChatMessage chatMessage)
        {
            // REVIEW: is this safe to do? We're holding on to this instance
            // when this should really be a fire and forget.
            var contentTasks = links.Select(ExtractContent).ToArray();
            Task.Factory.ContinueWhenAll(contentTasks, tasks => {
                foreach (var task in tasks) {
                    if (task.IsFaulted) {
                        Trace.TraceError(task.Exception.GetBaseException().Message);
                        continue;
                    }

                    if (String.IsNullOrEmpty(task.Result)) {
                        continue;
                    }

                    // Try to get content from each url we're resolved in the query
                    string extractedContent = "<p>" + task.Result + "</p>";

                    // If we did get something, update the message and notify all clients
                    chatMessage.Content += extractedContent;

                    Clients[chatRoom.Name].addMessageContent(chatMessage.Id, extractedContent);
                }
            });
        }
Exemple #16
0
 private void HandleRejoin(ChatRoom room, ChatUser user)
 {
     JoinRoom(user, room);
 }
Exemple #17
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);
        }
Exemple #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);
        }
Exemple #19
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);
        }
Exemple #20
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));
 }
Exemple #21
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);
        }
Exemple #22
0
        private ChatRoom AddRoom(string name)
        {
            var chatRoom = new ChatRoom { Name = name };

            _db.Rooms.Add(chatRoom);

            return chatRoom;
        }