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 Iceshrimp.Backend.Controllers.Attributes;
using Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Federation.ActivityPub;
using Iceshrimp.Backend.Core.Federation.ActivityStreams;
using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types;
using Iceshrimp.Backend.Core.Middleware;

View file

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

View file

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

View file

@ -1,7 +1,7 @@
using Iceshrimp.Backend.Controllers.Schemas;
using Iceshrimp.Backend.Core.Database.Tables;
namespace Iceshrimp.Backend.Controllers.Renderers.Entity;
namespace Iceshrimp.Backend.Controllers.Renderers;
public class UserRenderer {
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.Core.Database;
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.Database;
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.Tables;
using Iceshrimp.Backend.Core.Federation.ActivityStreams;
@ -27,18 +26,15 @@ public class ActivityHandlerService(
//TODO: validate inboxUserId
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);
throw new NotImplementedException();
}
case "https://www.w3.org/ns/activitystreams#Like": {
throw new NotImplementedException();
}
case "https://www.w3.org/ns/activitystreams#Follow": {
case ASActivity.Types.Follow: {
if (activity.Object is { } obj) return Follow(obj, activity.Actor, activity.Id);
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);
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 Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
namespace Iceshrimp.Backend.Core.Federation.ActivityPub;
public class ActivityRenderer(IOptions<Config.InstanceSection> config) {
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 Microsoft.Extensions.Options;
namespace Iceshrimp.Backend.Controllers.Renderers.ActivityPub;
namespace Iceshrimp.Backend.Core.Federation.ActivityPub;
public class NoteRenderer(IOptions<Config.InstanceSection> config) {
public ASNote Render(Note note) {

View file

@ -7,7 +7,7 @@ using Iceshrimp.Backend.Core.Middleware;
using Microsoft.EntityFrameworkCore;
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 async Task<ASActor> Render(User user) {

View file

@ -11,6 +11,18 @@ public class ASActivity : ASObject {
[J("https://www.w3.org/ns/activitystreams#object")]
[JC(typeof(ASObjectConverter))]
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>;

View file

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

View file

@ -53,4 +53,10 @@ public class ASNote : ASObject {
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
public static ASObject? Deserialize(JToken token) {
const string ns = "https://www.w3.org/ns/activitystreams";
return token.Type switch {
JTokenType.Object => token["@type"]?[0]?.Value<string>() switch {
"https://www.w3.org/ns/activitystreams#Application" => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Group" => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Organization" => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Person" => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Service" => token.ToObject<ASActor>(),
"https://www.w3.org/ns/activitystreams#Note" => token.ToObject<ASNote>(),
"https://www.w3.org/ns/activitystreams#Create" => token.ToObject<ASActivity>(),
"https://www.w3.org/ns/activitystreams#Follow" => token.ToObject<ASActivity>(),
ASActor.Types.Person => token.ToObject<ASActor>(),
ASActor.Types.Service => token.ToObject<ASActor>(),
ASActor.Types.Group => token.ToObject<ASActor>(),
ASActor.Types.Organization => token.ToObject<ASActor>(),
ASActor.Types.Application => token.ToObject<ASActor>(),
ASNote.Types.Note => token.ToObject<ASNote>(),
ASActivity.Types.Create => 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>()
},
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.Database;
using Iceshrimp.Backend.Core.Database.Tables;

View file

@ -11,7 +11,7 @@ namespace Iceshrimp.Tests;
public static class MockObjects {
public static readonly ASActor ASActor = new() {
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"),
Username = "test",
DisplayName = "Test account",