Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
name: formatting-and-unit-tests
on: [push, pull_request]

jobs:
#dotnet-format:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout repo
# uses: actions/checkout@v2
#
# - name: Add dotnet-format problem matcher
# uses: xt0rted/dotnet-format-problem-matcher@v1
#
# - name: Restore dotnet tools
# uses: xt0rted/dotnet-tool-restore@v1
#
# - name: Run dotnet format
# uses: xt0rted/dotnet-format@v1

jobs:
project-build:
runs-on: ubuntu-latest

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,4 @@ Thumbs.db
Desktop.ini
.DS_Store
.idea/
*.Development.json
26 changes: 25 additions & 1 deletion FileReceiver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{165C050E
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Integrations", "Integrations", "{C02CCF88-D61D-41B1-9E65-4DBC4580462D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Mega", "src\FileReceiver.Mega\FileReceiver.Mega.csproj", "{25D09645-AC3C-40E7-8729-979B0D38F57A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Integrations.Mega", "src\FileReceiver.Integrations.Mega\FileReceiver.Integrations.Mega.csproj", "{25D09645-AC3C-40E7-8729-979B0D38F57A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Tests.Fakers", "tests\FileReceiver.Tests.Fakers\FileReceiver.Tests.Fakers.csproj", "{E421D5E4-8760-4936-BFB3-3E313EB223D1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{FBC36705-5D1A-4AB5-BAA2-B50CDE8C1124}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Infrastructure.Configuration", "src\FileReceiver.Infrastructure.Configuration\FileReceiver.Infrastructure.Configuration.csproj", "{27DC8C49-6776-4B50-AF30-AC1D87AEE754}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Integrations.Configuration", "src\FileReceiver.Integrations.Configuration\FileReceiver.Integrations.Configuration.csproj", "{B4E3F3B3-1B5B-4CBA-9D6F-B0BA84C7A3E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileReceiver.Infrastructure.HostedServices", "src\FileReceiver.Infrastructure.HostedServices\FileReceiver.Infrastructure.HostedServices.csproj", "{3E7B47CB-F6C3-4861-9BE6-6373FD8AC927}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -49,6 +57,10 @@ Global
{C02CCF88-D61D-41B1-9E65-4DBC4580462D} = {9045B708-F21E-4EB8-ABB2-76EB6695F6A3}
{25D09645-AC3C-40E7-8729-979B0D38F57A} = {C02CCF88-D61D-41B1-9E65-4DBC4580462D}
{E421D5E4-8760-4936-BFB3-3E313EB223D1} = {165C050E-2183-4DB4-A63D-B362532F77F4}
{FBC36705-5D1A-4AB5-BAA2-B50CDE8C1124} = {9045B708-F21E-4EB8-ABB2-76EB6695F6A3}
{27DC8C49-6776-4B50-AF30-AC1D87AEE754} = {FBC36705-5D1A-4AB5-BAA2-B50CDE8C1124}
{B4E3F3B3-1B5B-4CBA-9D6F-B0BA84C7A3E2} = {C02CCF88-D61D-41B1-9E65-4DBC4580462D}
{3E7B47CB-F6C3-4861-9BE6-6373FD8AC927} = {FBC36705-5D1A-4AB5-BAA2-B50CDE8C1124}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{70B0EF0C-D42D-4CF2-93E2-357FC9190A32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -91,5 +103,17 @@ Global
{E421D5E4-8760-4936-BFB3-3E313EB223D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E421D5E4-8760-4936-BFB3-3E313EB223D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E421D5E4-8760-4936-BFB3-3E313EB223D1}.Release|Any CPU.Build.0 = Release|Any CPU
{27DC8C49-6776-4B50-AF30-AC1D87AEE754}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{27DC8C49-6776-4B50-AF30-AC1D87AEE754}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27DC8C49-6776-4B50-AF30-AC1D87AEE754}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27DC8C49-6776-4B50-AF30-AC1D87AEE754}.Release|Any CPU.Build.0 = Release|Any CPU
{B4E3F3B3-1B5B-4CBA-9D6F-B0BA84C7A3E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4E3F3B3-1B5B-4CBA-9D6F-B0BA84C7A3E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4E3F3B3-1B5B-4CBA-9D6F-B0BA84C7A3E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4E3F3B3-1B5B-4CBA-9D6F-B0BA84C7A3E2}.Release|Any CPU.Build.0 = Release|Any CPU
{3E7B47CB-F6C3-4861-9BE6-6373FD8AC927}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E7B47CB-F6C3-4861-9BE6-6373FD8AC927}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E7B47CB-F6C3-4861-9BE6-6373FD8AC927}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E7B47CB-F6C3-4861-9BE6-6373FD8AC927}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Binary file removed IssueReproduce.zip
Binary file not shown.
8 changes: 1 addition & 7 deletions src/FileReceiver.Bl.Abstract/FileReceiver.Bl.Abstract.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="16.0.1" />
<PackageReference Include="Telegram.Bot" Version="16.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FileReceiver.Common\FileReceiver.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Hosting.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<HintPath>..\..\..\..\..\..\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.Hosting.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
18 changes: 16 additions & 2 deletions src/FileReceiver.Bl.Abstract/Services/IBotTransactionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public interface IBotTransactionService
/// </summary>
/// <param name="userId">Transaction's owner</param>
/// <param name="transactionType"><see cref="TransactionType">Type</see> of the transaction which will be completed</param>
/// <param name="data"><see cref="TransactionModel"/> the represents some metadata about the transaction.</param>
/// <returns><see cref="Task{TResult}"/></returns>
Task<TransactionModel> Create(long userId, TransactionType transactionType);
Task<TransactionModel> Create(long userId, TransactionType transactionType, TransactionDataModel data = null);

/// <summary>
/// Receives the last active transaction with a given type for user.
Expand All @@ -30,7 +31,16 @@ public interface IBotTransactionService
/// <param name="transactionType"><see cref="TransactionType">Type</see> of the transaction which will be completed</param>
/// <exception cref="TransactionNotFoundException">Throws when transaction with this type is not exists for user or it's not active</exception>
/// <returns><see cref="Task{TransactionModel}"/></returns>
Task<TransactionModel> Get(long userId, TransactionType transactionType);
Task<TransactionModel> Get(long userId, TransactionType? transactionType = null);

/// <summary>
/// Receives the last active transaction with a given type for user.
/// </summary>
/// <param name="userId">Transaction's owner</param>
/// <param name="transactionType"><see cref="TransactionType">Type</see> of the transaction which will be completed</param>
/// <exception cref="TransactionNotFoundException">Throws when transaction with this type is not exists for user or it's not active</exception>
/// <returns><see cref="Task{TransactionModel}"/></returns>
Task<TransactionModel> GetNullIfNotExists(long userId, TransactionType? transactionType = null);

/// <summary>
/// Updates the last active transaction with a given type for user.
Expand All @@ -57,5 +67,9 @@ public interface IBotTransactionService
/// <returns><see cref="Task"/></returns>
Task Complete(long userId, TransactionType transactionType);

/// <summary>
/// Aborts all transactions for user with a given userId.
/// </summary>
Task AbortAllForUser(long userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

using FileReceiver.Common.Models;

using Telegram.Bot.Types;

namespace FileReceiver.Bl.Abstract.Services
{
public interface IUserRegistrationService
public interface IUserService
{
Task CreateNewUserAsync(Update update);
Task SetFirstNameAsync(long userId, string firstName);
Task SetLastNameAsync(long userId, string lastName);
Task SetSecretWordAsync(long userId, string secretWord);
Task<UserModel> CompleteRegistrationAsync(long userId);
Task<bool> CheckIfExists(long userId);
Task<UserModel> Get(long userId);
Task<UserModel> Add(UserModel user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public ICallbackQueryHandler CreateCallbackHandler(CallbackQuery callbackQuery)
=> _serviceProvider.GetService<EditProfileCallbackQueryHandler>(),
{ Data: { } data } when data.StartsWith("fr-session")
=> _serviceProvider.GetService<FileReceivingSessionCallbackQueryHandler>(),
_ => _serviceProvider.GetService<DefaultCallbackQueryHandler>(),
};
}
}
Expand Down
19 changes: 4 additions & 15 deletions src/FileReceiver.Bl.Impl/FileReceiver.Bl.Impl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,15 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="5.0.1" />
<PackageReference Include="Telegram.Bot" Version="16.0.1" />
<PackageReference Include="Telegram.Bot.Extensions.Polling" Version="0.2.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.Caching.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<HintPath>..\..\..\..\..\..\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.Caching.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<HintPath>..\..\..\..\..\..\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
</Reference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dal\FileReceiver.Dal.Abstract\FileReceiver.Dal.Abstract.csproj" />
<ProjectReference Include="..\dal\FileReceiver.Dal.Entities\FileReceiver.Dal.Entities.csproj" />
<ProjectReference Include="..\FileReceiver.Bl.Abstract\FileReceiver.Bl.Abstract.csproj" />
<ProjectReference Include="..\FileReceiver.Common\FileReceiver.Common.csproj" />
<ProjectReference Include="..\FileReceiver.Mega\FileReceiver.Mega.csproj" />
<ProjectReference Include="..\FileReceiver.Integrations.Mega\FileReceiver.Integrations.Mega.csproj" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion src/FileReceiver.Bl.Impl/FileReceiverBlInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static void AddServices(this IServiceCollection services)
{
services.AddTransient<IUpdateHandlerService, UpdateHandlerService>();
services.AddTransient<IBotMessagesService, BotMessagesService>();
services.AddTransient<IUserRegistrationService, UserRegistrationService>();
services.AddTransient<IUserService, UserService>();
services.AddTransient<IFileReceivingSessionService, FileReceivingSessionService>();
services.AddTransient<IFileReceivingService, FileReceivingService>();
services.AddTransient<IBotTransactionService, BotTransactionService>();
Expand Down Expand Up @@ -74,6 +74,7 @@ private static void AddCallbackQueryHandlers(this IServiceCollection services)
{
services.AddTransient<EditProfileCallbackQueryHandler, EditProfileCallbackQueryHandler>();
services.AddTransient<FileReceivingSessionCallbackQueryHandler, FileReceivingSessionCallbackQueryHandler>();
services.AddTransient<DefaultCallbackQueryHandler, DefaultCallbackQueryHandler>();
}

private static void AddFactories(this IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;

using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Bl.Abstract.Services;
using FileReceiver.Common.Extensions;

using Microsoft.Extensions.Logging;

using Telegram.Bot.Types;

namespace FileReceiver.Bl.Impl.Handlers.CallbackQuery
{
public class DefaultCallbackQueryHandler : ICallbackQueryHandler
{
private readonly IBotMessagesService _botMessagesService;
private readonly ILogger<DefaultCallbackQueryHandler> _logger;

public DefaultCallbackQueryHandler(
IBotMessagesService botMessagesService,
ILogger<DefaultCallbackQueryHandler> logger)
{
_botMessagesService = botMessagesService;
_logger = logger;
}

public async Task HandleCallback(Telegram.Bot.Types.CallbackQuery callbackQuery)
{
var userId = new Update()
{
CallbackQuery = callbackQuery
}.GetTgUserId();

_logger.LogDebug("Invalid callback query received. Callback query {query}", callbackQuery);
await _botMessagesService.SendNotSupportedAsync(userId, "This button press can't be handled!");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,89 +1,75 @@
using System.Threading.Tasks;

using AutoMapper;

using FileReceiver.Bl.Abstract.Factories;
using FileReceiver.Bl.Abstract.Handlers;
using FileReceiver.Bl.Abstract.Services;
using FileReceiver.Common.Enums;
using FileReceiver.Common.Models;
using FileReceiver.Dal.Abstract.Repositories;
using FileReceiver.Dal.Entities;

using Microsoft.Extensions.Logging;

namespace FileReceiver.Bl.Impl.Handlers.CallbackQuery
{
public class EditProfileCallbackQueryHandler : ICallbackQueryHandler
{
private readonly IBotMessagesService _botMessagesService;
private readonly IUserRegistrationService _userRegistrationService;
private readonly ITransactionRepository _transactionRepository;
private readonly IMapper _mapper;
private readonly IUserService _userService;
private readonly IBotTransactionService _transactionService;
private readonly ILogger<EditProfileCallbackQueryHandler> _logger;

public EditProfileCallbackQueryHandler(
IBotMessagesService botMessagesService,
IUserRegistrationService userRegistrationService,
ITransactionRepository transactionRepository,
IMapper mapper)
IUserService userService,
IBotTransactionService transactionService,
ILogger<EditProfileCallbackQueryHandler> logger)
{
_botMessagesService = botMessagesService;
_userRegistrationService = userRegistrationService;
_transactionRepository = transactionRepository;
_mapper = mapper;
_userService = userService;
_transactionService = transactionService;
_logger = logger;
}

public async Task HandleCallback(Telegram.Bot.Types.CallbackQuery callbackQuery)
{
switch (callbackQuery)
{
case { Data: "profile-edit-first-name", From: { } from }:
await CreateProfileEditingTransactionAsync(from.Id, ProfileEditingAction.UpdateFirstName,
await CreateProfileEditingTransaction(from.Id, ProfileEditingAction.UpdateFirstName,
"Okay, let's change your first name. Send me the new one plz");
break;
case { Data: "profile-edit-last-name", From: { } from }:
await CreateProfileEditingTransactionAsync(from.Id, ProfileEditingAction.UpdateLastName,
await CreateProfileEditingTransaction(from.Id, ProfileEditingAction.UpdateLastName,
"Okay, let's change your last name. Send me the new one plz");
break;
case { Data: "profile-edit-secret-word", From: { } from }:
await CreateProfileEditingTransactionAsync(from.Id, ProfileEditingAction.UpdateSecretWord,
await CreateProfileEditingTransaction(from.Id, ProfileEditingAction.UpdateSecretWord,
"Okay, let's change your secret word. Send me the new one plz");
break;
}
}

private async Task CreateProfileEditingTransactionAsync(long fromId, ProfileEditingAction editingAction,
private async Task CreateProfileEditingTransaction(long userId, ProfileEditingAction editingAction,
string messageForUser)
{
await _userRegistrationService.CompleteRegistrationAsync(fromId);
if (await CheckIfUserHasUncompletedTransactionAndNotify(fromId))
_logger.LogDebug("Edit profile callback query with edit action {editAction} received from {userId}.",
editingAction, userId);
await _userService.CompleteRegistrationAsync(userId);
var userLastTransaction = await _transactionService.GetNullIfNotExists(userId);
if (userLastTransaction is not null)
{
await _botMessagesService.SendErrorAsync(userId,
"You have uncompleted action. Use command /cancel to undo it and that use a command /profile_edit");
_logger.LogWarning("Profile editing transaction wasn't created for {userId} " +
"because he has an uncompleted transaction with {activeTransactionId}",
userId, userLastTransaction.Id);
return;
}

var transactionData = new TransactionDataModel();
transactionData.AddDataPiece(TransactionDataParameter.ProfileEditingAction,
editingAction);

var transaction = new TransactionModel()
{
UserId = fromId,
TransactionType = TransactionType.EditProfile,
TransactionState = TransactionState.Active,
TransactionData = transactionData,
};
await _transactionRepository.AddAsync(_mapper.Map<TransactionEntity>(transaction));
await _botMessagesService.SendTextMessageAsync(fromId, messageForUser);
}

private async Task<bool> CheckIfUserHasUncompletedTransactionAndNotify(long fromId)
{
if (await _transactionRepository.GetLastActiveTransactionByUserId(fromId) is null)
{
return false;
}
var transaction = await _transactionService.Create(userId, TransactionType.EditProfile);
transaction.TransactionData.AddDataPiece(TransactionDataParameter.ProfileEditingAction, editingAction);
_logger.LogDebug("Profile editing transaction was created for {userId} " +
"transaction id is {activeTransactionId}", userId, transaction.Id);

await _botMessagesService.SendErrorAsync(fromId,
"You have uncompleted action. Use command /cancel to undo it and that use a command /profile_edit");
return true;
await _transactionService.Add(transaction);
await _botMessagesService.SendTextMessageAsync(userId, messageForUser);
}
}
}
Loading