public async Task <ChatHubRoom> CreateChatHubRoomClientModelAsync(ChatHubRoom room) { List <ChatHubMessage> lastMessages = new List <ChatHubMessage>(); if (room.OneVsOne()) { lastMessages = await this.chatHubRepository.GetChatHubMessages(room.Id, new TimeSpan(24, 0, 0)).ToListAsync(); lastMessages = lastMessages != null && lastMessages.Any() ? lastMessages.Select(item => this.CreateChatHubMessageClientModel(item)).ToList() : new List <ChatHubMessage>(); } List <ChatHubUser> onlineUsers = await this.chatHubRepository.GetOnlineUsers(room.Id).ToListAsync(); onlineUsers = onlineUsers != null && onlineUsers.Any() ? onlineUsers = onlineUsers.Select(item => this.CreateChatHubUserClientModel(item)).ToList() : new List <ChatHubUser>(); return(new ChatHubRoom() { Id = room.Id, ModuleId = room.ModuleId, Title = room.Title, Content = room.Content, ImageUrl = room.ImageUrl, Type = room.Type, Status = room.Status, Messages = lastMessages, Users = onlineUsers, CreatedOn = room.CreatedOn, CreatedBy = room.CreatedBy, ModifiedBy = room.ModifiedBy, ModifiedOn = room.ModifiedOn }); }
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); } }
public async Task <ChatHubRoom> CreateChatHubRoomClientModelAsync(ChatHubRoom room) { IList <ChatHubMessage> lastMessages = new List <ChatHubMessage>(); if (room.OneVsOne()) { lastMessages = this.chatHubRepository.GetChatHubMessages(room.Id, 42); lastMessages = lastMessages != null && lastMessages.Any() ? lastMessages.Select(item => this.CreateChatHubMessageClientModel(item)).ToList() : new List <ChatHubMessage>(); } List <ChatHubUser> onlineUsers = await this.chatHubRepository.GetChatHubUsersByRoom(room).Online().ToListAsync(); onlineUsers = onlineUsers != null && onlineUsers.Any() ? onlineUsers = onlineUsers.Select(item => this.CreateChatHubUserClientModel(item)).ToList() : new List <ChatHubUser>(); IQueryable <ChatHubModerator> moderatorsQuery = this.chatHubRepository.GetChatHubModerators(room); IList <ChatHubModerator> moderatorsList = await moderatorsQuery.ToListAsync(); IQueryable <ChatHubWhitelistUser> whitelistUsersQuery = this.chatHubRepository.GetChatHubWhitelistUsers(room); IList <ChatHubWhitelistUser> whitelistUsersList = await whitelistUsersQuery.ToListAsync(); IQueryable <ChatHubBlacklistUser> blacklistUsersQuery = this.chatHubRepository.GetChatHubBlacklistUsers(room); IList <ChatHubBlacklistUser> blacklistUsersList = await blacklistUsersQuery.ToListAsync(); ChatHubUser creator = await this.chatHubRepository.GetUserByIdAsync(room.CreatorId); return(new ChatHubRoom() { Id = room.Id, ModuleId = room.ModuleId, Title = room.Title, Content = room.Content, BackgroundColor = room.BackgroundColor ?? "#999999", ImageUrl = room.ImageUrl, Type = room.Type, Status = room.Status, OneVsOneId = room.OneVsOneId, CreatorId = room.CreatorId, Creator = creator, Messages = lastMessages, Users = onlineUsers, Moderators = moderatorsList, WhitelistUsers = whitelistUsersList, BlacklistUsers = blacklistUsersList, CreatedOn = room.CreatedOn, CreatedBy = room.CreatedBy, ModifiedBy = room.ModifiedBy, ModifiedOn = room.ModifiedOn }); }
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); } }
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); } }