Skip to content
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
14 changes: 7 additions & 7 deletions ProjectV/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<PackageVersion Include="FileHelpers" Version="3.5.2" />
<PackageVersion Include="FSharp.Core" Version="8.0.400" />
<PackageVersion Include="FluentAssertions" Version="6.7.0" />
<PackageVersion Include="Google.Apis" Version="1.68.0" />
<PackageVersion Include="Google.Apis.Auth" Version="1.68.0" />
<PackageVersion Include="Google.Apis.Core" Version="1.68.0" />
<PackageVersion Include="Google.Apis.Drive.v3" Version="1.68.0.3627" />
<PackageVersion Include="Google.Apis" Version="1.74.0" />
<PackageVersion Include="Google.Apis.Auth" Version="1.74.0" />
<PackageVersion Include="Google.Apis.Core" Version="1.74.0" />
<PackageVersion Include="Google.Apis.Drive.v3" Version="1.74.0.4135" />
<PackageVersion Include="Gridsum.DataflowEx" Version="2.0.0" />
<PackageVersion Include="HttpToSocks5Proxy" Version="1.4.0" />
<PackageVersion Include="JsonSubTypes" Version="2.0.1" />
Expand Down Expand Up @@ -46,15 +46,15 @@
<PackageVersion Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageVersion Include="Prism.Unity" Version="9.0.537" />
<PackageVersion Include="Prism.Wpf" Version="9.0.537" />
<PackageVersion Include="RestSharp" Version="112.1.0" />
<PackageVersion Include="RestSharp" Version="114.0.0" />
<PackageVersion Include="SteamWebApiLib" Version="1.1.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="10.1.7" />
<PackageVersion Include="System.Data.SqlClient" Version="4.9.1" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.18.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="10.0.8" />
<PackageVersion Include="Telegram.Bot" Version="22.2.0" />
<PackageVersion Include="TMDbLib" Version="2.2.0" />
<PackageVersion Include="Telegram.Bot" Version="22.10.0.1" />
<PackageVersion Include="TMDbLib" Version="3.0.0" />
<PackageVersion Include="Unquote" Version="7.0.1" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public DataMapperTmdbConfig()

