Пример #1
0
        public void AddChatMessageToDatabase()
        {
            // Arrage
            ChatMessage message1 = new ChatMessage() { ChatRoomId = 1, Content = "Test 1 Message", ChatUserId = 1, MessageSent = DateTime.Now };
            ChatMessage message2 = new ChatMessage() { ChatRoomId = 1, Content = "Test 2 Message", ChatUserId = 1, MessageSent = DateTime.Now };

            // Act
            _service.AddChatMessage(message1);
            _service.AddChatMessage(message2);

            var messages = _service.GetChatMessagesByRoom(1);

            // Assert
            Assert.IsNotNull(messages);
        }
Пример #2
0
        // Send message to Groups
        public void SendMessage(string message)
        {
            ChatMessage chatMessage = new ChatMessage();
            ChatUserVM cuVM = new ChatUserVM();

            var user = activeUsers.Find(u => u.ConnectionId == Context.ConnectionId);
            var admin = _service.GetChatUserByConnectionId(Context.ConnectionId);

            if (user != null)
            {
                string roomName = Clients.Caller.roomName;

                var room = _service.GetActiveRoomByName(roomName);

                chatMessage.ChatRoomId = room.ChatRoomId;
                chatMessage.Content = message;
                chatMessage.MessageSent = GetLocalTime();
                chatMessage.UserName = user.userName;

                _service.AddChatMessage(chatMessage);

                room.ChatMessages.Add(chatMessage);
                _service.UpdateChatRoom(room);

                Clients.OthersInGroup(roomName).DisplayMessage(user.userName, message, roomName);
            }
            else if (admin != null)
            {
                string roomName = Clients.Caller.roomName;

                var room = _service.GetActiveRoomByName(roomName);
                var chatAdminUser = _service.GetChatUserByConnectionId(Context.ConnectionId);

                chatMessage.ChatRoomId = room.ChatRoomId;
                chatMessage.ChatRoom = room;
                chatMessage.Content = message;
                chatMessage.MessageSent = GetLocalTime();
                chatMessage.UserName = admin.UserName;
                chatMessage.ChatUserId = chatAdminUser.ChatUserId;
                chatMessage.ChatUser = chatAdminUser;

                _service.AddChatMessage(chatMessage);

                room.ChatMessages.Add(chatMessage);
                _service.UpdateChatRoom(room);

                cuVM.ChatUserId = chatAdminUser.ChatUserId;
                cuVM.FirstName = chatAdminUser.FirstName;
                cuVM.LastName = chatAdminUser.LastName;

                cuVM.UserName = chatAdminUser.UserName;

                //Clients.OthersInGroup(roomName).DisplayMessage(admin, message);
                Clients.OthersInGroup(roomName).DisplayMessageToClient(cuVM.FirstName, message);

            }
        }
Пример #3
0
        // Add ChatUser and ChatRoom to SignalR Groups
        public void JoinGroup(string groupName)
        {
            Groups.Add(Context.ConnectionId, groupName);

            // If admin, add to ChatRoom's list of active ChatUsers
            //var user = activeUsers.Find(u => u.ConnectionId == Context.ConnectionId);
            //var room = activeChatRooms.Find(r => r.ChatRoomName == groupName);

            var adminUser = _service.GetChatUserByConnectionId(Context.ConnectionId);
            var chatRoom = _service.GetActiveRoomByName(groupName);
            var defaultCannedMessage = _service.GetDefaultCannedMessage();

            if (adminUser != null && chatRoom != null)
            {
                ChatRoomVM roomVM = new ChatRoomVM();
                ChatMessage message = new ChatMessage();

                // Initial Welcome Message
                if (chatRoom.FirstMessage == true)
                {
                    if (defaultCannedMessage != null)
                    {
                        message.ChatRoom = chatRoom;
                        message.ChatRoomId = chatRoom.ChatRoomId;
                        message.UserName = adminUser.UserName;
                        message.Content = defaultCannedMessage.CannedMessageContent;
                        message.MessageSent = GetLocalTime();
                        chatRoom.ChatMessages.Add(message);
                        chatRoom.FirstMessage = false;

                        Clients.Group(groupName).DisplayMessage(message.UserName, message.Content, groupName);
                        Clients.Group(groupName).DisplayMessageToClient(adminUser.FirstName, message.Content);
                    }
                }

                ++chatRoom.ActiveUserCount;
                chatRoom.AdminChatUserId = adminUser.ChatUserId;
                chatRoom.HasAdminUser = true;
                chatRoom.ChatUser = adminUser;

                adminUser.ChatRooms.Add(chatRoom);

                roomVM.HasAdminUser = chatRoom.HasAdminUser;
                roomVM.ActiveUserCount = chatRoom.ActiveUserCount;
                roomVM.ChatRoomName = chatRoom.ChatRoomName;

                _service.UpdateChatRoom(chatRoom);
                _service.UpdateAdminUser(adminUser);

                Clients.OthersInGroup(groupName).userConnected(adminUser.FirstName);
                Clients.Group("adminGroup").updateActiveRoom(roomVM);

                RoomFeedVM roomFeed = new RoomFeedVM();

                roomFeed.UsersInRoom = chatRoom.ActiveUserCount;
                roomFeed.StartTime = chatRoom.StartTime;
                roomFeed.RoomName = chatRoom.ChatRoomName;
                roomFeed.HelperName = adminUser.FirstName + " " + adminUser.LastName;
                roomFeed.ClientId = chatRoom.ChatUserId;
                if (chatRoom.HasAdminUser == true) { roomFeed.BeingHelped = "Yes"; } else { roomFeed.BeingHelped = "No"; }

                // TODO: Update Chat Room in Live Feed
                Clients.All.updateRoom(roomFeed);
            }
        }
Пример #4
0
 public void AddChatMessage(ChatMessage message)
 {
     _repo.AddNewChatMessage(message);
 }
Пример #5
0
 public void AddNewChatMessage(ChatMessage message)
 {
     _dbContext.ChatMessages.Add(message);
     _dbContext.SaveChanges();
 }