Ejemplo n.º 1
0
        public async Task Handle(ITelegramBotClient client, Message message, CancellationToken cancellationToken)
        {
            var user = await _userCache.GetUserAsync(message.From.Id, cancellationToken);

            if (user is null)
            {
                await client.SendTextMessageAsync(
                    chatId : message.Chat,
                    text : L10n.strings.DontKnowYouMessage,
                    replyToMessageId : message.MessageId,
                    cancellationToken : cancellationToken
                    );

                return;
            }

            if (user is SecondaryUser secondaryUser)
            {
                user = await _userCache.GetLoggedUserAsync(secondaryUser, cancellationToken);
            }

            if (user is LoggedUser loggedUser)
            {
                var packages = await _flypack.LoginAndFetchPackagesAsync(loggedUser.Username, _decrypter.Decrypt(loggedUser.Password, loggedUser.Salt));
                await SendPackagesToChat(client, packages, message.Chat.Id, cancellationToken);
            }
        }
Ejemplo n.º 2
0
        public async Task AnswerLoginAttemptNotification(ITelegramBotClient client, User user, Message message, string answer, SecondaryUser attemptingUser, CancellationToken cancellationToken)
        {
            var tasks = new List <Task>(6);

            tasks.Add(
                client.EditMessageTextAsync(message.Chat.Id, message.MessageId, string.Format(L10n.strings.InlineQueryAnswerMessage, message.Text, answer), parseMode: ParseMode.Markdown, cancellationToken: cancellationToken)
                );
            if (answer == L10n.strings.LoginAttemptDenyKeyword)
            {
                tasks.AddRange(new[]
                {
                    client.SendTextMessageAsync(message.Chat, L10n.strings.ChangeYourPasswordWarningMessage, cancellationToken: cancellationToken),
                    client.SendTextMessageAsync(attemptingUser.ChatIdentifier, L10n.strings.DeniedLoginAttemptMessage, cancellationToken: cancellationToken),
                    _userRepository.UpdateUnauthorizedUsersAsync(user.Id, attemptingUser, cancellationToken)
                });
            }
            else if (answer == L10n.strings.LoginAttemptAllowKeyword)
            {
                tasks.AddRange(new[]
                {
                    client.SendTextMessageAsync(attemptingUser.ChatIdentifier, L10n.strings.AllowedLoginAttemptMessage, cancellationToken: cancellationToken),
                    _userRepository.UpdateAuthorizedUsersAsync(user.Id, attemptingUser, cancellationToken)
                });
            }

            tasks.AddRange(new[]
            {
                _session.RemoveAsync(attemptingUser.ChatIdentifier, cancellationToken),
                _session.RemoveAsync(message.Chat.Id, cancellationToken)
            });

            await Task.WhenAll(tasks);

            if (answer != L10n.strings.LoginAttemptAllowKeyword)
            {
                return;
            }

            var cachedUser = await _userCache.GetLoggedUserAsync(user.Id, cancellationToken);

            cachedUser.AuthorizedUsers = cachedUser.AuthorizedUsers ?? new List <SecondaryUser>(1);
            cachedUser.AuthorizedUsers.Add(attemptingUser);
            _userCache.AddOrUpdate(cachedUser);

            var packages = await _flypack.GetCurrentPackagesAsync(user.Id, cancellationToken);

            await SendPackagesToChat(client, packages, attemptingUser.ChatIdentifier, cancellationToken);
        }