private void CreateChatHandler(string requestInJson) { CreateChatRequest request = JsonSerializer.Deserialize <CreateChatRequest>(requestInJson); try { User creator = server.UserRepository.GetByName(request.Sender); if (creator == null || creator.State != UserState.Authorized) { throw new NedoGramException("Invalid user"); } if (server.ChatRepository.GetChat(request.ChatName) != null) { throw new NedoGramException("Chat with this name already exists"); } aesEncryption.GenerateKey(); byte[] key = aesEncryption.GetKey(); IChat newChat = new Chat(creator, request.ChatName, key); newChat.AddUser(creator); server.UserRepository.UpdateState(creator.Name, UserState.InChat); server.ChatRepository.AddChat(newChat); creator.CurrentChat = newChat; var response = new CreateChatResponse { ChatName = newChat.Name, Code = StatusCode.Ok, Key = newChat.Key, RequestId = request.Id, }; SendMessageAesEncrypted( response, ClientAesKey); Console.WriteLine($"Chat created. ChatName - {newChat.Name}, Creator - {creator.Name}"); } // todo: system exception should not be available to the client catch (Exception exception) { var errorResponse = new CreateChatResponse { Code = StatusCode.ServerError, RequestId = request.Id, Message = exception.Message }; SendMessageAesEncrypted(errorResponse, ClientAesKey); throw; } }
encryptionChatDatas[^ 1] = new EncryptionChatData() { Chat = chat, AESEncryptedKey = request.AESEncryptedChatKey, KeySession = user.KeySession, SessionKeySign = request.ChatKeySign, EncryptedTitle = request.EncryptedTitle }; chat.EncryptionChatDatas.AddRange(encryptionChatDatas); await _context.SaveChangesAsync(); UserEvent[] events = new UserEvent[users.Length]; for (int i = 0; i < events.Length; i++) { events[i] = new UserEvent() { EventType = Enums.UserEventType.ChatCreated, User = users[i], Params = new List <UserEventParam>() { new UserEventParam() { Alias = "chatId", Value = chat.Id } } }; await _hubContext.Clients.User(users[i].Id).SendAsync("NewEvent", new GetUserEventsResponse() { UserEvents = new ExtUserEvent[] { (ExtUserEvent)events[i] } }); } _context.UserEvents.AddRange(events); await _context.SaveChangesAsync(); CreateChatResponse createChatResponse = new CreateChatResponse() { ChatId = chat.Id }; return(MainResponse.GetSuccess(createChatResponse)); }
private void CreateChatResponseHandler(string responseInJson) { CreateChatResponse response = JsonSerializer.Deserialize <CreateChatResponse>(responseInJson); if (response.Code != StatusCode.Ok) { Console.WriteLine(response.Message); return; } chatKey = response.Key; chatName = response.ChatName; userState = UserState.InChat; Console.WriteLine("Chat was created successfully"); Console.WriteLine($"Connected to the chat {chatName}"); }
public override void Call(CreateChatRequest packet, IUser sender, IServerManager manager) { if (!sender.IsLogIn) { return; } var chat = new Chat { Id = manager.Data.Chats.Count + 1, Name = packet.Name }; chat.Profiles.Add(manager.Data.Profiles.Where(x => x.Id == sender.Id).First()); manager.Data.Chats.Add(chat); var p = new CreateChatResponse { Id = chat.Id }; sender.Connector.Send(p); }