chore: format with csharpier

This commit is contained in:
sam 2024-10-09 17:35:11 +02:00
parent 2f516dcb73
commit 4f54077c68
59 changed files with 2000 additions and 942 deletions

View file

@ -6,7 +6,11 @@ using DbMessage = Catalogger.Backend.Database.Models.Message;
namespace Catalogger.Backend.Database.Queries;
public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionService encryptionService)
public class MessageRepository(
ILogger logger,
DatabaseContext db,
IEncryptionService encryptionService
)
{
private readonly ILogger _logger = logger.ForContext<MessageRepository>();
@ -14,8 +18,10 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
{
_logger.Debug("Saving message {MessageId}", msg.ID);
var metadata = new Metadata(IsWebhook: msg.WebhookID.HasValue,
msg.Attachments.Select(a => new Attachment(a.Filename, a.Size, a.ContentType.Value)));
var metadata = new Metadata(
IsWebhook: msg.WebhookID.HasValue,
msg.Attachments.Select(a => new Attachment(a.Filename, a.Size, a.ContentType.Value))
);
var dbMessage = new DbMessage
{
@ -24,12 +30,22 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
ChannelId = msg.ChannelID.ToUlong(),
GuildId = msg.GuildID.ToUlong(),
EncryptedContent =
await Task.Run(
() => encryptionService.Encrypt(string.IsNullOrWhiteSpace(msg.Content) ? "None" : msg.Content), ct),
EncryptedUsername = await Task.Run(() => encryptionService.Encrypt(msg.Author.Tag()), ct),
EncryptedMetadata = await Task.Run(() => encryptionService.Encrypt(JsonSerializer.Serialize(metadata)), ct),
AttachmentSize = msg.Attachments.Select(a => a.Size).Sum()
EncryptedContent = await Task.Run(
() =>
encryptionService.Encrypt(
string.IsNullOrWhiteSpace(msg.Content) ? "None" : msg.Content
),
ct
),
EncryptedUsername = await Task.Run(
() => encryptionService.Encrypt(msg.Author.Tag()),
ct
),
EncryptedMetadata = await Task.Run(
() => encryptionService.Encrypt(JsonSerializer.Serialize(metadata)),
ct
),
AttachmentSize = msg.Attachments.Select(a => a.Size).Sum(),
};
db.Add(dbMessage);
@ -56,17 +72,32 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
}
else
{
var metadata = new Metadata(IsWebhook: msg.WebhookID.HasValue,
msg.Attachments.Select(a => new Attachment(a.Filename, a.Size, a.ContentType.Value)));
var metadata = new Metadata(
IsWebhook: msg.WebhookID.HasValue,
msg.Attachments.Select(a => new Attachment(a.Filename, a.Size, a.ContentType.Value))
);
var dbMsg = await db.Messages.FindAsync(msg.ID.Value);
if (dbMsg == null) throw new CataloggerError("Message was null despite HasProxyInfoAsync returning true");
if (dbMsg == null)
throw new CataloggerError(
"Message was null despite HasProxyInfoAsync returning true"
);
dbMsg.EncryptedContent = await Task.Run(
() => encryptionService.Encrypt(string.IsNullOrWhiteSpace(msg.Content) ? "None" : msg.Content), ct);
dbMsg.EncryptedUsername = await Task.Run(() => encryptionService.Encrypt(msg.Author.Tag()), ct);
dbMsg.EncryptedMetadata =
await Task.Run(() => encryptionService.Encrypt(JsonSerializer.Serialize(metadata)), ct);
() =>
encryptionService.Encrypt(
string.IsNullOrWhiteSpace(msg.Content) ? "None" : msg.Content
),
ct
);
dbMsg.EncryptedUsername = await Task.Run(
() => encryptionService.Encrypt(msg.Author.Tag()),
ct
);
dbMsg.EncryptedMetadata = await Task.Run(
() => encryptionService.Encrypt(JsonSerializer.Serialize(metadata)),
ct
);
db.Update(dbMsg);
await db.SaveChangesAsync(ct);
@ -80,17 +111,26 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
_logger.Debug("Retrieving message {MessageId}", id);
var dbMsg = await db.Messages.FindAsync(id);
if (dbMsg == null) return null;
if (dbMsg == null)
return null;
return new Message(dbMsg.Id, dbMsg.OriginalId, dbMsg.UserId, dbMsg.ChannelId, dbMsg.GuildId, dbMsg.Member,
return new Message(
dbMsg.Id,
dbMsg.OriginalId,
dbMsg.UserId,
dbMsg.ChannelId,
dbMsg.GuildId,
dbMsg.Member,
dbMsg.System,
Username: await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedUsername), ct),
Content: await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedContent), ct),
Metadata: dbMsg.EncryptedMetadata != null
? JsonSerializer.Deserialize<Metadata>(
await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedMetadata), ct))
await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedMetadata), ct)
)
: null,
dbMsg.AttachmentSize);
dbMsg.AttachmentSize
);
}
/// <summary>
@ -101,12 +141,19 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
{
_logger.Debug("Checking if message {MessageId} has proxy information", id);
var msg = await db.Messages.Select(m => new { m.Id, m.OriginalId }).FirstOrDefaultAsync(m => m.Id == id);
var msg = await db
.Messages.Select(m => new { m.Id, m.OriginalId })
.FirstOrDefaultAsync(m => m.Id == id);
return (msg != null, msg?.OriginalId != null);
}
public async Task SetProxiedMessageDataAsync(ulong id, ulong originalId, ulong authorId, string? systemId,
string? memberId)
public async Task SetProxiedMessageDataAsync(
ulong id,
ulong originalId,
ulong authorId,
string? systemId,
string? memberId
)
{
_logger.Debug("Setting proxy information for message {MessageId}", id);
@ -151,4 +198,4 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
public record Metadata(bool IsWebhook, IEnumerable<Attachment> Attachments);
public record Attachment(string Filename, int Size, string ContentType);
}
}

