From b3b2adb127b3ed1ac4b52bc32dff079ca527778f Mon Sep 17 00:00:00 2001 From: pancakes Date: Thu, 19 Sep 2024 19:25:35 +1000 Subject: [PATCH] [backend] Add sensitive field to emojis --- .../Controllers/Web/EmojiController.cs | 17 ++++++---- .../Controllers/Web/Renderers/NoteRenderer.cs | 3 +- .../DatabaseContextModelSnapshot.cs | 4 +++ .../20240919080240_SensitiveEmoji.cs | 32 +++++++++++++++++++ .../Core/Database/Tables/Emoji.cs | 3 ++ .../Core/Services/EmojiService.cs | 13 +++++--- Iceshrimp.Shared/Schemas/Web/EmojiResponse.cs | 1 + .../Schemas/Web/UpdateEmojiRequest.cs | 9 +++--- 8 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 Iceshrimp.Backend/Core/Database/Migrations/v2024.1-beta4/20240919080240_SensitiveEmoji.cs diff --git a/Iceshrimp.Backend/Controllers/Web/EmojiController.cs b/Iceshrimp.Backend/Controllers/Web/EmojiController.cs index 2c48dd94..1a331463 100644 --- a/Iceshrimp.Backend/Controllers/Web/EmojiController.cs +++ b/Iceshrimp.Backend/Controllers/Web/EmojiController.cs @@ -37,7 +37,8 @@ public class EmojiController( Aliases = p.Aliases, Category = p.Category, PublicUrl = p.PublicUrl, - License = p.License + License = p.License, + Sensitive = p.Sensitive }) .ToListAsync(); } @@ -58,7 +59,8 @@ public class EmojiController( Aliases = emoji.Aliases, Category = emoji.Category, PublicUrl = emoji.PublicUrl, - License = emoji.License + License = emoji.License, + Sensitive = emoji.Sensitive }; } @@ -78,7 +80,8 @@ public class EmojiController( Aliases = [], Category = null, PublicUrl = emoji.PublicUrl, - License = null + License = null, + Sensitive = false }; } @@ -103,7 +106,8 @@ public class EmojiController( Aliases = [], Category = null, PublicUrl = cloned.PublicUrl, - License = null + License = null, + Sensitive = cloned.Sensitive }; } @@ -125,7 +129,7 @@ public class EmojiController( public async Task UpdateEmoji(string id, UpdateEmojiRequest request) { var emoji = await emojiSvc.UpdateLocalEmoji(id, request.Name, request.Aliases, request.Category, - request.License) ?? + request.License, request.Sensitive) ?? throw GracefulException.NotFound("Emoji not found"); return new EmojiResponse @@ -136,7 +140,8 @@ public class EmojiController( Aliases = emoji.Aliases, Category = emoji.Category, PublicUrl = emoji.PublicUrl, - License = emoji.License + License = emoji.License, + Sensitive = emoji.Sensitive }; } diff --git a/Iceshrimp.Backend/Controllers/Web/Renderers/NoteRenderer.cs b/Iceshrimp.Backend/Controllers/Web/Renderers/NoteRenderer.cs index 671b5b1d..4b9b1418 100644 --- a/Iceshrimp.Backend/Controllers/Web/Renderers/NoteRenderer.cs +++ b/Iceshrimp.Backend/Controllers/Web/Renderers/NoteRenderer.cs @@ -191,7 +191,8 @@ public class NoteRenderer( Aliases = p.Aliases, Category = p.Category, PublicUrl = p.PublicUrl, - License = p.License + License = p.License, + Sensitive = p.Sensitive }) .ToListAsync(); } diff --git a/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs b/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs index 72926cbb..b848dfda 100644 --- a/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs +++ b/Iceshrimp.Backend/Core/Database/Migrations/DatabaseContextModelSnapshot.cs @@ -1024,6 +1024,10 @@ namespace Iceshrimp.Backend.Core.Database.Migrations .HasColumnName("publicUrl") .HasDefaultValueSql("''::character varying"); + b.Property("Sensitive") + .HasColumnType("boolean") + .HasColumnName("sensitive"); + b.Property("Type") .HasMaxLength(64) .HasColumnType("character varying(64)") diff --git a/Iceshrimp.Backend/Core/Database/Migrations/v2024.1-beta4/20240919080240_SensitiveEmoji.cs b/Iceshrimp.Backend/Core/Database/Migrations/v2024.1-beta4/20240919080240_SensitiveEmoji.cs new file mode 100644 index 00000000..2ed9d5bb --- /dev/null +++ b/Iceshrimp.Backend/Core/Database/Migrations/v2024.1-beta4/20240919080240_SensitiveEmoji.cs @@ -0,0 +1,32 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Infrastructure; + +#nullable disable + +namespace Iceshrimp.Backend.Core.Database.Migrations +{ + /// + [DbContext(typeof(DatabaseContext))] + [Migration("20240919080240_SensitiveEmoji")] + public partial class SensitiveEmoji : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "sensitive", + table: "emoji", + type: "boolean", + nullable: false, + defaultValue: false); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "sensitive", + table: "emoji"); + } + } +} diff --git a/Iceshrimp.Backend/Core/Database/Tables/Emoji.cs b/Iceshrimp.Backend/Core/Database/Tables/Emoji.cs index 470946ad..5b5ca7e9 100644 --- a/Iceshrimp.Backend/Core/Database/Tables/Emoji.cs +++ b/Iceshrimp.Backend/Core/Database/Tables/Emoji.cs @@ -57,6 +57,9 @@ public class Emoji [Column("height")] public int? Height { get; set; } + [Column("sensitive")] + public bool Sensitive { get; set; } + public string GetPublicUri(Config.InstanceSection config) => Host == null ? $"https://{config.WebDomain}/emoji/{Name}" : throw new Exception("Cannot access PublicUri for remote emoji"); diff --git a/Iceshrimp.Backend/Core/Services/EmojiService.cs b/Iceshrimp.Backend/Core/Services/EmojiService.cs index 8cbf3b99..e2489c34 100644 --- a/Iceshrimp.Backend/Core/Services/EmojiService.cs +++ b/Iceshrimp.Backend/Core/Services/EmojiService.cs @@ -56,7 +56,8 @@ public partial class EmojiService( OriginalUrl = driveFile.Url, PublicUrl = driveFile.AccessUrl, Width = driveFile.Properties.Width, - Height = driveFile.Properties.Height + Height = driveFile.Properties.Height, + Sensitive = false }; emoji.Uri = emoji.GetPublicUri(config.Value); @@ -81,7 +82,8 @@ public partial class EmojiService( OriginalUrl = driveFile.Url, PublicUrl = driveFile.AccessUrl, Width = driveFile.Properties.Width, - Height = driveFile.Properties.Height + Height = driveFile.Properties.Height, + Sensitive = existing.Sensitive }; emoji.Uri = emoji.GetPublicUri(config.Value); @@ -130,7 +132,8 @@ public partial class EmojiService( UpdatedAt = DateTime.UtcNow, OriginalUrl = emojo.Image?.Url?.Link ?? throw new Exception("Emoji.Image has no url"), PublicUrl = emojo.Image.Url.Link, - Uri = emojo.Id + Uri = emojo.Id, + Sensitive = false }; await db.AddAsync(dbEmojo); await db.SaveChangesAsync(); @@ -204,7 +207,7 @@ public partial class EmojiService( } public async Task UpdateLocalEmoji( - string id, string? name, List? aliases, string? category, string? license + string id, string? name, List? aliases, string? category, string? license, bool? sensitive ) { var emoji = await db.Emojis.FirstOrDefaultAsync(p => p.Id == id); @@ -228,6 +231,8 @@ public partial class EmojiService( if (license != null) emoji.License = license; + if (sensitive.HasValue) emoji.Sensitive = sensitive.Value; + await db.SaveChangesAsync(); return emoji; diff --git a/Iceshrimp.Shared/Schemas/Web/EmojiResponse.cs b/Iceshrimp.Shared/Schemas/Web/EmojiResponse.cs index 8015a361..ce1238bf 100644 --- a/Iceshrimp.Shared/Schemas/Web/EmojiResponse.cs +++ b/Iceshrimp.Shared/Schemas/Web/EmojiResponse.cs @@ -9,4 +9,5 @@ public class EmojiResponse public required string? Category { get; set; } public required string PublicUrl { get; set; } public required string? License { get; set; } + public required bool Sensitive { get; set; } } \ No newline at end of file diff --git a/Iceshrimp.Shared/Schemas/Web/UpdateEmojiRequest.cs b/Iceshrimp.Shared/Schemas/Web/UpdateEmojiRequest.cs index 7f59c557..408bf2ae 100644 --- a/Iceshrimp.Shared/Schemas/Web/UpdateEmojiRequest.cs +++ b/Iceshrimp.Shared/Schemas/Web/UpdateEmojiRequest.cs @@ -2,8 +2,9 @@ namespace Iceshrimp.Shared.Schemas.Web; public class UpdateEmojiRequest { - public string? Name { get; set; } - public List? Aliases { get; set; } - public string? Category { get; set; } - public string? License { get; set; } + public string? Name { get; set; } + public List? Aliases { get; set; } + public string? Category { get; set; } + public string? License { get; set; } + public bool? Sensitive { get; set; } } \ No newline at end of file