Skip to content

Commit

Permalink
update tools and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1seL committed Jan 5, 2025
1 parent 8acce00 commit 1f265e9
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "8.0.10",
"version": "9.0.0",
"commands": [
"dotnet-ef"
]
],
"rollForward": false
}
}
}
11 changes: 5 additions & 6 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="MassTransit.Newtonsoft" Version="8.3.3" />
<PackageVersion Include="MassTransit.Quartz" Version="8.3.4" />
<PackageVersion Include="MassTransit.RabbitMQ" Version="8.3.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
Expand All @@ -33,9 +32,10 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.1" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.2" />
<PackageVersion Include="Polly" Version="8.2.0" />
<PackageVersion Include="Sentry.Extensions.Logging" Version="4.13.0" />
<PackageVersion Include="Quartz.Serialization.SystemTextJson" Version="3.13.1" />
<PackageVersion Include="Sentry.Extensions.Logging" Version="5.0.0" />
<PackageVersion Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageVersion Include="Serilog.Enrichers.Sensitive" Version="1.7.3" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="9.0.0" />
Expand All @@ -46,8 +46,7 @@
<PackageVersion Include="Serilog.Sinks.Elasticsearch" Version="10.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.XUnit" Version="3.0.5" />
<PackageVersion Include="Telegram.Bot" Version="22.2.0" />
<PackageVersion Include="Telegram.Bot.Extensions.Markup" Version="1.0.2" />
<PackageVersion Include="Telegram.Bot" Version="22.3.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="Xunit.DependencyInjection" Version="8.9.0" />
<PackageVersion Include="Xunit.DependencyInjection.Logging" Version="8.1.0" />
Expand All @@ -56,4 +55,4 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
</ItemGroup>
</Project>
</Project>
12 changes: 6 additions & 6 deletions src/SpinRallyBot.Bot/Services/BotInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ namespace SpinRallyBot.Services;
internal class BotInit : IHostedService {
private readonly ITelegramBotClient _botClient;
private readonly ILogger<BotInit> _logger;
private readonly IScopedMediator _mediator;
private readonly IServiceProvider _serviceProvider;

public BotInit(ITelegramBotClient botClient,
ILogger<BotInit> logger,
IScopedMediator mediator) {
public BotInit(ITelegramBotClient botClient, ILogger<BotInit> logger, IServiceProvider serviceProvider) {
_botClient = botClient;
_logger = logger;
_mediator = mediator;
_serviceProvider = serviceProvider;
}

public async Task StartAsync(CancellationToken cancellationToken) {
_logger.LogInformation("Initialize bot (commands, etc)");
await InitCommands(cancellationToken);
await _mediator.Send(new InitUpdaterJob(false), cancellationToken);
await using AsyncServiceScope scope = _serviceProvider.CreateAsyncScope();
var mediator = scope.ServiceProvider.GetRequiredService<IScopedMediator>();
await mediator.Send(new InitUpdaterJob(false), cancellationToken);
}

public Task StopAsync(CancellationToken cancellationToken) {
Expand Down
2 changes: 0 additions & 2 deletions src/SpinRallyBot.Bot/SpinRallyBot.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

<ItemGroup>
<PackageReference Include="MassTransit.Abstractions" />
<PackageReference Include="MassTransit.Newtonsoft" />
<PackageReference Include="MassTransit.Quartz" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Telegram.Bot" />
<PackageReference Include="Telegram.Bot.Extensions.Markup" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 9 additions & 4 deletions src/SpinRallyBot.Bot/Utils/UserExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using Telegram.Bot.Extensions.Markup;

namespace SpinRallyBot.Utils;

public static class UserExtensions {
private static string MentionMarkdown(ChatId userId, string name, ParseMode parseMode = ParseMode.Markdown) {
string tgLink = $"tg://user?id={userId}";
return parseMode == ParseMode.Markdown
? $"[{name}]({tgLink})"
: $"[{StringExtensions.EscapeMarkdown(name, parseMode)}]({tgLink})";
}

public static string ToMentionMarkdownV2(this User user) {
return Tools.MentionMarkdown(user.Id,
user.Username is not null ? $"@{user.Username}" : $"{user.FirstName} {user.LastName}".Trim(),
return MentionMarkdown(
user.Id, user.Username is not null ? $"@{user.Username}" : $"{user.FirstName} {user.LastName}".Trim(),
ParseMode.MarkdownV2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:CollationDefinition:case_insensitive", "en-u-ks-primary,en-u-ks-primary,icu,False")
.HasAnnotation("ProductVersion", "8.0.1")
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ partial class SqliteDbContextModelSnapshot : ModelSnapshot
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.HasAnnotation("ProductVersion", "9.0.0");

modelBuilder.Entity("SpinRallyBot.Models.BackNavigationEntity", b =>
{
Expand Down Expand Up @@ -78,7 +78,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("PlayerUrl");

b.ToTable("Players");
b.ToTable("Players", (string)null);
});

modelBuilder.Entity("SpinRallyBot.Models.SubscriptionEntity", b =>
Expand Down
2 changes: 1 addition & 1 deletion src/SpinRallyBot.Utils/SpinRallyBot.Utils.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Telegram.Bot.Extensions.Markup" />
<PackageReference Include="Telegram.Bot" />
</ItemGroup>
</Project>
41 changes: 39 additions & 2 deletions src/SpinRallyBot.Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
using Telegram.Bot.Extensions.Markup;
using System.Collections.Immutable;
using System.Text.RegularExpressions;
using Telegram.Bot.Types.Enums;

namespace SpinRallyBot;

public static class StringExtensions {
private static ImmutableDictionary<(ParseMode parseMode, MessageEntityType? entityType), string> Escaped =>
new Dictionary<(ParseMode parseMode, MessageEntityType? entityType), string> {
{ (ParseMode.Markdown, null), Regex.Escape("_*`[") },
{ (ParseMode.MarkdownV2, MessageEntityType.Pre), Regex.Escape("""\`""") },
{ (ParseMode.MarkdownV2, MessageEntityType.Code), Regex.Escape("""\`""") },
{ (ParseMode.MarkdownV2, MessageEntityType.TextLink), Regex.Escape("""\)""") }, {
(ParseMode.MarkdownV2, null),
Regex.Escape(str: """\_*()~`>#+-=|{}.![]""")
.Replace("]", "\\]", StringComparison.Ordinal)
.Replace("-", "\\-", StringComparison.Ordinal)
},
}.ToImmutableDictionary();

public static string EscapeMarkdown(
string text,
ParseMode parseMode = ParseMode.Markdown,
MessageEntityType? entityType = default) {
string escaped = (parseMode, entityType) switch {
(ParseMode.Markdown, _) => Escaped[(ParseMode.Markdown, null)],
(ParseMode.MarkdownV2, MessageEntityType.Pre) => Escaped[(ParseMode.MarkdownV2, MessageEntityType.Pre)],
(ParseMode.MarkdownV2, MessageEntityType.Code) => Escaped[(ParseMode.MarkdownV2, MessageEntityType.Code)],
(ParseMode.MarkdownV2, MessageEntityType.TextLink) => Escaped[
(ParseMode.MarkdownV2, MessageEntityType.TextLink)],
(ParseMode.MarkdownV2, _) => Escaped[(ParseMode.MarkdownV2, null)],
_ => throw new ArgumentException("Only ParseMode.Markdown and ParseMode.MarkdownV2 allowed.",
nameof(parseMode)),
};

return Regex.Replace(
input: text,
pattern: $"([{escaped}])",
replacement: """\$1""",
RegexOptions.CultureInvariant,
matchTimeout: TimeSpan.FromSeconds(1));
}

public static string ToEscapedMarkdownV2(this string str) {
return Tools.EscapeMarkdown(str, ParseMode.MarkdownV2);
return EscapeMarkdown(str, ParseMode.MarkdownV2);
}
}
28 changes: 11 additions & 17 deletions src/SpinRallyBot/HostApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json;
using Quartz;
using Serilog.Enrichers.Sensitive;
using SpinRallyBot.Events.PlayerRatingChangedConsumers;
using SpinRallyBot.Serilog;
using SpinRallyBot.Subscriptions;
using Telegram.Bot;

namespace SpinRallyBot;

Expand Down Expand Up @@ -45,12 +45,12 @@ public static HostApplicationBuilder AddMassTransit(this HostApplicationBuilder
if (builder.Configuration["AMQP_URI"] is { } amqpUri) {
x.UsingRabbitMq((context, cfg) => {
cfg.Host(amqpUri);
ConfigureNewtonsoft(cfg);
cfg.ConfigureJsonSerializerOptions(AddJsonBotApiJsonSerializerOptions);
cfg.ConfigureEndpoints(context);
});
} else {
x.UsingInMemory((context, cfg) => {
ConfigureNewtonsoft(cfg);
cfg.ConfigureJsonSerializerOptions(AddJsonBotApiJsonSerializerOptions);
cfg.ConfigureEndpoints(context);
});
}
Expand All @@ -62,19 +62,6 @@ public static HostApplicationBuilder AddMassTransit(this HostApplicationBuilder
return builder;
}

private static void ConfigureNewtonsoft(IBusFactoryConfigurator cfg) {
cfg.UseNewtonsoftJsonSerializer();
cfg.ConfigureNewtonsoftJsonSerializer(_ => new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Include,
ContractResolver = new CamelCasePropertyNamesContractResolver {
IgnoreSerializableAttribute = true,
IgnoreShouldSerializeMembers = true
},
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
});
}

public static HostApplicationBuilder AddQuartz(this HostApplicationBuilder builder) {
builder.Services.AddQuartz(q => {
q.MisfireThreshold = TimeSpan.FromHours(1);
Expand All @@ -97,8 +84,15 @@ public static HostApplicationBuilder AddQuartz(this HostApplicationBuilder build
default:
throw new Exception($"Unsupported provider: {provider}");
}

s.UseSystemTextJsonSerializer();
});
});
return builder;
}

private static JsonSerializerOptions AddJsonBotApiJsonSerializerOptions(JsonSerializerOptions options) {
JsonBotAPI.Configure(options);
return options;
}
}
1 change: 1 addition & 0 deletions src/SpinRallyBot/SpinRallyBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" />
<PackageReference Include="Quartz.Serialization.SystemTextJson" />
<PackageReference Include="Sentry.Extensions.Logging" />
<PackageReference Include="Serilog.Enrichers.Environment" />
<PackageReference Include="Serilog.Enrichers.Sensitive" />
Expand Down

0 comments on commit 1f265e9

Please sign in to comment.