View file

@ -6,21 +6,34 @@ namespace Catalogger.Backend.Database.Queries;
public static class QueryExtensions
{
public static async ValueTask<Guild> GetGuildAsync(this DatabaseContext db, Snowflake id,
CancellationToken ct = default) => await db.GetGuildAsync(id.ToUlong(), ct);
public static async ValueTask<Guild> GetGuildAsync(
this DatabaseContext db,
Snowflake id,
CancellationToken ct = default
) => await db.GetGuildAsync(id.ToUlong(), ct);
public static async ValueTask<Guild> GetGuildAsync(this DatabaseContext db, Optional<Snowflake> id,
CancellationToken ct = default) => await db.GetGuildAsync(id.ToUlong(), ct);
public static async ValueTask<Guild> GetGuildAsync(
this DatabaseContext db,
Optional<Snowflake> id,
CancellationToken ct = default
) => await db.GetGuildAsync(id.ToUlong(), ct);
public static async ValueTask<Guild> GetGuildAsync(this DatabaseContext db, ulong id,
CancellationToken ct = default)
public static async ValueTask<Guild> GetGuildAsync(
this DatabaseContext db,
ulong id,
CancellationToken ct = default
)
{
var guild = await db.Guilds.FindAsync([id], ct);
if (guild == null) throw new CataloggerError("Guild not found, was not initialized during guild create");
if (guild == null)
throw new CataloggerError("Guild not found, was not initialized during guild create");
return guild;
}
public static async Task<Watchlist?> GetWatchlistEntryAsync(this DatabaseContext db, Snowflake guildId,
Snowflake userId, CancellationToken ct = default) =>
await db.Watchlists.FindAsync([guildId.Value, userId.Value], ct);
}
public static async Task<Watchlist?> GetWatchlistEntryAsync(
this DatabaseContext db,
Snowflake guildId,
Snowflake userId,
CancellationToken ct = default
) => await db.Watchlists.FindAsync([guildId.Value, userId.Value], ct);
}