From 36e5ddd5af63d350e8ce486c094815036d9c3759 Mon Sep 17 00:00:00 2001
From: Andriy <30056636+West14@users.noreply.github.com>
Date: Mon, 14 Nov 2022 00:23:36 +0200
Subject: [PATCH] [store] refactor, add new purchasables
---
Core/Core.csproj | 1 +
modules/ContentStore/ContentStore.cs | 2 +-
modules/ContentStore/Handler/NSFW.cs | 37 ++++++++++++
modules/ContentStore/Handler/Store.cs | 20 ++++---
modules/ContentStore/Model/RandomMedia.cs | 10 ++--
.../Service/AbstractKittiesService.cs | 59 +++++++++++++++++++
.../ContentStore/Service/GayKittiesService.cs | 14 +++++
.../Service/IRandomMediaService.cs | 4 +-
.../Service/StraightKittiesService.cs | 18 ++++++
9 files changed, 149 insertions(+), 16 deletions(-)
create mode 100644 modules/ContentStore/Handler/NSFW.cs
create mode 100644 modules/ContentStore/Service/AbstractKittiesService.cs
create mode 100644 modules/ContentStore/Service/GayKittiesService.cs
create mode 100644 modules/ContentStore/Service/StraightKittiesService.cs
diff --git a/Core/Core.csproj b/Core/Core.csproj
index afda299..d230e0d 100644
--- a/Core/Core.csproj
+++ b/Core/Core.csproj
@@ -17,6 +17,7 @@
+
diff --git a/modules/ContentStore/ContentStore.cs b/modules/ContentStore/ContentStore.cs
index b7a1345..7710d77 100644
--- a/modules/ContentStore/ContentStore.cs
+++ b/modules/ContentStore/ContentStore.cs
@@ -24,7 +24,7 @@ public class ContentStore : Module
{
foreach (var serviceType in MediaServiceTypes)
{
- services.AddSingleton(typeof(IRandomMediaService), serviceType);
+ services.AddScoped(typeof(IRandomMediaService), serviceType);
}
}
}
\ No newline at end of file
diff --git a/modules/ContentStore/Handler/NSFW.cs b/modules/ContentStore/Handler/NSFW.cs
new file mode 100644
index 0000000..3a5754e
--- /dev/null
+++ b/modules/ContentStore/Handler/NSFW.cs
@@ -0,0 +1,37 @@
+using BotFramework;
+using BotFramework.Attributes;
+using BotFramework.Enums;
+using Kruzya.TelegramBot.Core.Data;
+using Kruzya.TelegramBot.Core.Extensions;
+using Telegram.Bot;
+using Telegram.Bot.Types;
+
+namespace West.TelegramBot.ContentStore.Handler;
+
+public class NSFW : BotEventHandler
+{
+ private readonly CoreContext _db;
+
+ public NSFW(CoreContext db)
+ {
+ _db = db;
+ }
+
+ [Command("nsfw", CommandParseMode.Both)]
+ public async Task HandleNSFW()
+ {
+ var canUse = await Bot.GetChatMemberAsync(Chat.Id, From.Id) is ChatMemberAdministrator { CanDeleteMessages: true }
+ or ChatMemberOwner;
+ if (!canUse)
+ {
+ return;
+ }
+
+ var option = await _db.UserValues.FindOrCreateOption(Chat.Id, "AllowNSFW");
+ var newValue = !option.GetValue();
+ option.SetValue(newValue);
+ await _db.SaveChangesAsync();
+
+ await Bot.SendTextMessageAsync(Chat.Id,$"NSFW {(newValue ? "enabled" : "disabled")}");
+ }
+}
\ No newline at end of file
diff --git a/modules/ContentStore/Handler/Store.cs b/modules/ContentStore/Handler/Store.cs
index 3dd7d85..0850e05 100644
--- a/modules/ContentStore/Handler/Store.cs
+++ b/modules/ContentStore/Handler/Store.cs
@@ -8,7 +8,6 @@ using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
-using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups;
using West.TelegramBot.ContentStore.Service;
@@ -47,11 +46,14 @@ public class Store : BotEventHandler
await Bot.SendTextMessageAsync(Chat.Id, $"{user.ToHtml()} ({reputation}), чего пожелаете?", ParseMode.Html,
replyMarkup: new InlineKeyboardMarkup(
_provider.GetServices()
- .OrderBy(x => x.StoreItem.Order)
- .Select(x => x.StoreItem.ToButton(From.Id))
- .Chunk(2)
- .ToList()
- )
+ .ToAsyncEnumerable()
+ .WhereAwait(async x => await x.CanView(Chat, user))
+ .OrderBy(x => x.StoreItem.Order)
+ .Select(x => x.StoreItem.ToButton(From.Id))
+ .ToEnumerable()
+ .Chunk(2)
+ .ToList()
+ )
);
}
@@ -110,14 +112,14 @@ public class Store : BotEventHandler
ParseMode.Html);
var randomMedia = await mediaService!.GetRandomMediaAsync();
- var file = new InputOnlineFile(randomMedia.Url);
+ var file = randomMedia.File;
switch (randomMedia.Type)
{
case InputMediaType.Photo:
- await Bot.SendPhotoAsync(Chat.Id, file, randomMedia.Caption, replyToMessageId: messageId);
+ await Bot.SendPhotoAsync(Chat.Id, file, randomMedia.Caption, replyToMessageId: messageId, parseMode: ParseMode.Html);
break;
case InputMediaType.Video:
- await Bot.SendVideoAsync(Chat.Id, file, caption: randomMedia.Caption, replyToMessageId: messageId);
+ await Bot.SendVideoAsync(Chat.Id, file, caption: randomMedia.Caption, replyToMessageId: messageId, parseMode: ParseMode.Html);
break;
}
}
diff --git a/modules/ContentStore/Model/RandomMedia.cs b/modules/ContentStore/Model/RandomMedia.cs
index dedb55e..d1b3021 100644
--- a/modules/ContentStore/Model/RandomMedia.cs
+++ b/modules/ContentStore/Model/RandomMedia.cs
@@ -1,17 +1,17 @@
-using Telegram.Bot.Types;
-using Telegram.Bot.Types.Enums;
+using Telegram.Bot.Types.Enums;
+using Telegram.Bot.Types.InputFiles;
namespace West.TelegramBot.ContentStore.Model;
public class RandomMedia
{
- public RandomMedia(string url, string? caption = null)
+ public RandomMedia(InputOnlineFile file, string? caption = null)
{
- Url = url;
+ File = file;
Caption = caption;
}
- public string Url;
+ public InputOnlineFile File;
public string? Caption;
public InputMediaType Type { get; set; } = InputMediaType.Photo;
diff --git a/modules/ContentStore/Service/AbstractKittiesService.cs b/modules/ContentStore/Service/AbstractKittiesService.cs
new file mode 100644
index 0000000..411f8b3
--- /dev/null
+++ b/modules/ContentStore/Service/AbstractKittiesService.cs
@@ -0,0 +1,59 @@
+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 Telegram.Bot.Types.InputFiles;
+using West.TelegramBot.ContentStore.Model;
+
+namespace West.TelegramBot.ContentStore.Service;
+
+public abstract class AbstractKittiesService : IRandomMediaService
+{
+ private readonly CoreContext _db;
+ protected abstract string VideoType { get; }
+
+ public abstract StoreItem StoreItem { get; }
+ private readonly HttpClient _httpClient;
+
+ protected AbstractKittiesService(IConfiguration configuration, CoreContext db)
+ {
+ _db = db;
+ var section = configuration.GetSection("KittiesService");
+
+ var uri = section.GetValue("Uri");
+ var token = section.GetValue("Token");
+
+ _httpClient = new HttpClient
+ {
+ BaseAddress = uri,
+ DefaultRequestHeaders =
+ {
+ Authorization = new AuthenticationHeaderValue("Bearer", token)
+ }
+ };
+ }
+
+ public async Task GetRandomMediaAsync()
+ {
+ var response = await _httpClient.GetAsync($"v1/{VideoType}");
+ var headers = response.Headers;
+ var videoSource = headers.GetValues("X-Video-Source").FirstOrDefault(string.Empty);
+ var videoTitleBase64 = headers.GetValues("X-Video-Title").FirstOrDefault(string.Empty);
+ var videoTitle = Encoding.UTF8.GetString(Convert.FromBase64String(videoTitleBase64));
+
+ return new RandomMedia(
+ new InputOnlineFile(await response.Content.ReadAsStreamAsync()),
+ new HtmlString().Url(videoSource, videoTitle).ToString()
+ );
+ }
+
+ 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/GayKittiesService.cs b/modules/ContentStore/Service/GayKittiesService.cs
new file mode 100644
index 0000000..1c0bf91
--- /dev/null
+++ b/modules/ContentStore/Service/GayKittiesService.cs
@@ -0,0 +1,14 @@
+using Kruzya.TelegramBot.Core.Data;
+using Microsoft.Extensions.Configuration;
+using West.TelegramBot.ContentStore.Model;
+
+namespace West.TelegramBot.ContentStore.Service;
+
+public class GayKittiesService : AbstractKittiesService
+{
+ public GayKittiesService(IConfiguration configuration, CoreContext db) : base(configuration, db)
+ {
+ }
+
+ protected override string VideoType => "gay";
+ public override StoreItem StoreItem { get; } = new("kitties", "Прон (не геи)", 40, 110);}
\ No newline at end of file
diff --git a/modules/ContentStore/Service/IRandomMediaService.cs b/modules/ContentStore/Service/IRandomMediaService.cs
index 4d63126..2d6060e 100644
--- a/modules/ContentStore/Service/IRandomMediaService.cs
+++ b/modules/ContentStore/Service/IRandomMediaService.cs
@@ -1,4 +1,5 @@
-using West.TelegramBot.ContentStore.Model;
+using Telegram.Bot.Types;
+using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service;
@@ -7,4 +8,5 @@ public interface IRandomMediaService
public StoreItem StoreItem { get; }
public Task GetRandomMediaAsync();
+ public async Task CanView(Chat chat, User user) => true;
}
\ No newline at end of file
diff --git a/modules/ContentStore/Service/StraightKittiesService.cs b/modules/ContentStore/Service/StraightKittiesService.cs
new file mode 100644
index 0000000..181ed85
--- /dev/null
+++ b/modules/ContentStore/Service/StraightKittiesService.cs
@@ -0,0 +1,18 @@
+using System.Net.Http.Headers;
+using BotFramework.Utils;
+using Kruzya.TelegramBot.Core.Data;
+using Microsoft.Extensions.Configuration;
+using Telegram.Bot.Types.InputFiles;
+using West.TelegramBot.ContentStore.Model;
+
+namespace West.TelegramBot.ContentStore.Service;
+
+public class StraightKittiesService : AbstractKittiesService
+{
+ protected override string VideoType => "straight";
+ public override StoreItem StoreItem { get; } = new("kitties", "Прон", 40, 100);
+
+ public StraightKittiesService(IConfiguration configuration, CoreContext db) : base(configuration, db)
+ {
+ }
+}
\ No newline at end of file