[quote] Code cleanup.

This commit is contained in:
West14
2022-01-21 14:18:56 +02:00
parent 8ad2d9968e
commit a9d284dc9d
11 changed files with 138 additions and 125 deletions
@@ -10,7 +10,7 @@ namespace West.TelegramBot.ChatQuotes.Json
return type.GetProperties()
.Select(p => {
var jp = base.CreateProperty(p, memberSerialization);
jp.ValueProvider = new NullToEmptyStringValueProvider(p);
jp.ValueProvider = new NullToEmptyObjectValueProvider(p);
return jp;
}).ToList();
}
@@ -4,10 +4,10 @@ using Telegram.Bot.Types;
namespace West.TelegramBot.ChatQuotes.Json;
public class NullToEmptyStringValueProvider : IValueProvider
public class NullToEmptyObjectValueProvider : IValueProvider
{
private readonly PropertyInfo _memberInfo;
public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
public NullToEmptyObjectValueProvider(PropertyInfo memberInfo)
{
_memberInfo = memberInfo;
}
@@ -0,0 +1,6 @@
namespace West.TelegramBot.ChatQuotes.Model.Quote;
public enum MediaType
{
Sticker = 0
}
+47
View File
@@ -0,0 +1,47 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Telegram.Bot.Types;
using File = Telegram.Bot.Types.File;
namespace West.TelegramBot.ChatQuotes.Model.Quote;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Message
{
public List<File>? Media { get; set; }
public MediaType? MediaType { get; set; }
public long ChatId { get; set; }
public bool Avatar { get; set; } = true;
public User? From { get; set; }
public string? Text { get; set; }
public Telegram.Bot.Types.Message? ReplyMessage { get; set; }
public static Message FromMessage(Telegram.Bot.Types.Message msg)
{
var quoteMessage = new Message();
if (msg.Text != null)
{
quoteMessage.Text = msg.Text;
}
var sticker = msg.Sticker;
if (sticker != null)
{
var file = new File
{
FileId = sticker.FileId,
FileSize = sticker.FileSize,
FileUniqueId = sticker.FileUniqueId
};
quoteMessage.Media = new List<File> { file };
quoteMessage.MediaType = Quote.MediaType.Sticker;
}
quoteMessage.ChatId = msg.Chat.Id;
quoteMessage.ReplyMessage = msg.ReplyToMessage;
quoteMessage.From = User.FromTgUser(msg.From!);
return quoteMessage;
}
}
+35
View File
@@ -0,0 +1,35 @@
#nullable enable
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace West.TelegramBot.ChatQuotes.Model.Quote;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Request
{
public Request(Telegram.Bot.Types.Message msg)
{
// TODO: move default values to config
Format = "webp";
BackgroundColor = "#1b1429";
Width = 512;
Height = 768;
Scale = 2;
Messages = new List<Message> {Message.FromMessage(msg)};
}
public Type Type { get; set; }
public string Format { get; set; }
public string BackgroundColor { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Scale { get; set; }
public List<Message>? Messages { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this, ChatQuotes.SerializerSettings);
}
}
@@ -0,0 +1,16 @@
#nullable disable
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace West.TelegramBot.ChatQuotes.Model.Quote;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Response
{
public bool Ok { get; set; }
public Result Result { get; set; }
public static Response FromJson(string json)
=> JsonConvert.DeserializeObject<Response>(json, ChatQuotes.SerializerSettings);
}
+13
View File
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace West.TelegramBot.ChatQuotes.Model.Quote;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Result
{
public string Image { get; set; }
public Type Type { get; set; }
public long Width { get; set; }
public long Height { get; set; }
}
+8
View File
@@ -0,0 +1,8 @@
namespace West.TelegramBot.ChatQuotes.Model.Quote;
public enum Type
{
Quote = 0,
Image,
Null
}
-82
View File
@@ -1,82 +0,0 @@
#nullable enable
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Telegram.Bot.Types;
using File = Telegram.Bot.Types.File;
namespace West.TelegramBot.ChatQuotes.Model;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class QuoteRequest
{
public QuoteRequest(Message msg)
{
// TODO: move default values to config
Format = "webp";
BackgroundColor = "#1b1429";
Width = 512;
Height = 768;
Scale = 2;
Messages = new List<QuoteMessage> {QuoteMessage.FromMessage(msg)};
}
public QuoteType Type { get; set; }
public string Format { get; set; }
public string BackgroundColor { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Scale { get; set; }
public List<QuoteMessage>? Messages { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this, ChatQuotes.SerializerSettings);
}
}
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class QuoteMessage
{
public List<File>? Media { get; set; }
public QuoteMediaType? MediaType { get; set; }
public long ChatId { get; set; }
public bool Avatar { get; set; } = true;
public User? From { get; set; }
public string? Text { get; set; }
public Message? ReplyMessage { get; set; }
public static QuoteMessage FromMessage(Message msg)
{
var quoteMessage = new QuoteMessage();
if (msg.Text != null)
{
quoteMessage.Text = msg.Text;
}
var sticker = msg.Sticker;
if (sticker != null)
{
var file = new File
{
FileId = sticker.FileId,
FileSize = sticker.FileSize,
FileUniqueId = sticker.FileUniqueId
};
quoteMessage.Media = new List<File> {file};
quoteMessage.MediaType = QuoteMediaType.Sticker;
}
quoteMessage.ChatId = msg.Chat.Id;
quoteMessage.ReplyMessage = msg.ReplyToMessage;
quoteMessage.From = User.FromTgUser(msg.From!);
return quoteMessage;
}
}
public enum QuoteMediaType
{
Sticker = 0
}
-35
View File
@@ -1,35 +0,0 @@
#nullable disable
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace West.TelegramBot.ChatQuotes.Model;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class QuoteResponse
{
public bool Ok { get; set; }
public Result Result { get; set; }
public static QuoteResponse FromJson(string json)
=> JsonConvert.DeserializeObject<QuoteResponse>(json, ChatQuotes.SerializerSettings);
}
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Result
{
public string Image { get; set; }
public QuoteType Type { get; set; }
public long Width { get; set; }
public long Height { get; set; }
}
public enum QuoteType
{
Quote = 0,
Image,
Null
}
+10 -5
View File
@@ -4,12 +4,15 @@ using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ChatQuotes.Model;
using West.TelegramBot.ChatQuotes.Model.Quote;
using Message = Telegram.Bot.Types.Message;
namespace West.TelegramBot.ChatQuotes;
public class QuoteGeneratorService
{
private readonly HttpClient _httpClient;
private readonly MediaTypeHeaderValue _mediaType;
public QuoteGeneratorService(IConfiguration configuration)
{
@@ -17,22 +20,24 @@ public class QuoteGeneratorService
{
BaseAddress = new Uri(configuration.GetValue<string>("QuoteApiUrl"))
};
_mediaType = MediaTypeHeaderValue.Parse("application/json");
}
public async Task<Stream> GenerateQuoteImage(Message msg)
public async Task<Stream?> GenerateQuoteImage(Message msg)
{
var request = new QuoteRequest(msg);
var request = new Request(msg);
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(request.ToJson().ToCharArray()))
{
Headers =
{
ContentType = MediaTypeHeaderValue.Parse("application/json")
ContentType = _mediaType
}
};
var resp = await _httpClient.PostAsync("/generate", content);
var quoteResponse = JsonConvert.DeserializeObject<QuoteResponse>(await resp.Content.ReadAsStringAsync());
var quoteResponse = JsonConvert.DeserializeObject<Response>(await resp.Content.ReadAsStringAsync());
return new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image));
return quoteResponse.Ok ? new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image)) : null;
}
}