Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start V3, refactor code paste service #1033

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dotnet_naming_style.interface_style.capitalization = pascal_case
dotnet_naming_style.interface_style.required_prefix = I

# Async methods are suffixed with Async
dotnet_naming_rule.async_methods_should_be_suffixed.severity = warning
dotnet_naming_rule.async_methods_should_be_suffixed.severity = none
dotnet_naming_rule.async_methods_should_be_suffixed.symbols = async_methods
dotnet_naming_rule.async_methods_should_be_suffixed.style = async_method_style

Expand Down
12 changes: 7 additions & 5 deletions src/Modix.Bot/Modules/IlModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using Discord.Commands;
using Microsoft.Extensions.Options;
using Modix.Data.Models.Core;
using Modix.Services;
using Modix.Services.AutoRemoveMessage;
using Modix.Services.CodePaste;
using Modix.Services.CommandHelp;
using Modix.Services.Utilities;
using Serilog;
Expand All @@ -22,12 +22,12 @@ public class IlModule : ModuleBase
{
private const string DefaultIlRemoteUrl = "http://csdiscord-repl-service:31337/Il";
private readonly string _ilUrl;
private readonly CodePasteService _pasteService;
private readonly PasteService _pasteService;
private readonly IAutoRemoveMessageService _autoRemoveMessageService;
private readonly IHttpClientFactory _httpClientFactory;

public IlModule(
CodePasteService pasteService,
PasteService pasteService,
IAutoRemoveMessageService autoRemoveMessageService,
IHttpClientFactory httpClientFactory,
IOptions<ModixConfig> modixConfig)
Expand Down Expand Up @@ -118,10 +118,12 @@ private async Task<EmbedBuilder> BuildEmbedAsync(IGuildUser guildUser, string co

embed.AddField(a => a.WithName("Code").WithValue(Format.Code(code, "cs")));

const int MAX_LENGTH = 990;

embed.AddField(a => a.WithName($"Result:")
.WithValue(Format.Code(result.TruncateTo(990), "asm")));
.WithValue(Format.Code(result.TruncateTo(MAX_LENGTH), "asm")));

await embed.UploadToServiceIfBiggerThan(result, 990, _pasteService);
await embed.UploadToServiceIfBiggerThan(result, MAX_LENGTH, _pasteService);

return embed;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Modix.Bot/Modules/ReplModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using Discord.Commands;
using Microsoft.Extensions.Options;
using Modix.Data.Models.Core;
using Modix.Services;
using Modix.Services.AutoRemoveMessage;
using Modix.Services.CodePaste;
using Modix.Services.CommandHelp;
using Modix.Services.Utilities;
using Newtonsoft.Json;
Expand All @@ -36,12 +36,12 @@ public class ReplModule : ModuleBase
private const int MaxFormattedFieldSize = 1000;
private const string DefaultReplRemoteUrl = "http://csdiscord-repl-service:31337/Eval";
private readonly string _replUrl;
private readonly CodePasteService _pasteService;
private readonly PasteService _pasteService;
private readonly IAutoRemoveMessageService _autoRemoveMessageService;
private readonly IHttpClientFactory _httpClientFactory;

public ReplModule(
CodePasteService pasteService,
PasteService pasteService,
IAutoRemoveMessageService autoRemoveMessageService,
IHttpClientFactory httpClientFactory,
IOptions<ModixConfig> modixConfig)
Expand Down
79 changes: 0 additions & 79 deletions src/Modix.Services/CodePaste/CodePasteService.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/Modix.Services/CodePaste/CodePasteSetup.cs

This file was deleted.

39 changes: 39 additions & 0 deletions src/Modix.Services/PasteService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#nullable enable
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Modix.Services.Utilities;
using Newtonsoft.Json.Linq;

namespace Modix.Services;

public class PasteService(IHttpClientFactory httpClientFactory, ILogger<PasteService> logger)
{
private const string PASTE_URL = "https://paste.mod.gg/";

public async Task<string?> UploadPaste(string textToUpload)
{
var content = FormatUtilities.BuildContent(textToUpload);

var client = httpClientFactory.CreateClient(HttpClientNames.TimeoutFiveSeconds);

var response = await client.PostAsync($"{PASTE_URL}documents", content);

if (!response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();

logger.LogError("Failed uploading paste to {Url}, failed with response {ResponseCode}, with body: {Body}",
PASTE_URL,
response.StatusCode,
body);

return null;
}

var urlResponse = await response.Content.ReadAsStringAsync();
var pasteKey = JObject.Parse(urlResponse)["key"]?.Value<string>();

return $"{PASTE_URL}{pasteKey}";
}
}
20 changes: 11 additions & 9 deletions src/Modix.Services/Utilities/DiscordWebhookSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;
using Discord;
using Discord.Webhook;
using Modix.Services.CodePaste;
using Newtonsoft.Json;
using Serilog;
using Serilog.Configuration;
Expand All @@ -15,7 +14,7 @@ namespace Modix.Services.Utilities;

public sealed class DiscordWebhookSink : ILogEventSink, IAsyncDisposable
{
private readonly Lazy<CodePasteService> _codePasteService;
private readonly Lazy<PasteService> _codePasteService;
private readonly DiscordWebhookClient _discordWebhookClient;
private readonly IFormatProvider _formatProvider;
private readonly JsonSerializerSettings _jsonSerializerSettings;
Expand All @@ -27,7 +26,7 @@ public DiscordWebhookSink(
ulong webhookId,
string webhookToken,
IFormatProvider formatProvider,
Lazy<CodePasteService> codePasteService)
Lazy<PasteService> codePasteService)
{
_codePasteService = codePasteService;
_discordWebhookClient = new DiscordWebhookClient(webhookId, webhookToken);
Expand Down Expand Up @@ -75,12 +74,15 @@ public async Task ProcessLogEventItemsAsync()

var eventAsJson = JsonConvert.SerializeObject(logEvent, _jsonSerializerSettings);

var url = await _codePasteService.Value.UploadCodeAsync(eventAsJson);
var url = await _codePasteService.Value.UploadPaste(eventAsJson);

message.AddField(new EmbedFieldBuilder()
.WithIsInline(false)
.WithName("Full Log Event")
.WithValue($"[view on paste.mod.gg]({url})"));
if (!string.IsNullOrWhiteSpace(url))
{
message.AddField(new EmbedFieldBuilder()
.WithIsInline(false)
.WithName("Full Log Event")
.WithValue($"[view on paste.mod.gg]({url})"));
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -121,7 +123,7 @@ public async ValueTask DisposeAsync()

public static class DiscordWebhookSinkExtensions
{
public static LoggerConfiguration DiscordWebhookSink(this LoggerSinkConfiguration config, ulong id, string token, LogEventLevel minLevel, Lazy<CodePasteService> codePasteService)
public static LoggerConfiguration DiscordWebhookSink(this LoggerSinkConfiguration config, ulong id, string token, LogEventLevel minLevel, Lazy<PasteService> codePasteService)
=> config.Sink(new DiscordWebhookSink(id, token, null, codePasteService), minLevel);
}

Expand Down
35 changes: 17 additions & 18 deletions src/Modix.Services/Utilities/FormatUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using Discord;

using Humanizer;
using Humanizer.Localisation;

using Modix.Data.Models.Moderation;
using Modix.Services.CodePaste;

namespace Modix.Services.Utilities
{
Expand All @@ -38,18 +34,16 @@ public static string StripFormatting(string code) =>
//strip out the ` characters and code block markers
_buildContentRegex.Replace(code.Trim(), string.Empty);

public static async Task UploadToServiceIfBiggerThan(this EmbedBuilder embed, string content, uint size, CodePasteService service)
public static async Task UploadToServiceIfBiggerThan(this EmbedBuilder embed, string content, uint size,
PasteService service)
{
if (content.Length > size)
{
try
{
var resultLink = await service.UploadCodeAsync(content);
embed.AddField(a => a.WithName("More...").WithValue($"[View on Hastebin]({resultLink})"));
}
catch (WebException we)
var resultLink = await service.UploadPaste(content);

if (!string.IsNullOrWhiteSpace(resultLink))
{
embed.AddField(a => a.WithName("More...").WithValue(we.Message));
embed.AddField(a => a.WithName("More...").WithValue($"[View on paste.mod.gg]({resultLink})"));
}
}
}
Expand Down Expand Up @@ -87,7 +81,8 @@ public static IReadOnlyCollection<string> CollapsePlurals(IReadOnlyCollection<st
Value: x
));

var groupedBySingulars = withSingulars.GroupBy(x => x.Singular, x => x.Value, new SequenceEqualityComparer<string>());
var groupedBySingulars =
withSingulars.GroupBy(x => x.Singular, x => x.Value, new SequenceEqualityComparer<string>());

var withDistinctParts = new HashSet<string>[groupedBySingulars.Count()][];

Expand Down Expand Up @@ -129,7 +124,8 @@ public static IReadOnlyCollection<string> CollapsePlurals(IReadOnlyCollection<st
? word.First()
: word.Last();

parenthesized[aliasIndex][wordIndex] = $"{longestForm[..indexOfDifference]}({longestForm[indexOfDifference..]})";
parenthesized[aliasIndex][wordIndex] =
$"{longestForm[..indexOfDifference]}({longestForm[indexOfDifference..]})";
}
else
{
Expand Down Expand Up @@ -202,6 +198,7 @@ public static string FormatCodeForEmbed(string language, string sourceCode, int
AddRemainingLineComment();
break;
}

braceOnlyLinesEliminated++;
}
else if (!TryAddLine(line))
Expand Down Expand Up @@ -244,11 +241,13 @@ public static string FormatCodeForEmbed(string language, string sourceCode, int
bool TryAddLine(string line)
{
var remainingCount = GetRemainingLineCount();
var possibleRemainingLineCommentLength = remainingCount > 1 // 1, because the current line is included in the count
? GetRemainingLineCountComment(remainingCount).Length
: 0;
var possibleRemainingLineCommentLength =
remainingCount > 1 // 1, because the current line is included in the count
? GetRemainingLineCountComment(remainingCount).Length
: 0;

if (line.Length + currentLength + possibleRemainingLineCommentLength + 1 > maxLength) // +1 because of the newline that will be added later
if (line.Length + currentLength + possibleRemainingLineCommentLength + 1 >
maxLength) // +1 because of the newline that will be added later
return false;

processedLines.Add(line);
Expand Down
4 changes: 2 additions & 2 deletions src/Modix/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using Modix.Data.Repositories;
using Modix.Services;
using Modix.Services.AutoRemoveMessage;
using Modix.Services.CodePaste;
using Modix.Services.CommandHelp;
using Modix.Services.Core;
using Modix.Services.Csharp;
Expand Down Expand Up @@ -157,7 +156,6 @@ public static IServiceCollection AddModix(
.AddModixCore()
.AddModixModeration()
.AddModixPromotions()
.AddCodePaste()
.AddCommandHelp()
.AddGuildStats()
.AddModixTags()
Expand All @@ -166,6 +164,8 @@ public static IServiceCollection AddModix(
.AddEmojiStats()
.AddImages();

services.AddScoped<PasteService>();

services.AddScoped<IQuoteService, QuoteService>();
services.AddSingleton<IBehavior, MessageLinkBehavior>();
services.AddMemoryCache();
Expand Down
4 changes: 2 additions & 2 deletions src/Modix/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using Modix.Configuration;
using Modix.Data;
using Modix.Data.Models.Core;
using Modix.Services.CodePaste;
using Modix.Services;
using Modix.Services.Utilities;
using Modix.Web;
using Newtonsoft.Json.Converters;
Expand Down Expand Up @@ -106,7 +106,7 @@ private static void ConfigureServices(WebApplicationBuilder builder, IConfigurat
if (webhookId.HasValue && webhookToken != null)
{
lc
.WriteTo.DiscordWebhookSink(webhookId.Value, webhookToken, LogEventLevel.Error, new Lazy<CodePasteService>(sp.GetRequiredService<CodePasteService>));
.WriteTo.DiscordWebhookSink(webhookId.Value, webhookToken, LogEventLevel.Error, new Lazy<PasteService>(sp.GetRequiredService<PasteService>));
}
});

Expand Down
Loading