From 81da2782f19992ebb8374a5970d47cad3e255c68 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Wed, 24 Jan 2024 10:22:40 +0300 Subject: [PATCH 01/29] youtube configuration and client --- Dodo1000Bot.Api/appsettings.json | 3 ++ Dodo1000Bot.Services/Clients/YouTubeClient.cs | 32 +++++++++++++++++++ .../Configuration/AppConfiguration.cs | 2 ++ .../Configuration/YouTubeConfiguration.cs | 6 ++++ 4 files changed, 43 insertions(+) create mode 100644 Dodo1000Bot.Services/Clients/YouTubeClient.cs create mode 100644 Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index de8608a..6b0da24 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -38,5 +38,8 @@ "MysqlConnectionString": "", "Management": { "IncomingToken": "" + }, + "YouTube": { + "ApiKey": "" } } diff --git a/Dodo1000Bot.Services/Clients/YouTubeClient.cs b/Dodo1000Bot.Services/Clients/YouTubeClient.cs new file mode 100644 index 0000000..eea6c94 --- /dev/null +++ b/Dodo1000Bot.Services/Clients/YouTubeClient.cs @@ -0,0 +1,32 @@ +using System.Net.Http; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Dodo1000Bot.Models.RealtimeBoard; +using Dodo1000Bot.Services.Configuration; +using Dodo1000Bot.Services.Extensions; + +namespace Dodo1000Bot.Services.Clients; + +public class YouTubeClient +{ + private readonly YouTubeConfiguration _configuration; + private readonly HttpClient _httpClient; + private readonly JsonSerializerOptions _serializerOptions; + + public YouTubeClient(YouTubeConfiguration configuration, HttpClient httpClient, JsonSerializerOptions serializerOptions) + { + _configuration = configuration; + _httpClient = httpClient; + _serializerOptions = serializerOptions; + } + + public async Task Videos(string channelId, CancellationToken cancellationToken) + { + var url = $"?part=snippet&channelId={channelId}&order=date&type=video&maxResults=50&key={_configuration.ApiKey}"; + + var statistics = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); + + return statistics; + } +} \ No newline at end of file diff --git a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs index b701457..363af1b 100644 --- a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs @@ -23,5 +23,7 @@ public class AppConfiguration public PushNotificationsConfiguration PushNotifications { get; set; } public ManagementConfiguration Management { get; set; } + + public YouTubeConfiguration YouTube { get; set; } } } diff --git a/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs b/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs new file mode 100644 index 0000000..4d6b43a --- /dev/null +++ b/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs @@ -0,0 +1,6 @@ +namespace Dodo1000Bot.Services.Configuration; + +public class YouTubeConfiguration +{ + public string ApiKey { get; set; } +} \ No newline at end of file From ca4c3df742bcc9207c190c7b2cb926e16f01e087 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Thu, 25 Jan 2024 09:11:11 +0300 Subject: [PATCH 02/29] registered YouTubeClient --- .../DependencyModules/ExternalServicesRegistration.cs | 2 ++ Dodo1000Bot.Api/appsettings.json | 3 ++- Dodo1000Bot.Services/Clients/YouTubeClient.cs | 2 +- .../Configuration/YouTubeConfiguration.cs | 2 ++ Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs | 10 ++++++++++ 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs diff --git a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs index c6847d3..390243a 100644 --- a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs +++ b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs @@ -28,6 +28,8 @@ internal static void AddExternalServices(this IServiceCollection services, AppCo nameof(configuration.RealtimeBoardApiClientEndpoint)); services.AddHttpClient(configuration.RestcountriesApiClientEndpoint, nameof(configuration.RestcountriesApiClientEndpoint)); + services.AddHttpClient(configuration.YouTube.Endpoint, + nameof(configuration.RestcountriesApiClientEndpoint)); } private static SessionsClient RegisterDialogflowSessionsClient(IServiceProvider provider) diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index 6b0da24..347be0c 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -40,6 +40,7 @@ "IncomingToken": "" }, "YouTube": { - "ApiKey": "" + "ApiKey": "", + "Endpoint": "https://youtube.googleapis.com/youtube/v3/search" } } diff --git a/Dodo1000Bot.Services/Clients/YouTubeClient.cs b/Dodo1000Bot.Services/Clients/YouTubeClient.cs index eea6c94..a262d76 100644 --- a/Dodo1000Bot.Services/Clients/YouTubeClient.cs +++ b/Dodo1000Bot.Services/Clients/YouTubeClient.cs @@ -8,7 +8,7 @@ namespace Dodo1000Bot.Services.Clients; -public class YouTubeClient +public class YouTubeClient : IYouTubeClient { private readonly YouTubeConfiguration _configuration; private readonly HttpClient _httpClient; diff --git a/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs b/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs index 4d6b43a..43f1092 100644 --- a/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs @@ -3,4 +3,6 @@ public class YouTubeConfiguration { public string ApiKey { get; set; } + + public string Endpoint { get; set; } } \ No newline at end of file diff --git a/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs b/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs new file mode 100644 index 0000000..dc5f1ef --- /dev/null +++ b/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs @@ -0,0 +1,10 @@ +using System.Threading; +using System.Threading.Tasks; +using Dodo1000Bot.Models.RealtimeBoard; + +namespace Dodo1000Bot.Services; + +public interface IYouTubeClient +{ + Task Videos(string channelId, CancellationToken cancellationToken); +} \ No newline at end of file From 72be059cae21c2ffe70589e8302a0e024a8429a8 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Thu, 25 Jan 2024 09:12:36 +0300 Subject: [PATCH 03/29] removed redundant registrations --- .../DependencyModules/ExternalServicesRegistration.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs index 390243a..c82cf9d 100644 --- a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs +++ b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs @@ -16,12 +16,6 @@ internal static class ExternalServicesRegistration { internal static void AddExternalServices(this IServiceCollection services, AppConfiguration configuration) { - services.AddSingleton(RegisterDialogflowSessionsClient); - - services.AddSingleton(RegisterRedisClient); - - services.AddSingleton(RegisterCacheService); - services.AddHttpClient(configuration.GlobalApiEndpoint, nameof(configuration.GlobalApiEndpoint)); services.AddHttpClient(configuration.RealtimeBoardApiClientEndpoint, From 0153d39f262e2fd9a02ad29ee4ffacd56f4234b8 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Thu, 25 Jan 2024 09:13:21 +0300 Subject: [PATCH 04/29] refactored --- Dodo1000Bot.Services/NotificationsService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dodo1000Bot.Services/NotificationsService.cs b/Dodo1000Bot.Services/NotificationsService.cs index 2873f3c..884db6b 100644 --- a/Dodo1000Bot.Services/NotificationsService.cs +++ b/Dodo1000Bot.Services/NotificationsService.cs @@ -43,9 +43,9 @@ public async Task PushNotifications(CancellationToken cancellationToken) return; } - notifications = notifications.Where(n => n.Type != NotificationType.Admin).ToList(); + var sortedNotifications = notifications.Where(n => n.Type != NotificationType.Admin).ToList(); - IEnumerable>> tasks = _notifyServices.Select(s => s.NotifyAbout(notifications, cancellationToken)); + IEnumerable>> tasks = _notifyServices.Select(s => s.NotifyAbout(sortedNotifications, cancellationToken)); IEnumerable[] tasksResults = await Task.WhenAll(tasks); From ba49928a88ea3f0591edeb890c0c6b3df8dcdfe5 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Thu, 25 Jan 2024 09:20:26 +0300 Subject: [PATCH 05/29] SocialNetworksCheckAndNotifyJob --- Dodo1000Bot.Api/DependencyConfiguration.cs | 4 ++-- .../Jobs/SocialNetworksCheckAndNotifyJob.cs | 16 ++++++++++++++++ Dodo1000Bot.Api/appsettings.json | 3 +++ .../Configuration/AppConfiguration.cs | 2 ++ .../SocialNetworksJobConfiguration.cs | 6 ++++++ 5 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs create mode 100644 Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs diff --git a/Dodo1000Bot.Api/DependencyConfiguration.cs b/Dodo1000Bot.Api/DependencyConfiguration.cs index af803e6..b7be24c 100644 --- a/Dodo1000Bot.Api/DependencyConfiguration.cs +++ b/Dodo1000Bot.Api/DependencyConfiguration.cs @@ -18,12 +18,12 @@ internal static void Configure(IServiceCollection services, IConfiguration appCo services.AddSingleton(configuration); services.AddSingleton(configuration.HttpLog); - services.AddSingleton(configuration.Redis); - services.AddSingleton(configuration.Dialogflow); services.AddSingleton(configuration.PushNotifications); services.AddSingleton(configuration.UnitsJob); services.AddSingleton(configuration.StatisticsJob); + services.AddSingleton(configuration.SocialNetworksJob); services.AddSingleton(configuration.Management); + services.AddSingleton(configuration.YouTube); services.AddInternalServices(); services.AddJobs(configuration); diff --git a/Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs b/Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs new file mode 100644 index 0000000..8b82930 --- /dev/null +++ b/Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs @@ -0,0 +1,16 @@ +using System; +using Dodo1000Bot.Services; +using Dodo1000Bot.Services.Configuration; +using Microsoft.Extensions.Logging; + +namespace Dodo1000Bot.Api.Jobs; + +public class SocialNetworksCheckAndNotifyJob: CheckAndNotifyJob +{ + public SocialNetworksCheckAndNotifyJob( + ILogger log, + IServiceProvider provider, + SocialNetworksJobConfiguration configuration) : base(log, provider, configuration) + { + } +} \ No newline at end of file diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index 347be0c..8682ba5 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -32,6 +32,9 @@ "StatisticsJob": { "RefreshEveryTime": "0:10:00" }, + "SocialNetworksJob": { + "RefreshEveryTime": "0:10:00" + }, "GlobalApiEndpoint": "https://globalapi.dodois.io/api/v2/", "RealtimeBoardApiClientEndpoint": "", "RestcountriesApiClientEndpoint": "https://restcountries.com/v3.1/", diff --git a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs index 363af1b..ae94d2e 100644 --- a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs @@ -12,6 +12,8 @@ public class AppConfiguration public StatisticsJobConfiguration StatisticsJob { get; set; } + public SocialNetworksJobConfiguration SocialNetworksJob { get; set; } + public string GlobalApiEndpoint { get; set; } public string RealtimeBoardApiClientEndpoint { get; set; } diff --git a/Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs b/Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs new file mode 100644 index 0000000..1ef6250 --- /dev/null +++ b/Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs @@ -0,0 +1,6 @@ +namespace Dodo1000Bot.Services.Configuration +{ + public class SocialNetworksJobConfiguration : CheckAndNotifyJobConfiguration + { + } +} \ No newline at end of file From 9e2ce69b126a52fc961903542108717166f2c26a Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Thu, 25 Jan 2024 09:20:36 +0300 Subject: [PATCH 06/29] SocialNetworksService --- Dodo1000Bot.Services/SocialNetworksService.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Dodo1000Bot.Services/SocialNetworksService.cs diff --git a/Dodo1000Bot.Services/SocialNetworksService.cs b/Dodo1000Bot.Services/SocialNetworksService.cs new file mode 100644 index 0000000..8baf6a5 --- /dev/null +++ b/Dodo1000Bot.Services/SocialNetworksService.cs @@ -0,0 +1,24 @@ +using System.Threading; +using System.Threading.Tasks; +using Dodo1000Bot.Services.Interfaces; +using Microsoft.Extensions.Logging; + +namespace Dodo1000Bot.Services; + +public class SocialNetworksService: CheckAndNotifyService +{ + private readonly ILogger _log; + private readonly IYouTubeClient _youTubeClient; + private readonly INotificationsService _notificationsService; + private readonly ISnapshotsRepository _snapshotsRepository; + + public SocialNetworksService(ILogger log) + { + _log = log; + } + + public override Task CheckAndNotify(CancellationToken cancellationToken) + { + throw new System.NotImplementedException(); + } +} \ No newline at end of file From 856ea29288232612d0f77886c27f55c4a71238d4 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Fri, 9 Feb 2024 22:03:42 +0300 Subject: [PATCH 07/29] renamed --- Dodo1000Bot.Api/DependencyConfiguration.cs | 2 +- .../Jobs/SocialNetworksCheckAndNotifyJob.cs | 16 ---------------- Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs | 16 ++++++++++++++++ Dodo1000Bot.Api/appsettings.json | 2 +- .../Configuration/AppConfiguration.cs | 2 +- .../SocialNetworksJobConfiguration.cs | 6 ------ .../Configuration/YoutubeJobConfiguration.cs | 6 ++++++ ...ocialNetworksService.cs => YoutubeService.cs} | 8 ++++---- 8 files changed, 29 insertions(+), 29 deletions(-) delete mode 100644 Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs create mode 100644 Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs delete mode 100644 Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs create mode 100644 Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs rename Dodo1000Bot.Services/{SocialNetworksService.cs => YoutubeService.cs} (66%) diff --git a/Dodo1000Bot.Api/DependencyConfiguration.cs b/Dodo1000Bot.Api/DependencyConfiguration.cs index b7be24c..bbe5b78 100644 --- a/Dodo1000Bot.Api/DependencyConfiguration.cs +++ b/Dodo1000Bot.Api/DependencyConfiguration.cs @@ -21,7 +21,7 @@ internal static void Configure(IServiceCollection services, IConfiguration appCo services.AddSingleton(configuration.PushNotifications); services.AddSingleton(configuration.UnitsJob); services.AddSingleton(configuration.StatisticsJob); - services.AddSingleton(configuration.SocialNetworksJob); + services.AddSingleton(configuration.YoutubeJob); services.AddSingleton(configuration.Management); services.AddSingleton(configuration.YouTube); diff --git a/Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs b/Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs deleted file mode 100644 index 8b82930..0000000 --- a/Dodo1000Bot.Api/Jobs/SocialNetworksCheckAndNotifyJob.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using Dodo1000Bot.Services; -using Dodo1000Bot.Services.Configuration; -using Microsoft.Extensions.Logging; - -namespace Dodo1000Bot.Api.Jobs; - -public class SocialNetworksCheckAndNotifyJob: CheckAndNotifyJob -{ - public SocialNetworksCheckAndNotifyJob( - ILogger log, - IServiceProvider provider, - SocialNetworksJobConfiguration configuration) : base(log, provider, configuration) - { - } -} \ No newline at end of file diff --git a/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs b/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs new file mode 100644 index 0000000..005563f --- /dev/null +++ b/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs @@ -0,0 +1,16 @@ +using System; +using Dodo1000Bot.Services; +using Dodo1000Bot.Services.Configuration; +using Microsoft.Extensions.Logging; + +namespace Dodo1000Bot.Api.Jobs; + +public class YoutubeCheckAndNotifyJob: CheckAndNotifyJob +{ + public YoutubeCheckAndNotifyJob( + ILogger log, + IServiceProvider provider, + YoutubeJobConfiguration configuration) : base(log, provider, configuration) + { + } +} \ No newline at end of file diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index 8682ba5..a5f1308 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -32,7 +32,7 @@ "StatisticsJob": { "RefreshEveryTime": "0:10:00" }, - "SocialNetworksJob": { + "YoutubeJob": { "RefreshEveryTime": "0:10:00" }, "GlobalApiEndpoint": "https://globalapi.dodois.io/api/v2/", diff --git a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs index ae94d2e..bd27223 100644 --- a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs @@ -12,7 +12,7 @@ public class AppConfiguration public StatisticsJobConfiguration StatisticsJob { get; set; } - public SocialNetworksJobConfiguration SocialNetworksJob { get; set; } + public YoutubeJobConfiguration YoutubeJob { get; set; } public string GlobalApiEndpoint { get; set; } diff --git a/Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs b/Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs deleted file mode 100644 index 1ef6250..0000000 --- a/Dodo1000Bot.Services/Configuration/SocialNetworksJobConfiguration.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Dodo1000Bot.Services.Configuration -{ - public class SocialNetworksJobConfiguration : CheckAndNotifyJobConfiguration - { - } -} \ No newline at end of file diff --git a/Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs b/Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs new file mode 100644 index 0000000..1b76f3b --- /dev/null +++ b/Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs @@ -0,0 +1,6 @@ +namespace Dodo1000Bot.Services.Configuration +{ + public class YoutubeJobConfiguration : CheckAndNotifyJobConfiguration + { + } +} \ No newline at end of file diff --git a/Dodo1000Bot.Services/SocialNetworksService.cs b/Dodo1000Bot.Services/YoutubeService.cs similarity index 66% rename from Dodo1000Bot.Services/SocialNetworksService.cs rename to Dodo1000Bot.Services/YoutubeService.cs index 8baf6a5..6fd9218 100644 --- a/Dodo1000Bot.Services/SocialNetworksService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -5,20 +5,20 @@ namespace Dodo1000Bot.Services; -public class SocialNetworksService: CheckAndNotifyService +public class YoutubeService: CheckAndNotifyService { - private readonly ILogger _log; + private readonly ILogger _log; private readonly IYouTubeClient _youTubeClient; private readonly INotificationsService _notificationsService; private readonly ISnapshotsRepository _snapshotsRepository; - public SocialNetworksService(ILogger log) + public YoutubeService(ILogger log) { _log = log; } public override Task CheckAndNotify(CancellationToken cancellationToken) { - throw new System.NotImplementedException(); + var videos = await _youTubeClient.Videos(cancellationToken); } } \ No newline at end of file From 2dc4557d7c650f3bc98b4643f33b2f5ca5c0b56f Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Fri, 9 Feb 2024 22:03:57 +0300 Subject: [PATCH 08/29] CheckAndNotifyService refactpred to interface --- Dodo1000Bot.Services/CheckAndNotifyService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dodo1000Bot.Services/CheckAndNotifyService.cs b/Dodo1000Bot.Services/CheckAndNotifyService.cs index 8d48319..898eefc 100644 --- a/Dodo1000Bot.Services/CheckAndNotifyService.cs +++ b/Dodo1000Bot.Services/CheckAndNotifyService.cs @@ -3,8 +3,8 @@ namespace Dodo1000Bot.Services { - public abstract class CheckAndNotifyService + public interface CheckAndNotifyService { - public abstract Task CheckAndNotify(CancellationToken cancellationToken); + public Task CheckAndNotify(CancellationToken cancellationToken); } } \ No newline at end of file From 1416fa57938eb29b1c5b3e55784cf84d45357bca Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Mon, 12 Feb 2024 09:02:50 +0300 Subject: [PATCH 09/29] refactored --- Dodo1000Bot.Api/DependencyConfiguration.cs | 2 +- Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs | 4 ++-- Dodo1000Bot.Api/appsettings.json | 7 +++++-- .../Configuration/AppConfiguration.cs | 2 +- .../Configuration/YoutubeConfiguration_1.cs | 7 +++++++ .../Configuration/YoutubeJobConfiguration.cs | 6 ------ Dodo1000Bot.Services/StatisticsService.cs | 2 +- Dodo1000Bot.Services/UnitsService.cs | 2 +- Dodo1000Bot.Services/YoutubeService.cs | 12 +++++++++--- 9 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs delete mode 100644 Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs diff --git a/Dodo1000Bot.Api/DependencyConfiguration.cs b/Dodo1000Bot.Api/DependencyConfiguration.cs index bbe5b78..14f86a1 100644 --- a/Dodo1000Bot.Api/DependencyConfiguration.cs +++ b/Dodo1000Bot.Api/DependencyConfiguration.cs @@ -21,7 +21,7 @@ internal static void Configure(IServiceCollection services, IConfiguration appCo services.AddSingleton(configuration.PushNotifications); services.AddSingleton(configuration.UnitsJob); services.AddSingleton(configuration.StatisticsJob); - services.AddSingleton(configuration.YoutubeJob); + services.AddSingleton(configuration.Youtube); services.AddSingleton(configuration.Management); services.AddSingleton(configuration.YouTube); diff --git a/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs b/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs index 005563f..bbdc3c4 100644 --- a/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs +++ b/Dodo1000Bot.Api/Jobs/YoutubeCheckAndNotifyJob.cs @@ -5,12 +5,12 @@ namespace Dodo1000Bot.Api.Jobs; -public class YoutubeCheckAndNotifyJob: CheckAndNotifyJob +public class YoutubeCheckAndNotifyJob: CheckAndNotifyJob { public YoutubeCheckAndNotifyJob( ILogger log, IServiceProvider provider, - YoutubeJobConfiguration configuration) : base(log, provider, configuration) + YoutubeConfiguration configuration) : base(log, provider, configuration) { } } \ No newline at end of file diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index a5f1308..362370f 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -32,8 +32,11 @@ "StatisticsJob": { "RefreshEveryTime": "0:10:00" }, - "YoutubeJob": { - "RefreshEveryTime": "0:10:00" + "Youtube": { + "RefreshEveryTime": "0:10:00", + "Channels": [ + "UCkPTVsNw2R5lwQ_VxF6Wuow" + ] }, "GlobalApiEndpoint": "https://globalapi.dodois.io/api/v2/", "RealtimeBoardApiClientEndpoint": "", diff --git a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs index bd27223..4247ee1 100644 --- a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs @@ -12,7 +12,7 @@ public class AppConfiguration public StatisticsJobConfiguration StatisticsJob { get; set; } - public YoutubeJobConfiguration YoutubeJob { get; set; } + public YoutubeConfiguration Youtube { get; set; } public string GlobalApiEndpoint { get; set; } diff --git a/Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs b/Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs new file mode 100644 index 0000000..6a3dfda --- /dev/null +++ b/Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs @@ -0,0 +1,7 @@ +namespace Dodo1000Bot.Services.Configuration +{ + public class YoutubeConfiguration : CheckAndNotifyJobConfiguration + { + public string[] Channels { get; set; } + } +} \ No newline at end of file diff --git a/Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs b/Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs deleted file mode 100644 index 1b76f3b..0000000 --- a/Dodo1000Bot.Services/Configuration/YoutubeJobConfiguration.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Dodo1000Bot.Services.Configuration -{ - public class YoutubeJobConfiguration : CheckAndNotifyJobConfiguration - { - } -} \ No newline at end of file diff --git a/Dodo1000Bot.Services/StatisticsService.cs b/Dodo1000Bot.Services/StatisticsService.cs index c31a84e..25abb48 100644 --- a/Dodo1000Bot.Services/StatisticsService.cs +++ b/Dodo1000Bot.Services/StatisticsService.cs @@ -26,7 +26,7 @@ public StatisticsService(ILogger log, IRealtimeBoardApiClient _notificationsService = notificationsService; } - public override async Task CheckAndNotify(CancellationToken cancellationToken) + public async Task CheckAndNotify(CancellationToken cancellationToken) { try { diff --git a/Dodo1000Bot.Services/UnitsService.cs b/Dodo1000Bot.Services/UnitsService.cs index 2ef2934..c46522e 100644 --- a/Dodo1000Bot.Services/UnitsService.cs +++ b/Dodo1000Bot.Services/UnitsService.cs @@ -37,7 +37,7 @@ public UnitsService( _countriesService = countriesService; } - public override async Task CheckAndNotify(CancellationToken cancellationToken) + public async Task CheckAndNotify(CancellationToken cancellationToken) { try { diff --git a/Dodo1000Bot.Services/YoutubeService.cs b/Dodo1000Bot.Services/YoutubeService.cs index 6fd9218..b1f30a8 100644 --- a/Dodo1000Bot.Services/YoutubeService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -1,5 +1,6 @@ using System.Threading; using System.Threading.Tasks; +using Dodo1000Bot.Services.Configuration; using Dodo1000Bot.Services.Interfaces; using Microsoft.Extensions.Logging; @@ -8,17 +9,22 @@ namespace Dodo1000Bot.Services; public class YoutubeService: CheckAndNotifyService { private readonly ILogger _log; + private readonly YoutubeConfiguration _configuration; private readonly IYouTubeClient _youTubeClient; private readonly INotificationsService _notificationsService; private readonly ISnapshotsRepository _snapshotsRepository; - public YoutubeService(ILogger log) + public YoutubeService(ILogger log, YoutubeConfiguration configuration) { _log = log; + _configuration = configuration; } - public override Task CheckAndNotify(CancellationToken cancellationToken) + public async Task CheckAndNotify(CancellationToken cancellationToken) { - var videos = await _youTubeClient.Videos(cancellationToken); + foreach(var channel in _configuration.Channels) + { + var videos = await _youTubeClient.Videos(channel, cancellationToken); + } } } \ No newline at end of file From ee38e2785ad36d750bebc49097e5e2fdaf9a898c Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Mon, 12 Feb 2024 09:32:37 +0300 Subject: [PATCH 10/29] refactored --- Dodo1000Bot.Api/appsettings.json | 6 ++---- Dodo1000Bot.Services/Clients/YouTubeClient.cs | 4 ++-- Dodo1000Bot.Services/Configuration/AppConfiguration.cs | 2 +- .../Configuration/YouTubeConfiguration.cs | 8 -------- ...{YoutubeConfiguration_1.cs => YoutubeConfiguration.cs} | 4 ++++ 5 files changed, 9 insertions(+), 15 deletions(-) delete mode 100644 Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs rename Dodo1000Bot.Services/Configuration/{YoutubeConfiguration_1.cs => YoutubeConfiguration.cs} (66%) diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index 362370f..f457c87 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -34,6 +34,8 @@ }, "Youtube": { "RefreshEveryTime": "0:10:00", + "ApiKey": "", + "Endpoint": "https://youtube.googleapis.com/youtube/v3/search", "Channels": [ "UCkPTVsNw2R5lwQ_VxF6Wuow" ] @@ -44,9 +46,5 @@ "MysqlConnectionString": "", "Management": { "IncomingToken": "" - }, - "YouTube": { - "ApiKey": "", - "Endpoint": "https://youtube.googleapis.com/youtube/v3/search" } } diff --git a/Dodo1000Bot.Services/Clients/YouTubeClient.cs b/Dodo1000Bot.Services/Clients/YouTubeClient.cs index a262d76..55fc857 100644 --- a/Dodo1000Bot.Services/Clients/YouTubeClient.cs +++ b/Dodo1000Bot.Services/Clients/YouTubeClient.cs @@ -10,11 +10,11 @@ namespace Dodo1000Bot.Services.Clients; public class YouTubeClient : IYouTubeClient { - private readonly YouTubeConfiguration _configuration; + private readonly YoutubeConfiguration _configuration; private readonly HttpClient _httpClient; private readonly JsonSerializerOptions _serializerOptions; - public YouTubeClient(YouTubeConfiguration configuration, HttpClient httpClient, JsonSerializerOptions serializerOptions) + public YouTubeClient(YoutubeConfiguration configuration, HttpClient httpClient, JsonSerializerOptions serializerOptions) { _configuration = configuration; _httpClient = httpClient; diff --git a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs index 4247ee1..872c96f 100644 --- a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs @@ -26,6 +26,6 @@ public class AppConfiguration public ManagementConfiguration Management { get; set; } - public YouTubeConfiguration YouTube { get; set; } + public YoutubeConfiguration YouTube { get; set; } } } diff --git a/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs b/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs deleted file mode 100644 index 43f1092..0000000 --- a/Dodo1000Bot.Services/Configuration/YouTubeConfiguration.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Dodo1000Bot.Services.Configuration; - -public class YouTubeConfiguration -{ - public string ApiKey { get; set; } - - public string Endpoint { get; set; } -} \ No newline at end of file diff --git a/Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs b/Dodo1000Bot.Services/Configuration/YoutubeConfiguration.cs similarity index 66% rename from Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs rename to Dodo1000Bot.Services/Configuration/YoutubeConfiguration.cs index 6a3dfda..a8b1b20 100644 --- a/Dodo1000Bot.Services/Configuration/YoutubeConfiguration_1.cs +++ b/Dodo1000Bot.Services/Configuration/YoutubeConfiguration.cs @@ -2,6 +2,10 @@ namespace Dodo1000Bot.Services.Configuration { public class YoutubeConfiguration : CheckAndNotifyJobConfiguration { + public string ApiKey { get; set; } + + public string Endpoint { get; set; } + public string[] Channels { get; set; } } } \ No newline at end of file From d53c0afc281d842dad367c444dceeb1803e92d26 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Mon, 12 Feb 2024 09:32:47 +0300 Subject: [PATCH 11/29] youtube models --- Dodo1000Bot.Models/Youtube/SearchResponse.cs | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Dodo1000Bot.Models/Youtube/SearchResponse.cs diff --git a/Dodo1000Bot.Models/Youtube/SearchResponse.cs b/Dodo1000Bot.Models/Youtube/SearchResponse.cs new file mode 100644 index 0000000..8fe9600 --- /dev/null +++ b/Dodo1000Bot.Models/Youtube/SearchResponse.cs @@ -0,0 +1,24 @@ +namespace Dodo1000Bot.Models.Youtube; + +public class SearchResponse +{ + public Items[] Items { get; set; } +} + +public class Items +{ + public Id Id { get; set; } + public Snippet Snippet { get; set; } +} + +public class Id +{ + public string VideoId { get; set; } +} + +public class Snippet +{ + public string PublishedAt { get; set; } + public string LiveBroadcastContent { get; set; } + public string PublishTime { get; set; } +} \ No newline at end of file From 39a9ebc7d98f52849119bf8535151ac1fb18374f Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Tue, 20 Feb 2024 21:53:57 +0300 Subject: [PATCH 12/29] check difference --- Dodo1000Bot.Models/Youtube/SearchResponse.cs | 4 +- Dodo1000Bot.Services/Clients/YouTubeClient.cs | 8 ++-- .../Interfaces/IYouTubeClient.cs | 4 +- Dodo1000Bot.Services/YoutubeService.cs | 41 +++++++++++++++++-- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/Dodo1000Bot.Models/Youtube/SearchResponse.cs b/Dodo1000Bot.Models/Youtube/SearchResponse.cs index 8fe9600..4d067f5 100644 --- a/Dodo1000Bot.Models/Youtube/SearchResponse.cs +++ b/Dodo1000Bot.Models/Youtube/SearchResponse.cs @@ -2,10 +2,10 @@ public class SearchResponse { - public Items[] Items { get; set; } + public Video[] Items { get; set; } } -public class Items +public class Video { public Id Id { get; set; } public Snippet Snippet { get; set; } diff --git a/Dodo1000Bot.Services/Clients/YouTubeClient.cs b/Dodo1000Bot.Services/Clients/YouTubeClient.cs index 55fc857..1d2d63c 100644 --- a/Dodo1000Bot.Services/Clients/YouTubeClient.cs +++ b/Dodo1000Bot.Services/Clients/YouTubeClient.cs @@ -2,7 +2,7 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Dodo1000Bot.Models.RealtimeBoard; +using Dodo1000Bot.Models.Youtube; using Dodo1000Bot.Services.Configuration; using Dodo1000Bot.Services.Extensions; @@ -21,12 +21,12 @@ public YouTubeClient(YoutubeConfiguration configuration, HttpClient httpClient, _serializerOptions = serializerOptions; } - public async Task Videos(string channelId, CancellationToken cancellationToken) + public async Task SearchVideos(string channelId, CancellationToken cancellationToken) { var url = $"?part=snippet&channelId={channelId}&order=date&type=video&maxResults=50&key={_configuration.ApiKey}"; - var statistics = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); + var searchResponse = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); - return statistics; + return searchResponse.Items; } } \ No newline at end of file diff --git a/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs b/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs index dc5f1ef..f60ea07 100644 --- a/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs +++ b/Dodo1000Bot.Services/Interfaces/IYouTubeClient.cs @@ -1,10 +1,10 @@ using System.Threading; using System.Threading.Tasks; -using Dodo1000Bot.Models.RealtimeBoard; +using Dodo1000Bot.Models.Youtube; namespace Dodo1000Bot.Services; public interface IYouTubeClient { - Task Videos(string channelId, CancellationToken cancellationToken); + Task SearchVideos(string channelId, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/Dodo1000Bot.Services/YoutubeService.cs b/Dodo1000Bot.Services/YoutubeService.cs index b1f30a8..9f1d04c 100644 --- a/Dodo1000Bot.Services/YoutubeService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -1,5 +1,9 @@ -using System.Threading; +using System.Linq; +using System.Threading; using System.Threading.Tasks; +using Dodo1000Bot.Models; +using Dodo1000Bot.Models.Domain; +using Dodo1000Bot.Models.Youtube; using Dodo1000Bot.Services.Configuration; using Dodo1000Bot.Services.Interfaces; using Microsoft.Extensions.Logging; @@ -14,17 +18,48 @@ public class YoutubeService: CheckAndNotifyService private readonly INotificationsService _notificationsService; private readonly ISnapshotsRepository _snapshotsRepository; - public YoutubeService(ILogger log, YoutubeConfiguration configuration) + public YoutubeService( + ILogger log, + YoutubeConfiguration configuration, + IYouTubeClient youTubeClient, + INotificationsService notificationsService, + ISnapshotsRepository snapshotsRepository) { _log = log; _configuration = configuration; + _youTubeClient = youTubeClient; + _notificationsService = notificationsService; + _snapshotsRepository = snapshotsRepository; } public async Task CheckAndNotify(CancellationToken cancellationToken) { foreach(var channel in _configuration.Channels) { - var videos = await _youTubeClient.Videos(channel, cancellationToken); + var snapshotName = GetChannelSnapshotName(channel); + var videosSnapshot = + await _snapshotsRepository.Get(snapshotName, cancellationToken); + var videos = await _youTubeClient.SearchVideos(channel, cancellationToken); + + var difference = videos.ExceptBy(videosSnapshot.Data.Select(v => v.Id), c => c.Id); + + foreach (var video in difference) + { + var notification = new Notification(NotificationType.Admin) + { + Payload = new NotificationPayload + { + Text = $"Let's watch it together! https://youtu.be/{video.Id.VideoId} 👀" + } + }; + + await _notificationsService.Save(notification, cancellationToken); + } } } + + private string GetChannelSnapshotName(string channel) + { + return $"{nameof(_youTubeClient.SearchVideos)}_{channel}"; + } } \ No newline at end of file From 371f9c7586ec0edc5c3685402476ac335506d5a4 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Tue, 20 Feb 2024 22:13:47 +0300 Subject: [PATCH 13/29] CreateVideosSnapshotIfNotExists --- .../InternalServicesRegistration.cs | 1 + .../DependencyModules/JobsRegistration.cs | 1 + Dodo1000Bot.Api/Jobs/FirstRunJob.cs | 21 +++++----- Dodo1000Bot.Services/Clients/YouTubeClient.cs | 6 +-- Dodo1000Bot.Services/YoutubeService.cs | 42 ++++++++++++++++++- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Dodo1000Bot.Api/DependencyModules/InternalServicesRegistration.cs b/Dodo1000Bot.Api/DependencyModules/InternalServicesRegistration.cs index b5f8c97..16e4d54 100644 --- a/Dodo1000Bot.Api/DependencyModules/InternalServicesRegistration.cs +++ b/Dodo1000Bot.Api/DependencyModules/InternalServicesRegistration.cs @@ -11,6 +11,7 @@ internal static void AddInternalServices(this IServiceCollection services) services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Dodo1000Bot.Api/DependencyModules/JobsRegistration.cs b/Dodo1000Bot.Api/DependencyModules/JobsRegistration.cs index 5fac288..7a0cebf 100644 --- a/Dodo1000Bot.Api/DependencyModules/JobsRegistration.cs +++ b/Dodo1000Bot.Api/DependencyModules/JobsRegistration.cs @@ -13,5 +13,6 @@ internal static void AddJobs(this IServiceCollection services, AppConfiguration services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); + services.AddHostedService(); } } \ No newline at end of file diff --git a/Dodo1000Bot.Api/Jobs/FirstRunJob.cs b/Dodo1000Bot.Api/Jobs/FirstRunJob.cs index cea3064..246d23e 100644 --- a/Dodo1000Bot.Api/Jobs/FirstRunJob.cs +++ b/Dodo1000Bot.Api/Jobs/FirstRunJob.cs @@ -1,28 +1,27 @@ -using System; -using System.Threading; +using System.Threading; using System.Threading.Tasks; using Dodo1000Bot.Services; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Dodo1000Bot.Api.Jobs; public class FirstRunJob: IHostedService { - private readonly IServiceProvider _provider; + private readonly UnitsService _unitsService; + private readonly YoutubeService _youtubeService; - public FirstRunJob(IServiceProvider provider, UnitsService unitsService) + public FirstRunJob(UnitsService unitsService, YoutubeService youtubeService) { - _provider = provider; + _unitsService = unitsService; + _youtubeService = youtubeService; } public async Task StartAsync(CancellationToken cancellationToken) { - await using var scope = _provider.CreateAsyncScope(); - var unitsService = scope.ServiceProvider.GetRequiredService(); - - await unitsService.CreateUnitsCountSnapshotIfNotExists(cancellationToken); - await unitsService.CreateUnitsSnapshotIfNotExists(cancellationToken); + await _unitsService.CreateUnitsCountSnapshotIfNotExists(cancellationToken); + await _unitsService.CreateUnitsSnapshotIfNotExists(cancellationToken); + + await _youtubeService.CreateVideosSnapshotIfNotExists(cancellationToken); } public Task StopAsync(CancellationToken cancellationToken) diff --git a/Dodo1000Bot.Services/Clients/YouTubeClient.cs b/Dodo1000Bot.Services/Clients/YouTubeClient.cs index 1d2d63c..5b31868 100644 --- a/Dodo1000Bot.Services/Clients/YouTubeClient.cs +++ b/Dodo1000Bot.Services/Clients/YouTubeClient.cs @@ -1,10 +1,10 @@ using System.Net.Http; +using System.Net.Http.Json; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Dodo1000Bot.Models.Youtube; using Dodo1000Bot.Services.Configuration; -using Dodo1000Bot.Services.Extensions; namespace Dodo1000Bot.Services.Clients; @@ -23,9 +23,9 @@ public YouTubeClient(YoutubeConfiguration configuration, HttpClient httpClient, public async Task SearchVideos(string channelId, CancellationToken cancellationToken) { - var url = $"?part=snippet&channelId={channelId}&order=date&type=video&maxResults=50&key={_configuration.ApiKey}"; + var url = $"?part=snippet&channelId={channelId}&order=date&type=video&maxResults=5&key={_configuration.ApiKey}"; - var searchResponse = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); + var searchResponse = await _httpClient.GetFromJsonAsync(url, _serializerOptions, cancellationToken); return searchResponse.Items; } diff --git a/Dodo1000Bot.Services/YoutubeService.cs b/Dodo1000Bot.Services/YoutubeService.cs index 9f1d04c..b8069f2 100644 --- a/Dodo1000Bot.Services/YoutubeService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Dodo1000Bot.Models; @@ -41,7 +42,7 @@ public async Task CheckAndNotify(CancellationToken cancellationToken) await _snapshotsRepository.Get(snapshotName, cancellationToken); var videos = await _youTubeClient.SearchVideos(channel, cancellationToken); - var difference = videos.ExceptBy(videosSnapshot.Data.Select(v => v.Id), c => c.Id); + var difference = videos.ExceptBy(videosSnapshot.Data.Select(v => v.Id.VideoId), c => c.Id.VideoId); foreach (var video in difference) { @@ -58,6 +59,43 @@ public async Task CheckAndNotify(CancellationToken cancellationToken) } } + public async Task CreateVideosSnapshotIfNotExists(CancellationToken cancellationToken) + { + foreach (var channel in _configuration.Channels) + { + try + { + var snapshotName = GetChannelSnapshotName(channel); + + var videosSnapshot = + await _snapshotsRepository.Get(snapshotName, cancellationToken); + + if (videosSnapshot?.Data is not null) + { + _log.LogInformation("videosSnapshot is not null"); + return; + } + + var videos = await _youTubeClient.SearchVideos(channel, cancellationToken); + + await UpdateSnapshot(snapshotName, videos, cancellationToken); + } + catch (Exception e) + { + _log.LogError(e, "Can't {methodName}", nameof(CreateVideosSnapshotIfNotExists)); + } + } + } + + private async Task UpdateSnapshot(string snapshotName, TData data, CancellationToken cancellationToken) + { + _log.LogInformation("Start UpdateSnapshot for snapshotName {snapshotName}", snapshotName); + var newSnapshot = Snapshot.Create(snapshotName, data); + + await _snapshotsRepository.Save(newSnapshot, cancellationToken); + _log.LogInformation("Finish UpdateSnapshot for snapshotName {snapshotName}", snapshotName); + } + private string GetChannelSnapshotName(string channel) { return $"{nameof(_youTubeClient.SearchVideos)}_{channel}"; From 82afba8395dd4ed89b53d6ba22a11d67b8e3d535 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Tue, 20 Feb 2024 22:15:07 +0300 Subject: [PATCH 14/29] formatting --- Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs index 0067fdb..022676a 100644 --- a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs +++ b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs @@ -8,7 +8,6 @@ using Dodo1000Bot.Services; using Microsoft.Extensions.Logging; using Telegram.Bot; -using Telegram.Bot.Types; using Telegram.Bot.Exceptions; using Telegram.Bot.Types.Enums; @@ -38,7 +37,7 @@ public async Task> NotifyAbout(IList users = await _usersRepository.GetUsers(Source.Telegram, cancellationToken); + IList users = await _usersRepository.GetUsers(Source.Telegram, cancellationToken); if (users?.Any() != true) { @@ -55,7 +54,7 @@ public async Task> NotifyAbout(IList> PushNotificationsToUser(IList notifications, Models.Domain.User user, CancellationToken cancellationToken) + private async Task> PushNotificationsToUser(IList notifications, User user, CancellationToken cancellationToken) { var pushedNotifications = new List(); From 4cdf9ef353f9abd0bf293597b11cc8555ff4037f Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Tue, 20 Feb 2024 22:18:20 +0300 Subject: [PATCH 15/29] used GetFromJsonAsync extension instead of custom --- .../Clients/GlobalApiClient.cs | 6 ++--- .../Clients/RealtimeBoardApiClient.cs | 4 +-- .../Clients/RestcountriesApiClient.cs | 4 +-- .../Extensions/HttpClientExtensions.cs | 25 ------------------- 4 files changed, 7 insertions(+), 32 deletions(-) delete mode 100644 Dodo1000Bot.Services/Extensions/HttpClientExtensions.cs diff --git a/Dodo1000Bot.Services/Clients/GlobalApiClient.cs b/Dodo1000Bot.Services/Clients/GlobalApiClient.cs index 795e7d9..d5d208f 100644 --- a/Dodo1000Bot.Services/Clients/GlobalApiClient.cs +++ b/Dodo1000Bot.Services/Clients/GlobalApiClient.cs @@ -1,10 +1,10 @@ using System.Net.Http; +using System.Net.Http.Json; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Dodo1000Bot.Models; using Dodo1000Bot.Models.GlobalApi; -using Dodo1000Bot.Services.Extensions; namespace Dodo1000Bot.Services.Clients { @@ -23,7 +23,7 @@ public async Task UnitsCount(CancellationToken { const string url = "units/count"; - var unitsCount = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); + var unitsCount = await _httpClient.GetFromJsonAsync(url, _serializerOptions, cancellationToken); return unitsCount; } @@ -32,7 +32,7 @@ public async Task> UnitsOfBrandAtCountry(Brands brand, { var url = $"{brand}/units/all/{countryId}"; - var unitsCount = await _httpClient.GetAsync>(url, _serializerOptions, cancellationToken); + var unitsCount = await _httpClient.GetFromJsonAsync>(url, _serializerOptions, cancellationToken); return unitsCount; } diff --git a/Dodo1000Bot.Services/Clients/RealtimeBoardApiClient.cs b/Dodo1000Bot.Services/Clients/RealtimeBoardApiClient.cs index b91dd5d..745d533 100644 --- a/Dodo1000Bot.Services/Clients/RealtimeBoardApiClient.cs +++ b/Dodo1000Bot.Services/Clients/RealtimeBoardApiClient.cs @@ -1,9 +1,9 @@ using System.Net.Http; +using System.Net.Http.Json; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Dodo1000Bot.Models.RealtimeBoard; -using Dodo1000Bot.Services.Extensions; namespace Dodo1000Bot.Services.Clients { @@ -22,7 +22,7 @@ public async Task Statistics(CancellationToken cancellationToken) { const string url = "statistics"; - var statistics = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); + var statistics = await _httpClient.GetFromJsonAsync(url, _serializerOptions, cancellationToken); return statistics; } diff --git a/Dodo1000Bot.Services/Clients/RestcountriesApiClient.cs b/Dodo1000Bot.Services/Clients/RestcountriesApiClient.cs index 43b9554..a8d4716 100644 --- a/Dodo1000Bot.Services/Clients/RestcountriesApiClient.cs +++ b/Dodo1000Bot.Services/Clients/RestcountriesApiClient.cs @@ -1,9 +1,9 @@ using System.Net.Http; +using System.Net.Http.Json; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Dodo1000Bot.Models.Restcountries; -using Dodo1000Bot.Services.Extensions; namespace Dodo1000Bot.Services.Clients; @@ -23,7 +23,7 @@ public async Task GetNames(CancellationToken cancellationToken, param var joinedCodes = string.Join(",", codes); var url = $"alpha?codes={joinedCodes}&fields=name"; - var countries = await _httpClient.GetAsync(url, _serializerOptions, cancellationToken); + var countries = await _httpClient.GetFromJsonAsync(url, _serializerOptions, cancellationToken); return countries; } diff --git a/Dodo1000Bot.Services/Extensions/HttpClientExtensions.cs b/Dodo1000Bot.Services/Extensions/HttpClientExtensions.cs deleted file mode 100644 index 5430a7a..0000000 --- a/Dodo1000Bot.Services/Extensions/HttpClientExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Net.Http; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Dodo1000Bot.Services.Extensions -{ - public static class HttpClientExtensions - { - public static async Task GetAsync(this HttpClient httpClient, string url, - JsonSerializerOptions serializerOptions, CancellationToken cancellationToken) - { - url = url.ToLower(); - - var response = await httpClient.GetAsync(url, cancellationToken); - - response.EnsureSuccessStatusCode(); - var allUnitsString = await response.Content.ReadAsStringAsync(cancellationToken); - - var result = JsonSerializer.Deserialize(allUnitsString, serializerOptions); - - return result; - } - } -} \ No newline at end of file From 7dd5ff2a7a0b8e881711125dd26ade7e5c2fa4b5 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 19:24:15 +0300 Subject: [PATCH 16/29] UpdateSnapshot for each channel --- Dodo1000Bot.Services/YoutubeService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dodo1000Bot.Services/YoutubeService.cs b/Dodo1000Bot.Services/YoutubeService.cs index b8069f2..5750fca 100644 --- a/Dodo1000Bot.Services/YoutubeService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -56,6 +56,8 @@ public async Task CheckAndNotify(CancellationToken cancellationToken) await _notificationsService.Save(notification, cancellationToken); } + + await UpdateSnapshot(snapshotName, videos, cancellationToken); } } From 8ae6489c2b117e0b899c2eb94064cc1f3388c6b1 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 19:24:37 +0300 Subject: [PATCH 17/29] send messages to admins --- Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs | 5 +++++ Dodo1000Bot.Models/Domain/User.cs | 2 ++ Dodo1000Bot.Services/NotificationsService.cs | 4 +--- Dodo1000Bot.Services/Repositories/UsersRepository.cs | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs index 022676a..f9385f1 100644 --- a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs +++ b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs @@ -60,6 +60,11 @@ private async Task> PushNotificationsToUser(IList n.Type != NotificationType.Admin).ToList(); - - IEnumerable>> tasks = _notifyServices.Select(s => s.NotifyAbout(sortedNotifications, cancellationToken)); + IEnumerable>> tasks = _notifyServices.Select(s => s.NotifyAbout(notifications, cancellationToken)); IEnumerable[] tasksResults = await Task.WhenAll(tasks); diff --git a/Dodo1000Bot.Services/Repositories/UsersRepository.cs b/Dodo1000Bot.Services/Repositories/UsersRepository.cs index ef9aea4..354f02b 100644 --- a/Dodo1000Bot.Services/Repositories/UsersRepository.cs +++ b/Dodo1000Bot.Services/Repositories/UsersRepository.cs @@ -21,7 +21,7 @@ public UsersRepository(MySqlConnection connection) public async Task> GetUsers(Source messengerType, CancellationToken cancellationToken) { var users = await _connection.QueryAsync(new CommandDefinition( - @"SELECT Id, MessengerUserId, MessengerType FROM users + @"SELECT Id, MessengerUserId, MessengerType, IsAdmin FROM users WHERE MessengerType = @messengerType", new { From 9a5c80d89e44dc0541b2f9b1283fb3e788868662 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 19:24:45 +0300 Subject: [PATCH 18/29] refactored --- Dodo1000Bot.Models/Domain/Notification.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dodo1000Bot.Models/Domain/Notification.cs b/Dodo1000Bot.Models/Domain/Notification.cs index 3564b9e..1a892c1 100644 --- a/Dodo1000Bot.Models/Domain/Notification.cs +++ b/Dodo1000Bot.Models/Domain/Notification.cs @@ -12,6 +12,6 @@ public Notification(NotificationType type) public NotificationPayload Payload { get; set; } - public string Distinction => $"{Payload}"; + public string Distinction => Payload.ToString(); } } \ No newline at end of file From 751deb4cdd547fd88598dba745c206005f858708 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 20:33:58 +0300 Subject: [PATCH 19/29] removed exceeded --- Dodo1000Bot.Api/DependencyConfiguration.cs | 1 - .../ExternalServicesRegistration.cs | 46 +------------------ 2 files changed, 1 insertion(+), 46 deletions(-) diff --git a/Dodo1000Bot.Api/DependencyConfiguration.cs b/Dodo1000Bot.Api/DependencyConfiguration.cs index 14f86a1..22acad4 100644 --- a/Dodo1000Bot.Api/DependencyConfiguration.cs +++ b/Dodo1000Bot.Api/DependencyConfiguration.cs @@ -21,7 +21,6 @@ internal static void Configure(IServiceCollection services, IConfiguration appCo services.AddSingleton(configuration.PushNotifications); services.AddSingleton(configuration.UnitsJob); services.AddSingleton(configuration.StatisticsJob); - services.AddSingleton(configuration.Youtube); services.AddSingleton(configuration.Management); services.AddSingleton(configuration.YouTube); diff --git a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs index c82cf9d..11c10eb 100644 --- a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs +++ b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs @@ -1,14 +1,8 @@ -using System; -using Dodo1000Bot.Api.Extensions; +using Dodo1000Bot.Api.Extensions; using Dodo1000Bot.Services; using Dodo1000Bot.Services.Clients; -using Google.Apis.Auth.OAuth2; -using Google.Cloud.Dialogflow.V2; using Dodo1000Bot.Services.Configuration; -using GranSteL.Helpers.Redis; -using Grpc.Auth; using Microsoft.Extensions.DependencyInjection; -using StackExchange.Redis; namespace Dodo1000Bot.Api.DependencyModules { @@ -25,43 +19,5 @@ internal static void AddExternalServices(this IServiceCollection services, AppCo services.AddHttpClient(configuration.YouTube.Endpoint, nameof(configuration.RestcountriesApiClientEndpoint)); } - - private static SessionsClient RegisterDialogflowSessionsClient(IServiceProvider provider) - { - var configuration = provider.GetService(); - - var credential = GoogleCredential.FromFile(configuration.JsonPath).CreateScoped(SessionsClient.DefaultScopes); - - var clientBuilder = new SessionsClientBuilder - { - ChannelCredentials = credential.ToChannelCredentials() - }; - - var client = clientBuilder.Build(); - - return client; - } - - private static IDatabase RegisterRedisClient(IServiceProvider provider) - { - var configuration = provider.GetService(); - - var redisClient = ConnectionMultiplexer.Connect(configuration.ConnectionString); - - var dataBase = redisClient.GetDatabase(); - - return dataBase; - } - - private static RedisCacheService RegisterCacheService(IServiceProvider provider) - { - var configuration = provider.GetService(); - - var db = provider.GetService(); - - var service = new RedisCacheService(db, configuration.KeyPrefix); - - return service; - } } } From 7f6a8af65c277a6006ef1f7bf998aefb5a1d007a Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 20:34:38 +0300 Subject: [PATCH 20/29] fixed --- .../DependencyModules/ExternalServicesRegistration.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs index 11c10eb..9062954 100644 --- a/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs +++ b/Dodo1000Bot.Api/DependencyModules/ExternalServicesRegistration.cs @@ -17,7 +17,7 @@ internal static void AddExternalServices(this IServiceCollection services, AppCo services.AddHttpClient(configuration.RestcountriesApiClientEndpoint, nameof(configuration.RestcountriesApiClientEndpoint)); services.AddHttpClient(configuration.YouTube.Endpoint, - nameof(configuration.RestcountriesApiClientEndpoint)); + nameof(configuration.YouTube)); } } } From 59899b7e5419a3a64236ca632818aa1a956bc930 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 20:36:45 +0300 Subject: [PATCH 21/29] more readable condition --- Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs index f9385f1..334c8b1 100644 --- a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs +++ b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs @@ -60,7 +60,7 @@ private async Task> PushNotificationsToUser(IList Date: Sat, 24 Feb 2024 20:37:45 +0300 Subject: [PATCH 22/29] removed exceeded --- Dodo1000Bot.Services/Configuration/AppConfiguration.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs index 872c96f..c688aab 100644 --- a/Dodo1000Bot.Services/Configuration/AppConfiguration.cs +++ b/Dodo1000Bot.Services/Configuration/AppConfiguration.cs @@ -12,8 +12,6 @@ public class AppConfiguration public StatisticsJobConfiguration StatisticsJob { get; set; } - public YoutubeConfiguration Youtube { get; set; } - public string GlobalApiEndpoint { get; set; } public string RealtimeBoardApiClientEndpoint { get; set; } From ef992552f191c4de1b95f62b5d0371fbe0ad4741 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 20:40:00 +0300 Subject: [PATCH 23/29] removed Dialogflow and Redis configs --- Dodo1000Bot.Api/appsettings.json | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Dodo1000Bot.Api/appsettings.json b/Dodo1000Bot.Api/appsettings.json index f457c87..7bb7f31 100644 --- a/Dodo1000Bot.Api/appsettings.json +++ b/Dodo1000Bot.Api/appsettings.json @@ -13,16 +13,6 @@ "ExcludeBodiesWithWords": [ "ping", "pong" ], "IncludeEndpoints": ["dialogflow", "events"] }, - "Dialogflow": { - "ProjectId": "", - "LanguageCode": "", - "JsonPath": "", - "LogQuery": false - }, - "Redis": { - "ConnectionString": "", - "KeyPrefix": "" - }, "PushNotifications": { "EveryTime": "0:01:00" }, From befeb67e5bfb34f286396201cd0d5619252f6425 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sat, 24 Feb 2024 20:46:53 +0300 Subject: [PATCH 24/29] LiveBroadcastContentValidValues --- Dodo1000Bot.Services/YoutubeService.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Dodo1000Bot.Services/YoutubeService.cs b/Dodo1000Bot.Services/YoutubeService.cs index 5750fca..8a15a30 100644 --- a/Dodo1000Bot.Services/YoutubeService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -13,6 +13,12 @@ namespace Dodo1000Bot.Services; public class YoutubeService: CheckAndNotifyService { + private static readonly string[] LiveBroadcastContentValidValues = new[] + { + "none", + "live" + }; + private readonly ILogger _log; private readonly YoutubeConfiguration _configuration; private readonly IYouTubeClient _youTubeClient; @@ -46,6 +52,11 @@ public async Task CheckAndNotify(CancellationToken cancellationToken) foreach (var video in difference) { + if (!LiveBroadcastContentValidValues.Contains(video.Snippet.LiveBroadcastContent)) + { + continue; + } + var notification = new Notification(NotificationType.Admin) { Payload = new NotificationPayload From fce2b07ee223faa84be4551132906c7a2d1007a0 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sun, 25 Feb 2024 21:27:14 +0300 Subject: [PATCH 25/29] changed access level --- Dodo1000Bot.Api/Jobs/CheckAndNotifyJob.cs | 2 +- Dodo1000Bot.Api/Jobs/RepeatableJob.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dodo1000Bot.Api/Jobs/CheckAndNotifyJob.cs b/Dodo1000Bot.Api/Jobs/CheckAndNotifyJob.cs index 15ea1a2..bcf748e 100644 --- a/Dodo1000Bot.Api/Jobs/CheckAndNotifyJob.cs +++ b/Dodo1000Bot.Api/Jobs/CheckAndNotifyJob.cs @@ -14,7 +14,7 @@ public class CheckAndNotifyJob : RepeatableJob { private readonly IServiceProvider _provider; - public CheckAndNotifyJob(ILogger> log, + protected CheckAndNotifyJob(ILogger> log, IServiceProvider provider, TConfiguration configuration) : base(log, configuration.RefreshEveryTime) { diff --git a/Dodo1000Bot.Api/Jobs/RepeatableJob.cs b/Dodo1000Bot.Api/Jobs/RepeatableJob.cs index efabc87..f049e9c 100644 --- a/Dodo1000Bot.Api/Jobs/RepeatableJob.cs +++ b/Dodo1000Bot.Api/Jobs/RepeatableJob.cs @@ -11,7 +11,7 @@ public abstract class RepeatableJob : BackgroundService private readonly TimeSpan _repeatEveryTime; private readonly string _jobName; - protected ILogger Log { get; } + private ILogger Log { get; } protected RepeatableJob(ILogger log, TimeSpan repeatEveryTime) { From 5b9eb6097c4b689084d6f3c8add05f9cbaebfbd5 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sun, 25 Feb 2024 21:49:57 +0300 Subject: [PATCH 26/29] NotifyAbout_AdminNotifications_SentOnlyToAdminUsers --- .../TelegramNotifyServiceTests.cs | 36 +++++++++++++++++++ .../TelegramNotifyService.cs | 2 +- Dodo1000Bot.Models/Domain/Notification.cs | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Dodo1000Bot.Messengers.Telegram.Tests/TelegramNotifyServiceTests.cs b/Dodo1000Bot.Messengers.Telegram.Tests/TelegramNotifyServiceTests.cs index a87ded6..4ad36bf 100644 --- a/Dodo1000Bot.Messengers.Telegram.Tests/TelegramNotifyServiceTests.cs +++ b/Dodo1000Bot.Messengers.Telegram.Tests/TelegramNotifyServiceTests.cs @@ -111,5 +111,41 @@ public async Task NotifyAbout_AnyNotificationsAndUsers_SentAllNotificationsToAll Assert.AreEqual(notification.Id, pushedNotification.NotificationId); Assert.AreEqual(user.Id, pushedNotification.UserId); } + + [Test] + public async Task NotifyAbout_AdminNotifications_SentOnlyToAdminUsers() + { + var payload = _fixture.Build() + .With(n => n.Text) + .Create(); + var notification = _fixture.Build() + .With(n => n.Payload, payload) + .With(n => n.Type, NotificationType.Admin) + .Create(); + var ordinaryUser = _fixture.Build() + .With(u => u.Id) + .With(u => u.MessengerUserId, _fixture.Create().ToString) + .Create(); + var adminUser = _fixture.Build() + .With(u => u.Id) + .With(u => u.MessengerUserId, _fixture.Create().ToString) + .With(u => u.IsAdmin, true) + .Create(); + + var ct = CancellationToken.None; + + _usersRepositoryMock.Setup(r => r.GetUsers(Source.Telegram, ct)).ReturnsAsync(new []{ adminUser, ordinaryUser }); + _clientMock.Setup(c => c.SendTextMessageAsync(adminUser.MessengerUserId, notification.Payload.Text, + It.IsAny(), It.IsAny>(), + It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), + It.IsAny(), ct)).ReturnsAsync(() => null); + + var pushedNotifications = (await _target.NotifyAbout(new []{notification}, ct)).ToArray(); + + Assert.IsNotEmpty(pushedNotifications); + + Assert.True(pushedNotifications.All(n => n.UserId == adminUser.Id)); + } } } diff --git a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs index 334c8b1..9a53e6f 100644 --- a/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs +++ b/Dodo1000Bot.Messengers.Telegram/TelegramNotifyService.cs @@ -60,7 +60,7 @@ private async Task> PushNotificationsToUser(IList Date: Sun, 25 Feb 2024 22:01:46 +0300 Subject: [PATCH 27/29] notify only about live --- Dodo1000Bot.Services/YoutubeService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Dodo1000Bot.Services/YoutubeService.cs b/Dodo1000Bot.Services/YoutubeService.cs index 8a15a30..e9c9f20 100644 --- a/Dodo1000Bot.Services/YoutubeService.cs +++ b/Dodo1000Bot.Services/YoutubeService.cs @@ -13,9 +13,7 @@ namespace Dodo1000Bot.Services; public class YoutubeService: CheckAndNotifyService { - private static readonly string[] LiveBroadcastContentValidValues = new[] - { - "none", + private static readonly string[] LiveBroadcastContentValidValues = { "live" }; From 7cf538c2f26840443f70b8456c798a9d5deeffd5 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Sun, 25 Feb 2024 22:31:16 +0300 Subject: [PATCH 28/29] Version 1.26.0 --- Dodo1000Bot.Api/Dodo1000Bot.Api.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dodo1000Bot.Api/Dodo1000Bot.Api.csproj b/Dodo1000Bot.Api/Dodo1000Bot.Api.csproj index 605cb24..8c56735 100644 --- a/Dodo1000Bot.Api/Dodo1000Bot.Api.csproj +++ b/Dodo1000Bot.Api/Dodo1000Bot.Api.csproj @@ -2,7 +2,7 @@ net6.0 - 1.25.0 + 1.26.0 f449de95-800a-40ef-8716-6e80b7f0977d From f2e5a07cc8b766c311d56cd8b07dbb5c14723cbe Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Mon, 26 Feb 2024 09:01:35 +0300 Subject: [PATCH 29/29] added Title --- Dodo1000Bot.Models/Youtube/SearchResponse.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Dodo1000Bot.Models/Youtube/SearchResponse.cs b/Dodo1000Bot.Models/Youtube/SearchResponse.cs index 4d067f5..567da95 100644 --- a/Dodo1000Bot.Models/Youtube/SearchResponse.cs +++ b/Dodo1000Bot.Models/Youtube/SearchResponse.cs @@ -21,4 +21,5 @@ public class Snippet public string PublishedAt { get; set; } public string LiveBroadcastContent { get; set; } public string PublishTime { get; set; } + public string Title { get; set; } } \ No newline at end of file