public ChatSession(SecondaryUser attemptingUser, long chatIdentifier, long userIdentifier, SessionScope scope) { ChatIdentifier = chatIdentifier; UserIdentifier = userIdentifier; AttemptingUser = attemptingUser; Messages = new List <SessionMessage>(); Scope = scope; LastUpdateAt = DateTime.Now; }
public Task RemoveUnauthorizedUserAsync(long identifier, SecondaryUser unauthorizedUser, CancellationToken cancellationToken = default) { var filter = Builders <LoggedUser> .Filter.Eq(x => x.Identifier, identifier); var removeUser = Builders <LoggedUser> .Update .PullFilter(x => x.UnauthorizedUsers, x => x.Identifier == unauthorizedUser.Identifier); return(_users.UpdateOneAsync(filter, removeUser, null, cancellationToken)); }
public Task UpdateUnauthorizedUsersAsync(long identifier, SecondaryUser unauthorizedUser, CancellationToken cancellationToken = default) { var filter = Builders <LoggedUser> .Filter.Eq(x => x.Identifier, identifier); var removeUser = Builders <LoggedUser> .Update .PullFilter(x => x.AuthorizedUsers, x => x.Identifier == unauthorizedUser.Identifier) .PullFilter(x => x.UnauthorizedUsers, x => x.Identifier == unauthorizedUser.Identifier); var addUser = Builders <LoggedUser> .Update .AddToSet(x => x.UnauthorizedUsers, unauthorizedUser); return(_users.BulkWriteAsync(new[] { new UpdateOneModel <LoggedUser>(filter, removeUser), new UpdateOneModel <LoggedUser>(filter, addUser) }, null, cancellationToken)); }
public void Add(SecondaryUser attemptingUser, long chatIdentifier, long userIdentifier, SessionScope scope) { ChatSession session; if (_sessions.ContainsKey(chatIdentifier)) { session = _sessions[chatIdentifier]; session.LastUpdateAt = DateTime.Now; session.Scope = scope; session.AttemptingUser = attemptingUser; } else { session = new ChatSession(attemptingUser, chatIdentifier, userIdentifier, scope); } _sessions[session.ChatIdentifier] = session; }
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); }