Use file-scoped namespaces

This commit is contained in:
Andriy
2023-07-29 15:14:52 +03:00
parent a24332370d
commit 8ab067a027
58 changed files with 2222 additions and 2280 deletions
+30 -31
View File
@@ -2,45 +2,44 @@
using Telegram.Bot;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core.Extensions
namespace Kruzya.TelegramBot.Core.Extensions;
public static class BotExtension
{
public static class BotExtension
public static async Task<bool> CanPunishMember(this ITelegramBotClient bot, Chat chat, User user, User victim = null)
{
public static async Task<bool> CanPunishMember(this ITelegramBotClient bot, Chat chat, User user, User victim = null)
var canUse = await IsUserAdminAsync(bot, chat, user);
if (victim != null && canUse)
{
var canUse = await IsUserAdminAsync(bot, chat, user);
if (victim != null && canUse)
{
var victimMember = await bot.GetChatMemberAsync(chat, victim.Id);
canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator && !victim.IsBot;
}
return canUse;
var victimMember = await bot.GetChatMemberAsync(chat, victim.Id);
canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator && !victim.IsBot;
}
public static async Task<bool> IsUserAdminAsync(this ITelegramBotClient bot, Chat chat, User user)
return canUse;
}
public static async Task<bool> IsUserAdminAsync(this ITelegramBotClient bot, Chat chat, User user)
{
var callerMember = await bot.GetChatMemberAsync(chat, user.Id);
var isAdmin = callerMember is ChatMemberOwner;
if (callerMember is ChatMemberAdministrator callerAdmin)
{
var callerMember = await bot.GetChatMemberAsync(chat, user.Id);
var isAdmin = callerMember is ChatMemberOwner;
if (callerMember is ChatMemberAdministrator callerAdmin)
{
isAdmin = callerAdmin.CanRestrictMembers;
}
return isAdmin;
isAdmin = callerAdmin.CanRestrictMembers;
}
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat)
{
return await CanDeleteMessagesAsync(bot, chat, await bot.GetMeAsync());
}
return isAdmin;
}
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat, User user)
{
return await bot.GetChatMemberAsync(chat.Id, user.Id) is ChatMemberOwner
or ChatMemberAdministrator {CanRestrictMembers: true};
}
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat)
{
return await CanDeleteMessagesAsync(bot, chat, await bot.GetMeAsync());
}
public static async Task<bool> CanDeleteMessagesAsync(this ITelegramBotClient bot, Chat chat, User user)
{
return await bot.GetChatMemberAsync(chat.Id, user.Id) is ChatMemberOwner
or ChatMemberAdministrator {CanRestrictMembers: true};
}
}
+13 -14
View File
@@ -3,20 +3,19 @@ using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Kruzya.TelegramBot.Core.Extensions
{
public static class HttpClientExtension
{
public static async Task<T> GetJsonAsync<T>(this HttpClient client, Uri requestUri)
{
var response = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<T>(response);
}
namespace Kruzya.TelegramBot.Core.Extensions;
public static async Task<T> GetJsonAsync<T>(this HttpClient client, string requestUri)
{
var response = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<T>(response);
}
public static class HttpClientExtension
{
public static async Task<T> GetJsonAsync<T>(this HttpClient client, Uri requestUri)
{
var response = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<T>(response);
}
public static async Task<T> GetJsonAsync<T>(this HttpClient client, string requestUri)
{
var response = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<T>(response);
}
}
+12 -13
View File
@@ -2,18 +2,17 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Kruzya.TelegramBot.Core.Extensions
{
internal static class ModuleArrayExtension
{
public static void ConfigureServices(this Module[] modules, IServiceCollection services)
{
foreach (var module in modules) module.ConfigureServices(services);
}
namespace Kruzya.TelegramBot.Core.Extensions;
public static void Configure(this Module[] modules, IApplicationBuilder app, IWebHostEnvironment env)
{
foreach (var module in modules) module.Configure(app, env);
}
internal static class ModuleArrayExtension
{
public static void ConfigureServices(this Module[] modules, IServiceCollection services)
{
foreach (var module in modules) module.ConfigureServices(services);
}
}
public static void Configure(this Module[] modules, IApplicationBuilder app, IWebHostEnvironment env)
{
foreach (var module in modules) module.Configure(app, env);
}
}
+9 -10
View File
@@ -1,15 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace Kruzya.TelegramBot.Core.Extensions
{
public static partial class RepositoryExtension
{
public static TEntity Create<TEntity>(this DbSet<TEntity> repository) where TEntity : class, new()
{
var entity = new TEntity();
repository.Add(entity);
namespace Kruzya.TelegramBot.Core.Extensions;
return entity;
}
public static partial class RepositoryExtension
{
public static TEntity Create<TEntity>(this DbSet<TEntity> repository) where TEntity : class, new()
{
var entity = new TEntity();
repository.Add(entity);
return entity;
}
}
+11 -12
View File
@@ -1,17 +1,16 @@
using System.Web;
namespace Kruzya.TelegramBot.Core.Extensions
{
public static class StringExtension
{
public static string HtmlEncode(this string content)
{
return HttpUtility.HtmlEncode(content);
}
namespace Kruzya.TelegramBot.Core.Extensions;
public static string HtmlDecode(this string content)
{
return HttpUtility.HtmlDecode(content);
}
public static class StringExtension
{
public static string HtmlEncode(this string content)
{
return HttpUtility.HtmlEncode(content);
}
public static string HtmlDecode(this string content)
{
return HttpUtility.HtmlDecode(content);
}
}
+20 -21
View File
@@ -1,31 +1,30 @@
using System.Text;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core.Extensions
namespace Kruzya.TelegramBot.Core.Extensions;
public static class UserExtension
{
public static class UserExtension
public static string ToHtml(this User user, bool byUsername = false)
{
public static string ToHtml(this User user, bool byUsername = false)
{
var text = new StringBuilder();
text.Append($"<a href=\"tg://user?id={user.Id}\">");
var text = new StringBuilder();
text.Append($"<a href=\"tg://user?id={user.Id}\">");
if (byUsername && !string.IsNullOrWhiteSpace(user.Username))
{
text.Append(user.Username);
}
else
{
text.Append($"{user.FirstName} {user.LastName}".Trim().HtmlEncode());
}
return text.Append("</a>").ToString();
}
public static string ToLog(this User user)
if (byUsername && !string.IsNullOrWhiteSpace(user.Username))
{
var name = $"{user.FirstName} {user.LastName}".Trim();
return $"{name} (ID: {user.Id}, Username: {user.Username ?? "N/A"})";
text.Append(user.Username);
}
else
{
text.Append($"{user.FirstName} {user.LastName}".Trim().HtmlEncode());
}
return text.Append("</a>").ToString();
}
public static string ToLog(this User user)
{
var name = $"{user.FirstName} {user.LastName}".Trim();
return $"{name} (ID: {user.Id}, Username: {user.Username ?? "N/A"})";
}
}