Merge pull request #19 from Bubuni-Team/feature/store/kitties

[store] refactor, add new purchasables
This commit is contained in:
Andriy
2022-11-14 01:12:44 +02:00
committed by GitHub
11 changed files with 177 additions and 20 deletions
+1
View File
@@ -17,6 +17,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+1 -1
View File
@@ -24,7 +24,7 @@ public class ContentStore : Module
{ {
foreach (var serviceType in MediaServiceTypes) foreach (var serviceType in MediaServiceTypes)
{ {
services.AddSingleton(typeof(IRandomMediaService), serviceType); services.AddScoped(typeof(IRandomMediaService), serviceType);
} }
} }
} }
+37
View File
@@ -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<bool>();
option.SetValue(newValue);
await _db.SaveChangesAsync();
await Bot.SendTextMessageAsync(Chat.Id,$"NSFW {(newValue ? "enabled" : "disabled")}");
}
}
+6 -4
View File
@@ -8,7 +8,6 @@ using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups; using Telegram.Bot.Types.ReplyMarkups;
using West.TelegramBot.ContentStore.Service; using West.TelegramBot.ContentStore.Service;
@@ -47,8 +46,11 @@ public class Store : BotEventHandler
await Bot.SendTextMessageAsync(Chat.Id, $"<b>{user.ToHtml()} ({reputation})</b>, чего пожелаете?", ParseMode.Html, await Bot.SendTextMessageAsync(Chat.Id, $"<b>{user.ToHtml()} ({reputation})</b>, чего пожелаете?", ParseMode.Html,
replyMarkup: new InlineKeyboardMarkup( replyMarkup: new InlineKeyboardMarkup(
_provider.GetServices<IRandomMediaService>() _provider.GetServices<IRandomMediaService>()
.ToAsyncEnumerable()
.WhereAwait(async x => await x.CanView(Chat, user))
.OrderBy(x => x.StoreItem.Order) .OrderBy(x => x.StoreItem.Order)
.Select(x => x.StoreItem.ToButton(From.Id)) .Select(x => x.StoreItem.ToButton(From.Id))
.ToEnumerable()
.Chunk(2) .Chunk(2)
.ToList() .ToList()
) )
@@ -110,14 +112,14 @@ public class Store : BotEventHandler
ParseMode.Html); ParseMode.Html);
var randomMedia = await mediaService!.GetRandomMediaAsync(); var randomMedia = await mediaService!.GetRandomMediaAsync();
var file = new InputOnlineFile(randomMedia.Url); var file = randomMedia.File;
switch (randomMedia.Type) switch (randomMedia.Type)
{ {
case InputMediaType.Photo: 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; break;
case InputMediaType.Video: 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; break;
} }
} }
+5 -5
View File
@@ -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; namespace West.TelegramBot.ContentStore.Model;
public class RandomMedia public class RandomMedia
{ {
public RandomMedia(string url, string? caption = null) public RandomMedia(InputOnlineFile file, string? caption = null)
{ {
Url = url; File = file;
Caption = caption; Caption = caption;
} }
public string Url; public InputOnlineFile File;
public string? Caption; public string? Caption;
public InputMediaType Type { get; set; } = InputMediaType.Photo; public InputMediaType Type { get; set; } = InputMediaType.Photo;
@@ -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>("Uri");
var token = section.GetValue<string>("Token");
_httpClient = new HttpClient
{
BaseAddress = uri,
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Bearer", token)
}
};
}
public async Task<RandomMedia> 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<bool> CanView(Chat chat, User user)
{
var option = await _db.UserValues.FindOrCreateOption(chat.Id, "AllowNSFW");
return option.GetValue(false);
}
}
@@ -1,15 +1,20 @@
using Telegram.Bot.Types.Enums; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service; namespace West.TelegramBot.ContentStore.Service;
public class AnimBoobsService : IRandomMediaService public class AnimBoobsService : IRandomMediaService
{ {
private readonly CoreContext _db;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
public StoreItem StoreItem { get; } = new("anim_boobs", "Сиськи движущиеся", 120, 40); public StoreItem StoreItem { get; } = new("anim_boobs", "Сиськи движущиеся", 120, 40);
public AnimBoobsService() public AnimBoobsService(CoreContext db)
{ {
_db = db;
_httpClient = new HttpClient() _httpClient = new HttpClient()
{ {
BaseAddress = new Uri("https://westdev.me/_boobs/") BaseAddress = new Uri("https://westdev.me/_boobs/")
@@ -24,4 +29,11 @@ public class AnimBoobsService : IRandomMediaService
Type = InputMediaType.Video Type = InputMediaType.Video
}; };
} }
public async Task<bool> CanView(Chat chat, User user)
{
var option = await _db.UserValues.FindOrCreateOption(chat.Id, "AllowNSFW");
return option.GetValue(false);
}
} }
@@ -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);}
@@ -1,4 +1,5 @@
using West.TelegramBot.ContentStore.Model; using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service; namespace West.TelegramBot.ContentStore.Service;
@@ -7,4 +8,5 @@ public interface IRandomMediaService
public StoreItem StoreItem { get; } public StoreItem StoreItem { get; }
public Task<RandomMedia> GetRandomMediaAsync(); public Task<RandomMedia> GetRandomMediaAsync();
public async Task<bool> CanView(Chat chat, User user) => true;
} }
+14 -2
View File
@@ -1,15 +1,20 @@
using Newtonsoft.Json; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service; namespace West.TelegramBot.ContentStore.Service;
public class OBoobsService : IRandomMediaService public class OBoobsService : IRandomMediaService
{ {
private readonly CoreContext _db;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
public StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60, 30); public StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60, 30);
public OBoobsService() public OBoobsService(CoreContext db)
{ {
_db = db;
_httpClient = new HttpClient _httpClient = new HttpClient
{ {
BaseAddress = new Uri("http://api.oboobs.ru") BaseAddress = new Uri("http://api.oboobs.ru")
@@ -26,4 +31,11 @@ public class OBoobsService : IRandomMediaService
return new RandomMedia($"https://media.oboobs.ru/boobs/{boobsId}.jpg", boobsItem.Model); return new RandomMedia($"https://media.oboobs.ru/boobs/{boobsId}.jpg", boobsItem.Model);
} }
public async Task<bool> CanView(Chat chat, User user)
{
var option = await _db.UserValues.FindOrCreateOption(chat.Id, "AllowNSFW");
return option.GetValue(false);
}
} }
@@ -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)
{
}
}