public async void OnAddedClientsToChat(object sender, AddedClientsToChatEventArgs container) { if (_cachedClientProperies.TryGetValue(container.NameOfClientSender, out ClientProperties clientProperties) && InfoChats.TryGetValue(container.NumberChat, out InfoChat infoChat)) { if (container.NameOfClientSender == infoChat.OwnerChat) { List <Guid> idClientsForSendMessage = new List <Guid>();//Создание списка id для рассылки им сообщений List <string> NameForChange = container.Clients; await Task.Run(() => CreateClientListForChangeInfoChat(ref NameForChange, container.NumberChat, ref idClientsForSendMessage)); var SendMessageForCreateChat = Task.Run ( () => _server.Send(idClientsForSendMessage, Container.GetContainer(nameof(AddChatResponse), new AddChatResponse(container.NameOfClientSender, container.NumberChat, infoChat.NameOfClients))) ); InfoChat buffer = infoChat; foreach (var item in NameForChange) { infoChat.NameOfClients.Add(item); if (_cachedClientProperies.TryGetValue(item, out ClientProperties value)) { var bufferClientProp = value; value.NumbersChat.Add(container.NumberChat); _cachedClientProperies.TryUpdate(item, value, bufferClientProp); } } List <Guid> idClientsForSendMessageAddClient = new List <Guid>(); await Task.Run(() => AddNamesForMail(infoChat.NameOfClients, ref idClientsForSendMessageAddClient)); var SendMessageToClient = Task.Run(() => _server.Send(idClientsForSendMessageAddClient, Container.GetContainer(nameof(AddClientToChatResponse), new AddClientToChatResponse(container.NameOfClientSender, NameForChange, container.NumberChat)))); InfoChats.TryUpdate(container.NumberChat, infoChat, buffer); if (!await Task.Run(() => _data.AddClientToChat(new AddClientToChat { NumberChat = container.NumberChat, NameOfClients = NameForChange }))) { //Ошибка на добавление в БД } } } }
public async void OnRemovedClientsFromChat(object sender, RemovedClientsFromChatEventArgs container) { if (_cachedClientProperies.TryGetValue(container.NameOfRemover, out ClientProperties clientProperties) && InfoChats.TryGetValue(container.NumberChat, out InfoChat infoChat)) { if (container.NameOfRemover == infoChat.OwnerChat) { List <Guid> idClientsForRemoveChat = new List <Guid>();//Создание списка id для рассылки им сообщений List <string> NamesForChange = container.Clients; await Task.Run(() => CreateClientListForChangeInfoChat(ref NamesForChange, container.NumberChat, ref idClientsForRemoveChat)); var SendMessageForRemoveChat = Task.Run(() => _server.Send(idClientsForRemoveChat, Container.GetContainer(nameof(RemoveChatResponse), new RemoveChatResponse(container.NameOfRemover, container.NumberChat)))); InfoChat bufferForUpdate = infoChat; foreach (var item in container.Clients) { infoChat.NameOfClients.Remove(item); } List <Guid> idClientsForRemovedClient = new List <Guid>(); await Task.Run(() => AddNamesForMail(infoChat.NameOfClients, ref idClientsForRemovedClient)); var SendMessageChangeClientList = Task.Run(() => _server.Send(idClientsForRemovedClient, Container.GetContainer(nameof(RemoveClientFromChatResponse), new RemoveClientFromChatResponse(container.NameOfRemover, container.Clients, container.NumberChat))) ); InfoChats.TryUpdate(container.NumberChat, infoChat, bufferForUpdate); if (!await Task.Run(() => _data.RemoveClientFromChat(new RemoveClientFromChat { NumberChat = container.NumberChat, NameOfClients = container.Clients }))) { //Ошибка на удаление в БД } } } }
public async void OnRequestNumbersChats(object sender, ClientRequestedNumbersChatEventArgs container) { if (_connection.cachedClientName.TryGetValue(container.NameOfClientSender, out Guid clientGuid)) { List <InfoAboutChat> AllInfoAboutChat = new List <InfoAboutChat>(); if (_cachedClientProperies.TryGetValue(container.NameOfClientSender, out ClientProperties clientProperties)) { foreach (var numberChat in clientProperties.NumbersChat) { if (InfoChats.TryGetValue(numberChat, out InfoChat infoChat)) { AllInfoAboutChat.Add(new InfoAboutChat(numberChat, infoChat.OwnerChat, infoChat.NameOfClients)); } } } else { _cachedClientProperies.TryAdd(container.NameOfClientSender, new ClientProperties() { NumbersChat = new List <int>() { NumberGeneralChat } }); if (InfoChats.TryGetValue(NumberGeneralChat, out InfoChat infoChat)) { InfoChat lastValue = infoChat; infoChat.NameOfClients.Add(container.NameOfClientSender); InfoChats.TryUpdate(NumberGeneralChat, infoChat, lastValue); } AllInfoAboutChat.Add(new InfoAboutChat(NumberGeneralChat, infoChat.OwnerChat, infoChat.NameOfClients)); var SendMessageAboutConnectNewClient = Task.Run ( () => _server.SendAll(clientGuid, Container.GetContainer(nameof(AddClientToChatResponse), new AddClientToChatResponse("Server", new List <string> { container.NameOfClientSender }, NumberGeneralChat))) ); if (!await Task.Run(() => _data.AddClientToChat(new AddClientToChat { NumberChat = NumberGeneralChat, NameOfClients = new List <string> { container.NameOfClientSender } }))) { } } var SendMessageToClient = Task.Run ( () => _server.Send(new List <Guid> { clientGuid }, Container.GetContainer(nameof(NumbersAccessibleChatsResponse), new NumbersAccessibleChatsResponse(AllInfoAboutChat))) ); } }