From 4893c1240d6a9a1097b7c3aa4deca3798425bb59 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 21 Jul 2023 18:40:24 +0300 Subject: [PATCH] [store] Make store use AllowNSFW option --- modules/ContentStore/ContentStore.cs | 3 ++- modules/ContentStore/Handler/Store.cs | 2 +- .../Service/AbstractKittiesService.cs | 13 ++++------ .../Service/AbstractNsfwService.cs | 19 +++++++++++++++ .../ContentStore/Service/AnimBoobsService.cs | 24 ++++++------------- .../ContentStore/Service/CapyApiService.cs | 4 ++-- .../ContentStore/Service/GayKittiesService.cs | 3 ++- .../Service/IRandomMediaService.cs | 2 +- modules/ContentStore/Service/OBoobsService.cs | 18 ++++---------- .../Service/SomeRandomApiAbstractService.cs | 2 +- .../Service/StraightKittiesService.cs | 3 ++- 11 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 modules/ContentStore/Service/AbstractNsfwService.cs diff --git a/modules/ContentStore/ContentStore.cs b/modules/ContentStore/ContentStore.cs index 294f28a..62416b9 100644 --- a/modules/ContentStore/ContentStore.cs +++ b/modules/ContentStore/ContentStore.cs @@ -1,4 +1,5 @@ using Kruzya.TelegramBot.Core; +using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Options; using Microsoft.Extensions.DependencyInjection; using West.TelegramBot.ContentStore.Option; @@ -24,7 +25,7 @@ public class ContentStore : Module public override void ConfigureServices(IServiceCollection services) { - services.AddScoped(); + services.AddOption(); foreach (var serviceType in MediaServiceTypes) { diff --git a/modules/ContentStore/Handler/Store.cs b/modules/ContentStore/Handler/Store.cs index 6ad2819..21737fa 100644 --- a/modules/ContentStore/Handler/Store.cs +++ b/modules/ContentStore/Handler/Store.cs @@ -110,7 +110,7 @@ public class Store : CommonHandler { await PerformPurchase(mediaService, messageId); } - catch (Exception e) + catch (Exception) { answer = "Товар недоступен"; await Bot.EditMessageTextAsync(Chat.Id, diff --git a/modules/ContentStore/Service/AbstractKittiesService.cs b/modules/ContentStore/Service/AbstractKittiesService.cs index 6f53ba8..133f252 100644 --- a/modules/ContentStore/Service/AbstractKittiesService.cs +++ b/modules/ContentStore/Service/AbstractKittiesService.cs @@ -1,25 +1,23 @@ using System.Net.Http.Headers; using System.Text; using BotFramework.Utils; -using Kruzya.TelegramBot.Core.Data; -using Kruzya.TelegramBot.Core.Extensions; using Microsoft.Extensions.Configuration; using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; namespace West.TelegramBot.ContentStore.Service; public abstract class AbstractKittiesService : IRandomMediaService { - private readonly CoreContext _db; + private readonly AllowNsfw _allowNsfw; protected abstract string VideoType { get; } public abstract StoreItem StoreItem { get; } private readonly HttpClient _httpClient; - protected AbstractKittiesService(IConfiguration configuration, CoreContext db) + protected AbstractKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) { - _db = db; var section = configuration.GetSection("KittiesService"); var uri = section.GetValue("Uri"); @@ -33,6 +31,7 @@ public abstract class AbstractKittiesService : IRandomMediaService Authorization = new AuthenticationHeaderValue("Bearer", token) } }; + _allowNsfw = allowNsfw; } public async Task GetRandomMediaAsync() @@ -52,8 +51,6 @@ public abstract class AbstractKittiesService : IRandomMediaService public async Task CanView(Chat chat, User user) { - var option = await _db.UserValues.FindOrCreateOption(chat.Id, "AllowNSFW"); - - return option.GetValue(false); + return await _allowNsfw.GetValueAsync(chat.Id); } } \ No newline at end of file diff --git a/modules/ContentStore/Service/AbstractNsfwService.cs b/modules/ContentStore/Service/AbstractNsfwService.cs new file mode 100644 index 0000000..bb96667 --- /dev/null +++ b/modules/ContentStore/Service/AbstractNsfwService.cs @@ -0,0 +1,19 @@ +using Telegram.Bot.Types; +using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; + +namespace West.TelegramBot.ContentStore.Service; + +public abstract class AbstractNsfwService : IRandomMediaService +{ + private readonly AllowNsfw _allowNsfw; + public abstract StoreItem StoreItem { get; } + public abstract Task GetRandomMediaAsync(); + + protected AbstractNsfwService(AllowNsfw allowNsfw) + { + _allowNsfw = allowNsfw; + } + + public async Task CanView(Chat chat, User user) => await _allowNsfw.GetValueAsync(chat.Id); +} \ No newline at end of file diff --git a/modules/ContentStore/Service/AnimBoobsService.cs b/modules/ContentStore/Service/AnimBoobsService.cs index 6c50d58..78083d1 100644 --- a/modules/ContentStore/Service/AnimBoobsService.cs +++ b/modules/ContentStore/Service/AnimBoobsService.cs @@ -1,27 +1,24 @@ -using Kruzya.TelegramBot.Core.Data; -using Kruzya.TelegramBot.Core.Extensions; -using Telegram.Bot.Types; +using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; namespace West.TelegramBot.ContentStore.Service; -public class AnimBoobsService : IRandomMediaService +public class AnimBoobsService : AbstractNsfwService { - private readonly CoreContext _db; private readonly HttpClient _httpClient; - public StoreItem StoreItem { get; } = new("anim_boobs", "Сиськи движущиеся", 120, 40); + public override StoreItem StoreItem { get; } = new("anim_boobs", "Сиськи движущиеся", 120, 40); - public AnimBoobsService(CoreContext db) + public AnimBoobsService(AllowNsfw allowNsfw) : base(allowNsfw) { - _db = db; - _httpClient = new HttpClient() + _httpClient = new HttpClient { BaseAddress = new Uri("https://westdev.me/_boobs/") }; } - public async Task GetRandomMediaAsync() + public override async Task GetRandomMediaAsync() { var fileName = await _httpClient.GetStringAsync("index.php"); // https://bugs.telegram.org/c/23674 workaround @@ -33,11 +30,4 @@ public class AnimBoobsService : IRandomMediaService Nsfw = true }; } - - public async Task CanView(Chat chat, User user) - { - var option = await _db.UserValues.FindOrCreateOption(chat.Id, "AllowNSFW"); - - return option.GetValue(false); - } } \ No newline at end of file diff --git a/modules/ContentStore/Service/CapyApiService.cs b/modules/ContentStore/Service/CapyApiService.cs index 46a454c..a1f932c 100644 --- a/modules/ContentStore/Service/CapyApiService.cs +++ b/modules/ContentStore/Service/CapyApiService.cs @@ -6,8 +6,8 @@ namespace West.TelegramBot.ContentStore.Service; public class CapyApiService : IRandomMediaService { public StoreItem StoreItem { get; } = new("capybara", "Капибара", 60, 25); - public async Task GetRandomMediaAsync() + public Task GetRandomMediaAsync() { - return new RandomMedia(new InputFileUrl($"https://api.capy.lol/v1/capybara?{new Random().Next(0, 10000)}")); + return Task.FromResult(new RandomMedia(new InputFileUrl($"https://api.capy.lol/v1/capybara?{new Random().Next(0, 10000)}"))); } } \ No newline at end of file diff --git a/modules/ContentStore/Service/GayKittiesService.cs b/modules/ContentStore/Service/GayKittiesService.cs index 16f393b..e01a701 100644 --- a/modules/ContentStore/Service/GayKittiesService.cs +++ b/modules/ContentStore/Service/GayKittiesService.cs @@ -1,12 +1,13 @@ using Kruzya.TelegramBot.Core.Data; using Microsoft.Extensions.Configuration; using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; namespace West.TelegramBot.ContentStore.Service; public class GayKittiesService : AbstractKittiesService { - public GayKittiesService(IConfiguration configuration, CoreContext db) : base(configuration, db) + public GayKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) : base(configuration, allowNsfw) { } diff --git a/modules/ContentStore/Service/IRandomMediaService.cs b/modules/ContentStore/Service/IRandomMediaService.cs index 2d6060e..9b29447 100644 --- a/modules/ContentStore/Service/IRandomMediaService.cs +++ b/modules/ContentStore/Service/IRandomMediaService.cs @@ -8,5 +8,5 @@ public interface IRandomMediaService public StoreItem StoreItem { get; } public Task GetRandomMediaAsync(); - public async Task CanView(Chat chat, User user) => true; + public Task CanView(Chat chat, User user) => Task.FromResult(true); } \ No newline at end of file diff --git a/modules/ContentStore/Service/OBoobsService.cs b/modules/ContentStore/Service/OBoobsService.cs index 40f697f..a60b896 100644 --- a/modules/ContentStore/Service/OBoobsService.cs +++ b/modules/ContentStore/Service/OBoobsService.cs @@ -3,25 +3,24 @@ using Kruzya.TelegramBot.Core.Extensions; using Newtonsoft.Json; using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; namespace West.TelegramBot.ContentStore.Service; -public class OBoobsService : IRandomMediaService +public class OBoobsService : AbstractNsfwService { - private readonly CoreContext _db; private readonly HttpClient _httpClient; - public StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60, 30); + public override StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60, 30); - public OBoobsService(CoreContext db) + public OBoobsService(AllowNsfw allowNsfw) : base(allowNsfw) { - _db = db; _httpClient = new HttpClient { BaseAddress = new Uri("http://api.oboobs.ru") }; } - public async Task GetRandomMediaAsync() + public override async Task GetRandomMediaAsync() { // "/boobs/{start=0; sql offset}/{count=1; sql limit}/{order=-id;[id,rank,-rank,interest,-interest,random]}/ var httpResp = await _httpClient.GetAsync("boobs/1/1/random"); @@ -31,11 +30,4 @@ public class OBoobsService : IRandomMediaService return new RandomMedia(new InputFileUrl($"https://media.oboobs.ru/boobs/{boobsId}.jpg"), boobsItem.Model, true); } - - public async Task CanView(Chat chat, User user) - { - var option = await _db.UserValues.FindOrCreateOption(chat.Id, "AllowNSFW"); - - return option.GetValue(false); - } } \ No newline at end of file diff --git a/modules/ContentStore/Service/SomeRandomApiAbstractService.cs b/modules/ContentStore/Service/SomeRandomApiAbstractService.cs index 788b733..9c82aa1 100644 --- a/modules/ContentStore/Service/SomeRandomApiAbstractService.cs +++ b/modules/ContentStore/Service/SomeRandomApiAbstractService.cs @@ -22,7 +22,7 @@ public abstract class SomeRandomApiAbstractService : IRandomMediaService public async Task GetRandomMediaAsync() { var response = await _httpClient.GetStringAsync($"https://some-random-api.com/animal/{ApiId}"); - var image = JsonConvert.DeserializeObject(response); + var image = JsonConvert.DeserializeObject(response)!; return new RandomMedia(new InputFileUrl(image.Image), image.Fact); } diff --git a/modules/ContentStore/Service/StraightKittiesService.cs b/modules/ContentStore/Service/StraightKittiesService.cs index f614455..e346906 100644 --- a/modules/ContentStore/Service/StraightKittiesService.cs +++ b/modules/ContentStore/Service/StraightKittiesService.cs @@ -1,6 +1,7 @@ using Kruzya.TelegramBot.Core.Data; using Microsoft.Extensions.Configuration; using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; namespace West.TelegramBot.ContentStore.Service; @@ -9,7 +10,7 @@ public class StraightKittiesService : AbstractKittiesService protected override string VideoType => "straight"; public override StoreItem StoreItem { get; } = new("kitties_straight", "Прон", 40, 100); - public StraightKittiesService(IConfiguration configuration, CoreContext db) : base(configuration, db) + public StraightKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) : base(configuration, allowNsfw) { } } \ No newline at end of file