private async Task <Shared.DTO.Conversations.Chat> CreateGroup( AppUser creator, string chatImageUrl, string chatName, bool isPublic) { if (chatName.Length > MaxNameLength) { throw new InvalidDataException("Chat name was too long."); } try { var imageUrl = chatImageUrl ?? chatDataProvider.GetGroupPictureUrl(); var newChat = new ConversationDataModel { IsGroup = true, Name = chatName, FullImageUrl = imageUrl, ThumbnailUrl = imageUrl, IsPublic = isPublic }; await conversationRepository.AddAsync(newChat); await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create( creator.Id, newChat)); var role = await rolesRepository.AddAsync( ChatRoleDataModel.Create(newChat, creator.Id, ChatRole.Creator)); newChat.Roles = new List <ChatRoleDataModel>() { role }; newChat.participants = new List <AppUser> { creator }; await unitOfWork.Commit(); return(newChat.ToChatDto(creator.Id)); } catch (Exception e) { throw new InvalidDataException("Couldn't create group because of unexpected error.", e); } }
private async Task <Shared.DTO.Conversations.Chat> CreateDialog( AppUser creator, string dialogUserId, bool isSecure, string deviceId) { var user = await usersRepository.GetByIdAsync(creator.Id); if (user == null) { throw new KeyNotFoundException("Wrong creatorId."); } try { //if this is a dialogue , find a user with whom to create chat var secondDialogueUser = await usersRepository.GetByIdAsync(dialogUserId); if (secondDialogueUser == null) { throw new InvalidDataException("Wrong dialog user Id."); } DhPublicKeyDataModel dhPublicKey = null; if (isSecure) { dhPublicKey = await publicKeys.GetRandomKey(); } var newChat = new ConversationDataModel { IsGroup = false, IsSecure = isSecure, PublicKeyId = dhPublicKey?.Id, PublicKey = dhPublicKey, DeviceId = deviceId }; await conversationRepository.AddAsync(newChat); await usersConversationsRepository.AddAsync( UsersConversationDataModel.Create(dialogUserId, newChat) ); await usersConversationsRepository.AddAsync(UsersConversationDataModel.Create( creator.Id, newChat, deviceId)); newChat.participants = new List <AppUser> { user, secondDialogueUser }; await unitOfWork.Commit(); return(newChat.ToChatDto(creator.Id)); } catch (Exception e) { throw new InvalidDataException("Couldn't create dialog. Probably there exists one already.", e); } }