[store] refactor, add new purchasables

This commit is contained in:
Andriy
2022-11-14 00:23:36 +02:00
parent c50d5f63ba
commit 36e5ddd5af
9 changed files with 149 additions and 16 deletions
@@ -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);
}
}
@@ -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;
@@ -7,4 +8,5 @@ public interface IRandomMediaService
public StoreItem StoreItem { get; }
public Task<RandomMedia> GetRandomMediaAsync();
public async Task<bool> CanView(Chat chat, User user) => true;
}
@@ -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)
{
}
}