예제 #1
0
        public void DeleteChatHubRoomChatHubUser(int ChatHubRoomId, int ChatHubUserId)
        {
            try
            {
                ChatHubRoomChatHubUser item = this.GetChatHubRoomChatHubUser(ChatHubRoomId, ChatHubUserId);

                if (item != null)
                {
                    db.ChatHubRoomChatHubUser.Remove(item);
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }
예제 #2
0
        public ChatHubRoomChatHubUser AddChatHubRoomChatHubUser(ChatHubRoomChatHubUser ChatHubRoomChatHubUser)
        {
            try
            {
                var item = this.GetChatHubRoomChatHubUser(ChatHubRoomChatHubUser.ChatHubRoomId, ChatHubRoomChatHubUser.ChatHubUserId);
                if (item == null)
                {
                    db.ChatHubRoomChatHubUser.Add(ChatHubRoomChatHubUser);
                    db.SaveChanges();
                    return(ChatHubRoomChatHubUser);
                }

                return(item);
            }
            catch
            {
                throw;
            }
        }
예제 #3
0
        public async Task EnterChatRoom(int roomId)
        {
            ChatHubUser user = await this.GetChatHubUserAsync();

            ChatHubRoom room = chatHubRepository.GetChatHubRoom(roomId);

            if (this.chatHubRepository.GetChatHubUsersByRoom(room).Any(item => item.UserId == user.UserId))
            {
                throw new HubException("User already entered room.");
            }

            if (room.OneVsOne())
            {
                if (!this.chatHubService.IsValidOneVsOneConnection(room, user))
                {
                    throw new HubException("No valid one vs one room id.");
                }
            }

            if (room.Public() || room.OneVsOne())
            {
                ChatHubRoomChatHubUser room_user = new ChatHubRoomChatHubUser()
                {
                    ChatHubRoomId = room.Id,
                    ChatHubUserId = user.UserId
                };
                chatHubRepository.AddChatHubRoomChatHubUser(room_user);

                ChatHubRoom chatHubRoomClientModel = await this.chatHubService.CreateChatHubRoomClientModelAsync(room);

                foreach (var connection in user.Connections.Active())
                {
                    await Groups.AddToGroupAsync(connection.ConnectionId, room.Id.ToString());

                    await Clients.Client(connection.ConnectionId).SendAsync("AddRoom", chatHubRoomClientModel);
                }

                ChatHubUser chatHubUserClientModel = this.chatHubService.CreateChatHubUserClientModel(user);
                await Clients.Group(room.Id.ToString()).SendAsync("AddUser", chatHubUserClientModel, room.Id.ToString());

                await this.SendGroupNotification(string.Format("{0} entered chat room with client device {1}.", user.DisplayName, this.MakeStringAnonymous(Context.ConnectionId, 7, '*')), room.Id, Context.ConnectionId, user, ChatHubMessageType.Enter_Leave);
            }
        }
예제 #4
0
        public async Task EnterChatRoom(int roomId)
        {
            ChatHubUser user = await this.GetChatHubUserAsync();

            ChatHubRoom room = chatHubRepository.GetChatHubRoom(roomId);

            if (room.Status == ChatHubRoomStatus.Archived.ToString())
            {
                if (!Context.User.HasClaim(ClaimTypes.Role, RoleNames.Admin) && !Context.User.HasClaim(ClaimTypes.Role, RoleNames.Host))
                {
                    throw new HubException("You cannot enter an archived room.");
                }
            }

            if (room.Public() || room.Protected())
            {
                if (this.chatHubService.IsBlacklisted(room, user))
                {
                    throw new HubException("You have been added to blacklist for this room.");
                }
            }

            if (room.Protected())
            {
                if (!Context.User.Identity.IsAuthenticated)
                {
                    throw new HubException("This room is for authenticated user only.");
                }
            }

            if (room.Private())
            {
                if (!this.chatHubService.IsWhitelisted(room, user))
                {
                    if (room.CreatorId != user.UserId)
                    {
                        await this.AddWaitingRoomItem(user, room);

                        throw new HubException("No valid private room connection. You have been added to waiting list.");
                    }
                }
            }

            if (room.OneVsOne())
            {
                if (!this.chatHubService.IsValidOneVsOneConnection(room, user))
                {
                    throw new HubException("No valid one vs one room id.");
                }
            }

            if (room.Public() || room.Protected() || room.Private() || room.OneVsOne())
            {
                ChatHubRoomChatHubUser room_user = new ChatHubRoomChatHubUser()
                {
                    ChatHubRoomId = room.Id,
                    ChatHubUserId = user.UserId
                };
                chatHubRepository.AddChatHubRoomChatHubUser(room_user);

                ChatHubRoom chatHubRoomClientModel = await this.chatHubService.CreateChatHubRoomClientModelAsync(room);

                foreach (var connection in user.Connections.Active())
                {
                    await Groups.AddToGroupAsync(connection.ConnectionId, room.Id.ToString());

                    await Clients.Client(connection.ConnectionId).SendAsync("AddRoom", chatHubRoomClientModel);
                }

                ChatHubUser chatHubUserClientModel = this.chatHubService.CreateChatHubUserClientModel(user);
                await Clients.Group(room.Id.ToString()).SendAsync("AddUser", chatHubUserClientModel, room.Id.ToString());

                await this.SendGroupNotification(string.Format("{0} entered chat room with client device {1}.", user.DisplayName, this.chatHubService.MakeStringAnonymous(Context.ConnectionId, 7, '*')), room.Id, Context.ConnectionId, user, ChatHubMessageType.Enter_Leave);
            }
        }