[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
@@ -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
}