Refactor type constants

This commit is contained in:
Laura Hausmann 2024-01-27 02:54:05 +01:00
parent 831a1ceeba
commit a8bd625ce1
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
16 changed files with 56 additions and 32 deletions

View file

@ -1,8 +1,8 @@
using System.Net.Mime; using System.Net.Mime;
using Iceshrimp.Backend.Controllers.Attributes; using Iceshrimp.Backend.Controllers.Attributes;
using Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
using Iceshrimp.Backend.Controllers.Schemas; using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Federation.ActivityPub;
using Iceshrimp.Backend.Core.Federation.ActivityStreams; using Iceshrimp.Backend.Core.Federation.ActivityStreams;
using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types; using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types;
using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Middleware;

View file

@ -1,7 +1,7 @@
using Iceshrimp.Backend.Controllers.Schemas; using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
namespace Iceshrimp.Backend.Controllers.Renderers.Entity; namespace Iceshrimp.Backend.Controllers.Renderers;
public class NoteRenderer { public class NoteRenderer {
public static NoteResponse RenderOne(Note note) { public static NoteResponse RenderOne(Note note) {

View file

@ -1,7 +1,7 @@
using Iceshrimp.Backend.Controllers.Schemas; using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
namespace Iceshrimp.Backend.Controllers.Renderers.Entity; namespace Iceshrimp.Backend.Controllers.Renderers;
public class TimelineRenderer { public class TimelineRenderer {
public static TimelineResponse Render(IEnumerable<Note> notes, int limit) { public static TimelineResponse Render(IEnumerable<Note> notes, int limit) {

View file

@ -1,7 +1,7 @@
using Iceshrimp.Backend.Controllers.Schemas; using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
namespace Iceshrimp.Backend.Controllers.Renderers.Entity; namespace Iceshrimp.Backend.Controllers.Renderers;
public class UserRenderer { public class UserRenderer {
public static UserResponse RenderOne(User user) { public static UserResponse RenderOne(User user) {

View file

@ -1,4 +1,4 @@
using Iceshrimp.Backend.Controllers.Renderers.Entity; using Iceshrimp.Backend.Controllers.Renderers;
using Iceshrimp.Backend.Controllers.Schemas; using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;

View file

@ -1,4 +1,3 @@
using Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
using Iceshrimp.Backend.Core.Configuration; using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Federation.ActivityPub; using Iceshrimp.Backend.Core.Federation.ActivityPub;

View file

@ -1,4 +1,3 @@
using Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Federation.ActivityStreams; using Iceshrimp.Backend.Core.Federation.ActivityStreams;
@ -27,18 +26,15 @@ public class ActivityHandlerService(
//TODO: validate inboxUserId //TODO: validate inboxUserId
switch (activity.Type) { switch (activity.Type) {
case "https://www.w3.org/ns/activitystreams#Create": { case ASActivity.Types.Create: {
if (activity.Object is ASNote note) return noteSvc.ProcessNote(note, activity.Actor); if (activity.Object is ASNote note) return noteSvc.ProcessNote(note, activity.Actor);
throw new NotImplementedException(); throw new NotImplementedException();
} }
case "https://www.w3.org/ns/activitystreams#Like": { case ASActivity.Types.Follow: {
throw new NotImplementedException();
}
case "https://www.w3.org/ns/activitystreams#Follow": {
if (activity.Object is { } obj) return Follow(obj, activity.Actor, activity.Id); if (activity.Object is { } obj) return Follow(obj, activity.Actor, activity.Id);
throw GracefulException.UnprocessableEntity("Follow activity object is invalid"); throw GracefulException.UnprocessableEntity("Follow activity object is invalid");
} }
case "https://www.w3.org/ns/activitystreams#Unfollow": { case ASActivity.Types.Unfollow: {
if (activity.Object is { } obj) return Unfollow(obj, activity.Actor, activity.Id); if (activity.Object is { } obj) return Unfollow(obj, activity.Actor, activity.Id);
throw GracefulException.UnprocessableEntity("Unfollow activity object is invalid"); throw GracefulException.UnprocessableEntity("Unfollow activity object is invalid");
} }

View file

@ -2,7 +2,7 @@ using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types; using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Controllers.Renderers.ActivityPub; namespace Iceshrimp.Backend.Core.Federation.ActivityPub;
public class ActivityRenderer(IOptions<Config.InstanceSection> config) { public class ActivityRenderer(IOptions<Config.InstanceSection> config) {
public static ASActivity RenderCreate(ASObject obj, ASObject actor) { public static ASActivity RenderCreate(ASObject obj, ASObject actor) {

View file

@ -3,7 +3,7 @@ using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types; using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Controllers.Renderers.ActivityPub; namespace Iceshrimp.Backend.Core.Federation.ActivityPub;
public class NoteRenderer(IOptions<Config.InstanceSection> config) { public class NoteRenderer(IOptions<Config.InstanceSection> config) {
public ASNote Render(Note note) { public ASNote Render(Note note) {

View file

@ -7,7 +7,7 @@ using Iceshrimp.Backend.Core.Middleware;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Controllers.Renderers.ActivityPub; namespace Iceshrimp.Backend.Core.Federation.ActivityPub;
public class UserRenderer(IOptions<Config.InstanceSection> config, DatabaseContext db) { public class UserRenderer(IOptions<Config.InstanceSection> config, DatabaseContext db) {
public async Task<ASActor> Render(User user) { public async Task<ASActor> Render(User user) {

View file

@ -11,6 +11,18 @@ public class ASActivity : ASObject {
[J("https://www.w3.org/ns/activitystreams#object")] [J("https://www.w3.org/ns/activitystreams#object")]
[JC(typeof(ASObjectConverter))] [JC(typeof(ASObjectConverter))]
public ASObject? Object { get; set; } public ASObject? Object { get; set; }
public static class Types {
private const string Ns = "https://www.w3.org/ns/activitystreams";
public const string Create = $"{Ns}#Create";
public const string Delete = $"{Ns}#Delete";
public const string Follow = $"{Ns}#Follow";
public const string Unfollow = $"{Ns}#Unfollow";
public const string Accept = $"{Ns}#Accept";
public const string Undo = $"{Ns}#Undo";
public const string Like = $"{Ns}#Like";
}
} }
public sealed class ASActivityConverter : ASSerializer.ListSingleObjectConverter<ASActivity>; public sealed class ASActivityConverter : ASSerializer.ListSingleObjectConverter<ASActivity>;

View file

@ -12,11 +12,7 @@ public class ASActor : ASObject {
private const int SummaryLength = 2048; private const int SummaryLength = 2048;
private static readonly List<string> ActorTypes = [ private static readonly List<string> ActorTypes = [
"https://www.w3.org/ns/activitystreams#Person", Types.Person, Types.Service, Types.Group, Types.Organization, Types.Application
"https://www.w3.org/ns/activitystreams#Service",
"https://www.w3.org/ns/activitystreams#Group",
"https://www.w3.org/ns/activitystreams#Organization",
"https://www.w3.org/ns/activitystreams#Application"
]; ];
[J("https://misskey-hub.net/ns#_misskey_summary")] [J("https://misskey-hub.net/ns#_misskey_summary")]
@ -136,6 +132,16 @@ public class ASActor : ASObject {
_ => null _ => null
}; };
} }
public static class Types {
private const string Ns = "https://www.w3.org/ns/activitystreams";
public const string Application = $"{Ns}#Application";
public const string Group = $"{Ns}#Group";
public const string Organization = $"{Ns}#Organization";
public const string Person = $"{Ns}#Person";
public const string Service = $"{Ns}#Service";
}
} }
public class ASActorConverter : ASSerializer.ListSingleObjectConverter<ASActor>; public class ASActorConverter : ASSerializer.ListSingleObjectConverter<ASActor>;

View file

@ -53,4 +53,10 @@ public class ASNote : ASObject {
return Note.NoteVisibility.Specified; return Note.NoteVisibility.Specified;
} }
public static class Types {
private const string Ns = "https://www.w3.org/ns/activitystreams";
public const string Note = $"{Ns}#Note";
}
} }

View file

@ -15,16 +15,22 @@ public class ASObject {
//FIXME: don't recurse creates and co //FIXME: don't recurse creates and co
public static ASObject? Deserialize(JToken token) { public static ASObject? Deserialize(JToken token) {
const string ns = "https://www.w3.org/ns/activitystreams";
return token.Type switch { return token.Type switch {
JTokenType.Object => token["@type"]?[0]?.Value<string>() switch { JTokenType.Object => token["@type"]?[0]?.Value<string>() switch {
"https://www.w3.org/ns/activitystreams#Application" => token.ToObject<ASActor>(), ASActor.Types.Person => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Group" => token.ToObject<ASActor>(), ASActor.Types.Service => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Organization" => token.ToObject<ASActor>(), ASActor.Types.Group => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Person" => token.ToObject<ASActor>(), ASActor.Types.Organization => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Service" => token.ToObject<ASActor>(), ASActor.Types.Application => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Note" => token.ToObject<ASNote>(), ASNote.Types.Note => token.ToObject<ASNote>(),
"https://www.w3.org/ns/activitystreams#Create" => token.ToObject<ASActivity>(), ASActivity.Types.Create => token.ToObject<ASActivity>(),
"https://www.w3.org/ns/activitystreams#Follow" => token.ToObject<ASActivity>(), ASActivity.Types.Delete => token.ToObject<ASActivity>(),
ASActivity.Types.Follow => token.ToObject<ASActivity>(),
ASActivity.Types.Unfollow => token.ToObject<ASActivity>(),
ASActivity.Types.Accept => token.ToObject<ASActivity>(),
ASActivity.Types.Undo => token.ToObject<ASActivity>(),
ASActivity.Types.Like => token.ToObject<ASActivity>(),
_ => token.ToObject<ASObject>() _ => token.ToObject<ASObject>()
}, },
JTokenType.Array => Deserialize(token.First()), JTokenType.Array => Deserialize(token.First()),

View file

@ -1,4 +1,3 @@
using Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
using Iceshrimp.Backend.Core.Configuration; using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;

View file

@ -11,7 +11,7 @@ namespace Iceshrimp.Tests;
public static class MockObjects { public static class MockObjects {
public static readonly ASActor ASActor = new() { public static readonly ASActor ASActor = new() {
Id = $"https://example.org/users/{IdHelpers.GenerateSlowflakeId()}", Id = $"https://example.org/users/{IdHelpers.GenerateSlowflakeId()}",
Type = "https://www.w3.org/ns/activitystreams#Person", Type = ASActor.Types.Person,
Url = new ASLink("https://example.org/@test"), Url = new ASLink("https://example.org/@test"),
Username = "test", Username = "test",
DisplayName = "Test account", DisplayName = "Test account",