public TmdbServiceConfigurationInfo Transform(TMDbConfig dataObject)
{
var images = dataObject.Images;
return new TmdbServiceConfigurationInfo(
baseUrl: dataObject.Images.BaseUrl,
secureBaseUrl: dataObject.Images.SecureBaseUrl,
backdropSizes: dataObject.Images.BackdropSizes,
posterSizes: dataObject.Images.PosterSizes
baseUrl: images?.BaseUrl ?? string.Empty,
secureBaseUrl: images?.SecureBaseUrl ?? string.Empty,
backdropSizes: images?.BackdropSizes ?? [],
posterSizes: images?.PosterSizes ?? []
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DataMapperTmdbContainer()

public TmdbSearchContainer Transform(SearchContainer<SearchMovie> dataObject)
{
var results = dataObject.Results
var results = (dataObject.Results ?? [])
.Select(tmdb => _mapperTmdbMovie.Transform(tmdb))
.ToList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public TmdbMovieInfo Transform(SearchMovie dataObject)
{
return new TmdbMovieInfo(
thingId: dataObject.Id,
title: dataObject.Title,
title: dataObject.Title ?? string.Empty,
voteCount: dataObject.VoteCount,
voteAverage: dataObject.VoteAverage,
overview: dataObject.Overview,
overview: dataObject.Overview ?? string.Empty,
releaseDate: dataObject.ReleaseDate ?? new DateTime(),
popularity: dataObject.Popularity,
adult: dataObject.Adult,
genreIds: dataObject.GenreIds,
posterPath: dataObject.PosterPath
genreIds: dataObject.GenreIds ?? [],
posterPath: dataObject.PosterPath ?? string.Empty
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ public bool ThrowApiExceptions
/// <inheritdoc />
public string DefaultLanguage
{
get => _tmdbClient.DefaultLanguage;
get => _tmdbClient.DefaultLanguage ?? string.Empty;
set => _tmdbClient.DefaultLanguage = value.ThrowIfNull(nameof(value));
}

/// <inheritdoc />
public string DefaultCountry
{
get => _tmdbClient.DefaultCountry;
get => _tmdbClient.DefaultCountry ?? string.Empty;
set => _tmdbClient.DefaultCountry = value.ThrowIfNull(nameof(value));
}

public TmdbServiceConfigurationInfo Config => _configMapper.Transform(_tmdbClient.Config);

/// <inheritdoc />
public string ApiKey => _tmdbClient.ApiKey.ThrowIfNull(nameof(ApiKey));
public string ApiKey => _tmdbClient.ApiKey ?? string.Empty;

/// <inheritdoc />
public IWebProxy WebProxy => _tmdbClient.WebProxy.ThrowIfNull(nameof(WebProxy));
Expand Down Expand Up @@ -123,7 +123,7 @@ public async Task<TmdbServiceConfigurationInfo> GetConfigAsync()

try
{
SearchContainer<SearchMovie> response = await _tmdbClient.SearchMovieAsync(
SearchContainer<SearchMovie>? response = await _tmdbClient.SearchMovieAsync(
query: query,
page: page,
includeAdult: includeAdult,
Expand All @@ -133,6 +133,11 @@ public async Task<TmdbServiceConfigurationInfo> GetConfigAsync()
cancellationToken: cancellationToken
);

if (response is null)
{
return null;
}

return _dataMapper.Transform(response);
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using ProjectV.TelegramBotWebService.v1.Domain.Bot;
using ProjectV.TelegramBotWebService.v1.Domain.Handlers;
using ProjectV.TelegramBotWebService.v1.Domain.Polling;
using ProjectV.TelegramBotWebService.v1.Domain.Polling.Factories;
using ProjectV.TelegramBotWebService.v1.Domain.Polling.Handlers;
using ProjectV.TelegramBotWebService.v1.Domain.Receivers;
using ProjectV.TelegramBotWebService.v1.Domain.Service.Setup.Factories;
Expand Down Expand Up @@ -72,7 +71,6 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IBotWebhook, BotWebhook>();

services.AddSingleton<IBotPollingUpdateHandler, BotPollingUpdateHandler>();
services.AddSingleton<IBotPollingReceiverFactory, BotPollingReceiverFactory>();
services.AddSingleton<IBotPolling, BotPolling>();

var jwtOptionsSecion = Configuration.GetSection(nameof(JwtOptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public async Task<Message> SendMessageAsync(
string text,
ParseMode parseMode = ParseMode.None,
ReplyParameters? replyParameters = null,
IReplyMarkup? replyMarkup = null,
ReplyMarkup? replyMarkup = null,
LinkPreviewOptions? linkPreviewOptions = null,
int? messageThreadId = null,
IEnumerable<MessageEntity>? entities = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Task<Message> SendMessageAsync(
string text,
ParseMode parseMode = default,
ReplyParameters? replyParameters = default,
IReplyMarkup? replyMarkup = default,
ReplyMarkup? replyMarkup = default,
LinkPreviewOptions? linkPreviewOptions = default,
int? messageThreadId = default,
IEnumerable<MessageEntity>? entities = default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
using ProjectV.Logging;
using ProjectV.TelegramBotWebService.Options;
using ProjectV.TelegramBotWebService.v1.Domain.Bot;
using ProjectV.TelegramBotWebService.v1.Domain.Polling.Factories;
using ProjectV.TelegramBotWebService.v1.Domain.Polling.Handlers;
using Telegram.Bot;
using Telegram.Bot.Polling;

namespace ProjectV.TelegramBotWebService.v1.Domain.Polling
{
Expand All @@ -19,8 +20,6 @@ public sealed class BotPolling : IBotPolling

private readonly IBotService _botService;

private readonly IBotPollingReceiverFactory _processorFactory;

private readonly IBotPollingUpdateHandler _updateHandler;

private bool DropPendingUpdatesOnDelete => _options.Bot.Webhook.DropPendingUpdatesOnDelete;
Expand All @@ -29,12 +28,10 @@ public sealed class BotPolling : IBotPolling
public BotPolling(
IOptions<TelegramBotWebServiceOptions> options,
IBotService botService,
IBotPollingReceiverFactory processorFactory,
IBotPollingUpdateHandler updateHandler)
{
_options = options.GetCheckedValue();
_botService = botService.ThrowIfNull(nameof(botService));
_processorFactory = processorFactory.ThrowIfNull(nameof(processorFactory));
_updateHandler = updateHandler.ThrowIfNull(nameof(updateHandler));
}

Expand All @@ -48,8 +45,20 @@ public async Task StartReceivingUpdatesAsync(CancellationToken cancellationToken

_logger.Info("Starting receiving updates from Telegram API via polling.");

var updateReceiver = _processorFactory.Create(_options.Bot.Polling);
await updateReceiver.ReceiveAsync(_updateHandler, cancellationToken);
var pollingOptions = _options.Bot.Polling;
var receiverOptions = new ReceiverOptions
{
Offset = pollingOptions.Offset,
AllowedUpdates = pollingOptions.AllowedUpdates,
Limit = pollingOptions.Limit,
DropPendingUpdates = pollingOptions.DropPendingUpdates,
};

await _botService.BotClient.ReceiveAsync(
updateHandler: _updateHandler,
receiverOptions: receiverOptions,
cancellationToken: cancellationToken
);

_logger.Info("Receiving updates from Telegram API via polling has been finished.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using Acolyte.Assertions;
using ProjectV.Logging;
using ProjectV.TelegramBotWebService.Options;
using ProjectV.TelegramBotWebService.v1.Domain.Bot;
using ProjectV.TelegramBotWebService.v1.Domain.Polling.Receivers;
using Telegram.Bot.Polling;

namespace ProjectV.TelegramBotWebService.v1.Domain.Polling.Factories
Expand All @@ -13,45 +10,19 @@ public sealed class BotPollingReceiverFactory : IBotPollingReceiverFactory
private static readonly ILogger _logger =
LoggerFactory.CreateLoggerFor<BotPollingReceiverFactory>();

private readonly IBotService _botService;


public BotPollingReceiverFactory(
IBotService botService)
public BotPollingReceiverFactory()
{
_botService = botService.ThrowIfNull(nameof(botService));
}

#region IBotPollingProcessorFactory Implementation
#region IBotPollingReceiverFactory Implementation

public IUpdateReceiver Create(BotPollingOptions options)
public ReceiverOptions Create(BotPollingOptions options)
{
options.ThrowIfNull(nameof(options));

_logger.Info($"Creating bot polling receiver for mode: {options.ProcessingMode}.");

var receiverOptions = CreateReceiverOptions(options);

return options.ProcessingMode switch
{
_ when options.IsMode(BotPollingProcessingMode.LoopReceiving) => new DefaultUpdateReceiver(_botService.BotClient, receiverOptions),

_ when options.IsMode(BotPollingProcessingMode.AsyncQueuedReceiving) => new AsyncQueuedUpdateReceiver(_botService.BotClient, receiverOptions),

_ when options.IsMode(BotPollingProcessingMode.AsyncBlockingReceiving) => new AsyncBlockingUpdateReceiver(_botService.BotClient, receiverOptions),

_ => throw new ArgumentOutOfRangeException(
nameof(options),
options.ProcessingMode,
$"Unexpected processing mode: '{options.ProcessingMode}'."
)
};
}

#endregion
_logger.Info($"Creating receiver options for polling mode: {options.ProcessingMode}.");

private static ReceiverOptions CreateReceiverOptions(BotPollingOptions options)
{
return new ReceiverOptions
{
Offset = options.Offset,
Expand All @@ -60,5 +31,7 @@ private static ReceiverOptions CreateReceiverOptions(BotPollingOptions options)
DropPendingUpdates = options.DropPendingUpdates,
};
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using ProjectV.TelegramBotWebService.Options;
using ProjectV.TelegramBotWebService.Options;
using Telegram.Bot.Polling;

namespace ProjectV.TelegramBotWebService.v1.Domain.Polling.Factories
{
/// <summary>
/// Factory to create <see cref="ReceiverOptions" /> for polling.
/// </summary>
/// <remarks>
/// NOTE: This interface is kept for potential future use. As of Telegram.Bot 22.10,
/// the IUpdateReceiver abstraction has been removed from the library. Polling is now
/// performed via <see cref="TelegramBotClientExtensions.ReceiveAsync" /> directly.
/// </remarks>
public interface IBotPollingReceiverFactory
{
IUpdateReceiver Create(BotPollingOptions options);
ReceiverOptions Create(BotPollingOptions options);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;

namespace ProjectV.TelegramBotWebService.v1.Domain.Polling.Receivers
{
/// <summary>
/// Blocking update receiver implementation.
/// </summary>
/// <remarks>
/// NOTE: As of Telegram.Bot 22.10, <c>BlockingUpdateReceiver</c> has been removed from the
/// library. This class is kept for source compatibility but is no longer wired into the polling
/// pipeline. Polling is performed via
/// <see cref="TelegramBotClientExtensions.ReceiveAsync" /> directly.
/// </remarks>
public sealed class AsyncBlockingUpdateReceiver : AsyncUpdateReceiverBase
{

public AsyncBlockingUpdateReceiver(
ITelegramBotClient botClient,
ReceiverOptions? receiverOptions = default)
Expand All @@ -17,14 +25,13 @@ public AsyncBlockingUpdateReceiver(

#region UpdateReceiverBase Overridden Methods

protected override IAsyncEnumerable<Update> CreateAsyncEnumerator(
protected override async IAsyncEnumerable<Update> CreateAsyncEnumerator(
IUpdateHandler updateHandler)
{
return new BlockingUpdateReceiver(
botClient: _botClient,
receiverOptions: _receiverOptions,
pollingErrorHandler: (ex, token) => updateHandler.HandleErrorAsync(_botClient, ex, HandleErrorSource.HandleUpdateError, token)
);
// NOTE: BlockingUpdateReceiver was removed in Telegram.Bot 22.10.
// This method is no longer called from the polling pipeline.
await System.Threading.Tasks.Task.CompletedTask;
yield break;
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;

namespace ProjectV.TelegramBotWebService.v1.Domain.Polling.Receivers
{
/// <summary>
/// Queued update receiver implementation.
/// </summary>
/// <remarks>
/// NOTE: As of Telegram.Bot 22.10, <c>QueuedUpdateReceiver</c> has been removed from the
/// library. This class is kept for source compatibility but is no longer wired into the polling
/// pipeline. Polling is performed via
/// <see cref="TelegramBotClientExtensions.ReceiveAsync" /> directly.
/// </remarks>
public sealed class AsyncQueuedUpdateReceiver : AsyncUpdateReceiverBase
{
public AsyncQueuedUpdateReceiver(
Expand All @@ -16,14 +25,13 @@ public AsyncQueuedUpdateReceiver(

#region UpdateReceiverBase Overridden Methods

protected override IAsyncEnumerable<Update> CreateAsyncEnumerator(
protected override async IAsyncEnumerable<Update> CreateAsyncEnumerator(
IUpdateHandler updateHandler)
{
return new QueuedUpdateReceiver(
botClient: _botClient,
receiverOptions: _receiverOptions,
pollingErrorHandler: (ex, token) => updateHandler.HandleErrorAsync(_botClient, ex, HandleErrorSource.HandleUpdateError, token)
);
// NOTE: QueuedUpdateReceiver was removed in Telegram.Bot 22.10.
// This method is no longer called from the polling pipeline.
await System.Threading.Tasks.Task.CompletedTask;
yield break;
}

#endregion
Expand Down
Loading