From 9ebb4c7b5e4850a680f2f139233fd42657a66665 Mon Sep 17 00:00:00 2001 From: "Jeffrey T. Fritz" Date: Mon, 6 Nov 2023 10:31:03 -0500 Subject: [PATCH] Removed extra config, forced headers to Dictionary (#281) Co-authored-by: github-actions[bot] --- ...InMemoryProviderConfigurationRepository.cs | 35 +++-- .../Configuration/TwitterConfiguration.cs | 38 +++--- src/TagzApp.Providers.Twitter/StartTwitter.cs | 6 +- .../PostgresMessaging.cs | 5 +- src/TagzApp.Web/appsettings.json | 124 +++++++++--------- 5 files changed, 113 insertions(+), 95 deletions(-) diff --git a/src/TagzApp.Common/InMemoryProviderConfigurationRepository.cs b/src/TagzApp.Common/InMemoryProviderConfigurationRepository.cs index 4917d263..d687186c 100644 --- a/src/TagzApp.Common/InMemoryProviderConfigurationRepository.cs +++ b/src/TagzApp.Common/InMemoryProviderConfigurationRepository.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Configuration; +using System.Text.Json; namespace TagzApp.Common; public class InMemoryProviderConfigurationRepository : IProviderConfigurationRepository @@ -22,19 +23,37 @@ public async Task GetConfigurationSettingsAsync(string na foreach (var configSetting in configSettings) { - if (configSetting.Key == "Activated") - { - providerConfig.Activated = bool.Parse(configSetting.Value ?? "false"); - continue; - } - if (configSetting.Key == "Description") + switch (configSetting.Key.ToLowerInvariant()) { - providerConfig.Description = configSetting.Value ?? string.Empty; - continue; + case "activated": + providerConfig.Activated = bool.Parse(configSetting.Value ?? "false"); + continue; + break; + case "description": + providerConfig.Description = configSetting.Value ?? string.Empty; + continue; + break; + case "defaultheaders": + if (configSetting.GetChildren().Count() == 0 || configSetting.Value is string) + { + providerConfig.ConfigurationSettings!.Add(configSetting.Key, configSetting.Value ?? string.Empty); + continue; + } + var headerDict = new Dictionary(); + // iterate through the children of the DefaultHeaders section and add to the dictionary + foreach (var header in configSetting.GetChildren()) + { + headerDict.Add(header.Key, header.Value ?? string.Empty); + } + providerConfig.ConfigurationSettings!.Add("DefaultHeaders", JsonSerializer.Serialize(headerDict)); + continue; + break; + } providerConfig.ConfigurationSettings!.Add(configSetting.Key, configSetting.Value ?? string.Empty); + } return providerConfig; diff --git a/src/TagzApp.Providers.Twitter/Configuration/TwitterConfiguration.cs b/src/TagzApp.Providers.Twitter/Configuration/TwitterConfiguration.cs index 9200a240..70d39e5f 100644 --- a/src/TagzApp.Providers.Twitter/Configuration/TwitterConfiguration.cs +++ b/src/TagzApp.Providers.Twitter/Configuration/TwitterConfiguration.cs @@ -15,25 +15,25 @@ public class TwitterConfiguration : HttpClientOptions public bool Activated { get; set; } = false; - /// - /// Twitter issued API Key for the service - /// - public string ApiKey { get; set; } = string.Empty; - - /// - /// Twitter issued API Secret Key for the service - /// - public string ApiSecretKey { get; set; } = string.Empty; - - /// - /// Access token for Twitter - /// - public string AccessToken { get; set; } = string.Empty; - - /// - /// Access token secret for Twitter - /// - public string AccessTokenSecret { get; set; } = string.Empty; + ///// + ///// Twitter issued API Key for the service + ///// + //public string ApiKey { get; set; } = string.Empty; + + ///// + ///// Twitter issued API Secret Key for the service + ///// + //public string ApiSecretKey { get; set; } = string.Empty; + + ///// + ///// Access token for Twitter + ///// + //public string AccessToken { get; set; } = string.Empty; + + ///// + ///// Access token secret for Twitter + ///// + //public string AccessTokenSecret { get; set; } = string.Empty; /// /// Provider description diff --git a/src/TagzApp.Providers.Twitter/StartTwitter.cs b/src/TagzApp.Providers.Twitter/StartTwitter.cs index ca56ea9a..e8ab2b85 100644 --- a/src/TagzApp.Providers.Twitter/StartTwitter.cs +++ b/src/TagzApp.Providers.Twitter/StartTwitter.cs @@ -30,6 +30,8 @@ protected override void MapConfigurationValues(ProviderConfiguration providerCon { var config = providerConfiguration.ConfigurationSettings; + var headers = config["DefaultHeaders"]; + if (config != null) { _TwitterConfiguration = new TwitterConfiguration @@ -38,10 +40,6 @@ protected override void MapConfigurationValues(ProviderConfiguration providerCon BaseAddress = new Uri(config["BaseAddress"] ?? string.Empty), Timeout = TimeSpan.Parse(config["Timeout"] ?? string.Empty), DefaultHeaders = JsonSerializer.Deserialize?>(config["DefaultHeaders"]), - ApiKey = config["ApiKey"] ?? string.Empty, - ApiSecretKey = config["ApiSecretKey"] ?? string.Empty, - AccessToken = config["AccessToken"] ?? string.Empty, - AccessTokenSecret = config["AccessTokenSecret"] ?? string.Empty, Description = providerConfiguration.Description }; } diff --git a/src/TagzApp.Storage.Postgres/PostgresMessaging.cs b/src/TagzApp.Storage.Postgres/PostgresMessaging.cs index 4cf8b912..4c4720ce 100644 --- a/src/TagzApp.Storage.Postgres/PostgresMessaging.cs +++ b/src/TagzApp.Storage.Postgres/PostgresMessaging.cs @@ -61,8 +61,11 @@ internal async Task StartProviders(IEnumerable providers, if (provider is IHasNewestId newestIdProvider) { + + var SevenDaysAgo = DateTime.UtcNow.AddDays(-7); + var newestId = await context.Content.AsNoTracking() - .Where(c => c.Provider == provider.Id) + .Where(c => c.Provider == provider.Id && c.Timestamp > SevenDaysAgo) .OrderByDescending(c => c.ProviderId) .Select(c => c.ProviderId) .FirstOrDefaultAsync(); diff --git a/src/TagzApp.Web/appsettings.json b/src/TagzApp.Web/appsettings.json index 7eccd12a..5bd57c19 100644 --- a/src/TagzApp.Web/appsettings.json +++ b/src/TagzApp.Web/appsettings.json @@ -1,66 +1,64 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "ModerationEnabled": "true", - "ConnectionStrings": { - "SecurityContextConnection": "Data Source=TagzApp.Web.db" - }, - "AzureContentSafety": { - "Enabled": true - }, - "providers": { - "mastodon": { - "BaseAddress": "https://mastodon.social", - "Timeout": "00:03:00", - "DefaultHeaders": "{}", - "UseHttp2": true, - "Activated": true, - "Description": "Mastodon is a decentralized social network made up of independent servers organized around specific themes, topics, or interests." - }, - "youtube": { - "ApiKey": "my-random-key", - "Activated": true, - "Description": "YouTube is a free video sharing website that makes it easy to watch online videos. You can even create and upload your own videos to share with others." - }, - "twitchchat": { - "ChannelName": "csharpfritz", + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ModerationEnabled": "true", + "ConnectionStrings": { + "SecurityContextConnection": "Data Source=TagzApp.Web.db" + }, + "AzureContentSafety": { + "Enabled": true + }, + "providers": { + "mastodon": { + "BaseAddress": "https://mastodon.social", + "Timeout": "00:03:00", + "DefaultHeaders": "{}", + "UseHttp2": true, + "Activated": true, + "Description": "Mastodon is a decentralized social network made up of independent servers organized around specific themes, topics, or interests." + }, + "youtube": { + "ApiKey": "my-random-key", + "Activated": true, + "Description": "YouTube is a free video sharing website that makes it easy to watch online videos. You can even create and upload your own videos to share with others." + }, + "twitchchat": { + "ChannelName": "csharpfritz", "Activated": true - }, - "twitter": { - "Activated": false, - "BaseAddress": "https://api.twitter.com", - "Timeout": "00:03:00", - "DefaultHeaders": "{\"Authorization\": \"Bearer ******[Add to Local Secret Store INCLUDE BEARER prefix]*****\"}", - "ApiKey": "******[Add to Local Secret Store]******", - "ApiSecretKey": "******[Add to Local Secret Store]******", - "AccessToken": "******[Add to Local Secret Store]******", - "AccessTokenSecret": "******[Add to Local Secret Store]******", - "Description": "Twitter is a service for friends, family, and coworkers to communicate and stay connected through the exchange of quick, frequent messages" - }, - "blazot": { - "BaseAddress": "https://api.blazot.com", - "Timeout": "00:03:00", - "ApiKey": "******[Add to Local Secret Store]******", - "SecretAuthKey": "******[Add to Local Secret Store]******", - "WindowSeconds": 900, - "WindowRequests": 5, - "Activated": false, - "Description": "Blazot is an all new social networking platform and your launchpad to the social universe!" - } - }, - "HttpPolicies": { - "HttpCircuitBreaker": { - "DurationOfBreak": "00:01:00", - "ExceptionsAllowedBeforeBreaking": 20 - }, - "HttpRetry": { - "BackoffPower": 2, - "Count": 3 - } - }, - "AllowedHosts": "*" + }, + "twitter": { + "Activated": false, + "BaseAddress": "https://api.twitter.com", + "Timeout": "00:03:00", + "DefaultHeaders": { + "Authorization": "Bearer ******[Add to Local Secret Store INCLUDE BEARER prefix]*****" + }, + "Description": "Twitter is a service for friends, family, and coworkers to communicate and stay connected through the exchange of quick, frequent messages" + }, + "blazot": { + "BaseAddress": "https://api.blazot.com", + "Timeout": "00:03:00", + "ApiKey": "******[Add to Local Secret Store]******", + "SecretAuthKey": "******[Add to Local Secret Store]******", + "WindowSeconds": 900, + "WindowRequests": 5, + "Activated": false, + "Description": "Blazot is an all new social networking platform and your launchpad to the social universe!" + } + }, + "HttpPolicies": { + "HttpCircuitBreaker": { + "DurationOfBreak": "00:01:00", + "ExceptionsAllowedBeforeBreaking": 20 + }, + "HttpRetry": { + "BackoffPower": 2, + "Count": 3 + } + }, + "AllowedHosts": "*" } \ No newline at end of file