public static DhPublicKey ToDhPublicKey(this DhPublicKeyDataModel value)
 {
     return(new DhPublicKey
     {
         Generator = value.Generator,
         Modulus = value.Modulus
     });
 }
Пример #2
0
        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);
            }
        }