Exemple #1
0
        public async Task LeaveChatRoom(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 left 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))
                {
                    throw new HubException("No valid private room connection.");
                }
            }

            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())
            {
                this.chatHubRepository.DeleteChatHubRoomChatHubUser(roomId, user.UserId);
                ChatHubRoom chatHubRoomClientModel = await this.chatHubService.CreateChatHubRoomClientModelAsync(room);

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

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

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

                await this.SendGroupNotification(string.Format("{0} left chat room with client device {1}.", user.DisplayName, this.chatHubService.MakeStringAnonymous(Context.ConnectionId, 7, '*')), room.Id, Context.ConnectionId, user, ChatHubMessageType.Enter_Leave);
            }
        }
Exemple #2
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);
            }
        }